Use t.TempDir() for temp files in tests#355
Merged
Merged
Conversation
Replace manual os.CreateTemp/tempfile.New plus deferred cleanup with paths inside t.TempDir(), which the test framework cleans up even when a test fails early. Target files for AssembleFile and NewSparseFile are passed as nonexistent paths since both create the file (O_CREATE). Drops the tempfile dependency from make_test.go (still used by production code) and makes chop's invalid-store case independent of whether /tmp/desync exists.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replaces manual temp file/dir management (
os.CreateTemp/tempfile.New+io.Copy+Close+defer os.Remove) with paths insidet.TempDir(), simplifying the tests (net -75 lines).t.TempDir()is cleaned up by the test framework even when a test fails before its defers run, so this also stops leaking files into the system temp dir on test crashes.assemble_test.go: the four temp files inTestExtractand thedst/seed files inTestSeed/TestSelfSeedInPlacebecomeos.WriteFileinto a single per-test temp dir; thebytesandioimports drop out.selfseed_test.go,sparse-file_test.go: target files are passed as nonexistent paths — safe becauseAssembleFileandNewSparseFileboth open withO_CREATE(TestExtractalready exercised the nonexistent-file case without1).make_test.go: no longer usesgithub.com/folbricht/tempfilefor test inputs; the dependency remains for production code (local.go,extract.go).cmd/desync/config_test.go,options_test.go: config-file setup collapses to oneos.WriteFileper test.cmd/desync/chop_test.go: the "invalid store" case usedfilepath.Join(os.TempDir(), "desync"), which would behave differently if/tmp/desynchappened to exist; it now uses a guaranteed-nonexistent path undert.TempDir().Error checks on touched lines were converted to
requireper the project convention.go vet ./...andgo test ./...pass.