Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use go mod edit to get the main module #72

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 11 additions & 39 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,29 @@

import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
)

var (
getwd = os.Getwd
commandOutput = func(name string, args ...string) (string, error) {
output, err := exec.Command(name, args...).Output()
return string(output), err
}
)

func getMainModule() (string, error) {
args := []string{"go", "list", "-m", "-json"}
args := [...]string{"go", "mod", "edit", "-json"}

output, err := commandOutput(args[0], args[1:]...)
out, err := exec.Command(args[0], args[1:]...).Output()
if err != nil {
return "", fmt.Errorf("running `%s`: %w", strings.Join(args, " "), err)
return "", fmt.Errorf("running %q: %w", strings.Join(args[:], " "), err)

Check warning on line 15 in utils.go

View check run for this annotation

Codecov / codecov/patch

utils.go#L15

Added line #L15 was not covered by tests
}

cwd, err := getwd()
if err != nil {
return "", fmt.Errorf("getting wd: %w", err)
var info struct {
Module struct {
Path string `json:"Path"`
} `json:"Module"`
}

decoder := json.NewDecoder(strings.NewReader(output))

for {
// multiple JSON objects will be returned when using Go workspaces; see #63 for details.
var module struct {
Path string `json:"Path"`
Main bool `json:"Main"`
Dir string `json:"Dir"`
GoMod string `json:"GoMod"`
GoVersion string `json:"GoVersion"`
}
if err := decoder.Decode(&module); err != nil {
if errors.Is(err, io.EOF) {
return "", fmt.Errorf("main module not found\n%s", output)
}
return "", fmt.Errorf("decoding json: %w\n%s", err, output)
}

if module.Main && strings.HasPrefix(cwd, module.Dir) {
return module.Path, nil
}
if err := json.Unmarshal(out, &info); err != nil {
return "", fmt.Errorf("decoding module info: %w\n%s", err, out)

Check warning on line 24 in utils.go

View check run for this annotation

Codecov / codecov/patch

utils.go#L24

Added line #L24 was not covered by tests
}

return info.Module.Path, nil
}

// based on golang.org/x/tools/imports.VendorlessPath
Expand Down
36 changes: 0 additions & 36 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,6 @@ import (
. "go-simpler.org/assert/EF"
)

func Test_getMainModule(t *testing.T) {
tests := map[string]struct {
want, output string
}{
"single module": {
want: "module1",
output: `
{"Path": "module1", "Main": true, "Dir": "/path/to/module1"}`,
},
"multiple modules": {
want: "module1",
output: `
{"Path": "module1", "Main": true, "Dir": "/path/to/module1"}
{"Path": "module2", "Main": true, "Dir": "/path/to/module2"}`,
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
gwd, co := getwd, commandOutput
defer func() { getwd, commandOutput = gwd, co }()

getwd = func() (string, error) {
return "/path/to/module1/pkg", nil
}
commandOutput = func(string, ...string) (string, error) {
return test.output, nil
}

got, err := getMainModule()
assert.NoErr[F](t, err)
assert.Equal[E](t, got, test.want)
})
}
}

func Test_cutVendor(t *testing.T) {
tests := []struct {
path, want string
Expand Down
Loading