Skip to content

Commit a0448fc

Browse files
authored
Fix update-urls unit tests broken by #1875 (#2058)
Fixes: #2056.
1 parent da933ae commit a0448fc

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed

.github/workflows/tests.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656

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

6161
- name: Run go test
6262
run: go test -v -race -coverprofile coverage.txt -covermode atomic ./...
@@ -73,3 +73,10 @@ jobs:
7373
- name: Upload coverage to Codecov
7474
if: ${{ matrix.update-coverage }}
7575
uses: codecov/codecov-action@51d810878be5422784e86451c0e7c14e5860ec47 #v2.0.2
76+
77+
- name: Ensure go generate produces a zero diff for update-urls
78+
shell: bash
79+
run: cd update-urls && go generate -x ./... && git diff --exit-code; code=$?; git checkout -- .; (exit $code)
80+
81+
- name: Run go test for update-urls
82+
run: cd update-urls && go test -v -race ./...

update-urls/go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/google/go-github/update-urls
22

33
go 1.16
44

5-
require github.com/pmezard/go-difflib v1.0.0
5+
require (
6+
github.com/google/go-cmp v0.5.6
7+
github.com/pmezard/go-difflib v1.0.0
8+
)

update-urls/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
2+
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
13
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
24
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
6+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

update-urls/main.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ func main() {
126126
}
127127

128128
if err := os.Chdir("./github"); err != nil {
129-
log.Fatalf("Please run this from the go-github directory.")
129+
if err := os.Chdir("../github"); err != nil {
130+
log.Fatalf("Please run this from the go-github directory.")
131+
}
130132
}
131133

132134
pkgs, err := parser.ParseDir(fset, ".", sourceFilter, parser.ParseComments)
@@ -427,7 +429,7 @@ func (rafi *realAstFileIterator) Reset() {
427429
var count int
428430
for _, pkg := range rafi.pkgs {
429431
for filename, f := range pkg.Files {
430-
logf("Sending file #%v: %v to channel", count, filename)
432+
// logf("Sending file #%v: %v to channel", count, filename)
431433
rafi.ch <- &filenameAstFilePair{filename: filename, astFile: f}
432434
count++
433435
}
@@ -443,7 +445,7 @@ func (rafi *realAstFileIterator) Reset() {
443445

444446
func (rafi *realAstFileIterator) Next() *filenameAstFilePair {
445447
for pair := range rafi.ch {
446-
logf("Next: returning file %v", pair.filename)
448+
// logf("Next: returning file %v", pair.filename)
447449
return pair
448450
}
449451
return nil
@@ -704,7 +706,7 @@ func processAST(filename string, f *ast.File, services servicesMap, endpoints en
704706

705707
receiverName := recv.Names[0].Name
706708

707-
logf("ast.FuncDecl: %#v", *decl) // Doc, Recv, Name, Type, Body
709+
logf("\n\nast.FuncDecl: %#v", *decl) // Doc, Recv, Name, Type, Body
708710
logf("ast.FuncDecl.Name: %#v", *decl.Name) // NamePos, Name, Obj(nil)
709711
// logf("ast.FuncDecl.Recv: %#v", *decl.Recv) // Opening, List, Closing
710712
logf("ast.FuncDecl.Recv.List[0]: %#v", *recv) // Doc, Names, Type, Tag, Comment
@@ -1055,8 +1057,10 @@ func processCallExpr(expr *ast.CallExpr) (recv, funcName string, args []string)
10551057
case *ast.SelectorExpr: // X, Sel
10561058
logf("processCallExpr: X recv *ast.SelectorExpr: %#v", x.Sel)
10571059
recv = x.Sel.Name
1060+
case *ast.CallExpr: // Fun, LParen, Args, Ellipsis, RParen
1061+
logf("processCallExpr: X recv *ast.CallExpr: %#v", x)
10581062
default:
1059-
log.Fatalf("processCallExpr: unhandled X receiver type: %T", x)
1063+
log.Fatalf("processCallExpr: unhandled X receiver type: %T, funcName=%q", x, funcName)
10601064
}
10611065
default:
10621066
log.Fatalf("processCallExpr: unhandled Fun: %T", expr.Fun)
@@ -1191,9 +1195,7 @@ func parseEndpoint(s, method string) *Endpoint {
11911195
// eol = v
11921196
// }
11931197
path := strings.TrimSpace(s[len(method):eol])
1194-
if strings.HasPrefix(path, "{server}") { // Hack to remove {server}
1195-
path = strings.TrimPrefix(path, "{server}")
1196-
}
1198+
path = strings.TrimPrefix(path, "{server}")
11971199
path = paramLegacyRE.ReplaceAllString(path, "%v")
11981200
path = paramRE.ReplaceAllString(path, "%v")
11991201
// strip leading garbage

update-urls/main_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,10 @@ func TestSortAndMergeFileEdits(t *testing.T) {
433433
},
434434
}
435435

436+
fileEditEqual := cmp.Comparer(func(a, b *FileEdit) bool {
437+
return a.fromText == b.fromText && a.pos == b.pos && a.toText == b.toText
438+
})
439+
436440
for i, tt := range tests {
437441
t.Run(fmt.Sprintf("test #%v: %v", i, tt.name), func(t *testing.T) {
438442
got := sortAndMergeFileEdits(tt.fileEdits)
@@ -445,7 +449,7 @@ func TestSortAndMergeFileEdits(t *testing.T) {
445449
if i < len(tt.want) {
446450
wantFileEdit = tt.want[i]
447451
}
448-
if !cmp.Equal(got[i], wantFileEdit) {
452+
if !cmp.Equal(got[i], wantFileEdit, fileEditEqual) {
449453
t.Errorf("got[%v] =\n%#v\nwant[%v]:\n%#v", i, got[i], i, wantFileEdit)
450454
}
451455
}
@@ -538,6 +542,13 @@ func TestGitURL(t *testing.T) {
538542
}
539543
}
540544

545+
var endpointEqual = cmp.Comparer(func(a, b *Endpoint) bool {
546+
if a.httpMethod != b.httpMethod {
547+
return false
548+
}
549+
return cmp.Equal(a.urlFormats, b.urlFormats)
550+
})
551+
541552
func testWebPageHelper(t *testing.T, got, want map[string][]*Endpoint) {
542553
t.Helper()
543554

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

593-
if !cmp.Equal(got, tt.want) {
604+
if !cmp.Equal(got, tt.want, endpointEqual) {
594605
t.Errorf("parseEndpoint = %#v, want %#v", got, tt.want)
595606
}
596607
})

0 commit comments

Comments
 (0)