Skip to content

Commit 8f975be

Browse files
authored
fix: package type detection when local flag is empty. (#27)
1 parent 599c94c commit 8f975be

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package main
33
import (
44
"flag"
55
"fmt"
6-
"github.com/daixiang0/gci/pkg/gci"
76
"go/scanner"
87
"os"
8+
9+
"github.com/daixiang0/gci/pkg/gci"
910
)
1011

1112
var (

pkg/gci/gci.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,15 @@ func getPkgInfo(line string, comment bool) (string, string, string) {
178178

179179
func getPkgType(line, localFlag string) int {
180180
pkgName := strings.Trim(line, "\"\\`")
181-
if strings.HasPrefix(pkgName, localFlag) {
181+
182+
if localFlag != "" && strings.HasPrefix(pkgName, localFlag) {
182183
return local
183-
} else if isStandardPackage(pkgName) {
184+
}
185+
186+
if isStandardPackage(pkgName) {
184187
return standard
185188
}
189+
186190
return remote
187191
}
188192

pkg/gci/gci_test.go

+24-14
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,44 @@ import (
55
"testing"
66
)
77

8-
func TestGetPkgType(b *testing.T) {
8+
func TestGetPkgType(t *testing.T) {
99
testCases := []struct {
1010
Line string
1111
LocalFlag string
1212
ExpectedResult int
1313
}{
14+
{Line: `"foo/pkg/bar"`, LocalFlag: "", ExpectedResult: remote},
1415
{Line: `"foo/pkg/bar"`, LocalFlag: "foo", ExpectedResult: local},
15-
{Line: `"github.com/foo/bar"`, LocalFlag: "foo", ExpectedResult: remote},
16-
{Line: `"context"`, LocalFlag: "foo", ExpectedResult: standard},
17-
{Line: `"os/signal"`, LocalFlag: "foo", ExpectedResult: standard},
1816
{Line: `"foo/pkg/bar"`, LocalFlag: "bar", ExpectedResult: remote},
19-
{Line: `"github.com/foo/bar"`, LocalFlag: "bar", ExpectedResult: remote},
20-
{Line: `"context"`, LocalFlag: "bar", ExpectedResult: standard},
21-
{Line: `"os/signal"`, LocalFlag: "bar", ExpectedResult: standard},
2217
{Line: `"foo/pkg/bar"`, LocalFlag: "github.com/foo/bar", ExpectedResult: remote},
18+
19+
{Line: `"github.com/foo/bar"`, LocalFlag: "", ExpectedResult: remote},
20+
{Line: `"github.com/foo/bar"`, LocalFlag: "foo", ExpectedResult: remote},
21+
{Line: `"github.com/foo/bar"`, LocalFlag: "bar", ExpectedResult: remote},
2322
{Line: `"github.com/foo/bar"`, LocalFlag: "github.com/foo/bar", ExpectedResult: local},
23+
24+
{Line: `"context"`, LocalFlag: "", ExpectedResult: standard},
25+
{Line: `"context"`, LocalFlag: "context", ExpectedResult: local},
26+
{Line: `"context"`, LocalFlag: "foo", ExpectedResult: standard},
27+
{Line: `"context"`, LocalFlag: "bar", ExpectedResult: standard},
2428
{Line: `"context"`, LocalFlag: "github.com/foo/bar", ExpectedResult: standard},
29+
30+
{Line: `"os/signal"`, LocalFlag: "", ExpectedResult: standard},
31+
{Line: `"os/signal"`, LocalFlag: "os/signal", ExpectedResult: local},
32+
{Line: `"os/signal"`, LocalFlag: "foo", ExpectedResult: standard},
33+
{Line: `"os/signal"`, LocalFlag: "bar", ExpectedResult: standard},
2534
{Line: `"os/signal"`, LocalFlag: "github.com/foo/bar", ExpectedResult: standard},
2635
}
2736

28-
for _, _tCase := range testCases {
29-
tCase := _tCase
30-
testFn := func(t *testing.T) {
31-
result := getPkgType(tCase.Line, tCase.LocalFlag)
32-
if got, want := result, tCase.ExpectedResult; got != want {
37+
for _, tc := range testCases {
38+
tc := tc
39+
t.Run(fmt.Sprintf("%s:%s", tc.Line, tc.LocalFlag), func(t *testing.T) {
40+
t.Parallel()
41+
42+
result := getPkgType(tc.Line, tc.LocalFlag)
43+
if got, want := result, tc.ExpectedResult; got != want {
3344
t.Errorf("bad result: %d, expected: %d", got, want)
3445
}
35-
}
36-
b.Run(fmt.Sprintf("%s:%s", tCase.LocalFlag, tCase.Line), testFn)
46+
})
3747
}
3848
}

0 commit comments

Comments
 (0)