Skip to content

Commit

Permalink
Fix update-urls unit tests broken by #1875 (#2058)
Browse files Browse the repository at this point in the history
Fixes: #2056.
  • Loading branch information
gmlewis authored Aug 21, 2021
1 parent da933ae commit a0448fc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:

- name: Ensure go generate produces a zero diff
shell: bash
run: go generate -x ./... && git diff --exit-code; code=$?; git checkout -- .; (exit $code)
run: go generate -x ./... && git diff --exit-code; code=$?; git checkout -- .; (exit $code)

- name: Run go test
run: go test -v -race -coverprofile coverage.txt -covermode atomic ./...
Expand All @@ -73,3 +73,10 @@ jobs:
- name: Upload coverage to Codecov
if: ${{ matrix.update-coverage }}
uses: codecov/codecov-action@51d810878be5422784e86451c0e7c14e5860ec47 #v2.0.2

- name: Ensure go generate produces a zero diff for update-urls
shell: bash
run: cd update-urls && go generate -x ./... && git diff --exit-code; code=$?; git checkout -- .; (exit $code)

- name: Run go test for update-urls
run: cd update-urls && go test -v -race ./...
5 changes: 4 additions & 1 deletion update-urls/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/google/go-github/update-urls

go 1.16

require github.com/pmezard/go-difflib v1.0.0
require (
github.com/google/go-cmp v0.5.6
github.com/pmezard/go-difflib v1.0.0
)
4 changes: 4 additions & 0 deletions update-urls/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
18 changes: 10 additions & 8 deletions update-urls/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ func main() {
}

if err := os.Chdir("./github"); err != nil {
log.Fatalf("Please run this from the go-github directory.")
if err := os.Chdir("../github"); err != nil {
log.Fatalf("Please run this from the go-github directory.")
}
}

pkgs, err := parser.ParseDir(fset, ".", sourceFilter, parser.ParseComments)
Expand Down Expand Up @@ -427,7 +429,7 @@ func (rafi *realAstFileIterator) Reset() {
var count int
for _, pkg := range rafi.pkgs {
for filename, f := range pkg.Files {
logf("Sending file #%v: %v to channel", count, filename)
// logf("Sending file #%v: %v to channel", count, filename)
rafi.ch <- &filenameAstFilePair{filename: filename, astFile: f}
count++
}
Expand All @@ -443,7 +445,7 @@ func (rafi *realAstFileIterator) Reset() {

func (rafi *realAstFileIterator) Next() *filenameAstFilePair {
for pair := range rafi.ch {
logf("Next: returning file %v", pair.filename)
// logf("Next: returning file %v", pair.filename)
return pair
}
return nil
Expand Down Expand Up @@ -704,7 +706,7 @@ func processAST(filename string, f *ast.File, services servicesMap, endpoints en

receiverName := recv.Names[0].Name

logf("ast.FuncDecl: %#v", *decl) // Doc, Recv, Name, Type, Body
logf("\n\nast.FuncDecl: %#v", *decl) // Doc, Recv, Name, Type, Body
logf("ast.FuncDecl.Name: %#v", *decl.Name) // NamePos, Name, Obj(nil)
// logf("ast.FuncDecl.Recv: %#v", *decl.Recv) // Opening, List, Closing
logf("ast.FuncDecl.Recv.List[0]: %#v", *recv) // Doc, Names, Type, Tag, Comment
Expand Down Expand Up @@ -1055,8 +1057,10 @@ func processCallExpr(expr *ast.CallExpr) (recv, funcName string, args []string)
case *ast.SelectorExpr: // X, Sel
logf("processCallExpr: X recv *ast.SelectorExpr: %#v", x.Sel)
recv = x.Sel.Name
case *ast.CallExpr: // Fun, LParen, Args, Ellipsis, RParen
logf("processCallExpr: X recv *ast.CallExpr: %#v", x)
default:
log.Fatalf("processCallExpr: unhandled X receiver type: %T", x)
log.Fatalf("processCallExpr: unhandled X receiver type: %T, funcName=%q", x, funcName)
}
default:
log.Fatalf("processCallExpr: unhandled Fun: %T", expr.Fun)
Expand Down Expand Up @@ -1191,9 +1195,7 @@ func parseEndpoint(s, method string) *Endpoint {
// eol = v
// }
path := strings.TrimSpace(s[len(method):eol])
if strings.HasPrefix(path, "{server}") { // Hack to remove {server}
path = strings.TrimPrefix(path, "{server}")
}
path = strings.TrimPrefix(path, "{server}")
path = paramLegacyRE.ReplaceAllString(path, "%v")
path = paramRE.ReplaceAllString(path, "%v")
// strip leading garbage
Expand Down
17 changes: 14 additions & 3 deletions update-urls/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,10 @@ func TestSortAndMergeFileEdits(t *testing.T) {
},
}

fileEditEqual := cmp.Comparer(func(a, b *FileEdit) bool {
return a.fromText == b.fromText && a.pos == b.pos && a.toText == b.toText
})

for i, tt := range tests {
t.Run(fmt.Sprintf("test #%v: %v", i, tt.name), func(t *testing.T) {
got := sortAndMergeFileEdits(tt.fileEdits)
Expand All @@ -445,7 +449,7 @@ func TestSortAndMergeFileEdits(t *testing.T) {
if i < len(tt.want) {
wantFileEdit = tt.want[i]
}
if !cmp.Equal(got[i], wantFileEdit) {
if !cmp.Equal(got[i], wantFileEdit, fileEditEqual) {
t.Errorf("got[%v] =\n%#v\nwant[%v]:\n%#v", i, got[i], i, wantFileEdit)
}
}
Expand Down Expand Up @@ -538,6 +542,13 @@ func TestGitURL(t *testing.T) {
}
}

var endpointEqual = cmp.Comparer(func(a, b *Endpoint) bool {
if a.httpMethod != b.httpMethod {
return false
}
return cmp.Equal(a.urlFormats, b.urlFormats)
})

func testWebPageHelper(t *testing.T, got, want map[string][]*Endpoint) {
t.Helper()

Expand All @@ -551,7 +562,7 @@ func testWebPageHelper(t *testing.T, got, want map[string][]*Endpoint) {
if ok && i < len(w) {
wantEndpoint = w[i]
}
if !cmp.Equal(got[k][i], wantEndpoint) {
if !cmp.Equal(got[k][i], wantEndpoint, endpointEqual) {
t.Errorf("got[%q][%v] =\n%#v\nwant[%q][%v]:\n%#v", k, i, got[k][i], k, i, wantEndpoint)
}
}
Expand Down Expand Up @@ -590,7 +601,7 @@ func TestParseEndpoint(t *testing.T) {
t.Run(fmt.Sprintf("test #%v: %v", i, tt.name), func(t *testing.T) {
got := parseEndpoint(tt.s, tt.method)

if !cmp.Equal(got, tt.want) {
if !cmp.Equal(got, tt.want, endpointEqual) {
t.Errorf("parseEndpoint = %#v, want %#v", got, tt.want)
}
})
Expand Down

0 comments on commit a0448fc

Please sign in to comment.