Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support AST parsing with -trimpath flag #4

Merged
merged 2 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ permissions:

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
os:
- ubuntu-latest
- windows-latest
go-version:
- '1.19'
- '1.20'
- '1.21'
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Set up Go ${{ matrix.go-version }}
Expand All @@ -28,6 +31,8 @@ jobs:
run: go build -v ./...
- name: Test
run: go test -v ./...
- name: Test -trimpath
run: go test -trimpath -v ./...

lint:
runs-on: ubuntu-latest
Expand Down
20 changes: 14 additions & 6 deletions be/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package be_test

import (
"fmt"
"regexp"
"testing"
"time"

Expand Down Expand Up @@ -200,9 +201,9 @@ assertion %s is false
}

func TestEventually(t *testing.T) {
g := ghost.New(t)

t.Run("ok", func(t *testing.T) {
g := ghost.New(t)

count := 0
result := be.Eventually(func() ghost.Result {
count++
Expand All @@ -214,20 +215,27 @@ func TestEventually(t *testing.T) {
})

t.Run("not ok", func(t *testing.T) {
g := ghost.New(t)

count := 0
result := be.Eventually(func() ghost.Result {
count++
return be.Equal(count, -1)
}, 10*time.Millisecond, 5*time.Millisecond)
}, 100*time.Millisecond, 5*time.Millisecond)

g.Should(be.False(result.Ok))
g.Should(be.Equal(result.Message, `count != -1
got: 1
// TODO: This is a good signal for a native regexp assertion
matched, err := regexp.MatchString(`count != -1
got: \d+
want: -1
`))
`, result.Message)
g.NoError(err)
g.Should(be.True(matched))
})

t.Run("timeout", func(t *testing.T) {
g := ghost.New(t)

result := be.Eventually(func() ghost.Result {
time.Sleep(100 * time.Millisecond)
return be.True(true)
Expand Down
27 changes: 27 additions & 0 deletions ghostlib/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"go/format"
"go/parser"
"go/token"
"os"
"runtime"
"strings"
)
Expand Down Expand Up @@ -54,6 +55,8 @@ func callExprArgs(skip int) ([]ast.Expr, error) {
return nil, errors.New("failed to get file/line")
}

filename = findSystemFilepath(filename)

wantFunc := runtime.FuncForPC(pc)

fset := token.NewFileSet()
Expand All @@ -70,6 +73,30 @@ func callExprArgs(skip int) ([]ast.Expr, error) {
return node.Args, nil
}

// Passing the -trimpath flag will prevent looking up filepaths directly.
// In most cases, some suffix of the path will be a valid relative path, which
// we can use instead.
func findSystemFilepath(filename string) string {
if _, err := os.Stat(filename); err == nil {
return filename
}

cur := filename
for {
parts := strings.SplitN(cur, "/", 2)
if len(parts) < 2 {
break
}

cur = parts[1]
if _, err := os.Stat(cur); err == nil {
return cur
}
}

return filename
}

func callExprForFunc(
wantFunc *runtime.Func,
fset *token.FileSet,
Expand Down
1 change: 1 addition & 0 deletions tusk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ tasks:
- when: all
task: lint
- go test ./...
- go test -trimpath ./...
Loading