Skip to content

Commit

Permalink
Support AST parsing with -trimpath flag (#4)
Browse files Browse the repository at this point in the history
* Support AST parsing with -trimpath flag

Checking the trimmed path to see if some suffix is a valid filepath is
probably not a perfect system, but it should be able to work at least
some of the time, which is better than nothing.

Also test on windows, because managing filepaths is hard.

* Make eventually test more lenient

It turns out not every machine has enough juice to get an extra
goroutine running in the 10ms I was giving it.
  • Loading branch information
rliebz authored Jan 27, 2024
1 parent f7cc441 commit e335ae5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
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 ./...

0 comments on commit e335ae5

Please # to comment.