Workhorse: Replace cleanup with t.Cleanup
Overview
Before t.Cleanup was introduced in 1.14 we had to return a cleanup function from a helper and defer its execution in order to perform cleanup after the tests: for example, to stop the test HTTP server or remove a temp dir.
Examples:
internal/upload/artifacts_upload_test.go
128: cleanup func()
146: cleanup := func() {
157: return &testServer{url: ts.URL + Path + qs, writer: writer, buffer: &buffer, fileWriter: fileWriter, cleanup: cleanup}
214: defer s.cleanup()
...
internal/lsif_transformer/parser/ranges_test.go
11: r, cleanup := setup(t)
...
List of files/functions that can be potentially refactored:
-
( cleanup) --internal/upload/artifacts_upload_test.go -
( setup) --internal/lsif_transformer/parser/ranges_test.go -
( testAuthServer) --main_test.go-- this function can close the started server int.Cleanup: it will save us from callingdefer ts.Close()everytime -
( startWorkhorseServerWithConfig,startWorkhorseServer) --main_test.go-- these two functions can accept*testing.Tand internally cleanup: it will save us from callingdefer ws.Close()everytime -
( startWebsocketServer) --channel_test.go-- this function can accept*testing.Tand internally cleanup: it will save us from callingdefer ws.Close()everytime -
( wireupChannel) --channel_test.go-- contains calls totestAuthServer,startWorkhorseServer,startWebsocketServer: when these functions are refactored,wireupChanneldon't need to explicitly cleanup anything -
( startRemoteServer) --internal/upstream/upstream_test.go-- this function can close the started server int.Cleanup: it will save us from callingdefer rs.Close()everytime -
( startRailsServer) --internal/upstream/upstream_test.go-- this function can close the started server int.Cleanup: it will save us from callingdefer rs.Close()everytime -
( startWorkhorseServer) --internal/upstream/upstream_test.go-- this function can close the started server int.Cleanup: it will save us from callingdefer ws.Close()everytime
Proposal
With t.Cleanup the cleanup function can be registered, which means it will be called after the test is complete.
Example: Refactor testhelper.PrepareTestRootDir using t.... (gitlab-shell!493 - merged)
Edited by Gavin Hinfey