Skip to content

Commit

Permalink
add automated test verifying STDIN vs file-provided input
Browse files Browse the repository at this point in the history
This adds a basic automated test verifying the validity of the issue #20
fix.
  • Loading branch information
mdb committed Jan 15, 2024
1 parent 40f8d1b commit 0f8d033
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 3 deletions.
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ import (

var version = "development"

const (
treeDesc string = "[Optional] print changes in tree format"
jsonDesc string = "[Optional] print changes in json format"
sepTreeDesc string = "[Optional] print changes in tree format for add/delete/change/recreate changes"
)

func main() {
printVersion := flag.Bool("v", false, "print version")
tree := flag.Bool("tree", false, "[Optional] print changes in tree format")
json := flag.Bool("json", false, "[Optional] print changes in json format")
separateTree := flag.Bool("separate-tree", false, "[Optional] print changes in tree format for add/delete/change/recreate changes")
tree := flag.Bool("tree", false, treeDesc)
json := flag.Bool("json", false, jsonDesc)
separateTree := flag.Bool("separate-tree", false, sepTreeDesc)
drawable := flag.Bool("draw", false, "[Optional, used only with -tree or -separate-tree] draw trees instead of plain tree")
md := flag.Bool("md", false, "[Optional, used only with table view] output table as markdown")
outputFileName := flag.String("out", "", "[Optional] write output to file")
Expand Down
103 changes: 103 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package main

import (
"fmt"
"log"
"os"
"os/exec"
"strings"
"testing"
)

const (
testVersion string = "test"
testExecutable string = "tf-summarize-test"
expectedEmptyOutput string = `## foo
Terraform state outputs.
| Output | Value | Type
| --- | --- | --- |
`
)

func TestMain(m *testing.M) {
// compile a 'tf-summarize' for use in running tests
exe := exec.Command("go", "build", "-ldflags", fmt.Sprintf("-X main.version=%s", testVersion), "-o", testExecutable)
err := exe.Run()
if err != nil {
os.Exit(1)
}

m.Run()

// delete the compiled tf-summarize
err = os.Remove(testExecutable)
if err != nil {
log.Fatal(err)
}
}

func TestVersionArg(t *testing.T) {
args := []string{
"-v",
}

for _, arg := range args {
t.Run(fmt.Sprintf("when tf-summarize is passed '%s'", arg), func(t *testing.T) {
output, err := exec.Command(fmt.Sprintf("./%s", testExecutable), arg).CombinedOutput()
if err != nil {
t.Errorf("expected '%s' not to cause error; got '%v'", arg, err)
}

if !strings.Contains(string(output), testVersion) {
t.Errorf("expected '%s' to output version '%s'; got '%s'", arg, testVersion, output)
}
})
}
}

func TestTFSummarize(t *testing.T) {
tests := []struct {
command string
expectedError error
expectedOutput string
}{{
command: fmt.Sprintf("./%s -md example/tfplan.json", testExecutable),
expectedOutput: "basic.txt",
}, {
command: fmt.Sprintf("cat example/tfplan.json | ./%s -md", testExecutable),
expectedOutput: "basic.txt",
}}

for _, test := range tests {
t.Run(fmt.Sprintf("when tf-summarize is passed '%q'", test.command), func(t *testing.T) {
output, err := exec.Command("/bin/sh", "-c", test.command).CombinedOutput()
if err != nil && test.expectedError == nil {
t.Errorf("expected '%s' not to error; got '%v'", test.command, err)
}

b, err := os.ReadFile(fmt.Sprintf("testdata/%s", test.expectedOutput))
if err != nil {
t.Errorf("error reading file '%s': '%v'", test.expectedOutput, err)
}

expected := string(b)

if test.expectedError != nil && err == nil {
t.Errorf("expected error '%s'; got '%v'", test.expectedError.Error(), err)
}

if test.expectedError != nil && err != nil && test.expectedError.Error() != err.Error() {
t.Errorf("expected error '%s'; got '%v'", test.expectedError.Error(), err.Error())
}

if string(output) != expected {
t.Logf("expected output: \n%s", expected)
t.Logf("got output: \n%s", output)
t.Errorf("received unexpected output from '%s'", test.command)
}
})
}
}
9 changes: 9 additions & 0 deletions testdata/basic.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
| CHANGE | RESOURCE |
|--------|------------------------------------------------------------------------|
| add | `github_repository.terraform_plan_summary` |
| | `module.github["demo-repository"].github_branch.development` |
| | `module.github["demo-repository"].github_branch.main` |
| | `module.github["demo-repository"].github_repository.repository` |
| | `module.github["terraform-plan-summary"].github_branch.development` |
| | `module.github["terraform-plan-summary"].github_branch.main` |
| | `module.github["terraform-plan-summary"].github_repository.repository` |

0 comments on commit 0f8d033

Please sign in to comment.