ref(helm): refactor cleanup of environment after tests run

parent a29e6109
......@@ -85,13 +85,14 @@ func TestCreateStarterCmd(t *testing.T) {
if err != nil {
t.Fatal(err)
}
old := settings.Home
settings.Home = thome
cleanup := resetEnv()
defer func() {
settings.Home = old
os.RemoveAll(thome.String())
cleanup()
}()
settings.Home = thome
// Create a starter.
starterchart := filepath.Join(thome.String(), "starters")
os.Mkdir(starterchart, 0755)
......
......@@ -29,17 +29,18 @@ import (
)
func TestDependencyBuildCmd(t *testing.T) {
oldhome := settings.Home
hh, err := tempHelmHome(t)
if err != nil {
t.Fatal(err)
}
settings.Home = hh
cleanup := resetEnv()
defer func() {
os.RemoveAll(hh.String())
settings.Home = oldhome
cleanup()
}()
settings.Home = hh
srv := repotest.NewServer(hh.String())
defer srv.Stop()
_, err = srv.CopyCharts("testdata/testcharts/*.tgz")
......
......@@ -34,18 +34,18 @@ import (
)
func TestDependencyUpdateCmd(t *testing.T) {
// Set up a testing helm home
oldhome := settings.Home
hh, err := tempHelmHome(t)
if err != nil {
t.Fatal(err)
}
settings.Home = hh
cleanup := resetEnv()
defer func() {
os.RemoveAll(hh.String())
settings.Home = oldhome
cleanup()
}()
settings.Home = hh
srv := repotest.NewServer(hh.String())
defer srv.Stop()
copied, err := srv.CopyCharts("testdata/testcharts/*.tgz")
......@@ -129,18 +129,18 @@ func TestDependencyUpdateCmd(t *testing.T) {
}
func TestDependencyUpdateCmd_SkipRefresh(t *testing.T) {
// Set up a testing helm home
oldhome := settings.Home
hh, err := tempHelmHome(t)
if err != nil {
t.Fatal(err)
}
settings.Home = hh
cleanup := resetEnv()
defer func() {
os.RemoveAll(hh.String())
settings.Home = oldhome
cleanup()
}()
settings.Home = hh
srv := repotest.NewServer(hh.String())
defer srv.Stop()
copied, err := srv.CopyCharts("testdata/testcharts/*.tgz")
......
......@@ -32,15 +32,16 @@ func TestFetchCmd(t *testing.T) {
if err != nil {
t.Fatal(err)
}
old := settings.Home
settings.Home = hh
cleanup := resetEnv()
defer func() {
settings.Home = old
os.RemoveAll(hh.String())
cleanup()
}()
srv := repotest.NewServer(hh.String())
defer srv.Stop()
settings.Home = hh
// all flags will get "--home=TMDIR -d outdir" appended.
tests := []struct {
name string
......
......@@ -233,13 +233,8 @@ func ensureTestHome(home helmpath.Home, t *testing.T) error {
}
func TestRootCmd(t *testing.T) {
// reset env
defer func(origEnv []string) {
for _, pair := range origEnv {
kv := strings.SplitN(pair, "=", 2)
os.Setenv(kv[0], kv[1])
}
}(os.Environ())
cleanup := resetEnv()
defer cleanup()
tests := []struct {
name string
......@@ -312,3 +307,15 @@ func TestRootCmd(t *testing.T) {
})
}
}
func resetEnv() func() {
origSettings := settings
origEnv := os.Environ()
return func() {
settings = origSettings
for _, pair := range origEnv {
kv := strings.SplitN(pair, "=", 2)
os.Setenv(kv[0], kv[1])
}
}
}
......@@ -140,13 +140,14 @@ func TestInitCmd_dryRun(t *testing.T) {
if err != nil {
t.Fatal(err)
}
dbg := settings.Debug
settings.Debug = true
cleanup := resetEnv()
defer func() {
os.Remove(home)
settings.Debug = dbg
cleanup()
}()
settings.Debug = true
var buf bytes.Buffer
fc := fake.NewSimpleClientset()
cmd := &initCmd{
......
......@@ -143,14 +143,15 @@ func TestPackage(t *testing.T) {
}
ensureTestHome(helmpath.Home(tmp), t)
oldhome := settings.Home
settings.Home = helmpath.Home(tmp)
cleanup := resetEnv()
defer func() {
settings.Home = oldhome
os.Chdir(origDir)
os.RemoveAll(tmp)
cleanup()
}()
settings.Home = helmpath.Home(tmp)
for _, tt := range tests {
buf := bytes.NewBuffer(nil)
c := newPackageCmd(buf)
......
......@@ -63,25 +63,13 @@ func TestManuallyProcessArgs(t *testing.T) {
}
// resetEnv sets an env var, and returns a defer function to reset the env
func resetEnv(name, val string) func() {
original, ok := os.LookupEnv(name)
os.Setenv(name, val)
if ok {
return func() { os.Setenv(name, original) }
}
return func() { os.Unsetenv(name) }
}
func TestLoadPlugins(t *testing.T) {
// Set helm home to point to testdata
old := settings.Home
cleanup := resetEnv()
defer cleanup()
settings.Home = "testdata/helmhome"
cleanup := resetEnv("HELM_HOME", settings.Home.String())
defer func() {
settings.Home = old
cleanup()
}()
os.Setenv("HELM_HOME", settings.Home.String())
hh := settings.Home
out := bytes.NewBuffer(nil)
......@@ -148,14 +136,12 @@ func TestLoadPlugins(t *testing.T) {
}
func TestLoadPlugins_HelmNoPlugins(t *testing.T) {
// Set helm home to point to testdata
old := settings.Home
cleanup := resetEnv()
defer cleanup()
settings.Home = "testdata/helmhome"
cleanup := resetEnv("HELM_NO_PLUGINS", "1")
defer func() {
settings.Home = old
cleanup()
}()
os.Setenv("HELM_NO_PLUGINS", "1")
out := bytes.NewBuffer(nil)
cmd := &cobra.Command{}
......
......@@ -33,17 +33,18 @@ func TestRepoAddCmd(t *testing.T) {
t.Fatal(err)
}
oldhome := settings.Home
settings.Home = thome
cleanup := resetEnv()
defer func() {
srv.Stop()
settings.Home = oldhome
os.Remove(thome.String())
cleanup()
}()
if err := ensureTestHome(thome, t); err != nil {
t.Fatal(err)
}
settings.Home = thome
tests := []releaseCase{
{
name: "add a repository",
......@@ -67,18 +68,19 @@ func TestRepoAdd(t *testing.T) {
t.Fatal(err)
}
oldhome := settings.Home
settings.Home = thome
cleanup := resetEnv()
hh := thome
defer func() {
ts.Stop()
settings.Home = oldhome
os.Remove(thome.String())
cleanup()
}()
if err := ensureTestHome(hh, t); err != nil {
t.Fatal(err)
}
settings.Home = thome
if err := addRepository(testName, ts.URL(), hh, "", "", "", true); err != nil {
t.Error(err)
}
......
......@@ -33,18 +33,19 @@ func TestRepoRemove(t *testing.T) {
t.Fatal(err)
}
oldhome := settings.Home
settings.Home = thome
hh := helmpath.Home(thome)
cleanup := resetEnv()
defer func() {
ts.Stop()
settings.Home = oldhome
os.Remove(thome.String())
cleanup()
}()
if err := ensureTestHome(hh, t); err != nil {
t.Fatal(err)
}
settings.Home = thome
b := bytes.NewBuffer(nil)
if err := removeRepoLine(b, testName, hh); err == nil {
......
......@@ -34,13 +34,15 @@ func TestUpdateCmd(t *testing.T) {
if err != nil {
t.Fatal(err)
}
oldhome := settings.Home
settings.Home = thome
cleanup := resetEnv()
defer func() {
settings.Home = oldhome
os.Remove(thome.String())
cleanup()
}()
settings.Home = thome
out := bytes.NewBuffer(nil)
// Instead of using the HTTP updater, we provide our own for this test.
// The TestUpdateCharts test verifies the HTTP behavior independently.
......@@ -69,18 +71,19 @@ func TestUpdateCharts(t *testing.T) {
t.Fatal(err)
}
oldhome := settings.Home
settings.Home = thome
hh := helmpath.Home(thome)
cleanup := resetEnv()
defer func() {
ts.Stop()
settings.Home = oldhome
os.Remove(thome.String())
cleanup()
}()
if err := ensureTestHome(hh, t); err != nil {
t.Fatal(err)
}
settings.Home = thome
r, err := repo.NewChartRepository(&repo.Entry{
Name: "charts",
URL: ts.URL(),
......
......@@ -68,9 +68,10 @@ func TestSearchCmd(t *testing.T) {
},
}
oldhome := settings.Home
cleanup := resetEnv()
defer cleanup()
settings.Home = "testdata/helmhome"
defer func() { settings.Home = oldhome }()
for _, tt := range tests {
buf := bytes.NewBuffer(nil)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment