Skip to content

Commit

Permalink
Enhance LocalModule when no go sources found in project root
Browse files Browse the repository at this point in the history
  • Loading branch information
petr-korobeinikov committed Mar 2, 2024
1 parent e9e4f70 commit 5e89d5b
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 27 deletions.
65 changes: 44 additions & 21 deletions pkg/gci/gci_test.go
Expand Up @@ -63,28 +63,49 @@ func readConfig(t *testing.T, configPath string) *config.Config {
}

func TestRunWithLocalModule(t *testing.T) {
moduleDir := filepath.Join("testdata", "module")
// files with a corresponding '*.out.go' file containing the expected
// result of formatting
testedFiles := []string{
"main.go",
filepath.Join("internal", "foo", "lib.go"),
tests := []struct {
name string
moduleDir string
// files with a corresponding '*.out.go' file containing the expected
// result of formatting
testedFiles []string
}{
{
name: `default module test case`,
moduleDir: filepath.Join("testdata", "module"),
testedFiles: []string{
"main.go",
filepath.Join("internal", "foo", "lib.go"),
},
},
{
name: `module without go sources in root dir`,
moduleDir: filepath.Join("testdata", "module_cmd"),
testedFiles: []string{
filepath.Join("cmd", "client", "main.go"),
filepath.Join("cmd", "server", "main.go"),
filepath.Join("internal", "foo", "lib.go"),
},
},
}

// run subtests for expected module loading behaviour
chdir(t, moduleDir)
cfg := readConfig(t, "config.yaml")

for _, path := range testedFiles {
t.Run(path, func(t *testing.T) {
// *.go -> *.out.go
expected, err := os.ReadFile(strings.TrimSuffix(path, ".go") + ".out.go")
require.NoError(t, err)

_, got, err := LoadFormatGoFile(io.File{path}, *cfg)

require.NoError(t, err)
require.Equal(t, string(expected), string(got))
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// run subtests for expected module loading behaviour
chdir(t, tt.moduleDir)
cfg := readConfig(t, "config.yaml")

for _, path := range tt.testedFiles {
t.Run(path, func(t *testing.T) {
// *.go -> *.out.go
expected, err := os.ReadFile(strings.TrimSuffix(path, ".go") + ".out.go")
require.NoError(t, err)

_, got, err := LoadFormatGoFile(io.File{path}, *cfg)

require.NoError(t, err)
require.Equal(t, string(expected), string(got))
})
}
})
}
}
Expand All @@ -100,6 +121,8 @@ func TestRunWithLocalModuleWithPackageLoadFailure(t *testing.T) {
}

func TestRunWithLocalModuleWithModuleLookupError(t *testing.T) {
t.Skipf("In fact there is no error when no go files found in the project root")

dir := t.TempDir()
// error from trying to list packages under module with no go files
configContent := "sections:\n - LocalModule\n"
Expand Down
4 changes: 4 additions & 0 deletions pkg/gci/testdata/module_cmd/.gitattributes
@@ -0,0 +1,4 @@
# try and stop git running on Windows from converting line endings from
# in all Go files under this directory. This is to avoid inconsistent test
# results when `gci` strips "\r" characters
**/*.go eol=lf
1 change: 1 addition & 0 deletions pkg/gci/testdata/module_cmd/cmd/client/main.go
@@ -0,0 +1 @@
package main
1 change: 1 addition & 0 deletions pkg/gci/testdata/module_cmd/cmd/client/main.out.go
@@ -0,0 +1 @@
package main
1 change: 1 addition & 0 deletions pkg/gci/testdata/module_cmd/cmd/server/main.go
@@ -0,0 +1 @@
package main
1 change: 1 addition & 0 deletions pkg/gci/testdata/module_cmd/cmd/server/main.out.go
@@ -0,0 +1 @@
package main
4 changes: 4 additions & 0 deletions pkg/gci/testdata/module_cmd/config.yaml
@@ -0,0 +1,4 @@
sections:
- Standard
- Default
- LocalModule
3 changes: 3 additions & 0 deletions pkg/gci/testdata/module_cmd/go.mod
@@ -0,0 +1,3 @@
module service

go 1.20
1 change: 1 addition & 0 deletions pkg/gci/testdata/module_cmd/internal/bar/lib.go
@@ -0,0 +1 @@
package bar
1 change: 1 addition & 0 deletions pkg/gci/testdata/module_cmd/internal/foo/lib.go
@@ -0,0 +1 @@
package foo
1 change: 1 addition & 0 deletions pkg/gci/testdata/module_cmd/internal/foo/lib.out.go
@@ -0,0 +1 @@
package foo
1 change: 1 addition & 0 deletions pkg/gci/testdata/module_cmd/internal/lib.go
@@ -0,0 +1 @@
package internal
12 changes: 6 additions & 6 deletions pkg/section/local_module.go
Expand Up @@ -47,10 +47,10 @@ func (m *LocalModule) Configure() error {
}

func findLocalModules() ([]string, error) {
packages, err := packages.Load(
pkgs, err := packages.Load(
// find the package in the current dir and load its module
// NeedFiles so there is some more info in package errors
&packages.Config{Mode: packages.NeedModule | packages.NeedFiles},
&packages.Config{Mode: packages.NeedModule | packages.NeedFiles | packages.NeedName},
".",
)
if err != nil {
Expand All @@ -59,10 +59,10 @@ func findLocalModules() ([]string, error) {

uniqueModules := make(map[string]struct{})

for _, pkg := range packages {
if len(pkg.Errors) != 0 {
return nil, fmt.Errorf("error reading local packages: %v", pkg.Errors)
}
for _, pkg := range pkgs {
//if len(pkg.Errors) != 0 {
// return nil, fmt.Errorf("error reading local packages: %v", pkg.Errors)
//}
if pkg.Module != nil {
uniqueModules[pkg.Module.Path] = struct{}{}
}
Expand Down

0 comments on commit 5e89d5b

Please sign in to comment.