Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Use t.Cleanup instead of defer in Golang code tests #384

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions buildserver/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ func TestIntegration(t *testing.T) {
}
return orig(ctx, cloneURL, rev, zipURLTemplate)
}
defer func() {
t.Cleanup(func() {
gobuildserver.NewDepRepoVFS = orig
}()
})
}

ctx := context.Background()
Expand Down
8 changes: 4 additions & 4 deletions buildserver/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,18 +496,18 @@ func yza() {}
}
return nil, fmt.Errorf("no file system found for dep at %s rev %q", cloneURL, rev)
}
defer func() {
t.Cleanup(func() {
gobuildserver.NewDepRepoVFS = orig
}()
})

origRemoteFS := gobuildserver.RemoteFS
gobuildserver.RemoteFS = func(ctx context.Context, initializeParams lspext.InitializeParams) (ctxvfs.FileSystem, io.Closer, error) {
return mapFS(test.fs), ioutil.NopCloser(strings.NewReader("")), nil
}

defer func() {
t.Cleanup(func() {
gobuildserver.RemoteFS = origRemoteFS
}()
})
}

ctx := context.Background()
Expand Down
4 changes: 2 additions & 2 deletions langserver/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,11 @@ func integrationTest(
notifies <- req
return nil, nil
}))
defer func() {
t.Cleanup(func() {
if err := conn.Close(); err != nil {
t.Fatal("conn.Close:", err)
}
}()
})

rootURI := util.PathToURI(rootFSPath)
params := InitializeParams{
Expand Down
8 changes: 4 additions & 4 deletions langserver/internal/godef/go/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ func checkExprs(t *testing.T, pkg *ast.File, importer Importer, fset *token.File
default:
return true
}
defer func() {
t.Cleanup(func() {
if err := recover(); err != nil {
t.Fatalf("panic (%v) on %T", err, e)
//t.Fatalf("panic (%v) on %v at %v\n", err, e, FileSet.Position(e.Pos()))
}
}()
})
obj, _ := ExprType(e, importer, fset)
if obj == nil && mustResolve {
t.Errorf("no object for %v(%p, %T) at %v\n", e, e, e, fset.Position(e.Pos()))
Expand All @@ -127,9 +127,9 @@ func TestStdLib(t *testing.T) {
t.SkipNow()
}
Panic = false
defer func() {
t.Cleanup(func() {
Panic = true
}()
})
root := os.Getenv("GOROOT") + "/src"
cache := make(map[string]*ast.Package)
fset := token.NewFileSet()
Expand Down
4 changes: 2 additions & 2 deletions langserver/langserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1233,11 +1233,11 @@ func TestServer(t *testing.T) {
addr, done := startServer(t, jsonrpc2.HandlerWithError(h.handle))
defer done()
conn := dialServer(t, addr)
defer func() {
t.Cleanup(func() {
if err := conn.Close(); err != nil {
t.Fatal("conn.Close:", err)
}
}()
})

rootFSPath := util.UriToPath(test.rootURI)

Expand Down