Skip to content

Commit

Permalink
- F Add Approval Script Reporter
Browse files Browse the repository at this point in the history
Co-Authored-By: Llewellyn Falco <[email protected]>
Co-Authored-By: Stephen Young <[email protected]>
  • Loading branch information
3 people committed Dec 11, 2024
1 parent 4e1dc5e commit 15d1ffb
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ _testmain.go
*.test
*.prof
coverage.txt

.approval_tests_temp
4 changes: 4 additions & 0 deletions reporters/all_failing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
58 changes: 58 additions & 0 deletions reporters/reporter_that_creates_an_approval_script.go
Original file line number Diff line number Diff line change
@@ -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)

}
15 changes: 15 additions & 0 deletions utils/file_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 15d1ffb

Please sign in to comment.