From 15d1ffb738a9592215849cad05c9b879368e09fb Mon Sep 17 00:00:00 2001 From: Adam Leclerc Date: Wed, 11 Dec 2024 14:33:32 -0400 Subject: [PATCH] - F Add Approval Script Reporter Co-Authored-By: Llewellyn Falco Co-Authored-By: Stephen Young <661424+hownowstephen@users.noreply.github.com> --- .gitignore | 2 + reporters/all_failing.go | 4 ++ ...eporter_that_creates_an_approval_script.go | 58 +++++++++++++++++++ utils/file_utils.go | 15 +++++ 4 files changed, 79 insertions(+) create mode 100644 reporters/reporter_that_creates_an_approval_script.go diff --git a/.gitignore b/.gitignore index 3c9ebc7..d1e6bf4 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,5 @@ _testmain.go *.test *.prof coverage.txt + +.approval_tests_temp \ No newline at end of file diff --git a/reporters/all_failing.go b/reporters/all_failing.go index 788a1e3..9b8e51d 100644 --- a/reporters/all_failing.go +++ b/reporters/all_failing.go @@ -11,6 +11,10 @@ func NewAllFailingTestReporter() Reporter { return &allFailing{} } +func NewReportAllToClipboard() Reporter { + return &allFailing{} +} + func (s *allFailing) Report(approved, received string) bool { move := getMoveCommandText(approved, received) clipboardScratchData = clipboardScratchData + move + "\n" diff --git a/reporters/reporter_that_creates_an_approval_script.go b/reporters/reporter_that_creates_an_approval_script.go new file mode 100644 index 0000000..5cdbd60 --- /dev/null +++ b/reporters/reporter_that_creates_an_approval_script.go @@ -0,0 +1,58 @@ +package reporters + +import ( + "fmt" + "os" + + "github.com/approvals/go-approval-tests/utils" +) + +var ( + directory = ".approval_tests_temp" + filenameWithoutExtention = directory + "/approval_script" + filename = "" +) + +type approvalScript struct{} + +// NewAllFailingTestReporter copies move file command to your clipboard +func NewReporterThatCreatesAnApprovalScript() Reporter { + initializeFile() + return &approvalScript{} +} + +func (s *approvalScript) Report(approved, received string) bool { + move := getMoveCommandText(approved, received) + "\n" + + utils.AppendToFile(filename, move) + + return true +} + +func initializeFile() { + if filename != "" { + return + } + + filename = filenameWithoutExtention + ".sh" + + // create the file and setup the parent directory if needed + err := os.MkdirAll(directory, os.ModePerm) + if err != nil { + fmt.Println("Error creating directory: ", err) + return + } + + // create the file and make it executable in one step + file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755) + if err != nil { + fmt.Println("Error creating file: ", err) + return + } + file.Close() + + utils.AppendToFile(filename, "#!/bin/bash\n") + + fmt.Println("You can run the approval script by executing: ", filename) + +} diff --git a/utils/file_utils.go b/utils/file_utils.go index 0c8439f..a99c32b 100644 --- a/utils/file_utils.go +++ b/utils/file_utils.go @@ -30,3 +30,18 @@ func ReadFile(fileName string) (string, error) { return string(content), nil } + +func AppendToFile(fileName, text string) error { + f, err := os.OpenFile(fileName, os.O_APPEND|os.O_WRONLY, 0644) + if err != nil { + return err + } + + defer f.Close() + + if _, err := f.WriteString(text); err != nil { + return err + } + + return nil +}