Skip to content

Commit

Permalink
Add tests for file reformatting
Browse files Browse the repository at this point in the history
Signed-off-by: Norman Gehrsitz <norman.gehrsitz@gmx.de>
  • Loading branch information
ngehrsitz committed Jan 18, 2022
1 parent 68e1b4e commit 13f0e85
Show file tree
Hide file tree
Showing 38 changed files with 320 additions and 0 deletions.
71 changes: 71 additions & 0 deletions pkg/gci/gci_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package gci

import (
"io/ioutil"
"os"
"path"
"strings"
"testing"

"github.com/daixiang0/gci/pkg/gci/sections"
"github.com/daixiang0/gci/pkg/io"

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

var testFilesPath = "internal/testdata"

func isTestInputFile(file os.FileInfo) bool {
return !file.IsDir() && strings.HasSuffix(file.Name(), ".in.go")
}

func TestRun(t *testing.T) {
testFiles, err := io.FindFilesForPath(testFilesPath, isTestInputFile)
if err != nil {
t.Fatal(err)
}
for _, testFile := range testFiles {
fileBaseName := strings.TrimSuffix(testFile, ".in.go")
t.Run(fileBaseName, func(t *testing.T) {
t.Parallel()

gciCfg, err := initializeGciConfigFromYAML(fileBaseName + ".cfg.yaml")
if err != nil {
t.Fatal(err)
}

_, formattedFile, err := LoadFormatGoFile(io.File{fileBaseName + ".in.go"}, *gciCfg)
if err != nil {
t.Fatal(err)
}
expectedOutput, err := ioutil.ReadFile(fileBaseName + ".out.go")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, string(expectedOutput), string(formattedFile), "output")
assert.NoError(t, err)
})
}
}

func TestInitGciConfigFromEmptyYAML(t *testing.T) {
gciCfg, err := initializeGciConfigFromYAML(path.Join(testFilesPath, "defaultValues.cfg.yaml"))
assert.NoError(t, err)
_ = gciCfg
assert.Equal(t, DefaultSections(), gciCfg.Sections)
assert.Equal(t, DefaultSectionSeparators(), gciCfg.SectionSeparators)
assert.False(t, gciCfg.Debug)
assert.False(t, gciCfg.NoInlineComments)
assert.False(t, gciCfg.NoPrefixComments)
}

func TestInitGciConfigFromYAML(t *testing.T) {
gciCfg, err := initializeGciConfigFromYAML(path.Join(testFilesPath, "configTest.cfg.yaml"))
assert.NoError(t, err)
_ = gciCfg
assert.Equal(t, SectionList{sections.DefaultSection{}}, gciCfg.Sections)
assert.Equal(t, SectionList{sections.CommentLine{"---"}}, gciCfg.SectionSeparators)
assert.False(t, gciCfg.Debug)
assert.True(t, gciCfg.NoInlineComments)
assert.True(t, gciCfg.NoPrefixComments)
}
5 changes: 5 additions & 0 deletions pkg/gci/internal/testdata/already-good.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sections:
- Standard
- Default
- Prefix(github.com/local)
- Prefix(github.com/daixiang0)
8 changes: 8 additions & 0 deletions pkg/gci/internal/testdata/already-good.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main
import (
"fmt"

g "github.com/golang"

"github.com/daixiang0/gci"
)
8 changes: 8 additions & 0 deletions pkg/gci/internal/testdata/already-good.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main
import (
"fmt"

g "github.com/golang"

"github.com/daixiang0/gci"
)
5 changes: 5 additions & 0 deletions pkg/gci/internal/testdata/comment-with-slashslash.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sections:
- Standard
- Default
- Prefix(github.com/local)
- Prefix(github.com/daixiang0)
5 changes: 5 additions & 0 deletions pkg/gci/internal/testdata/comment-with-slashslash.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

import (
"fmt" // https://pkg.go.dev/fmt
)
5 changes: 5 additions & 0 deletions pkg/gci/internal/testdata/comment-with-slashslash.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

import (
"fmt" // https://pkg.go.dev/fmt
)
7 changes: 7 additions & 0 deletions pkg/gci/internal/testdata/configTest.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
sections:
- default
sectionseparators:
- comment(---)
no-inlineComments: true
no-prefixComments: true
Debug: true
Empty file.
5 changes: 5 additions & 0 deletions pkg/gci/internal/testdata/leading-comment.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sections:
- Standard
- Default
- Prefix(github.com/local)
- Prefix(github.com/daixiang0)
6 changes: 6 additions & 0 deletions pkg/gci/internal/testdata/leading-comment.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

import (
// foo
"fmt"
)
6 changes: 6 additions & 0 deletions pkg/gci/internal/testdata/leading-comment.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

import (
// foo
"fmt"
)
5 changes: 5 additions & 0 deletions pkg/gci/internal/testdata/multi-line-comment.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sections:
- Standard
- Default
- Prefix(github.com/local)
- Prefix(github.com/daixiang0)
15 changes: 15 additions & 0 deletions pkg/gci/internal/testdata/multi-line-comment.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package proc

