Centralize Go validation and build environments
This commit is contained in:
@@ -10,8 +10,8 @@ import (
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"gitea.maximumdirect.net/eric/ReleaseKit/internal/gotool"
|
||||
"gitea.maximumdirect.net/eric/ReleaseKit/internal/process"
|
||||
"gitea.maximumdirect.net/eric/ReleaseKit/internal/versioncontract"
|
||||
)
|
||||
@@ -216,35 +216,13 @@ func hasLocalTag(ctx context.Context, repositoryDir, version string) (bool, erro
|
||||
}
|
||||
|
||||
func runDefaultValidation(ctx context.Context, repositoryDir string, output io.Writer) error {
|
||||
environment := map[string]string{
|
||||
"GOWORK": "off",
|
||||
"GOPROXY": "off",
|
||||
"GONOPROXY": "none",
|
||||
"GOSUMDB": "off",
|
||||
"GOTOOLCHAIN": "local",
|
||||
tool, err := gotool.New(ctx, repositoryDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("prepare Go toolchain: %w", err)
|
||||
}
|
||||
var writer io.Writer = output
|
||||
if output != nil {
|
||||
writer = &synchronizedWriter{writer: output}
|
||||
}
|
||||
return process.Run(ctx, process.Invocation{
|
||||
Args: []string{"go", "test", "-mod=readonly", "-count=1", "./..."},
|
||||
Dir: repositoryDir,
|
||||
Env: environment,
|
||||
Stdout: writer,
|
||||
Stderr: writer,
|
||||
})
|
||||
}
|
||||
|
||||
type synchronizedWriter struct {
|
||||
mu sync.Mutex
|
||||
writer io.Writer
|
||||
}
|
||||
|
||||
func (w *synchronizedWriter) Write(p []byte) (int, error) {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
return w.writer.Write(p)
|
||||
_, err = tool.Run(ctx, repositoryDir, gotool.Native, gotool.Target{}, output,
|
||||
"test", "-mod=readonly", "-count=1", "./...")
|
||||
return err
|
||||
}
|
||||
|
||||
func validStableVersion(version string) bool {
|
||||
|
||||
@@ -90,6 +90,60 @@ func TestCheckRunsDefaultOfflineGoValidation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultValidationDoesNotInheritGOFLAGSRunFilter(t *testing.T) {
|
||||
repository := newRepository(t, "# Example Product v1.2.3\n\nRelease note.\n")
|
||||
writeTestFile(t, repository, "failure_test.go", "package candidate\nimport \"testing\"\nfunc TestExpectedFailure(t *testing.T) { t.Fatal(\"expected fixture failure\") }\n")
|
||||
gitRun(t, repository, "add", "failure_test.go")
|
||||
gitRun(t, repository, "commit", "-m", "add failing validation fixture")
|
||||
|
||||
for _, source := range []string{"process environment", "Go environment file"} {
|
||||
t.Run(source, func(t *testing.T) {
|
||||
unsetCandidateEnv(t, "GOFLAGS", "GOOS", "GOARCH")
|
||||
goEnv := filepath.Join(t.TempDir(), "go-env")
|
||||
if source == "Go environment file" {
|
||||
if err := os.WriteFile(goEnv, []byte("GOFLAGS=-run=^$\nGOOS=windows\nGOARCH=arm64\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
if err := os.WriteFile(goEnv, nil, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("GOFLAGS", "-run=^$")
|
||||
t.Setenv("GOOS", "windows")
|
||||
t.Setenv("GOARCH", "arm64")
|
||||
}
|
||||
t.Setenv("GOENV", goEnv)
|
||||
var output bytes.Buffer
|
||||
_, err := Check(context.Background(), Request{
|
||||
RepositoryDir: repository,
|
||||
Product: "Example Product",
|
||||
Version: testVersion,
|
||||
Output: &output,
|
||||
})
|
||||
if err == nil || !strings.Contains(output.String(), "TestExpectedFailure") {
|
||||
t.Fatalf("Check() error = %v, output = %q; wanted the failing standard test to run", err, output.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func unsetCandidateEnv(t *testing.T, keys ...string) {
|
||||
t.Helper()
|
||||
for _, key := range keys {
|
||||
value, exists := os.LookupEnv(key)
|
||||
if err := os.Unsetenv(key); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if exists {
|
||||
_ = os.Setenv(key, value)
|
||||
} else {
|
||||
_ = os.Unsetenv(key)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckRejectsInvalidVersionsBeforeGitAccess(t *testing.T) {
|
||||
for _, version := range []string{"v01.2.3", "v1.2.3-alpha", "1.2.3"} {
|
||||
t.Run(version, func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user