Skip to content

Commit

Permalink
test: add formatting on subdirs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
katcipis committed May 19, 2022
1 parent 0860d8b commit b9ab265
Showing 1 changed file with 69 additions and 11 deletions.
80 changes: 69 additions & 11 deletions cmd/terramate/e2etests/fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,19 @@ name = "name"
})

sprintf := fmt.Sprintf
s.BuildTree([]string{
sprintf("f:globals.tm:%s", unformattedHCL),
sprintf("f:another-stacks/globals.tm.hcl:%s", unformattedHCL),
sprintf("f:another-stacks/stack-1/globals.tm.hcl:%s", unformattedHCL),
sprintf("f:another-stacks/stack-2/globals.tm.hcl:%s", unformattedHCL),
sprintf("f:stacks/globals.tm:%s", unformattedHCL),
sprintf("f:stacks/stack-1/globals.tm:%s", unformattedHCL),
sprintf("f:stacks/stack-2/globals.tm:%s", unformattedHCL),
})
writeUnformattedFiles := func() {
s.BuildTree([]string{
sprintf("f:globals.tm:%s", unformattedHCL),
sprintf("f:another-stacks/globals.tm.hcl:%s", unformattedHCL),
sprintf("f:another-stacks/stack-1/globals.tm.hcl:%s", unformattedHCL),
sprintf("f:another-stacks/stack-2/globals.tm.hcl:%s", unformattedHCL),
sprintf("f:stacks/globals.tm:%s", unformattedHCL),
sprintf("f:stacks/stack-1/globals.tm:%s", unformattedHCL),
sprintf("f:stacks/stack-2/globals.tm:%s", unformattedHCL),
})
}

writeUnformattedFiles()

wantedFiles := []string{
"globals.tm",
Expand All @@ -72,12 +76,17 @@ name = "name"
}
wantedFilesStr := filesListOutput(wantedFiles)

assertFileContents := func(t *testing.T, path string, want string) {
t.Helper()
got := s.RootEntry().ReadFile(path)
assert.EqualStrings(t, want, string(got))
}

assertWantedFilesContents := func(t *testing.T, want string) {
t.Helper()

for _, file := range wantedFiles {
got := s.RootEntry().ReadFile(file)
assert.EqualStrings(t, want, string(got))
assertFileContents(t, file, want)
}
}

Expand Down Expand Up @@ -119,4 +128,53 @@ name = "name"
assertRunResult(t, cli.run("fmt"), runExpected{})
assertWantedFilesContents(t, formattedHCL)
})

t.Run("update unformatted files in subdirs", func(t *testing.T) {
writeUnformattedFiles()

anotherStacks := filepath.Join(s.RootDir(), "another-stacks")
cli := newCLI(t, anotherStacks)
assertRunResult(t, cli.run("fmt"), runExpected{
Stdout: filesListOutput([]string{
"globals.tm.hcl",
"stack-1/globals.tm.hcl",
"stack-2/globals.tm.hcl",
}),
})

assertFileContents(t, "another-stacks/globals.tm.hcl", formattedHCL)
assertFileContents(t, "another-stacks/stack-1/globals.tm.hcl", formattedHCL)
assertFileContents(t, "another-stacks/stack-2/globals.tm.hcl", formattedHCL)

assertFileContents(t, "globals.tm", unformattedHCL)
assertFileContents(t, "stacks/globals.tm", unformattedHCL)
assertFileContents(t, "stacks/stack-1/globals.tm", unformattedHCL)
assertFileContents(t, "stacks/stack-2/globals.tm", unformattedHCL)

stacks := filepath.Join(s.RootDir(), "stacks")
cli = newCLI(t, stacks)
assertRunResult(t, cli.run("fmt"), runExpected{
Stdout: filesListOutput([]string{
"globals.tm",
"stack-1/globals.tm",
"stack-2/globals.tm",
}),
})

assertFileContents(t, "another-stacks/globals.tm.hcl", formattedHCL)
assertFileContents(t, "another-stacks/stack-1/globals.tm.hcl", formattedHCL)
assertFileContents(t, "another-stacks/stack-2/globals.tm.hcl", formattedHCL)
assertFileContents(t, "stacks/globals.tm", formattedHCL)
assertFileContents(t, "stacks/stack-1/globals.tm", formattedHCL)
assertFileContents(t, "stacks/stack-2/globals.tm", formattedHCL)

assertFileContents(t, "globals.tm", unformattedHCL)

cli = newCLI(t, s.RootDir())
assertRunResult(t, cli.run("fmt"), runExpected{
Stdout: filesListOutput([]string{"globals.tm"}),
})

assertWantedFilesContents(t, formattedHCL)
})
}

0 comments on commit b9ab265

Please sign in to comment.