diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..69bcb89 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,14 @@ +version: 2 +updates: + - package-ecosystem: "gomod" + directory: "/" # Root directory of the repository + schedule: + interval: "weekly" + open-pull-requests-limit: 10 + rebase-strategy: "auto" + - package-ecosystem: "github-actions" + directory: "/" # Root directory of the repository + schedule: + interval: "daily" + open-pull-requests-limit: 5 + rebase-strategy: "auto" diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a9d8c15..bedbb73 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -32,3 +32,19 @@ jobs: - name: test run: go test ./... -race -coverprofile=coverage.txt -covermode=atomic + auto-merge: + needs: build-and-test + runs-on: ubuntu-latest + steps: + - name: Check out repo + uses: actions/checkout@v4 + - name: auto-merge + if: | + github.actor == 'dependabot[bot]' && + github.event_name == 'pull_request' + run: | + gh pr merge --auto --rebase "$PR_URL" + env: + PR_URL: ${{github.event.pull_request.html_url}} + # this secret needs to be in the settings.secrets.dependabot + GITHUB_TOKEN: ${{secrets.GH_ACTION_TOKEN}} diff --git a/approval_name.go b/approval_name.go index 5d93c71..dfb9145 100644 --- a/approval_name.go +++ b/approval_name.go @@ -48,28 +48,32 @@ func NewApprovalName(name string, fileName string) ApprovalName { func findFileName() (*string, error) { pc := make([]uintptr, 100) count := runtime.Callers(0, pc) + frames := runtime.CallersFrames(pc[:count]) - i := 0 - var lastFunc *runtime.Func + var lastFrame, testFrame *runtime.Frame - for ; i < count; i++ { - lastFunc = runtime.FuncForPC(pc[i]) - if isTestRunner(lastFunc) { + for { + frame, more := frames.Next() + if !more { break } + + if isTestRunner(&frame) { + testFrame = &frame + break + } + lastFrame = &frame } - testMethodPtr := pc[i-1] - testMethod := runtime.FuncForPC(testMethodPtr) - var fileName, _ = testMethod.FileLine(testMethodPtr) - if i == 0 || !isTestRunner(lastFunc) { + if !isTestRunner(testFrame) { return nil, fmt.Errorf("approvals: could not find the test method") } - return &fileName, nil + + return &lastFrame.File, nil } -func isTestRunner(f *runtime.Func) bool { - return f != nil && f.Name() == "testing.tRunner" || f.Name() == "testing.runExample" +func isTestRunner(f *runtime.Frame) bool { + return f != nil && f.Function == "testing.tRunner" || f.Function == "testing.runExample" } func (s *ApprovalName) compare(approvalFile, receivedFile string, reader io.Reader) error { 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..a4ff624 --- /dev/null +++ b/reporters/systemout_reporter.go @@ -0,0 +1,35 @@ +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", received) + printFileContent("Approved", approved) + + 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) +} diff --git a/utils/file_utils.go b/utils/file_utils.go index 57fb73a..0c8439f 100644 --- a/utils/file_utils.go +++ b/utils/file_utils.go @@ -19,3 +19,14 @@ func EnsureExists(fileName string) { ioutil.WriteFile(fileName, []byte(""), 0644) } + +// ReadFile reads the content of a file. +func ReadFile(fileName string) (string, error) { + content, err := ioutil.ReadFile(fileName) + + if err != nil { + return "", err + } + + return string(content), nil +}