From d26ff73fc99bcfa53539a49ba04595ee910137f1 Mon Sep 17 00:00:00 2001 From: Adam Leclerc Date: Fri, 22 Nov 2024 14:54:17 -0400 Subject: [PATCH] - F CI Now Uses Systemout reporter to print file contents --- examples_test.go | 3 +++ reporter_test.go | 5 +++- reporters/continuous_integration.go | 5 ++-- reporters/systemout_reporter.go | 36 +++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 reporters/systemout_reporter.go diff --git a/examples_test.go b/examples_test.go index f02492a..19fc172 100644 --- a/examples_test.go +++ b/examples_test.go @@ -5,7 +5,10 @@ import ( ) func ExampleVerifyString() { + t = makeExamplesRunLikeTests("ExampleVerifyString") + approvals.VerifyString(t, "Hello World!") + printFileContent("examples_test.ExampleVerifyString.received.txt") // Output: diff --git a/reporter_test.go b/reporter_test.go index 9d00492..15bfec3 100644 --- a/reporter_test.go +++ b/reporter_test.go @@ -1,6 +1,7 @@ package approvals import ( + "fmt" "os" "testing" @@ -15,7 +16,9 @@ type TestFailable struct { name string } -func (s *TestFailable) Fail() {} +func (s *TestFailable) Fail() { + fmt.Println("This test failed") +} func (s *TestFailable) Name() string { return s.name diff --git a/reporters/continuous_integration.go b/reporters/continuous_integration.go index 923feb7..a61efa9 100644 --- a/reporters/continuous_integration.go +++ b/reporters/continuous_integration.go @@ -19,8 +19,9 @@ func (s *continuousIntegration) Report(approved, received string) bool { if exists { ci, err := strconv.ParseBool(value) - if err == nil { - return ci + if err == nil && ci { + systemout := NewSystemoutReporter() + return systemout.Report(approved, received) } } diff --git a/reporters/systemout_reporter.go b/reporters/systemout_reporter.go new file mode 100644 index 0000000..3bed807 --- /dev/null +++ b/reporters/systemout_reporter.go @@ -0,0 +1,36 @@ +package reporters + +import ( + "fmt" + "path/filepath" + + "github.com/approvals/go-approval-tests/utils" +) + +type systemout struct{} + +// NewQuietReporter creates a new reporter that does nothing. +func NewSystemoutReporter() Reporter { + return &systemout{} +} + +func (s *systemout) Report(approved, received string) bool { + + approvedFull, _ := filepath.Abs(approved) + receivedFull, _ := filepath.Abs(received) + + fmt.Printf("approval files did not match\napproved: %v\nreceived: %v\n", approvedFull, receivedFull) + + printFileContent("Received", receivedFull) + printFileContent("Approved", approvedFull) + + return true +} + +func printFileContent(label, path string) { + content, err := utils.ReadFile(path) + if err != nil { + content = fmt.Sprintf("** Error reading %s file **", label) + } + fmt.Printf("%s content:\n%s\n", label, content) +}