Skip to content

Commit

Permalink
Replace testing of JUnitXMLTestReport directly with tests about (*Tes…
Browse files Browse the repository at this point in the history
…tJUnitXMLFile).Conclusion method
  • Loading branch information
SarahFrench committed Dec 17, 2024
1 parent 0cd3324 commit 608f2f3
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 170 deletions.
171 changes: 1 addition & 170 deletions internal/command/views/junit/junit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,176 +6,7 @@ import (
"github.com/hashicorp/terraform/internal/moduletest"
)

func Test_JUnitXMLTestReport(t *testing.T) {
cases := map[string]struct {
Suite *moduletest.Suite
XmlString string
}{
"no tests": {
XmlString: "<?xml version=\"1.0\" encoding=\"UTF-8\"?><testsuites></testsuites>",
Suite: &moduletest.Suite{},
},
"one passing test": {
XmlString: `<?xml version="1.0" encoding="UTF-8"?><testsuites>
<testsuite name="test_name.tftest.hcl" tests="1" skipped="0" failures="0" errors="0">
<testcase name="test_one" classname="test_name.tftest.hcl"></testcase>
</testsuite>
</testsuites>`,
Suite: &moduletest.Suite{
Status: moduletest.Skip,
Files: map[string]*moduletest.File{
"test_name.tftest.hcl": {
Name: "test_name.tftest.hcl",
Status: moduletest.Skip,
Runs: []*moduletest.Run{
{
Name: "test_one",
Status: moduletest.Pass,
},
},
},
},
},
},
"one skipped test": {
XmlString: `<?xml version="1.0" encoding="UTF-8"?><testsuites>
<testsuite name="test_name.tftest.hcl" tests="1" skipped="1" failures="0" errors="0">
<testcase name="test_one" classname="test_name.tftest.hcl">
<skipped></skipped>
</testcase>
</testsuite>
</testsuites>`,
Suite: &moduletest.Suite{
Status: moduletest.Skip,
Files: map[string]*moduletest.File{
"test_name.tftest.hcl": {
Name: "test_name.tftest.hcl",
Status: moduletest.Skip,
Runs: []*moduletest.Run{
{
Name: "test_one",
Status: moduletest.Skip,
},
},
},
},
},
},
"one failed test": {
XmlString: `<?xml version="1.0" encoding="UTF-8"?><testsuites>
<testsuite name="test_name.tftest.hcl" tests="1" skipped="0" failures="1" errors="0">
<testcase name="test_one" classname="test_name.tftest.hcl">
<failure message="Test run failed"></failure>
</testcase>
</testsuite>
</testsuites>`,
Suite: &moduletest.Suite{
Status: moduletest.Skip,
Files: map[string]*moduletest.File{
"test_name.tftest.hcl": {
Name: "test_name.tftest.hcl",
Status: moduletest.Skip,
Runs: []*moduletest.Run{
{
Name: "test_one",
Status: moduletest.Fail,
},
},
},
},
},
},
"three tests, each different status": {
XmlString: `<?xml version="1.0" encoding="UTF-8"?><testsuites>
<testsuite name="test_name.tftest.hcl" tests="3" skipped="1" failures="1" errors="0">
<testcase name="test_one" classname="test_name.tftest.hcl"></testcase>
<testcase name="test_two" classname="test_name.tftest.hcl">
<skipped></skipped>
</testcase>
<testcase name="test_three" classname="test_name.tftest.hcl">
<failure message="Test run failed"></failure>
</testcase>
</testsuite>
</testsuites>`,
Suite: &moduletest.Suite{
Status: moduletest.Skip,
Files: map[string]*moduletest.File{
"test_name.tftest.hcl": {
Name: "test_name.tftest.hcl",
Status: moduletest.Skip,
Runs: []*moduletest.Run{
{
Name: "test_one",
Status: moduletest.Pass,
},
{
Name: "test_two",
Status: moduletest.Skip,
},
{
Name: "test_three",
Status: moduletest.Fail,
},
},
},
},
},
},
"multiple test files with various tests": {
XmlString: `<?xml version="1.0" encoding="UTF-8"?><testsuites>
<testsuite name="test_file_one.tftest.hcl" tests="1" skipped="0" failures="0" errors="0">
<testcase name="test_one" classname="test_file_one.tftest.hcl"></testcase>
</testsuite>
<testsuite name="test_file_two.tftest.hcl" tests="2" skipped="1" failures="1" errors="0">
<testcase name="test_two" classname="test_file_two.tftest.hcl">
<skipped></skipped>
</testcase>
<testcase name="test_three" classname="test_file_two.tftest.hcl">
<failure message="Test run failed"></failure>
</testcase>
</testsuite>
</testsuites>`,
Suite: &moduletest.Suite{
Status: moduletest.Skip,
Files: map[string]*moduletest.File{
"test_file_one.tftest.hcl": {
Name: "test_file_one.tftest.hcl",
Status: moduletest.Skip,
Runs: []*moduletest.Run{
{
Name: "test_one",
Status: moduletest.Pass,
},
},
},
"test_file_two.tftest.hcl": {
Name: "test_file_two.tftest.hcl",
Status: moduletest.Skip,
Runs: []*moduletest.Run{
{
Name: "test_two",
Status: moduletest.Skip,
},
{
Name: "test_three",
Status: moduletest.Fail,
},
},
},
},
},
},
}

for tn, tc := range cases {
t.Run(tn, func(t *testing.T) {
b, _ := JUnitXMLTestReport(tc.Suite)
if string(b) != tc.XmlString {
t.Fatalf("wanted XML:\n%s\n got XML:\n%s\n", tc.XmlString, string(b))
}
})
}
}
// See TestTestJUnitXMLFile_Conclusion for testing of JUnitXMLTestReport

