Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into before-fail
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclerc-cio committed Nov 22, 2024
2 parents 5f21725 + 9e6f13c commit 6b25d02
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 14 deletions.
14 changes: 14 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -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"
16 changes: 16 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
28 changes: 16 additions & 12 deletions approval_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions reporters/continuous_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

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

0 comments on commit 6b25d02

Please sign in to comment.