import (
"context" // in-line comment
"fmt"
"os"

//nolint:depguard // A multi-line comment explaining why in
// this one case it's OK to use os/exec even though depguard
// is configured to force us to use dlib/exec instead.
"os/exec"

"golang.org/x/sys/unix"
"github.com/local/dlib/dexec"
)
15 changes: 15 additions & 0 deletions pkg/gci/internal/testdata/multi-line-comment.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package proc

import (
"context" // in-line comment
"fmt"
"os"
//nolint:depguard // A multi-line comment explaining why in
// this one case it's OK to use os/exec even though depguard
// is configured to force us to use dlib/exec instead.
"os/exec"

"golang.org/x/sys/unix"

"github.com/local/dlib/dexec"
)
5 changes: 5 additions & 0 deletions pkg/gci/internal/testdata/nochar-after-import.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sections:
- Standard
- Default
- Prefix(github.com/local)
- Prefix(github.com/daixiang0)
5 changes: 5 additions & 0 deletions pkg/gci/internal/testdata/nochar-after-import.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

import (
"fmt"
)
5 changes: 5 additions & 0 deletions pkg/gci/internal/testdata/nochar-after-import.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

import (
"fmt"
)
5 changes: 5 additions & 0 deletions pkg/gci/internal/testdata/nolint.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sections:
- Standard
- Default
- Prefix(github.com/local)
- Prefix(github.com/daixiang0)
9 changes: 9 additions & 0 deletions pkg/gci/internal/testdata/nolint.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"fmt"

"github.com/forbidden/pkg" //nolint:depguard

_ "github.com/daixiang0/gci" //nolint:depguard
)
9 changes: 9 additions & 0 deletions pkg/gci/internal/testdata/nolint.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"fmt"

"github.com/forbidden/pkg" //nolint:depguard

_ "github.com/daixiang0/gci" //nolint:depguard
)
Empty file.
5 changes: 5 additions & 0 deletions pkg/gci/internal/testdata/simple-case.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sections:
- Standard
- Default
- Prefix(github.com/local)
- Prefix(github.com/daixiang0)
8 changes: 8 additions & 0 deletions pkg/gci/internal/testdata/simple-case.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main
import (
"golang.org/x/tools"

"fmt"

"github.com/daixiang0/gci"
)
8 changes: 8 additions & 0 deletions pkg/gci/internal/testdata/simple-case.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main
import (
"fmt"

"golang.org/x/tools"

"github.com/daixiang0/gci"
)
15 changes: 15 additions & 0 deletions pkg/gci/internal/testdata/singlechecker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package proc

import (
"context" // in-line comment

//nolint:depguard // A multi-line comment explaining why in
// this one case it's OK to use os/exec even though depguard
// is configured to force us to use dlib/exec instead.
"os/exec"
)

func main() {
_ = context.Canceled
_ = exec.ErrNotFound
}
5 changes: 5 additions & 0 deletions pkg/gci/internal/testdata/whitespace-test.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sections:
- Standard
- Default
- Prefix(github.com/local)
- Prefix(github.com/daixiang0)
7 changes: 7 additions & 0 deletions pkg/gci/internal/testdata/whitespace-test.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import (
"fmt"
"github.com/golang" // golang
alias "github.com/daixiang0/gci"
)
9 changes: 9 additions & 0 deletions pkg/gci/internal/testdata/whitespace-test.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"fmt"

"github.com/golang" // golang

alias "github.com/daixiang0/gci"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sections:
- Standard
- Default
- Prefix(github.com/local)
- Prefix(github.com/daixiang0)
7 changes: 7 additions & 0 deletions pkg/gci/internal/testdata/with-above-comment-and-alias.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main
import (
"fmt"
// golang
_ "github.com/golang"
"github.com/daixiang0/gci"
)
9 changes: 9 additions & 0 deletions pkg/gci/internal/testdata/with-above-comment-and-alias.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main
import (
"fmt"

// golang
_ "github.com/golang"

"github.com/daixiang0/gci"
)
5 changes: 5 additions & 0 deletions pkg/gci/internal/testdata/with-alias.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sections:
- Standard
- Default
- Prefix(github.com/local)
- Prefix(github.com/daixiang0)
6 changes: 6 additions & 0 deletions pkg/gci/internal/testdata/with-alias.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main
import (
"fmt"
g "github.com/golang"
"github.com/daixiang0/gci"
)
8 changes: 8 additions & 0 deletions pkg/gci/internal/testdata/with-alias.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main
import (
"fmt"

g "github.com/golang"

"github.com/daixiang0/gci"
)
5 changes: 5 additions & 0 deletions pkg/gci/internal/testdata/with-comment-and-alias.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sections:
- Standard
- Default
- Prefix(github.com/local)
- Prefix(github.com/daixiang0)
6 changes: 6 additions & 0 deletions pkg/gci/internal/testdata/with-comment-and-alias.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main
import (
"fmt"
_ "github.com/golang" // golang
"github.com/daixiang0/gci"
)
8 changes: 8 additions & 0 deletions pkg/gci/internal/testdata/with-comment-and-alias.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main
import (
"fmt"

_ "github.com/golang" // golang

"github.com/daixiang0/gci"
)

0 comments on commit 13f0e85

Please sign in to comment.