Skip to content

Commit

Permalink
root_test: Add tests for new config and flag layout
Browse files Browse the repository at this point in the history
There isn't really a good way to test the config and flag override
code.  I've opted to call 'mr show' with different flag and config
settings to test out the code.  The tests include testing for
.git/lab/lab.toml overrides of ~/.config/lab/lab.toml and tests
for the override by the flag of the value in the config files.

Add tests for the new config and flag layout.

Signed-off-by: Prarit Bhargava <prarit@redhat.com>
  • Loading branch information
prarit committed Sep 24, 2020
1 parent 9bf2452 commit 010a623
Showing 1 changed file with 185 additions and 0 deletions.
185 changes: 185 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"testing"
"time"

"github.com/acarl005/stripansi"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -511,3 +512,187 @@ func getAppOutput(output []byte) []string {
}
return lines
}

func setConfigValues(repo string, configVal string, gitVal string) {
err := os.Rename(repo+"/lab.toml", "/home/travis/.config/lab/lab.toml")
if err != nil {
log.Fatal(err)
}

configfile, err := os.OpenFile("/home/travis/.config/lab/lab.toml", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}

if _, err := configfile.WriteString("\n[mr_show]\n comments = " + configVal + "\n"); err != nil {
log.Fatal(err)
}
configfile.Close()

err = os.Mkdir(repo+"/.git/lab/", 0700)
if err != nil {
log.Fatal(err)
}
gitfile, err := os.OpenFile(repo+"/.git/lab/lab.toml", os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}

if _, err = gitfile.WriteString("\n[mr_show]\n comments = " + gitVal + "\n"); err != nil {
log.Fatal(err)
}
gitfile.Close()
}

// There isn't a really good way to test the config override
// infrastruture, so just call 'mr show' and set 'mr_show.comments'
func Test_config_gitConfig_FF(t *testing.T) {
repo := copyTestRepo(t)

setConfigValues(repo, "false", "false")
os.Remove(repo + "/lab.toml")

cmd := exec.Command(labBinaryPath, "mr", "show", "1")
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Error(err)
}

out := string(b)
out = stripansi.Strip(out)

os.Remove("/home/travis/.config/lab/lab.toml")
// both configs set to false, comments should not be output
require.NotContains(t, string(b), `commented at`)
}

func Test_config_gitConfig_FT(t *testing.T) {
repo := copyTestRepo(t)

setConfigValues(repo, "false", "true")
os.Remove(repo + "/lab.toml")

cmd := exec.Command(labBinaryPath, "mr", "show", "1")
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Error(err)
}

out := string(b)
out = stripansi.Strip(out)

os.Remove("/home/travis/.config/lab/lab.toml")
// .config set to false and .git set to true, comments should be
// output
require.Contains(t, string(b), `commented at`)
}

func Test_config_gitConfig_TF(t *testing.T) {
repo := copyTestRepo(t)

setConfigValues(repo, "true", "false")
os.Remove(repo + "/lab.toml")

cmd := exec.Command(labBinaryPath, "mr", "show", "1")
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Error(err)
}

out := string(b)
out = stripansi.Strip(out)

os.Remove("/home/travis/.config/lab/lab.toml")
// .config set to true and .git set to false, comments should not be
// output
require.NotContains(t, string(b), `commented at`)
}

func Test_config_gitConfig_TT(t *testing.T) {
repo := copyTestRepo(t)

setConfigValues(repo, "true", "true")
os.Remove(repo + "/lab.toml")

cmd := exec.Command(labBinaryPath, "mr", "show", "1")
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Error(err)
}

out := string(b)
out = stripansi.Strip(out)

os.Remove("/home/travis/.config/lab/lab.toml")
// both configs set to true, comments should be output
require.Contains(t, string(b), `commented at`)
}

// Some flag and config tests do not have to be run.
// flag not set, config true == comments
// This case is handled by Test_config_gitConfig_TT
// flag not set, config false == no comments
// This case is handled by Test_config_gitConfig_FF
// flag not set, config not set == no comments
// flag set, config not set == comments
// These case are handled in cmd/mr_show_test.go

// flag set, config true == comments
func Test_flag_config_TT(t *testing.T) {
repo := copyTestRepo(t)

setConfigValues(repo, "true", "true")
os.Remove(repo + "/lab.toml")

cmd := exec.Command(labBinaryPath, "mr", "show", "1", "--comments")
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Error(err)
}

out := string(b)
out = stripansi.Strip(out)

os.Remove("/home/travis/.config/lab/lab.toml")
// both configs set to true, comments should be output
require.Contains(t, string(b), `commented at`)
}

// flag set, config false == comments
func Test_flag_config_TF(t *testing.T) {
repo := copyTestRepo(t)

setConfigValues(repo, "false", "false")
os.Remove(repo + "/lab.toml")

cmd := exec.Command(labBinaryPath, "mr", "show", "1", "--comments")
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Error(err)
}

out := string(b)
out = stripansi.Strip(out)

os.Remove("/home/travis/.config/lab/lab.toml")
// both configs set to true, comments should be output
require.Contains(t, string(b), `commented at`)
}

0 comments on commit 010a623

Please sign in to comment.