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 efdf4db
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
95 changes: 95 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

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

const (
testVersion string = "test"
testExecutable string = "tf-summarize-test"
)

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 efdf4db

Please sign in to comment.