Skip to content

Commit

Permalink
fix: permission denied when running from test suites (#44)
Browse files Browse the repository at this point in the history
closes #43
  • Loading branch information
gkampitakis committed Sep 7, 2022
1 parent 13a0de9 commit 5079c95
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 9 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ jobs:
with:
version: 'latest'
args: -c ./golangci.yml
- name: Golines
- name: Format lint
run: |
go install github.com/segmentio/golines@latest && \
golines . -w && \
git diff --quiet
make install-tools && make format && git diff --quiet
test:
name: Run tests
runs-on: ${{ matrix.os }}
Expand All @@ -51,4 +49,4 @@ jobs:
with:
go-version: ${{matrix.go}}
- name: Run Tests
run: go test -cover -race -v ./...
run: make test-verbose
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.PHONY: install-tools lint test test-verbose format

install-tools:
# Install linting tools
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.49.0
go install mvdan.cc/gofumpt@latest
go install github.com/segmentio/golines@latest

lint:
golangci-lint run -c ./golangci.yml ./...

format:
gofumpt -l -w -extra .
golines . -w

test:
go test -race -count=1 -cover ./...

test-verbose:
go test -race -count=1 -v -cover ./...
2 changes: 1 addition & 1 deletion snaps/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
// os.Exit(v)
// }
func Clean() {
if _, fName := baseCaller(); fName == "testing.tRunner" {
if _, fName := baseCaller(); fName != "TestMain" {
colors.Fprint(
os.Stdout,
colors.Yellow,
Expand Down
43 changes: 41 additions & 2 deletions snaps/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"fmt"
"regexp"
"runtime"
"strings"
"sync"
"unicode"
"unicode/utf8"

"github.com/gkampitakis/ciinfo"
"github.com/kr/pretty"
Expand Down Expand Up @@ -133,7 +136,7 @@ func baseCaller() (string, string) {
prevFile = file
pc, file, _, ok = runtime.Caller(i)
if !ok {
break
return "", ""
}

f := runtime.FuncForPC(pc)
Expand All @@ -142,14 +145,50 @@ func baseCaller() (string, string) {
}

funcName = f.Name()
if f.Name() == "testing.tRunner" {
if funcName == "testing.tRunner" {
break
}

// special case handling test runners
// tested with testify/suite, packagestest and testcase
segments := strings.Split(funcName, ".")
for _, segment := range segments {
if !isTest(segment, "Test") {
continue
}

// packagestest is same as tRunner where we step one caller further
// so we need to return the prevFile in testcase and testify/suite we return the current file
// e.g. funcName golang.org/x/tools/go/packages/packagestest.TestAll.func1
if strings.Contains(funcName, "packagestest") {
// return only the Function Name
// e.g. "go-snaps-testing-suite/src/issues.(*ExampleTestSuite).TestExampleSnapshot"
// will return TestExampleSnapshot
return prevFile, segment
}

return file, segment
}
}

return prevFile, funcName
}

// Stolen from the `go test` tool
//
// isTest tells whether name looks like a test
// It is a Test (say) if there is a character after Test that is not a lower-case letter
func isTest(name, prefix string) bool {
if !strings.HasPrefix(name, prefix) {
return false
}
if len(name) == len(prefix) { // "Test" is ok
return true
}
r, _ := utf8.DecodeRuneInString(name[len(prefix):])
return !unicode.IsLower(r)
}

func unescapeEndChars(input string) string {
return endCharEscapedRegexp.ReplaceAllLiteralString(input, "---")
}
Expand Down
34 changes: 33 additions & 1 deletion snaps/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,31 @@ func TestUtils(t *testing.T) {
test.Equal(t, expectedRegexp, escapedTestIDRegexp.String())
})
})
}

func TestBaseCallerNested(t *testing.T) {
file, fName := baseCaller()

test.Contains(t, file, "/snaps/utils_test.go")
test.Contains(t, fName, "TestBaseCallerNested")
}

func testBaseCallerNested(t *testing.T) {
file, fName := baseCaller()

test.Contains(t, file, "/snaps/utils_test.go")
test.Contains(t, fName, "TestBaseCaller")
}

func TestBaseCallerHelper(t *testing.T) {
t.Helper()
file, fName := baseCaller()

test.Contains(t, file, "/snaps/utils_test.go")
test.Contains(t, fName, "TestBaseCaller")
}

func TestBaseCaller(t *testing.T) {
t.Run("should return correct baseCaller", func(t *testing.T) {
var (
file string
Expand All @@ -54,6 +78,14 @@ func TestUtils(t *testing.T) {
}()

test.Contains(t, file, "/snaps/utils_test.go")
test.Contains(t, fName, "testing.tRunner")
test.Contains(t, fName, "TestBaseCaller")
})

t.Run("should return parent function", func(t *testing.T) {
testBaseCallerNested(t)
})

t.Run("should return function's name", func(t *testing.T) {
TestBaseCallerNested(t)
})
}

0 comments on commit 5079c95

Please sign in to comment.