Skip to content

Commit

Permalink
Add test covering wiki switching
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhuan Oliveira committed May 7, 2022
1 parent 979db6b commit e8b5712
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
58 changes: 57 additions & 1 deletion pkg/testutils/testutils.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package testutils

import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

type TestState struct {
WikicmdBinaryPath string
WikicmdConfigPath string
}

type ConfigField int

const (
Config_field_default ConfigField = iota
)

func RunWikiCmd(state *TestState, command string) (string, error) {
commandParameters := toCommandParameters(command)

Expand All @@ -27,10 +37,56 @@ func RunWikiCmd(state *TestState, command string) (string, error) {
}

func StartupTest() *TestState {
configFilePath, err := mktemp()
if err != nil {
panic(err)
}

configContent, err := os.ReadFile(fmt.Sprintf("%s/tests/e2e/wikicmd_config.json", pwd()))
if err != nil {
panic(err)
}

os.WriteFile(configFilePath, configContent, 0644)

return &TestState{
fmt.Sprintf("%s/bin/wikicmd", pwd()),
fmt.Sprintf("%s/tests/e2e/wikicmd_config.json", pwd()),
configFilePath,
}
}

func AssertConfig(t *testing.T, state *TestState, field ConfigField, expectedValue string) {
var jsonParsed map[string]interface{}

configContent, err := os.ReadFile(state.WikicmdConfigPath)
if err != nil {
panic(err)
}

err = json.Unmarshal(configContent, &jsonParsed)
if err != nil {
panic(err)
}

value := ""
if field == Config_field_default {
value = jsonParsed["default"].(string)
}

assert.Equal(
t,
value,
expectedValue,
)
}

func mktemp() (string, error) {
result, err := exec.Command("mktemp").Output()
if err != nil {
return "", err
}

return string(result), nil
}

func pwd() string {
Expand Down
14 changes: 14 additions & 0 deletions tests/e2e/cmd_switch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,17 @@ The available Wikis you can switch to are: my_wiki,another_wiki
`,
)
}

func TestSwitchingToAnotherWiki(t *testing.T) {
testState := testutils.StartupTest()
commandResult, _ := testutils.RunWikiCmd(testState, "switch another_wiki")

assert.Equal(
t,
commandResult,
`Done!
`,
)

testutils.AssertConfig(t, testState, testutils.Config_field_default, "another_wiki")
}

0 comments on commit e8b5712

Please sign in to comment.