Skip to content

Commit

Permalink
fix: running in Go workspace (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
silkeh committed Aug 16, 2023
1 parent fb0f11f commit 0ef7035
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 11 deletions.
7 changes: 7 additions & 0 deletions testdata/src/go.work
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
go 1.18

use (
./builtins
./custom
./tests
)
49 changes: 38 additions & 11 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,56 @@ package musttag

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"}

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

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 := json.Unmarshal(data, &module); err != nil {
return "", fmt.Errorf("decoding json: %w: %s", err, string(data))
cwd, err := getwd()
if err != nil {
return "", fmt.Errorf("getting wd: %w", err)
}

return module.Path, nil
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
}
}
}
54 changes: 54 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package musttag

import (
"testing"

"go-simpler.org/assert"
. "go-simpler.org/assert/dotimport"
)

func Test_getMainModule(t *testing.T) {
test := func(name, want, output string) {
t.Helper()
t.Run(name, func(t *testing.T) {
t.Helper()

gwd := getwd
co := commandOutput
defer func() {
getwd = gwd
commandOutput = co
}()

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

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

test("single module", "module1", `
{
"Path": "module1",
"Main": true,
"Dir": "/path/to/module1"
}`)

test("multiple modules", "module1", `
{
"Path": "module1",
"Main": true,
"Dir": "/path/to/module1"
}
{
"Path": "module2",
"Main": true,
"Dir": "/path/to/module2"
}`)
}

0 comments on commit 0ef7035

Please sign in to comment.