Skip to content

Commit

Permalink
fix blank in the config and convert test data as string (#172)
Browse files Browse the repository at this point in the history
* fix blank in the config and convert test data as string

Signed-off-by: Loong <[email protected]>

* fix CI

Signed-off-by: Loong <[email protected]>

---------

Signed-off-by: Loong <[email protected]>
  • Loading branch information
daixiang0 authored Sep 18, 2023
1 parent e452b4b commit 1dcd02c
Show file tree
Hide file tree
Showing 140 changed files with 1,268 additions and 990 deletions.
12 changes: 5 additions & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"io/ioutil"
"sort"
"strings"

Expand Down Expand Up @@ -73,19 +72,18 @@ func (g YamlConfig) Parse() (*Config, error) {
return &Config{g.Cfg, sections, sectionSeparators}, nil
}

func InitializeGciConfigFromYAML(filePath string) (*Config, error) {
func ParseConfig(in string) (*Config, error) {
config := YamlConfig{}
yamlData, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(yamlData, &config)

err := yaml.Unmarshal([]byte(in), &config)
if err != nil {
return nil, err
}

gciCfg, err := config.Parse()
if err != nil {
return nil, err
}

return gciCfg, nil
}
22 changes: 0 additions & 22 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,13 @@
package config

import (
"path"
"testing"

"github.com/stretchr/testify/assert"

"github.com/daixiang0/gci/pkg/section"
)

var testFilesPath = "../gci/internal/testdata"

func TestInitGciConfigFromEmptyYAML(t *testing.T) {
gciCfg, err := InitializeGciConfigFromYAML(path.Join(testFilesPath, "defaultValues.cfg.yaml"))
assert.NoError(t, err)
assert.Equal(t, section.DefaultSections(), gciCfg.Sections)
assert.Equal(t, section.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)
assert.Equal(t, section.SectionList{section.Default{}}, gciCfg.Sections)
assert.False(t, gciCfg.Debug)
assert.True(t, gciCfg.SkipGenerated)
assert.False(t, gciCfg.CustomOrder)
}

// the custom sections sort alphabetically as default.
func TestParseOrder(t *testing.T) {
cfg := YamlConfig{
Expand Down
8 changes: 7 additions & 1 deletion pkg/gci/gci.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,17 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
return nil, nil, err
}

return LoadFormat(src, file.Path(), cfg)
}

func LoadFormat(in []byte, path string, cfg config.Config) (src, dist []byte, err error) {
src = in

if cfg.SkipGenerated && parse.IsGeneratedFileByComment(string(src)) {
return src, src, nil
}

imports, headEnd, tailStart, cStart, cEnd, err := parse.ParseFile(src, file.Path())
imports, headEnd, tailStart, cStart, cEnd, err := parse.ParseFile(src, path)
if err != nil {
if errors.Is(err, parse.NoImportError{}) {
return src, src, nil
Expand Down
89 changes: 10 additions & 79 deletions pkg/gci/gci_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package gci

import (
"os"
"runtime"
"strings"
"fmt"
"testing"

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

"github.com/daixiang0/gci/pkg/config"
"github.com/daixiang0/gci/pkg/io"
"github.com/daixiang0/gci/pkg/log"
)

Expand All @@ -19,91 +15,26 @@ func init() {
defer log.L().Sync()
}

var testFilesPath = "internal/testdata"

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

func TestRun(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping test on Windows")
}
// if runtime.GOOS == "windows" {
// t.Skip("Skipping test on Windows")
// }

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

gciCfg, err := config.InitializeGciConfigFromYAML(fileBaseName + ".cfg.yaml")
for i := range testCases {
t.Run(fmt.Sprintf("run case: %s", testCases[i].name), func(t *testing.T) {
config, err := config.ParseConfig(testCases[i].config)
if err != nil {
t.Fatal(err)
}

inputSrcFile := io.File{FilePath: fileBaseName + ".in.go"}
inputSrc, err := inputSrcFile.Load()
require.NoError(t, err)

unmodifiedFile, formattedFile, err := LoadFormatGoFile(inputSrcFile, *gciCfg)
old, new, err := LoadFormat([]byte(testCases[i].in), "", *config)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, inputSrc, unmodifiedFile)

expectedOutput, err := os.ReadFile(fileBaseName + ".out.go")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, string(expectedOutput), string(formattedFile), "output")
assert.NoError(t, err)
assert.Equal(t, testCases[i].in, string(old))
assert.Equal(t, testCases[i].out, string(new))
})
}
}

// func TestSkippingOverIncorrectlyFormattedFiles(t *testing.T) {
// cfg, err := config.YamlConfig{}.Parse()
// assert.NoError(t, err)

// var importUnclosedCtr, noImportCtr, validCtr int
// var files []io.FileObj
// files = append(files, TestFile{io.File{FilePath: "internal/skipTest/import-unclosed.testgo"}, &importUnclosedCtr})
// files = append(files, TestFile{io.File{FilePath: "internal/skipTest/no-import.testgo"}, &noImportCtr})
// files = append(files, TestFile{io.File{FilePath: "internal/skipTest/valid.testgo"}, &validCtr})

// validFileProcessedChan := make(chan bool, len(files))

// generatorFunc := func() ([]io.FileObj, error) {
// return files, nil
// }
// fileAccessTestFunc := func(filePath string, unmodifiedFile, formattedFile []byte) error {
// validFileProcessedChan <- true
// return nil
// }
// err = ProcessFiles(generatorFunc, *cfg, fileAccessTestFunc)

// assert.NoError(t, err)
// // check all files have been accessed
// assert.Equal(t, importUnclosedCtr, 1)
// assert.Equal(t, noImportCtr, 1)
// assert.Equal(t, validCtr, 1)
// // check that processing for the valid file was called
// assert.True(t, <-validFileProcessedChan)
// }

// type TestFile struct {
// wrappedFile io.File
// accessCounter *int
// }

// func (t TestFile) Load() ([]byte, error) {
// *t.accessCounter++
// return t.wrappedFile.Load()
// }

// func (t TestFile) Path() string {
// return t.wrappedFile.Path()
// }
14 changes: 0 additions & 14 deletions pkg/gci/internal/skipTest/generated_code.go

This file was deleted.

8 changes: 0 additions & 8 deletions pkg/gci/internal/skipTest/import-unclosed.testgo

This file was deleted.

4 changes: 0 additions & 4 deletions pkg/gci/internal/skipTest/no-import.testgo

This file was deleted.

8 changes: 0 additions & 8 deletions pkg/gci/internal/skipTest/valid.testgo

This file was deleted.

1 change: 0 additions & 1 deletion pkg/gci/internal/testdata/already-good.cfg.yaml

This file was deleted.

9 changes: 0 additions & 9 deletions pkg/gci/internal/testdata/already-good.in.go

This file was deleted.

9 changes: 0 additions & 9 deletions pkg/gci/internal/testdata/already-good.out.go

This file was deleted.

1 change: 0 additions & 1 deletion pkg/gci/internal/testdata/blank-format.cfg.yaml

This file was deleted.

9 changes: 0 additions & 9 deletions pkg/gci/internal/testdata/blank-format.in.go

This file was deleted.

10 changes: 0 additions & 10 deletions pkg/gci/internal/testdata/blank-format.out.go

This file was deleted.

1 change: 0 additions & 1 deletion pkg/gci/internal/testdata/cgo-block-after-import.cfg.yaml

This file was deleted.

14 changes: 0 additions & 14 deletions pkg/gci/internal/testdata/cgo-block-after-import.in.go

This file was deleted.

15 changes: 0 additions & 15 deletions pkg/gci/internal/testdata/cgo-block-after-import.out.go

This file was deleted.

1 change: 0 additions & 1 deletion pkg/gci/internal/testdata/cgo-block-before-import.cfg.yaml

This file was deleted.

15 changes: 0 additions & 15 deletions pkg/gci/internal/testdata/cgo-block-before-import.in.go

This file was deleted.

15 changes: 0 additions & 15 deletions pkg/gci/internal/testdata/cgo-block-before-import.out.go

This file was deleted.

This file was deleted.

6 changes: 0 additions & 6 deletions pkg/gci/internal/testdata/cgo-block-mixed-with-content.in.go

This file was deleted.

6 changes: 0 additions & 6 deletions pkg/gci/internal/testdata/cgo-block-mixed-with-content.out.go

This file was deleted.

1 change: 0 additions & 1 deletion pkg/gci/internal/testdata/cgo-block-mixed.cfg.yaml

This file was deleted.

6 changes: 0 additions & 6 deletions pkg/gci/internal/testdata/cgo-block-mixed.in.go

This file was deleted.

6 changes: 0 additions & 6 deletions pkg/gci/internal/testdata/cgo-block-mixed.out.go

This file was deleted.

1 change: 0 additions & 1 deletion pkg/gci/internal/testdata/cgo-block-prefix.cfg.yaml

This file was deleted.

Loading

0 comments on commit 1dcd02c

Please sign in to comment.