Skip to content

Commit

Permalink
resolve/gosource: add rel file query with multiple main packages test
Browse files Browse the repository at this point in the history
Add a testcase for the gosource resolver that specifies as file query pattern
cmd/**/main.go and runs the goresolver from a different directory then the test
(aka app dir) directory.

This testcase currently fails because the relative file query patterns are not
relative to the specified working dir (app dir) but to the cwd
(golang/go#65965).

The existing testcase query_main_file is refactored for this purpose
The test_config.json files got a new field WorkingDir, the $WORKDIR
test_config.json variable is renamed to $TESTDIR.
  • Loading branch information
fho committed Mar 4, 2024
1 parent 92a4536 commit d68378d
Show file tree
Hide file tree
Showing 38 changed files with 223 additions and 163 deletions.
54 changes: 30 additions & 24 deletions internal/resolve/gosource/gosource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,33 @@ import (

"github.com/simplesurance/baur/v3/internal/log"
"github.com/simplesurance/baur/v3/internal/prettyprint"
"github.com/simplesurance/baur/v3/internal/testutils/ostest"
"github.com/simplesurance/baur/v3/internal/testutils/strtest"
)

type testCfg struct {
Cfg struct {
Environment []string
Queries []string
BuildFlags []string
Tests bool
WorkingDir string
}
ExpectedResults []string
}

func testCfgFromFile(t *testing.T, path string) *testCfg {
var result testCfg
fileContent, err := os.ReadFile(path)
require.NoError(t, err)
require.NoError(t, json.Unmarshal(fileContent, &result))

return &result
}

func TestResolve(t *testing.T) {
const testCfgFilename = "test_config.json"

type testCfg struct {
Cfg struct {
Environment []string
Queries []string
BuildFlags []string
Tests bool
}
ExpectedResults []string
}

testdataDirs, err := filepath.Glob(filepath.Join("testdata", "*"))
require.NoError(t, err)

Expand All @@ -38,33 +49,28 @@ func TestResolve(t *testing.T) {

for _, dir := range testdataDirs {
t.Run(dir, func(t *testing.T) {
var testCfg testCfg
log.RedirectToTestingLog(t)

log.StdLogger.SetOutput(log.NewTestLogOutput(t))
testCfg := testCfgFromFile(t, filepath.Join(dir, testCfgFilename))
require.NotEmpty(t, testCfg.Cfg.WorkingDir)
require.NotEmpty(t, testCfg.Cfg.Queries)
require.NotEmpty(t, testCfg.ExpectedResults)

require.NoError(t, os.Chdir(dir))

fileContent, err := os.ReadFile(testCfgFilename)
require.NoError(t, err)

require.ErrorIs(t, err, json.Unmarshal(fileContent, &testCfg))

cwd, err := os.Getwd()
require.NoError(t, err)
ostest.Chdir(t, filepath.FromSlash(strings.ReplaceAll(testCfg.Cfg.WorkingDir, "$TESTDIR", dir)))

for i := range testCfg.Cfg.Environment {
testCfg.Cfg.Environment[i] = strings.ReplaceAll(testCfg.Cfg.Environment[i], "$WORKDIR", cwd)
testCfg.Cfg.Environment[i] = strings.ReplaceAll(testCfg.Cfg.Environment[i], "$TESTDIR", dir)
}

for i := range testCfg.ExpectedResults {
testCfg.ExpectedResults[i] = strings.ReplaceAll(testCfg.ExpectedResults[i], "$WORKDIR", cwd)
testCfg.ExpectedResults[i] = strings.ReplaceAll(testCfg.ExpectedResults[i], "$TESTDIR", dir)
// The path separators in the test config are Unix style "/", they need to be converted to "\" when running on Windows
testCfg.ExpectedResults[i] = filepath.FromSlash(testCfg.ExpectedResults[i])
}

resolvedFiles, err := NewResolver(t.Logf).Resolve(
context.Background(),
cwd,
dir,
testCfg.Cfg.Environment,
testCfg.Cfg.BuildFlags,
testCfg.Cfg.Tests,
Expand Down
17 changes: 10 additions & 7 deletions internal/resolve/gosource/testdata/embedded_file/test_config.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
{
"Cfg": {
"Queries": ["./..."],
"WorkingDir": "$TESTDIR",
"Queries": [
"./..."
],
"Tests": true
},
"ExpectedResults": [
"$WORKDIR/main.go",
"$WORKDIR/embed_test.go",
"$WORKDIR/hello.txt",
"$WORKDIR/data/a.txt",
"$WORKDIR/data/b.txt",
"$WORKDIR/hello_test.txt"
"$TESTDIR/main.go",
"$TESTDIR/embed_test.go",
"$TESTDIR/hello.txt",
"$TESTDIR/data/a.txt",
"$TESTDIR/data/b.txt",
"$TESTDIR/hello_test.txt"
]
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
{
"Cfg": {
"Environment": [
"GO111MODULE=on",
"GOFLAGS=-mod=readonly"
],
"Queries": ["file=./main.go"]
},
"ExpectedResults": [
"$WORKDIR/main.go",
"$WORKDIR/generator/generator.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/dce.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/doc.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/hash.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/marshal.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/node.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/node_net.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/sql.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/time.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/util.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/uuid.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/version1.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/version4.go"
]
"Cfg": {
"WorkingDir": "$TESTDIR",
"Environment": [
"GO111MODULE=on",
"GOFLAGS=-mod=readonly"
],
"Queries": [
"file=./main.go"
]
},
"ExpectedResults": [
"$TESTDIR/main.go",
"$TESTDIR/generator/generator.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/dce.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/doc.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/hash.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/marshal.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/node.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/node_net.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/sql.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/time.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/util.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/uuid.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/version1.go",
"$GOMODCACHE/github.com/google/uuid@v1.1.1/version4.go"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"fmt"

"github.com/simplesurance/baur-test/intstr"
)

func main() {
fmt.Println(intstr.One)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package intstr

const One = "One"
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"Cfg": {
"WorkingDir": "$TESTDIR/..",
"Environment": [
"GO111MODULE=on",
"GOFLAGS=-mod=vendor"
],
"Queries": [
"fileglob=cmd/**/main.go"
]
},
"ExpectedResults": [
"$TESTDIR/cmd/one/main.go",
"$TESTDIR/cmd/two/main.go",
"$TESTDIR/generator/generator.go",
"$TESTDIR/intstr/intstr.go",
"$TESTDIR/vendor/github.com/google/uuid/dce.go",
"$TESTDIR/vendor/github.com/google/uuid/doc.go",
"$TESTDIR/vendor/github.com/google/uuid/hash.go",
"$TESTDIR/vendor/github.com/google/uuid/marshal.go",
"$TESTDIR/vendor/github.com/google/uuid/node.go",
"$TESTDIR/vendor/github.com/google/uuid/node_net.go",
"$TESTDIR/vendor/github.com/google/uuid/sql.go",
"$TESTDIR/vendor/github.com/google/uuid/time.go",
"$TESTDIR/vendor/github.com/google/uuid/util.go",
"$TESTDIR/vendor/github.com/google/uuid/uuid.go",
"$TESTDIR/vendor/github.com/google/uuid/version1.go",
"$TESTDIR/vendor/github.com/google/uuid/version4.go"
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"Cfg": {
"WorkingDir": "$TESTDIR",
"Environment": [
"GO111MODULE=on",
"GOFLAGS=-mod=vendor"
Expand All @@ -11,21 +12,21 @@
"Tests": true
},
"ExpectedResults": [
"$WORKDIR/main.go",
"$WORKDIR/main_test.go",
"$WORKDIR/generator/generator.go",
"$WORKDIR/generator/generator_test.go",
"$WORKDIR/vendor/github.com/google/uuid/dce.go",
"$WORKDIR/vendor/github.com/google/uuid/doc.go",
"$WORKDIR/vendor/github.com/google/uuid/hash.go",
"$WORKDIR/vendor/github.com/google/uuid/marshal.go",
"$WORKDIR/vendor/github.com/google/uuid/node.go",
"$WORKDIR/vendor/github.com/google/uuid/node_net.go",
"$WORKDIR/vendor/github.com/google/uuid/sql.go",
"$WORKDIR/vendor/github.com/google/uuid/time.go",
"$WORKDIR/vendor/github.com/google/uuid/util.go",
"$WORKDIR/vendor/github.com/google/uuid/uuid.go",
"$WORKDIR/vendor/github.com/google/uuid/version1.go",
"$WORKDIR/vendor/github.com/google/uuid/version4.go"
"$TESTDIR/main.go",
"$TESTDIR/main_test.go",
"$TESTDIR/generator/generator.go",
"$TESTDIR/generator/generator_test.go",
"$TESTDIR/vendor/github.com/google/uuid/dce.go",
"$TESTDIR/vendor/github.com/google/uuid/doc.go",
"$TESTDIR/vendor/github.com/google/uuid/hash.go",
"$TESTDIR/vendor/github.com/google/uuid/marshal.go",
"$TESTDIR/vendor/github.com/google/uuid/node.go",
"$TESTDIR/vendor/github.com/google/uuid/node_net.go",
"$TESTDIR/vendor/github.com/google/uuid/sql.go",
"$TESTDIR/vendor/github.com/google/uuid/time.go",
"$TESTDIR/vendor/github.com/google/uuid/util.go",
"$TESTDIR/vendor/github.com/google/uuid/uuid.go",
"$TESTDIR/vendor/github.com/google/uuid/version1.go",
"$TESTDIR/vendor/github.com/google/uuid/version4.go"
]
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
{
"Cfg": {
"WorkingDir": "$TESTDIR",
"Environment": [
"GO111MODULE=on",
"GOFLAGS=-mod=vendor"
],
"Queries": ["fileglob=**/*_test.go"],
"Queries": [
"fileglob=**/*_test.go"
],
"Tests": true
},
"ExpectedResults": [
"$WORKDIR/main.go",
"$WORKDIR/main_test.go",
"$WORKDIR/generator/generator.go",
"$WORKDIR/generator/generator_test.go",
"$WORKDIR/vendor/github.com/google/uuid/dce.go",
"$WORKDIR/vendor/github.com/google/uuid/doc.go",
"$WORKDIR/vendor/github.com/google/uuid/hash.go",
"$WORKDIR/vendor/github.com/google/uuid/marshal.go",
"$WORKDIR/vendor/github.com/google/uuid/node.go",
"$WORKDIR/vendor/github.com/google/uuid/node_net.go",
"$WORKDIR/vendor/github.com/google/uuid/sql.go",
"$WORKDIR/vendor/github.com/google/uuid/time.go",
"$WORKDIR/vendor/github.com/google/uuid/util.go",
"$WORKDIR/vendor/github.com/google/uuid/uuid.go",
"$WORKDIR/vendor/github.com/google/uuid/version1.go",
"$WORKDIR/vendor/github.com/google/uuid/version4.go"
"$TESTDIR/main.go",
"$TESTDIR/main_test.go",
"$TESTDIR/generator/generator.go",
"$TESTDIR/generator/generator_test.go",
"$TESTDIR/vendor/github.com/google/uuid/dce.go",
"$TESTDIR/vendor/github.com/google/uuid/doc.go",
"$TESTDIR/vendor/github.com/google/uuid/hash.go",
"$TESTDIR/vendor/github.com/google/uuid/marshal.go",
"$TESTDIR/vendor/github.com/google/uuid/node.go",
"$TESTDIR/vendor/github.com/google/uuid/node_net.go",
"$TESTDIR/vendor/github.com/google/uuid/sql.go",
"$TESTDIR/vendor/github.com/google/uuid/time.go",
"$TESTDIR/vendor/github.com/google/uuid/util.go",
"$TESTDIR/vendor/github.com/google/uuid/uuid.go",
"$TESTDIR/vendor/github.com/google/uuid/version1.go",
"$TESTDIR/vendor/github.com/google/uuid/version4.go"
]
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
{
"Cfg": {
"WorkingDir": "$TESTDIR",
"Environment": [
"GO111MODULE=on",
"GOFLAGS=-mod=vendor"
],
"Queries": ["fileglob=**/*_test.go"],
"BuildFlags": ["-tags=generatortest"],
"Queries": [
"fileglob=**/*_test.go"
],
"BuildFlags": [
"-tags=generatortest"
],
"Tests": true
},
"ExpectedResults": [
"$WORKDIR/generator/generator.go",
"$WORKDIR/generator/generator_test.go",
"$WORKDIR/vendor/github.com/google/uuid/dce.go",
"$WORKDIR/vendor/github.com/google/uuid/doc.go",
"$WORKDIR/vendor/github.com/google/uuid/hash.go",
"$WORKDIR/vendor/github.com/google/uuid/marshal.go",
"$WORKDIR/vendor/github.com/google/uuid/node.go",
"$WORKDIR/vendor/github.com/google/uuid/node_net.go",
"$WORKDIR/vendor/github.com/google/uuid/sql.go",
"$WORKDIR/vendor/github.com/google/uuid/time.go",
"$WORKDIR/vendor/github.com/google/uuid/util.go",
"$WORKDIR/vendor/github.com/google/uuid/uuid.go",
"$WORKDIR/vendor/github.com/google/uuid/version1.go",
"$WORKDIR/vendor/github.com/google/uuid/version4.go"
"$TESTDIR/generator/generator.go",
"$TESTDIR/generator/generator_test.go",
"$TESTDIR/vendor/github.com/google/uuid/dce.go",
"$TESTDIR/vendor/github.com/google/uuid/doc.go",
"$TESTDIR/vendor/github.com/google/uuid/hash.go",
"$TESTDIR/vendor/github.com/google/uuid/marshal.go",
"$TESTDIR/vendor/github.com/google/uuid/node.go",
"$TESTDIR/vendor/github.com/google/uuid/node_net.go",
"$TESTDIR/vendor/github.com/google/uuid/sql.go",
"$TESTDIR/vendor/github.com/google/uuid/time.go",
"$TESTDIR/vendor/github.com/google/uuid/util.go",
"$TESTDIR/vendor/github.com/google/uuid/uuid.go",
"$TESTDIR/vendor/github.com/google/uuid/version1.go",
"$TESTDIR/vendor/github.com/google/uuid/version4.go"
]
}

This file was deleted.

Loading

0 comments on commit d68378d

Please sign in to comment.