Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for commands #22

Merged
merged 40 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
767ae7f
Updates dependencies to latest
gdavison Jul 17, 2020
d8a9045
Adds tests for Golang block detection
gdavison Apr 9, 2020
787b04b
Adds tests for Markdown
gdavison Apr 9, 2020
a08fb88
Updates dependencies
gdavison Apr 13, 2020
5b56dec
Adds tests for `terrafmt blocks` command
gdavison Apr 13, 2020
50fdda4
Adds tests for `terrafmt diff` command
gdavison Apr 13, 2020
f5624af
Adds tests for `terrafmt fmt` command
gdavison Apr 14, 2020
b5a3c76
Adds tests for `terrafmt upgrade012` command
gdavison Jul 8, 2020
0627990
Replaces `bytes.Buffer` with `strings.Builder`
gdavison Jul 9, 2020
c9a2753
Adds noDiff case to fmt test
gdavison Jul 14, 2020
6a5bb27
Adds noDiff case to upgrade012 test
gdavison Jul 14, 2020
06fdae7
Renames Go test result files
gdavison Jul 15, 2020
7d78e04
Adds Markdown blocks tests
gdavison Jul 15, 2020
3519fcf
Adds Markdown diff tests
gdavison Jul 16, 2020
9100d90
Adds Markdown fmt tests
gdavison Jul 16, 2020
0a36daf
Adds Markdown upgrade012 tests
gdavison Jul 16, 2020
078fa63
Actually set `fix-finish-lines` on Markdown test case
gdavison Jul 16, 2020
ee002b7
Removes formatting directives from Markdown test cases
gdavison Jul 16, 2020
f8aef66
Adds testing for expected error messages
gdavison Jul 17, 2020
2cb1b58
Cleans up test failure output for error message tests
gdavison Jul 17, 2020
2ea97bb
Adds Afero to abstract filesystem
gdavison Jul 17, 2020
6b38de5
Adds filesystem tests for `fmt` command
gdavison Jul 17, 2020
2f14c46
Adds filesystem tests for `upgrade012` command
gdavison Jul 17, 2020
3f7dc8d
Fix up
gdavison Jul 17, 2020
3607741
Updates Afero to be direct dependency
gdavison Jul 20, 2020
e4750ea
Renames test values
gdavison Jul 20, 2020
3c087f1
Adds tests for `verbose` flag on `blocks` command
gdavison Jul 20, 2020
310b83f
Adds tests for `verbose` flag on `diff` command
gdavison Jul 20, 2020
c20717c
Adds tests for `verbose` flag on `fmt` command
gdavison Jul 20, 2020
2c7320c
Adds tests for `verbose` flag on `upgrade012` command
gdavison Jul 21, 2020
e36c2c1
Fixes linting errors
gdavison Jul 21, 2020
1e2dfdb
Fixes verbose tests. Recreates Afero filesystems for each test case
gdavison Jul 21, 2020
b0010c4
Updates test dependencies
gdavison Jul 22, 2020
54b7e67
Updates tests to accomodate "terraform" as Markdown codefence type
gdavison Jul 24, 2020
7214217
Refactors blocks tests to generate expected outputs
gdavison Jul 30, 2020
f9d081d
Removes additional parameter incorrectly included. Removes unneeded t…
gdavison Jul 31, 2020
f633066
Makes testcases subtests
gdavison Jul 31, 2020
a666303
Removes Log as a global variable
gdavison Jul 31, 2020
e52232f
Makes all cli tests parallel to speed them up.
gdavison Jul 31, 2020
b8ea66e
Preserves proper test cases when making tests paralllel
gdavison Aug 6, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
237 changes: 237 additions & 0 deletions cli/blocks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
package cli

import (
"fmt"
"strings"
"testing"

c "github.com/gookit/color"
"github.com/katbyte/terrafmt/lib/common"
"github.com/kylelemons/godebug/diff"
"github.com/spf13/afero"
)

type block struct {
endLine int
text string
}

