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 +}