Skip to content

Commit

Permalink
Rework LocalModule based on review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Petr Korobeinikov <pkorobeinikov@gmail.com>
  • Loading branch information
petr-korobeinikov committed Mar 7, 2024
1 parent 7bb14ac commit 03fdbc0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 98 deletions.
25 changes: 3 additions & 22 deletions pkg/gci/gci_test.go
Expand Up @@ -21,10 +21,6 @@ func init() {
}

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

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)
Expand Down Expand Up @@ -56,10 +52,10 @@ func chdir(t *testing.T, dir string) {
func readConfig(t *testing.T, configPath string) *config.Config {
rawConfig, err := os.ReadFile(configPath)
require.NoError(t, err)
config, err := config.ParseConfig(string(rawConfig))
cfg, err := config.ParseConfig(string(rawConfig))
require.NoError(t, err)

return config
return cfg
}

func TestRunWithLocalModule(t *testing.T) {
Expand Down Expand Up @@ -126,20 +122,5 @@ func TestRunWithLocalModuleWithPackageLoadFailure(t *testing.T) {

chdir(t, dir)
_, err := config.ParseConfig(configContent)
require.ErrorContains(t, err, "failed to load local modules: ")
}

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"
goModContent := "module example.com/foo\n"
require.NoError(t, os.WriteFile(filepath.Join(dir, "go.mod"), []byte(goModContent), 0o644))

chdir(t, dir)
_, err := config.ParseConfig(configContent)
require.ErrorContains(t, err, "error reading local packages: ")
require.ErrorContains(t, err, dir)
require.ErrorContains(t, err, "go.mod: open go.mod: no such file or directory")
}
23 changes: 0 additions & 23 deletions pkg/gci/testdata.go
Expand Up @@ -1293,29 +1293,6 @@ import (
"fmt"
"net"
)
`,
},
{
"basic module",
// implicitly relies on the local module name being: github.com/daixiang0/gci
`sections:
- Standard
- LocalModule
`,
`package main
import (
"os"
"github.com/daixiang0/gci/cmd/gci"
)
`,
`package main
import (
"os"
"github.com/daixiang0/gci/cmd/gci"
)
`,
},
}
64 changes: 11 additions & 53 deletions pkg/section/local_module.go
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"golang.org/x/mod/modfile"
"golang.org/x/tools/go/packages"

"github.com/daixiang0/gci/pkg/parse"
"github.com/daixiang0/gci/pkg/specificity"
Expand All @@ -15,15 +14,12 @@ import (
const LocalModuleType = "localmodule"

type LocalModule struct {
Paths []string
Path string
}

func (m *LocalModule) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecificity {
for _, modPath := range m.Paths {
// also check path etc.
if strings.HasPrefix(spec.Path, modPath) {
return specificity.LocalModule{}
}
if strings.HasPrefix(spec.Path, m.Path) {
return specificity.LocalModule{}
}

return specificity.MisMatch{}
Expand All @@ -40,59 +36,21 @@ func (m *LocalModule) Type() string {
// Configure configures the module section by finding the module
// for the current path
func (m *LocalModule) Configure() error {
modPaths, err := findLocalModules()
modPath, err := findLocalModule()
if err != nil {
return err
}
m.Paths = modPaths
return nil
}

func findLocalModules() ([]string, error) {
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},
".",
)
if err != nil {
return nil, fmt.Errorf("failed to load local modules: %v", err)
}

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

for _, pkg := range pkgs {
if len(pkg.Errors) != 0 && !errorsContainListError(pkg.Errors) {
return nil, fmt.Errorf("error reading local packages: %v", pkg.Errors)
}
if pkg.Module != nil {
uniqueModules[pkg.Module.Path] = struct{}{}
} else {
b, err := os.ReadFile("go.mod")
if err != nil {
return nil, fmt.Errorf("failed to read go.mod file directly: %v", err)
}

name := modfile.ModulePath(b)
uniqueModules[name] = struct{}{}
m.Path = modPath

}
}

modPaths := make([]string, 0, len(uniqueModules))
for mod := range uniqueModules {
modPaths = append(modPaths, mod)
}

return modPaths, nil
return nil
}

func errorsContainListError(errors []packages.Error) bool {
for _, v := range errors {
if v.Kind == packages.ListError {
return true
}
func findLocalModule() (string, error) {
b, err := os.ReadFile("go.mod")
if err != nil {
return "", fmt.Errorf("go.mod: %w", err)
}

return false
return modfile.ModulePath(b), nil
}

0 comments on commit 03fdbc0

Please sign in to comment.