Skip to content

Commit

Permalink
Cleanup in the test helpers (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
pior committed Feb 14, 2022
1 parent 85f1e0c commit 75429ce
Show file tree
Hide file tree
Showing 18 changed files with 61 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Expand Up @@ -13,7 +13,7 @@ linters:
- makezero
- misspell
- tenv
# - thelper
- thelper
- unconvert
- wastedassign

Expand Down
2 changes: 1 addition & 1 deletion pkg/helpers/debug/debug_test.go
Expand Up @@ -20,7 +20,7 @@ func TestFormatDebugInfo(t *testing.T) {
require.Contains(t, text, "Failed to read manifest: no manifest at")

writer := test.Project(tmpdir)
writer.Manifest().WriteString(t, "up: [{go: 1.2.3}]")
writer.Manifest().WriteString("up: [{go: 1.2.3}]")

text = FormatDebugInfo("", []string{}, tmpdir)
require.Contains(t, text, "0. `map[go:1.2.3]`")
Expand Down
10 changes: 5 additions & 5 deletions pkg/helpers/open/open_test.go
Expand Up @@ -12,7 +12,7 @@ import (
func TestFindLink(t *testing.T) {
tmpdir := t.TempDir()
writer := test.Project(tmpdir)
writer.Manifest().WriteString(t, "open: {doc: http://doc.com, logs: http://logs}")
writer.Manifest().WriteString("open: {doc: http://doc.com, logs: http://logs}")

proj := project.NewFromPath(tmpdir)

Expand All @@ -30,7 +30,7 @@ func TestFindLink(t *testing.T) {
func TestFindLinkDefault(t *testing.T) {
tmpdir := t.TempDir()
writer := test.Project(tmpdir)
writer.Manifest().WriteString(t, "open: {doc: http://doc.com}")
writer.Manifest().WriteString("open: {doc: http://doc.com}")

proj := project.NewFromPath(tmpdir)

Expand All @@ -43,7 +43,7 @@ func TestFindLinkGithub(t *testing.T) {
tmpdir := t.TempDir()
writer := test.Project(tmpdir)
writer.CreateGitRepo(t)
writer.Manifest().Empty(t)
writer.Manifest().Empty()

proj := project.NewFromPath(tmpdir)

Expand All @@ -66,12 +66,12 @@ func TestPrintLinks(t *testing.T) {

proj := project.NewFromPath(tmpdir)

writer.Manifest().WriteString(t, "")
writer.Manifest().WriteString("")

err := PrintLinks(proj)
require.Error(t, err)

writer.Manifest().WriteString(t, "open: {doc: http://doc.com}")
writer.Manifest().WriteString("open: {doc: http://doc.com}")
err = PrintLinks(proj)
require.NoError(t, err)
}
2 changes: 1 addition & 1 deletion pkg/manifest/manifest_test.go
Expand Up @@ -31,7 +31,7 @@ func TestLoad(t *testing.T) {
tmpdir := t.TempDir()

writer := test.Project(tmpdir)
writer.Manifest().WriteString(t, manifestContent)
writer.Manifest().WriteString(manifestContent)

man, err := Load(tmpdir)
require.NoError(t, err, "Load() failed")
Expand Down
4 changes: 2 additions & 2 deletions pkg/project/current_test.go
Expand Up @@ -13,7 +13,7 @@ func TestFindByPath(t *testing.T) {
tmpdir := t.TempDir()

writer := test.Project(tmpdir)
writer.Manifest().Empty(t)
writer.Manifest().Empty()

proj, err := findByPath(tmpdir)
require.NoError(t, err, "findByPath() failed")
Expand All @@ -25,7 +25,7 @@ func TestFindByPathNested(t *testing.T) {
tmpdir := t.TempDir()

writer := test.Project(tmpdir)
writer.Manifest().Empty(t)
writer.Manifest().Empty()

nestedDir := filepath.Join(tmpdir, "subdir1", "subdir2")
os.MkdirAll(nestedDir, os.ModePerm)
Expand Down
12 changes: 8 additions & 4 deletions pkg/test/file.go
Expand Up @@ -7,15 +7,19 @@ import (
)

// File creates a temporary directory and returns the full path of the file. Panic on error.
func File(t testing.TB, filename string) (tmpdir, tmpfile string) {
tmpdir = t.TempDir()
func File(tb testing.TB, filename string) (tmpdir, tmpfile string) {
tb.Helper()

tmpdir = tb.TempDir()
tmpfile = path.Join(tmpdir, filename)
return
}

// CreateFile creates a temporary directory, writes a file and returns the full path of the file. Panic on error.
func CreateFile(t testing.TB, filename string, content []byte) (tmpdir, tmpfile string) {
tmpdir, tmpfile = File(t, filename)
func CreateFile(tb testing.TB, filename string, content []byte) (tmpdir, tmpfile string) {
tb.Helper()

tmpdir, tmpfile = File(tb, filename)

err := os.WriteFile(tmpfile, content, 0600)
if err != nil {
Expand Down
8 changes: 6 additions & 2 deletions pkg/test/project.go
Expand Up @@ -21,12 +21,16 @@ func Project(path string) *projectWriter {

// GitInit initializes a simple Git repo
func (p *projectWriter) CreateGitRepo(t *testing.T) {
t.Helper()

p.runGit(t, "init")
p.runGit(t, "commit", "-m", "Commit1", "--allow-empty")
p.runGit(t, "remote", "add", "origin", "git@github.com:org1/repo1.git")
}

func (p *projectWriter) runGit(t *testing.T, args ...string) {
t.Helper()

cmd := exec.Command("git", args...)
cmd.Dir = p.path
cmd.Env = []string{
Expand All @@ -50,10 +54,10 @@ func (p *projectWriter) Manifest() *manifestWriter {
return &manifestWriter{path: filepath.Join(p.path, "dev.yml")}
}

func (m *manifestWriter) Empty(t *testing.T) {
func (m *manifestWriter) Empty() {
WriteFile(m.path, []byte(""))
}

func (m *manifestWriter) WriteString(t *testing.T, value string) {
func (m *manifestWriter) WriteString(value string) {
WriteFile(m.path, []byte(value))
}
6 changes: 3 additions & 3 deletions tests/cmd_custom_test.go
Expand Up @@ -21,7 +21,7 @@ commands:
func Test_Cmd_Custom(t *testing.T) {
c := CreateContextAndInit(t)

project := CreateProject(t, c, "project", devYmlMyCmd)
project := CreateProject(c, "project", devYmlMyCmd)
c.Cd(project.Path)

lines := c.Run("bud mycmd")
Expand All @@ -34,7 +34,7 @@ func Test_Cmd_Custom(t *testing.T) {
func Test_Cmd_Custom_Short_Syntax(t *testing.T) {
c := CreateContextAndInit(t)

project := CreateProject(t, c, "project", devYmlMyCmdShort)
project := CreateProject(c, "project", devYmlMyCmdShort)
c.Cd(project.Path)

lines := c.Run("bud mycmd")
Expand All @@ -44,7 +44,7 @@ func Test_Cmd_Custom_Short_Syntax(t *testing.T) {
func Test_Cmd_Custom_Always_Run_In_Project_Root(t *testing.T) {
c := CreateContextAndInit(t)

project := CreateProject(t, c, "project", devYmlMyCmd)
project := CreateProject(c, "project", devYmlMyCmd)
c.Cd(project.Path)
c.Run("mkdir foobar")
c.Cd("foobar")
Expand Down
2 changes: 1 addition & 1 deletion tests/cmd_inspect_test.go
Expand Up @@ -9,7 +9,7 @@ import (
func Test_Cmd_Inspect(t *testing.T) {
c := CreateContextAndInit(t)

CreateProject(t, c, "project",
CreateProject(c, "project",
`up:`,
`- node: '10.15.0'`,
`- pip: [requirements.txt]`,
Expand Down
4 changes: 3 additions & 1 deletion tests/context/context.go
Expand Up @@ -21,7 +21,9 @@ type TestContext struct {
debug bool
}

func New(config Config, t *testing.T) (*TestContext, error) {
func New(t *testing.T, config Config) (*TestContext, error) {
t.Helper()

var (
shellPath string
args []string
Expand Down
17 changes: 12 additions & 5 deletions tests/helper_test.go
Expand Up @@ -12,7 +12,9 @@ import (
var config context.Config // Initialized by TestMain()

func CreateContext(t *testing.T) *context.TestContext {
c, err := context.New(config, t)
t.Helper()

c, err := context.New(t, config)
require.NoError(t, err)

t.Cleanup(func() {
Expand All @@ -24,15 +26,17 @@ func CreateContext(t *testing.T) *context.TestContext {
}

func CreateContextAndInit(t *testing.T) *context.TestContext {
c := CreateContext(t)
t.Helper()

c := CreateContext(t)
output := c.Run(`eval "$(bud --shell-init)"`)
require.Len(t, output, 0)

return c
}

func OutputContains(t *testing.T, lines []string, subStrings ...string) {
t.Helper()

text := strings.Join(lines, "\n")

for _, subString := range subStrings {
Expand All @@ -43,6 +47,8 @@ func OutputContains(t *testing.T, lines []string, subStrings ...string) {
}

func OutputNotContain(t *testing.T, lines []string, subStrings ...string) {
t.Helper()

text := strings.Join(lines, "\n")

for _, subString := range subStrings {
Expand All @@ -53,6 +59,7 @@ func OutputNotContain(t *testing.T, lines []string, subStrings ...string) {
}

func OutputEqual(t *testing.T, lines []string, expectedLines ...string) {
t.Helper()
require.Equal(t, expectedLines, lines)
}

Expand All @@ -61,7 +68,7 @@ type Project struct {
Path string
}

func CreateProject(t *testing.T, c *context.TestContext, name string, devYmlLines ...string) Project {
func CreateProject(c *context.TestContext, name string, devYmlLines ...string) Project {
projectPath := "/home/tester/src/github.com/orgname/" + name
c.Run("mkdir -p " + projectPath)

Expand All @@ -72,7 +79,7 @@ func CreateProject(t *testing.T, c *context.TestContext, name string, devYmlLine
return Project{name, projectPath}
}

func (p *Project) UpdateDevYml(t *testing.T, c *context.TestContext, devYmlLines ...string) {
func (p *Project) UpdateDevYml(c *context.TestContext, devYmlLines ...string) {
path := p.Path + "/dev.yml"
c.Write(path, strings.Join(devYmlLines, "\n"))
}
14 changes: 7 additions & 7 deletions tests/task_custom_test.go
Expand Up @@ -19,7 +19,7 @@ up:
func Test_Task_Custom(t *testing.T) {
c := CreateContextAndInit(t)

CreateProject(t, c, "project", customTaskDevYml)
CreateProject(c, "project", customTaskDevYml)

// file does not exist -> task must run
c.Run("bud up")
Expand All @@ -35,7 +35,7 @@ func Test_Task_Custom(t *testing.T) {
func Test_Task_Custom_Subdir(t *testing.T) {
c := CreateContextAndInit(t)

CreateProject(t, c, "project", customTaskDevYml)
CreateProject(c, "project", customTaskDevYml)

// The command must work in a sub-dir, but run in the project root
c.Run("mkdir subdir")
Expand All @@ -50,7 +50,7 @@ func Test_Task_Custom_Subdir(t *testing.T) {
func Test_Task_Custom_Fails(t *testing.T) {
c := CreateContextAndInit(t)

CreateProject(t, c, "project", `
CreateProject(c, "project", `
up:
- custom:
name: TestCustom
Expand All @@ -67,7 +67,7 @@ func Test_Task_Custom_With_Env_From_Shell(t *testing.T) {

c.Run("export MYVAR=poipoi")

CreateProject(t, c, "project",
CreateProject(c, "project",
`env:`,
` MYVAR: poipoi`,
`up:`,
Expand All @@ -86,7 +86,7 @@ func Test_Task_Custom_With_Env_At_First_Run(t *testing.T) {
t.Skip("Fixme: env vars not set before tasks?")
c := CreateContextAndInit(t)

CreateProject(t, c, "project",
CreateProject(c, "project",
`env:`,
` MYVAR: poipoi`,
`up:`,
Expand All @@ -107,13 +107,13 @@ func Test_Task_Custom_With_Env_Previously_Set_By_DevBuddy(t *testing.T) {

c.Run("export MYVAR=poipoi")

p := CreateProject(t, c, "project",
p := CreateProject(c, "project",
`env:`,
` MYVAR: poipoi`,
)
c.Run("bud up")

p.UpdateDevYml(t, c,
p.UpdateDevYml(c,
`env:`,
` MYVAR: poipoi`,
`up:`,
Expand Down
6 changes: 3 additions & 3 deletions tests/task_go_test.go
Expand Up @@ -12,7 +12,7 @@ import (
func Test_Task_Go_Module(t *testing.T) {
c := CreateContextAndInit(t)

CreateProject(t, c, "project",
CreateProject(c, "project",
`up:`,
`- go:`,
` version: "1.15"`,
Expand Down Expand Up @@ -50,7 +50,7 @@ func main() {
func Test_Task_Go_Pre_Module(t *testing.T) {
c := CreateContextAndInit(t)

_ = CreateProject(t, c, "project",
_ = CreateProject(c, "project",
`up:`,
`- go: "1.15"`,
)
Expand All @@ -69,7 +69,7 @@ func Test_Task_Go_Pre_Module(t *testing.T) {
func Test_Task_Go_Check_GOPATH(t *testing.T) {
c := CreateContextAndInit(t)

CreateProject(t, c, "project",
CreateProject(c, "project",
`up:`,
`- go: "1.15"`,
)
Expand Down
4 changes: 2 additions & 2 deletions tests/task_node_test.go
Expand Up @@ -10,7 +10,7 @@ import (
func Test_Task_Node(t *testing.T) {
c := CreateContextAndInit(t)

CreateProject(t, c, "project",
CreateProject(c, "project",
`up:`,
`- node: '10.15.0'`,
)
Expand All @@ -26,7 +26,7 @@ func Test_Task_Node(t *testing.T) {
func Test_Task_Node_Npm_Install(t *testing.T) {
c := CreateContextAndInit(t)

CreateProject(t, c, "project",
CreateProject(c, "project",
`up:`,
`- node: '10.15.0'`,
)
Expand Down
2 changes: 1 addition & 1 deletion tests/task_pip_test.go
Expand Up @@ -10,7 +10,7 @@ import (
func Test_Task_Pip(t *testing.T) {
c := CreateContextAndInit(t)

CreateProject(t, c, "project",
CreateProject(c, "project",
`up:`,
`- python: 3.9.0`,
`- pip : [requirements.txt]`,
Expand Down
2 changes: 1 addition & 1 deletion tests/task_pipfile_test.go
Expand Up @@ -10,7 +10,7 @@ import (
func Test_Task_Pipfile(t *testing.T) {
c := CreateContextAndInit(t)

CreateProject(t, c, "project",
CreateProject(c, "project",
`up:`,
`- python: 3.9.0`,
`- pipfile`,
Expand Down

0 comments on commit 75429ce

Please sign in to comment.