var testcases = []struct {
name string
sourcefile string
lineCount int
expectedBlocks []block
}{
{
name: "Go no change",
sourcefile: "testdata/no_diffs.go",
lineCount: 29,
expectedBlocks: []block{
{
endLine: 12,
text: `resource "aws_s3_bucket" "simple" {
bucket = "tf-test-bucket-simple"
}`,
},
{
endLine: 20,
text: `resource "aws_s3_bucket" "with-parameters" {
bucket = "tf-test-bucket-with-parameters-%d"
}`,
},
{
endLine: 28,
text: `resource "aws_s3_bucket" "with-parameters-and-append" {
bucket = "tf-test-bucket-parameters-and-append-%d"
}`,
},
},
},
{
name: "Go formatting",
sourcefile: "testdata/has_diffs.go",
lineCount: 39,
expectedBlocks: []block{
{
endLine: 13,
text: `resource "aws_s3_bucket" "extra-lines" {

bucket = "tf-test-bucket-extra-lines"
}`,
},
{
endLine: 22,
text: `resource "aws_s3_bucket" "no-errors" {
bucket = "tf-test-bucket-no-errors-%d"
}`,
},
{
endLine: 30,
text: `resource "aws_s3_bucket" "extra-space" {
bucket = "tf-test-bucket-extra-space-%d"
}`,
},
{
endLine: 38,
text: `resource "aws_s3_bucket" "end-line" {
bucket = "tf-test-bucket-end-line-%d"
}`,
},
},
},
{
name: "Go fmt verbs",
sourcefile: "testdata/fmt_compat.go",
lineCount: 33,
expectedBlocks: []block{
{
endLine: 14,
text: `resource "aws_s3_bucket" "no-errors" {
bucket = "tf-test-bucket-no-errors-%d"

%s
}`,
},
{
endLine: 22,
text: `resource "aws_s3_bucket" "absolutely-nothing" {
bucket = "tf-test-bucket-absolutely-nothing"
}`,
},
{
endLine: 32,
text: `resource "aws_s3_bucket" "extra-space" {
bucket = "tf-test-bucket-extra-space-%d"

%s
}`,
},
},
},
{
name: "Markdown no change",
sourcefile: "testdata/no_diffs.md",
lineCount: 25,
expectedBlocks: []block{
{
endLine: 7,
text: `resource "aws_s3_bucket" "one" {
bucket = "tf-test-bucket-one"
}`,
},
{
endLine: 13,
text: `resource "aws_s3_bucket" "two" {
bucket = "tf-test-bucket-two"
}`,
},
{
endLine: 19,
text: `resource "aws_s3_bucket" "three" {
bucket = "tf-test-bucket-three"
}`,
},
},
},
{
name: "Markdown formatting",
sourcefile: "testdata/has_diffs.md",
lineCount: 27,
expectedBlocks: []block{
{
endLine: 8,
text: `resource "aws_s3_bucket" "extra-lines" {

bucket = "tf-test-bucket-extra-lines"
}`,
},
{
endLine: 14,
text: `resource "aws_s3_bucket" "no-errors" {
bucket = "tf-test-bucket-no-errors"
}`,
},
{
endLine: 20,
text: `resource "aws_s3_bucket" "extra-space" {
bucket = "tf-test-bucket-extra-space"
}`,
},
{
endLine: 27,
text: `resource "aws_s3_bucket" "end-line" {
bucket = "tf-test-bucket-end-line"
}
`,
},
},
},
}

func TestCmdBlocksDefault(t *testing.T) {
t.Parallel()

for _, testcase := range testcases {
testcase := testcase
t.Run(testcase.name, func(t *testing.T) {
t.Parallel()

fs := afero.NewReadOnlyFs(afero.NewOsFs())

expectedBuilder := strings.Builder{}
for i, block := range testcase.expectedBlocks {
fmt.Fprint(&expectedBuilder, c.Sprintf("\n<white>#######</> <cyan>B%d</><darkGray> @ #%d</>\n", i+1, block.endLine))
fmt.Fprint(&expectedBuilder, block.text, "\n")
}
expected := expectedBuilder.String()

var outB strings.Builder
var errB strings.Builder
log := common.CreateLogger(&errB)
err := findBlocksInFile(fs, log, testcase.sourcefile, false, nil, &outB, &errB)
actualStdOut := outB.String()
actualStdErr := errB.String()

if err != nil {
t.Fatalf("Got an error when none was expected: %v", err)
}

if actualStdOut != expected {
t.Errorf("Output does not match expected:\n%s", diff.Diff(actualStdOut, expected))
}

if actualStdErr != "" {
t.Errorf("Got error output:\n%s", actualStdErr)
}
})
}
}

func TestCmdBlocksVerbose(t *testing.T) {
t.Parallel()

for _, testcase := range testcases {
testcase := testcase
t.Run(testcase.name, func(t *testing.T) {
t.Parallel()

fs := afero.NewReadOnlyFs(afero.NewOsFs())

var outB strings.Builder
var errB strings.Builder
log := common.CreateLogger(&errB)
err := findBlocksInFile(fs, log, testcase.sourcefile, true, nil, &outB, &errB)
actualStdErr := errB.String()
if err != nil {
t.Fatalf("Case %q: Got an error when none was expected: %v", testcase.name, err)
}

expectedSummaryLine := c.String(fmt.Sprintf("Finished processing <cyan>%d</> lines <yellow>%d</> blocks!", testcase.lineCount, len(testcase.expectedBlocks)))

summaryLine := strings.TrimSpace(actualStdErr)
if summaryLine != expectedSummaryLine {
t.Errorf("Case %q: Unexpected summary:\nexpected %s\ngot %s", testcase.name, expectedSummaryLine, summaryLine)
}
})
}
}
Loading