Exemple #1
0
func TestEnvOperation(t *testing.T) {
	// Equivalent to os.Setenv("TEST_RUN_ENV", "fubar")
	/*if runtime.GOOS == "windows" {
		status:=Call(`set TEST_RUN_ENV=fubar`).Run()
		assert.Equal(t, "",status.Error())
	} else {
		status:=Call(`bash -c "export TEST_RUN_ENV=fubar"`).Run()
		assert.Equal(t, nil,status)
	}*/
	os.Setenv("TEST_RUN_ENV", "fubar")
	Env = makeEnvMap(os.Environ(), true)

	assert.Equal(t, "fubar", (*Env)["TEST_RUN_ENV"], "set/export env failed")
	output := bytes.NewBuffer(nil)
	if runtime.GOOS == "windows" {
		Call(`FOO=bar BAH=baz cmd /C 'echo %TEST_RUN_ENV% %FOO%'`).Pipe(Stdout, output).Run()
	} else {
		Call(`FOO=bar BAH=baz bash -c "echo -n $TEST_RUN_ENV $FOO"`).Pipe(Stdout, output).Run()
	}

	assert.Equal(t, "fubar bar", strings.Trim((*output).String(), " "))

	os.Unsetenv("TEST_RUN_ENV")
	/*
		if runtime.GOOS == "windows" {
			Call(`set TEST_RUN_ENV=`).Run()
		} else {
			Call(`unset TEST_RUN_ENV`).Run()
		}
	*/
}
Exemple #2
0
func TestIn(t *testing.T) {
	var output bytes.Buffer
	old, _ := os.Getwd()
	if runtime.GOOS == "windows" {
		Call("foo.cmd").In("test").Pipe(Stdout, &output).Run()
	} else {
		Call("bash foo.sh").In("test").Pipe(Stdout, &output).Run()
	}

	assert.Equal(t, "FOOBAR", strings.Trim(output.String(), " "))

	now, _ := os.Getwd()
	assert.Equal(t, now, old, "In failed to reset work directory")
}
Exemple #3
0
func TestEnvCombination(t *testing.T) {
	// set to os defaults
	Env = makeEnvMap(os.Environ(), false)
	Env = Env.combine([]string{"USER=$USER:$USER:func"})

	assert.Equal(t, user+":"+user+":func", (*Env)["USER"], "Should have been overriden by func environment")

	assert.Equal(t, len(os.Environ()), len(*Env), "Consolidated environment length changed.")

	Env = Env.combine([]string{"GOSU_NEW_VAR=foo"})
	assert.Equal(t, "foo", (*Env)["GOSU_NEW_VAR"], "Should have conslidated Env set")
	assert.Equal(t, len(os.Environ())+1, len(*Env), "Consolidated environment length should have increased by 1")

}
Exemple #4
0
func TestAt(t *testing.T) {
	var output bytes.Buffer
	if runtime.GOOS == "windows" {
		Call("foo.cmd").At("test").Pipe(Stdout, &output).Run()
	} else {
		Call("bash foo.sh").At("test").Pipe(Stdout, &output).Run()
	}

	assert.Equal(t, "FOOBAR", strings.Trim(output.String(), " "))
}
Exemple #5
0
func TestEnvInterpretation(t *testing.T) {

	// set back to default
	Env = makeEnvMap(os.Environ(), false)
	Env.Set(`USER1=$USER`, `USER2=$USER1`)

	Env = Env.combine([]string{"USER3=$USER2"})
	assert.Equal(t, user, (*Env)["USER1"], "Should have been evaluated")
	assert.Equal(t, user, (*Env)["USER3"], "Should have been evaluated during consolidation")

	Env = Env.combine([]string{"PATH=foo::bar::bah"})
	assert.Equal(t, "foo"+string(os.PathListSeparator)+"bar"+string(os.PathListSeparator)+"bah", (*Env)["PATH"], "Should have replaced run.PathSeparator")

	// set back to defaults
	Env = makeEnvMap(os.Environ(), false)
	Env.Set(`FOO=foo`, `FAIL=$FOObar:run`, `PASS=${FOO}bar:run`)

	assert.Equal(t, ":run", (*Env)["FAIL"], "$FOObar should have been interpreted as empty string.")
	assert.Equal(t, "foobar:run", (*Env)["PASS"], "${FOO}bar should have been interpreted accordingly.")
}
Exemple #6
0
func TestPrompt(t *testing.T) {
	stdin := os.Stdin
	defer func() {
		os.Stdin = stdin
	}()
	os.Stdin = r

	w.WriteString("username\n")

	assert.Equal(t, "username", strings.Trim(Prompt("Please input: "), " "))

}
Exemple #7
0
func TestWithError(t *testing.T) {
	var output bytes.Buffer
	var error bytes.Buffer
	status := Call(`
		$cat $t/doesnotexist
		$cat $t/bar
		`).With("t=test", "cat=cat").Pipe(Stdout, &output).Pipe(Stderr, &error).Run()

	assert.Error(t, status)
	assert.Contains(t, "line=1", status.Error())
	assert.Contains(t, "exit", status.Error())
	assert.Contains(t, "doesnotexist", error.String())
	assert.Equal(t, "", output.String())
}
Exemple #8
0
func TestPromotion(t *testing.T) {
	assert.Equal(t, "", os.Getenv("_foo"))
	assert.Equal(t, "", os.Getenv("_test_bar"))
	assert.Equal(t, "", os.Getenv("_test_opts"))
	Env = makeEnvMap([]string{"_foo", "_test_bar=bah", `_test_opts="a=b,c=d,*="`}, false)
	for key, _ := range *Env {
		Env.promote(key)
	}
	assert.Equal(t, "", os.Getenv("_foo"))
	assert.Equal(t, "bah", os.Getenv("_test_bar"))
	assert.Equal(t, "\"a=b,c=d,*=\"", os.Getenv("_test_opts"))
}
Exemple #9
0
func TestShell(t *testing.T) {
	if runtime.GOOS == "windows" {
		return
	}
	var output bytes.Buffer
	// use sh as default shell, which doesn't suport `-n`
	Shell(`echo foobar`).Pipe(Stdout, &output).Run()
	assert.Equal(t, "foobar\n", output.String(), "Simple bash failed.")

	output.Reset()
	Shell(`bash`, `echo -n foobar`).Pipe(Stdout, &output).Run()
	assert.Equal(t, "foobar", output.String(), "Simple bash failed.")

	output.Reset()
	Shell(`bash`, `
		echo -n foobar
		echo -n bahbaz
	`).Pipe(Stdout, &output).Run()
	assert.Equal(t, "foobarbahbaz", output.String(), "Multiline bash failed.")

	output.Reset()
	Shell(`bash`, `
		echo -n \
		foobar
	`).Pipe(Stdout, &output).Run()
	assert.Equal(t, "foobar", output.String(), "Bash line continuation failed.")

	output.Reset()
	Shell(`bash`, `
		echo -n "foobar"
	`).Pipe(Stdout, &output).Run()
	assert.Equal(t, "foobar", output.String(), "Bash quotes failed.")

	output.Reset()
	Shell(`bash`, `
		echo -n "fo\"obar"
	`).Pipe(Stdout, &output).Run()
	assert.Equal(t, "fo\"obar", output.String(), "Bash quoted command failed.")
}
Exemple #10
0
func TestMultiCommand(t *testing.T) {

	var output bytes.Buffer
	Call(cat+" test/foo\n"+cat+" test/bar").Pipe(Stdout, &output).Run()
	assert.Equal(t, "text from foo\nthis is content of bar\n", output.String())
}
Exemple #11
0
func TestChain(t *testing.T) {
	var output bytes.Buffer
	Call(`cat test/foo`).Call(`echo hello`).Call(`echo world`).Pipe(Stdout, &output).Run()
	assert.Equal(t, "text from foo\nhello\nworld\n", output.String())
}
Exemple #12
0
func TestSingleCommand(t *testing.T) {

	var output bytes.Buffer
	Call(`cat test/foo`).Pipe(Stdout, &output).Run()
	assert.Equal(t, "text from foo\n", output.String())
}
Exemple #13
0
func TestEnvInitialization(t *testing.T) {
	// set to os defaults
	Env = makeEnvMap(os.Environ(), false)
	Env.Set("USER=$USER:run")
	assert.Equal(t, user+":run", (*Env)["USER"], "Environment interpretation failed")
}