Repository: daixiang0/gci
Pull request #80 (2022-07-18 18:11:36)

fix: remove wrong test dependencies
- remove `github.com/tj/assert` (use `github.com/stretchr/testify` instead.
- update some `github.com/stretchr/testify`
- use `gopkg.in/yaml.v3`


Patch:
diff --git a/.github/workflows/gci.yml b/.github/workflows/gci.yml
index 9e4c1a4..7b05d40 100644
--- a/.github/workflows/gci.yml
+++ b/.github/workflows/gci.yml
@@ -71,7 +71,7 @@ jobs:
             ${{ matrix.target_os }}-${{ matrix.target_arch }}-go-${{ env.GOVER }}-build-
       - name: golangci-lint
         if: matrix.target_arch == 'amd64' && matrix.target_os == 'linux'
-        uses: golangci/golangci-lint-action@v3.1.0
+        uses: golangci/golangci-lint-action@v3.2.0
         with:
           version: ${{ env.GOLANGCILINT_VER }}
       - name: Run make test
diff --git a/go.mod b/go.mod
index 99db9da..151096b 100644
--- a/go.mod
+++ b/go.mod
@@ -5,12 +5,11 @@ go 1.18
 require (
 	github.com/hexops/gotextdiff v1.0.3
 	github.com/spf13/cobra v1.3.0
-	github.com/stretchr/testify v1.7.0
-	github.com/tj/assert v0.0.3
+	github.com/stretchr/testify v1.8.0
 	go.uber.org/zap v1.17.0
 	golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
 	golang.org/x/tools v0.1.5
-	gopkg.in/yaml.v2 v2.4.0
+	gopkg.in/yaml.v3 v3.0.1
 )
 
 require (
@@ -24,5 +23,4 @@ require (
 	golang.org/x/mod v0.5.0 // indirect
 	golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
 	golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
-	gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
 )
diff --git a/go.sum b/go.sum
index d492590..50568de 100644
--- a/go.sum
+++ b/go.sum
@@ -318,16 +318,17 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
 github.com/spf13/viper v1.10.0/go.mod h1:SoyBPwAtKDzypXNDFKN5kzH7ppppbGZtls1UpIy5AsM=
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
 github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
 github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
 github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
 github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
 github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
-github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
 github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
+github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
 github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
-github.com/tj/assert v0.0.3 h1:Df/BlaZ20mq6kuai7f5z2TvPFiwC3xaWJSDQNiIS3Rk=
-github.com/tj/assert v0.0.3/go.mod h1:Ne6X72Q+TB1AteidzQncjw9PabbMp4PBMZ1k+vd1Pvk=
 github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
 github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
 github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@@ -767,9 +768,9 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
 gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
-gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
-gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
 honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
 honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
diff --git a/pkg/config/config.go b/pkg/config/config.go
index e84911d..d2f1ad1 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -3,7 +3,7 @@ package config
 import (
 	"io/ioutil"
 
-	"gopkg.in/yaml.v2"
+	"gopkg.in/yaml.v3"
 
 	"github.com/daixiang0/gci/pkg/section"
 )
diff --git a/pkg/gci/gci.go b/pkg/gci/gci.go
index 3c92775..e19bad3 100644
--- a/pkg/gci/gci.go
+++ b/pkg/gci/gci.go
@@ -122,7 +122,7 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
 		return src, src, nil
 	}
 
-	imports, headEnd, tailStart, tailEnd, err := parse.ParseFile(src)
+	imports, headEnd, tailStart, err := parse.ParseFile(src, file.Path())
 	if err != nil {
 		if errors.Is(err, parse.NoImportError{}) {
 			return src, src, nil
@@ -141,7 +141,7 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
 	}
 
 	head := src[:headEnd]
-	tail := src[tailStart:tailEnd]
+	tail := src[tailStart:]
 
 	// sort for custom sections
 	allKeys := make([]string, 0, len(result))
diff --git a/pkg/parse/parse.go b/pkg/parse/parse.go
index 738cdd2..df0af3d 100644
--- a/pkg/parse/parse.go
+++ b/pkg/parse/parse.go
@@ -64,22 +64,31 @@ func getImports(imp *ast.ImportSpec) (start, end int, name string) {
 	return
 }
 
-func ParseFile(src []byte) (ImportList, int, int, int, error) {
-	parserMode := parser.Mode(0)
-	parserMode |= parser.ParseComments
+func ParseFile(src []byte, filename string) (ImportList, int, int, error) {
 	fileSet := token.NewFileSet()
-	f, err := parser.ParseFile(fileSet, "", src, parserMode)
+	f, err := parser.ParseFile(fileSet, filename, src, parser.ParseComments)
 	if err != nil {
-		return nil, 0, 0, 0, err
+		return nil, 0, 0, err
 	}
 
 	if len(f.Imports) == 0 {
-		return nil, 0, 0, 0, NoImportError{}
+		return nil, 0, 0, NoImportError{}
 	}
 
+	var headEnd int
+	var tailStart int
+
 	var data ImportList
-	for _, imp := range f.Imports {
+	for i, imp := range f.Imports {
 		start, end, name := getImports(imp)
+
+		if i == 0 {
+			headEnd = start
+		}
+		if i == len(f.Imports)-1 {
+			tailStart = end
+		}
+
 		data = append(data, &GciImports{
 			Start: start,
 			End:   end,
@@ -88,15 +97,11 @@ func ParseFile(src []byte) (ImportList, int, int, int, error) {
 		})
 	}
 
-	headEnd, _, _ := getImports(f.Imports[0])
-	_, tailStart, _ := getImports(f.Imports[len(f.Imports)-1])
-	tailEnd := f.Decls[len(f.Decls)-1].End()
-
 	sort.Sort(data)
-	return data, headEnd, tailStart, int(tailEnd), nil
+	return data, headEnd, tailStart, nil
 }
 
-// isGenerated reports whether the source file is generated code.
+// IsGeneratedFileByComment reports whether the source file is generated code.
 // Using a bit laxer rules than https://golang.org/s/generatedcode to
 // match more generated code.
 // Taken from https://github.com/golangci/golangci-lint.
diff --git a/pkg/section/parser.go b/pkg/section/parser.go
index aa87bc1..94e32af 100644
--- a/pkg/section/parser.go
+++ b/pkg/section/parser.go
@@ -25,10 +25,10 @@ func Parse(data []string) (SectionList, error) {
 			list = append(list, Standard{})
 		} else if s == "newline" {
 			list = append(list, NewLine{})
-		} else if strings.HasPrefix(s, "prefix(") && len(s) > 8 {
-			list = append(list, Custom{s[7 : len(s)-1]})
-		} else if strings.HasPrefix(s, "commentline(") && len(s) > 13 {
-			list = append(list, Custom{s[12 : len(s)-1]})
+		} else if strings.HasPrefix(s, "prefix(") && len(d) > 8 {
+			list = append(list, Custom{d[7 : len(d)-1]})
+		} else if strings.HasPrefix(s, "commentline(") && len(d) > 13 {
+			list = append(list, Custom{d[12 : len(d)-1]})
 		} else {
 			errString += fmt.Sprintf(" %s", s)
 		}


Test patch:
diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go
index ccfbc07..8d0936b 100644
--- a/pkg/config/config_test.go
+++ b/pkg/config/config_test.go
@@ -4,7 +4,7 @@ import (
 	"path"
 	"testing"
 
-	"github.com/tj/assert"
+	"github.com/stretchr/testify/assert"
 
 	"github.com/daixiang0/gci/pkg/section"
 )
diff --git a/pkg/gci/internal/testdata/comment-in-the-tail.cfg.yaml b/pkg/gci/internal/testdata/comment-in-the-tail.cfg.yaml
new file mode 100644
index 0000000..e666ab9
--- /dev/null
+++ b/pkg/gci/internal/testdata/comment-in-the-tail.cfg.yaml
@@ -0,0 +1,4 @@
+sections:
+  - Standard
+  - Default
+  - Prefix(github.com/daixiang0)
diff --git a/pkg/gci/internal/testdata/comment-in-the-tail.in.go b/pkg/gci/internal/testdata/comment-in-the-tail.in.go
new file mode 100644
index 0000000..2114dbd
--- /dev/null
+++ b/pkg/gci/internal/testdata/comment-in-the-tail.in.go
@@ -0,0 +1,13 @@
+package main
+import (
+	"fmt"
+
+	g "github.com/golang"
+
+	"github.com/daixiang0/gci"
+)
+
+
+type test int
+
+// test
diff --git a/pkg/gci/internal/testdata/comment-in-the-tail.out.go b/pkg/gci/internal/testdata/comment-in-the-tail.out.go
new file mode 100644
index 0000000..2114dbd
--- /dev/null
+++ b/pkg/gci/internal/testdata/comment-in-the-tail.out.go
@@ -0,0 +1,13 @@
+package main
+import (
+	"fmt"
+
+	g "github.com/golang"
+
+	"github.com/daixiang0/gci"
+)
+
+
+type test int
+
+// test
diff --git a/pkg/section/parser_test.go b/pkg/section/parser_test.go
index 66b121d..b8c64b7 100644
--- a/pkg/section/parser_test.go
+++ b/pkg/section/parser_test.go
@@ -25,6 +25,16 @@ func TestParse(t *testing.T) {
 			expectedSection: SectionList{Custom{"go"}},
 			expectedError:   nil,
 		},
+		{
+			input:           []string{"prefix(go-UPPER-case)"},
+			expectedSection: SectionList{Custom{"go-UPPER-case"}},
+			expectedError:   nil,
+		},
+		{
+			input:           []string{"PREFIX(go-UPPER-case)"},
+			expectedSection: SectionList{Custom{"go-UPPER-case"}},
+			expectedError:   nil,
+		},
 		{
 			input:           []string{"prefix("},
 			expectedSection: nil,