func Test_suiteFilesAsSortedList(t *testing.T) {
cases := map[string]struct {
Expand Down
187 changes: 187 additions & 0 deletions internal/command/views/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package views

import (
"fmt"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -3281,6 +3283,191 @@ func TestTestJSON_FatalInterruptSummary(t *testing.T) {
}
}

func TestTestJUnitXMLFile_Conclusion(t *testing.T) {
tcs := map[string]struct {
suite *moduletest.Suite
want string
}{
"no tests": {
want: "<?xml version=\"1.0\" encoding=\"UTF-8\"?><testsuites></testsuites>",
suite: &moduletest.Suite{},
},
"one passing test": {
want: `<?xml version="1.0" encoding="UTF-8"?><testsuites>
<testsuite name="test_name.tftest.hcl" tests="1" skipped="0" failures="0" errors="0">
<testcase name="test_one" classname="test_name.tftest.hcl"></testcase>
</testsuite>
</testsuites>`,
suite: &moduletest.Suite{
Status: moduletest.Skip,
Files: map[string]*moduletest.File{
"test_name.tftest.hcl": {
Name: "test_name.tftest.hcl",
Status: moduletest.Skip,
Runs: []*moduletest.Run{
{
Name: "test_one",
Status: moduletest.Pass,
},
},
},
},
},
},
"one skipped test": {
want: `<?xml version="1.0" encoding="UTF-8"?><testsuites>
<testsuite name="test_name.tftest.hcl" tests="1" skipped="1" failures="0" errors="0">
<testcase name="test_one" classname="test_name.tftest.hcl">
<skipped></skipped>
</testcase>
</testsuite>
</testsuites>`,
suite: &moduletest.Suite{
Status: moduletest.Skip,
Files: map[string]*moduletest.File{
"test_name.tftest.hcl": {
Name: "test_name.tftest.hcl",
Status: moduletest.Skip,
Runs: []*moduletest.Run{
{
Name: "test_one",
Status: moduletest.Skip,
},
},
},
},
},
},
"one failed test": {
want: `<?xml version="1.0" encoding="UTF-8"?><testsuites>
<testsuite name="test_name.tftest.hcl" tests="1" skipped="0" failures="1" errors="0">
<testcase name="test_one" classname="test_name.tftest.hcl">
<failure message="Test run failed"></failure>
</testcase>
</testsuite>
</testsuites>`,
suite: &moduletest.Suite{
Status: moduletest.Skip,
Files: map[string]*moduletest.File{
"test_name.tftest.hcl": {
Name: "test_name.tftest.hcl",
Status: moduletest.Skip,
Runs: []*moduletest.Run{
{
Name: "test_one",
Status: moduletest.Fail,
},
},
},
},
},
},
"three tests, each different status": {
want: `<?xml version="1.0" encoding="UTF-8"?><testsuites>
<testsuite name="test_name.tftest.hcl" tests="3" skipped="1" failures="1" errors="0">
<testcase name="test_one" classname="test_name.tftest.hcl"></testcase>
<testcase name="test_two" classname="test_name.tftest.hcl">
<skipped></skipped>
</testcase>
<testcase name="test_three" classname="test_name.tftest.hcl">
<failure message="Test run failed"></failure>
</testcase>
</testsuite>
</testsuites>`,
suite: &moduletest.Suite{
Status: moduletest.Skip,
Files: map[string]*moduletest.File{
"test_name.tftest.hcl": {
Name: "test_name.tftest.hcl",
Status: moduletest.Skip,
Runs: []*moduletest.Run{
{
Name: "test_one",
Status: moduletest.Pass,
},
{
Name: "test_two",
Status: moduletest.Skip,
},
{
Name: "test_three",
Status: moduletest.Fail,
},
},
},
},
},
},
"multiple test files with various tests": {
want: `<?xml version="1.0" encoding="UTF-8"?><testsuites>
<testsuite name="test_file_one.tftest.hcl" tests="1" skipped="0" failures="0" errors="0">
<testcase name="test_one" classname="test_file_one.tftest.hcl"></testcase>
</testsuite>
<testsuite name="test_file_two.tftest.hcl" tests="2" skipped="1" failures="1" errors="0">
<testcase name="test_two" classname="test_file_two.tftest.hcl">
<skipped></skipped>
</testcase>
<testcase name="test_three" classname="test_file_two.tftest.hcl">
<failure message="Test run failed"></failure>
</testcase>
</testsuite>
</testsuites>`,
suite: &moduletest.Suite{
Status: moduletest.Skip,
Files: map[string]*moduletest.File{
"test_file_one.tftest.hcl": {
Name: "test_file_one.tftest.hcl",
Status: moduletest.Skip,
Runs: []*moduletest.Run{
{
Name: "test_one",
Status: moduletest.Pass,
},
},
},
"test_file_two.tftest.hcl": {
Name: "test_file_two.tftest.hcl",
Status: moduletest.Skip,
Runs: []*moduletest.Run{
{
Name: "test_two",
Status: moduletest.Skip,
},
{
Name: "test_three",
Status: moduletest.Fail,
},
},
},
},
},
},
}

for tn, tc := range tcs {
t.Run(tn, func(t *testing.T) {

tmpDir := t.TempDir()
filename := fmt.Sprintf("%s/%s", tmpDir, "junit.xml")
view := &TestJUnitXMLFile{
filename: filename,
}

view.Conclusion(tc.suite)

// This implicitly tests that the XML file is written to the filename provided
b, err := os.ReadFile(filename)
if err != nil {
t.Fatalf("error opening XML file: %s", err)
}

if string(b) != tc.want {
t.Fatalf("wanted XML:\n%s\n got XML:\n%s\n", tc.want, string(b))
}
})
}
}

func dynamicValue(t *testing.T, value cty.Value, typ cty.Type) plans.DynamicValue {
d, err := plans.NewDynamicValue(value, typ)
if err != nil {
Expand Down

0 comments on commit 608f2f3

Please sign in to comment.