Skip to content

Commit

Permalink
! F Logging Approved Files
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclerc-cio committed Jan 30, 2025
1 parent 9a6026f commit 8b1a9db
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 20 deletions.
2 changes: 2 additions & 0 deletions approval_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func isTestRunner(f *runtime.Frame) bool {

func (s *ApprovalName) compare(approvalFile, receivedFile string, reader io.Reader) error {

GetApprovedFileLoggerInstance().Log(approvalFile)

received, err := io.ReadAll(reader)
if err != nil {
return err
Expand Down
61 changes: 61 additions & 0 deletions approved_file_log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package approvals

import (
"fmt"
"os"
"path/filepath"

"sync"

"github.com/approvals/go-approval-tests/utils"
)

var (
once sync.Once
instance *approvedFileLog
)

type approvedFileLog struct {
filename string
}

const approvalTempdirectory = ".approval_tests_temp"

func GetApprovedFileLoggerInstance() *approvedFileLog {

once.Do(func() {
instance = &approvedFileLog{
filename: approvalTempdirectory + "/.approved_files.log",
}
instance.initializeFile()
})

return instance
}

func (l approvedFileLog) initializeFile() {

// create the file and setup the parent directory if needed
err := os.MkdirAll(approvalTempdirectory, 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(l.filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
fmt.Println("Error creating file: ", err)
return
}

file.Close()

}

func (l approvedFileLog) Log(approvedFile string) {
// get the absolute path of approvedFile
approvedFile, _ = filepath.Abs(approvedFile)

utils.AppendToFile(l.filename, approvedFile+"\n")
}
19 changes: 0 additions & 19 deletions testdata/date_scrubber_test.TestSupportedFormats.approved.md

This file was deleted.

This file was deleted.

0 comments on commit 8b1a9db

Please sign in to comment.