Skip to content

Commit

Permalink
go.mod update
Browse files Browse the repository at this point in the history
  • Loading branch information
incu6us committed Aug 16, 2023
1 parent 80c08ef commit b0ad017
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ build-lint-linux-amd64:
build-lint-linux-arm64:
GOOS=linux GOARCH=arm64 go build -tags linter,osusergo -o bin/linux-arm64/goimportsreviserlint ./linter

.PHONY: build-macos-amd64
build-macos-amd64:
GOOS=darwin GOARCH=amd64 go build -o bin/macos-amd64/goimports-reviser .

.PHONY: update-std-package-list
update-std-package-list:
@go run -tags gen github.com/incu6us/goimports-reviser/v3/pkg/std/gen
10 changes: 8 additions & 2 deletions helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ import (
"github.com/incu6us/goimports-reviser/v3/reviser"
)

func DetermineProjectName(projectName, filePath string) (string, error) {
type Option func() (string, error)

func OSGetwdOption() (string, error) {
return os.Getwd()
}

func DetermineProjectName(projectName, filePath string, option Option) (string, error) {
if filePath == reviser.StandardInput {
var err error
filePath, err = os.Getwd()
filePath, err = option()
if err != nil {
return "", err
}
Expand Down
32 changes: 26 additions & 6 deletions helper/helper_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package helper

import (
"errors"
"os"
"testing"

"github.com/incu6us/goimports-reviser/v3/reviser"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/incu6us/goimports-reviser/v3/reviser"
)

func TestDetermineProjectName(t *testing.T) {
Expand All @@ -16,11 +17,13 @@ func TestDetermineProjectName(t *testing.T) {
type args struct {
projectName string
filePath string
option Option
}
tests := []struct {
name string
args args
want string
name string
args args
want string
wantErr bool
}{
{
name: "success with manual filepath",
Expand All @@ -31,6 +34,7 @@ func TestDetermineProjectName(t *testing.T) {
require.NoError(t, err)
return dir
}(),
option: OSGetwdOption,
},
want: "github.com/incu6us/goimports-reviser/v3",
},
Expand All @@ -39,16 +43,32 @@ func TestDetermineProjectName(t *testing.T) {
args: args{
projectName: "",
filePath: reviser.StandardInput,
option: OSGetwdOption,
},
want: "github.com/incu6us/goimports-reviser/v3",
},
{
name: "fail with manual filepath",
args: args{
projectName: "",
filePath: reviser.StandardInput,
option: func() (string, error) {
return "", errors.New("some error")
},
},
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

got, err := DetermineProjectName(tt.args.projectName, tt.args.filePath)
got, err := DetermineProjectName(tt.args.projectName, tt.args.filePath, tt.args.option)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func main() {
options = append(options, reviser.WithImportsOrder(order))
}

originProjectName, err := helper.DetermineProjectName(projectName, originPath)
originProjectName, err := helper.DetermineProjectName(projectName, originPath, helper.OSGetwdOption)
if err != nil {
fmt.Printf("%s\n\n", err)
printUsage()
Expand Down

0 comments on commit b0ad017

Please sign in to comment.