-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* correct input reading logic This addresses #20 and ensures input is read from the plan file -- and not via STDIN -- if a plan file argument is provided. This also seeks to improve some of the error messaging to be a bit more clear. * demo issue 20 fix via GH Actions Ideally, tf-summarize would feature a suite of automated tests verifying its functionality. In absenece of that, this demos the issue #20 fix via GH Actions. * use consistent TF version when invoking TF - ensure the generation of the `example` directory data is done in a consistent, reproducible fashion - ensure GH Actions uses the same version of TF expected by the `example` directory - add plan and plan JSON files to source control, for testing purposes * add automated test verifying STDIN vs file-provided input This adds a basic automated test verifying the validity of the issue #20 fix. * remove `demo` job from Build GH Actions workflow Per code review request, @dineshba would prefer this be kept in a separate file.
- Loading branch information
Showing
10 changed files
with
214 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ tf-summarize | |
**/.envrc | ||
**/.terraform | ||
*.swp | ||
|
||
example/tfplan |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1.1.4 |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | |