Skip to content

Commit

Permalink
internal/ci: copy how internal/vcs sets a clean env for git tests
Browse files Browse the repository at this point in the history
Otherwise any users who have a git config that signs commits
automatically with a password prompt, like Paul,
would run into trouble as the tests would attempt such a prompt.

Expose the test helper in vcs to reuse it.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Iafb14a06a12868231be6f2ffb39d50387c4c8f52
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198298
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Paul Jolly <paul@myitcv.io>
  • Loading branch information
mvdan committed Jul 23, 2024
1 parent 6a9997f commit 9025f67
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.
3 changes: 3 additions & 0 deletions internal/ci/checks/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (

"github.com/go-quicktest/qt"
"golang.org/x/tools/txtar"

"cuelang.org/go/internal/vcs"
)

func TestCommits(t *testing.T) {
Expand All @@ -36,6 +38,7 @@ func TestCommits(t *testing.T) {
t.Logf("commit:\n%s", commit)

dir := t.TempDir()
vcs.InitTestEnv(t)
mustRunCmd(t, dir, "git", "init")
mustRunCmd(t, dir, "git",
"-c", "user.email=cueckoo@gmail.com",
Expand Down
38 changes: 38 additions & 0 deletions internal/vcs/vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
)

Expand Down Expand Up @@ -133,3 +136,38 @@ type vcsNotFoundError struct {
func (e *vcsNotFoundError) Error() string {
return fmt.Sprintf("%s VCS not found in any parent of %q", e.kind, e.dir)
}

func homeEnvName() string {
switch runtime.GOOS {
case "windows":
return "USERPROFILE"
case "plan9":
return "home"
default:
return "HOME"
}
}

// InitTestEnv sets up the environment so that any executed VCS command
// won't be affected by the outer level environment.
//
// Note that this function is exposed so we can reuse it from other test packages
// which also need to use Go tests with VCS systems.
// Exposing a test helper is fine for now, given this is an internal package.
func InitTestEnv(t testing.TB) {
t.Helper()
path := os.Getenv("PATH")
systemRoot := os.Getenv("SYSTEMROOT")
// First unset all environment variables to make a pristine environment.
for _, kv := range os.Environ() {
key, _, _ := strings.Cut(kv, "=")
t.Setenv(key, "")
os.Unsetenv(key)
}
os.Setenv("PATH", path)
os.Setenv(homeEnvName(), "/no-home")
// Must preserve SYSTEMROOT on Windows: https://github.com/golang/go/issues/25513 et al
if runtime.GOOS == "windows" {
os.Setenv("SYSTEMROOT", systemRoot)
}
}
35 changes: 1 addition & 34 deletions internal/vcs/vcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -53,7 +51,7 @@ func TestGit(t *testing.T) {
_, err = New("git", subdir)
qt.Assert(t, qt.ErrorMatches(err, `git VCS not found in any parent of ".+"`))

initTestEnv(t)
InitTestEnv(t)
mustRunCmd(t, dir, "git", "init")
v, err := New("git", subdir)
qt.Assert(t, qt.IsNil(err))
Expand Down Expand Up @@ -187,26 +185,6 @@ func TestGit(t *testing.T) {
qt.Assert(t, qt.IsTrue(statusmissing.Uncommitted))
}

// initTestEnv sets up the environment so that
// any executed VCS command won't be affected
// by the outer level environment.
func initTestEnv(t *testing.T) {
path := os.Getenv("PATH")
systemRoot := os.Getenv("SYSTEMROOT")
// First unset all environment variables to make a pristine environment.
for _, kv := range os.Environ() {
key, _, _ := strings.Cut(kv, "=")
t.Setenv(key, "")
os.Unsetenv(key)
}
os.Setenv("PATH", path)
os.Setenv(homeEnvName(), "/no-home")
// Must preserve SYSTEMROOT on Windows: https://github.com/golang/go/issues/25513 et al
if runtime.GOOS == "windows" {
os.Setenv("SYSTEMROOT", systemRoot)
}
}

func mustRunCmd(t *testing.T, dir string, exe string, args ...string) {
c := exec.Command(exe, args...)
c.Dir = dir
Expand All @@ -219,14 +197,3 @@ func skipIfNoExecutable(t *testing.T, exeName string) {
t.Skipf("cannot find %q executable: %v", exeName, err)
}
}

func homeEnvName() string {
switch runtime.GOOS {
case "windows":
return "USERPROFILE"
case "plan9":
return "home"
default:
return "HOME"
}
}

0 comments on commit 9025f67

Please sign in to comment.