Skip to content

Commit

Permalink
cmd/cue: stop trying to set $HOME in script_test.go
Browse files Browse the repository at this point in the history
We used to have to try pretty hard to set up the OS-dependent versions
of $HOME and $XDG_CONFIG_HOME in our testscripts,
since we used to rely directly on APIs like os.UserConfigDir,
which error if they can't make use of those environment variables.

Since then, we have switched to $CUE_CONFIG_DIR and $CUE_CACHE_DIR,
so we only need to make sure that they are set so that our code
doesn't fall back to os.UserConfigDir and os.UserCacheDir.

In particular, only $CUE_CACHE_DIR actually needs to be set for now,
as our testscripts only write cache files, but no config files yet.
While here, put the cache directory under $WORK/.tmp to mimic
the rest of our directories, so it does not match `./...` patterns.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I2e1e0271f2e9d191d17f9ae3710763c2c9ea9381
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1177302
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@gmail.com>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
  • Loading branch information
mvdan committed Feb 23, 2024
1 parent 32a72ae commit ff8d497
Showing 1 changed file with 4 additions and 38 deletions.
42 changes: 4 additions & 38 deletions cmd/cue/cmd/script_test.go
Expand Up @@ -25,8 +25,6 @@ import (
"os"
"path"
"path/filepath"
"runtime"
goruntime "runtime"
"strings"
"testing"
"time"
Expand All @@ -45,10 +43,6 @@ import (
"cuelang.org/go/internal/registrytest"
)

const (
homeDirName = ".user-home"
)

// TestLatest checks that the examples match the latest language standard,
// even if still valid in backwards compatibility mode.
func TestLatest(t *testing.T) {
Expand Down Expand Up @@ -166,24 +160,10 @@ func TestScript(t *testing.T) {
return err
}

// Set up a home dir within work dir with a . prefix so that the
// Go/CUE pattern ./... does not descend into it.
home := filepath.Join(e.WorkDir, homeDirName)
if err := os.Mkdir(home, 0777); err != nil {
return err
}

e.Vars = append(e.Vars,
"GOPROXY="+srv.URL,
"GONOSUMDB=*", // GOPROXY is a private proxy
homeEnvName()+"="+home,
)
if runtime.GOOS == "windows" {
// os.UserConfigDir on Windows requires %AppData% to be set,
// and it does not fall back to the home directory in any way.
// Set it up as well, so that cmd/cue can function normally.
e.Vars = append(e.Vars, "AppData="+filepath.Join(home, "appdata"))
}
entries, err := os.ReadDir(e.WorkDir)
if err != nil {
return fmt.Errorf("cannot read workdir: %v", err)
Expand Down Expand Up @@ -217,10 +197,10 @@ func TestScript(t *testing.T) {
// This enables some tests to construct their own malformed
// CUE_REGISTRY values that still refer to the test registry.
"DEBUG_REGISTRY"+regID+"_HOST="+reg.Host(),
// While os.UserCacheDir on Linux is derived from $HOME,
// under Windows it's not, so avoid polluting the system cache
// directory by setting up the cache specifically.
"CUE_CACHE_DIR="+filepath.Join(e.WorkDir, "tmp/cache"),
// Some tests execute cue commands that need to write cache files.
// Since os.UserCacheDir relies on OS-specific env vars that we don't set,
// explicitly set up the cache directory somewhere predictable.
"CUE_CACHE_DIR="+filepath.Join(e.WorkDir, ".tmp/cache"),
)
e.Defer(reg.Close)
}
Expand Down Expand Up @@ -332,20 +312,6 @@ func tsExpand(ts *testscript.TestScript, s string) string {
})
}

// homeEnvName extracts the logic from os.UserHomeDir to get the
// name of the environment variable that should be used when
// setting the user's home directory
func homeEnvName() string {
switch goruntime.GOOS {
case "windows":
return "USERPROFILE"
case "plan9":
return "home"
default:
return "HOME"
}
}

func mainTestStdinPipe() error {
// Like MainTest, but sets stdin to a pipe,
// to emulate stdin reads like a terminal.
Expand Down

0 comments on commit ff8d497

Please sign in to comment.