Switch uses of os.Getenv that check for empty string to os.LookupEnv.

Whether the variable was found is already returned by syscall.Getenv.
os.Getenv drops this value while os.Lookupenv passes it along.

PiperOrigin-RevId: 351674032
This commit is contained in:
Dean Deng
2021-01-13 15:15:20 -08:00
committed by gVisor bot
parent f34aaf7ef1
commit 1efe0ebc59
6 changed files with 16 additions and 17 deletions
+2 -2
View File
@@ -191,8 +191,8 @@ func shuffle(b []int) {
}
func createFile(t *testing.T, size int64, initQueue bool) int {
tmpDir := os.Getenv("TEST_TMPDIR")
if tmpDir == "" {
tmpDir, ok := os.LookupEnv("TEST_TMPDIR")
if !ok {
tmpDir = os.Getenv("TMPDIR")
}
f, err := ioutil.TempFile(tmpDir, "sharedmem_test")
+7 -7
View File
@@ -83,11 +83,10 @@ func ConfigureExePath() error {
// TmpDir returns the absolute path to a writable directory that can be used as
// scratch by the test.
func TmpDir() string {
dir := os.Getenv("TEST_TMPDIR")
if dir == "" {
dir = "/tmp"
if dir, ok := os.LookupEnv("TEST_TMPDIR"); ok {
return dir
}
return dir
return "/tmp"
}
// Logger is a simple logging wrapper.
@@ -543,7 +542,7 @@ func IsStatic(filename string) (bool, error) {
//
// See https://docs.bazel.build/versions/master/test-encyclopedia.html#role-of-the-test-runner.
func TouchShardStatusFile() error {
if statusFile := os.Getenv("TEST_SHARD_STATUS_FILE"); statusFile != "" {
if statusFile, ok := os.LookupEnv("TEST_SHARD_STATUS_FILE"); ok {
cmd := exec.Command("touch", statusFile)
if b, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("touch %q failed:\n output: %s\n error: %s", statusFile, string(b), err.Error())
@@ -565,8 +564,9 @@ func TestIndicesForShard(numTests int) ([]int, error) {
shardTotal = 1
)
indexStr, totalStr := os.Getenv("TEST_SHARD_INDEX"), os.Getenv("TEST_TOTAL_SHARDS")
if indexStr != "" && totalStr != "" {
indexStr, indexOk := os.LookupEnv("TEST_SHARD_INDEX")
totalStr, totalOk := os.LookupEnv("TEST_TOTAL_SHARDS")
if indexOk && totalOk {
// Parse index and total to ints.
var err error
shardIndex, err = strconv.Atoi(indexStr)
+3 -4
View File
@@ -24,11 +24,10 @@ import (
)
func tmpDir() string {
dir := os.Getenv("TEST_TMPDIR")
if dir == "" {
dir = "/tmp"
if dir, ok := os.LookupEnv("TEST_TMPDIR"); ok {
return dir
}
return dir
return "/tmp"
}
type dir struct {
+1 -1
View File
@@ -114,7 +114,7 @@ func NewFromFlags() (*Config, error) {
if len(conf.RootDir) == 0 {
// If not set, set default root dir to something (hopefully) user-writeable.
conf.RootDir = "/var/run/runsc"
if runtimeDir := os.Getenv("XDG_RUNTIME_DIR"); runtimeDir != "" {
if runtimeDir, ok := os.LookupEnv("XDG_RUNTIME_DIR"); ok {
conf.RootDir = filepath.Join(runtimeDir, "runsc")
}
}
+2 -2
View File
@@ -353,8 +353,8 @@ func setup(t *testing.T) (*criutil.Crictl, func(), error) {
// because the shims will be installed there, and containerd may infer
// the binary name and search the PATH.
runtimeDir := path.Dir(runtime)
modifiedPath := os.Getenv("PATH")
if modifiedPath != "" {
modifiedPath, ok := os.LookupEnv("PATH")
if ok {
modifiedPath = ":" + modifiedPath // We prepend below.
}
modifiedPath = path.Dir(getContainerd()) + modifiedPath
+1 -1
View File
@@ -366,7 +366,7 @@ func registerProfile(mux *http.ServeMux) {
}
func envFlagString(name, def string) string {
if val := os.Getenv(name); val != "" {
if val, ok := os.LookupEnv(name); ok {
return val
}
return def