Skip to content

Commit

Permalink
pkg/util/getwd_test.go: Use subtests to get more descriptive error me…
Browse files Browse the repository at this point in the history
…ssage.
  • Loading branch information
xiaq committed Apr 3, 2020
1 parent 541266d commit 5c8dbf4
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions pkg/util/getwd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,31 @@ func TestGetwd(t *testing.T) {
}

var tests = []struct {
name string
home string
chdir string
wantWd string
}{
// When the working directory is outside HOME, it is not abbreviated.
{"/does/not/exist", tmpdir, tmpdir},
{"wd outside HOME not abbreviated", "/does/not/exist", tmpdir, tmpdir},

// When the working directory is HOME, it is abbreviated to ~.
{tmpdir, tmpdir, "~"},
// When the working directory is within HOME, the HOME part is
// abbreviated to ~.
{tmpdir, tmpdir + "/a", filepath.Join("~", "a")},
{"wd at HOME abbreviated", tmpdir, tmpdir, "~"},
{"wd inside HOME abbreviated", tmpdir, tmpdir + "/a", filepath.Join("~", "a")},

// When HOME is "", working directory is not abbreviated.
{"", tmpdir, tmpdir},
// When HOME is "/", working directory is not abbreviated, even though
// technically it is within HOME.
{"/", tmpdir, tmpdir},
{"wd not abbreviated when HOME is empty", "", tmpdir, tmpdir},
{"wd not abbreviated when HOME is slash", "/", tmpdir, tmpdir},
}

oldHome := os.Getenv("HOME")
defer os.Setenv("HOME", oldHome)

for _, test := range tests {
os.Setenv("HOME", test.home)
mustOK(os.Chdir(test.chdir))
if gotWd := Getwd(); gotWd != test.wantWd {
t.Errorf("Getwd() -> %v, want %v", gotWd, test.wantWd)
}
t.Run(test.name, func(t *testing.T) {
os.Setenv("HOME", test.home)
mustOK(os.Chdir(test.chdir))
if gotWd := Getwd(); gotWd != test.wantWd {
t.Errorf("Getwd() -> %v, want %v", gotWd, test.wantWd)
}
})
}

// Remove the working directory, and test that Getwd returns "?".
Expand Down

0 comments on commit 5c8dbf4

Please sign in to comment.