Skip to content

Use t.TempDir() for temp files in tests#355

Merged
folbricht merged 1 commit into
masterfrom
tempdir-migration
Jun 11, 2026
Merged

Use t.TempDir() for temp files in tests#355
folbricht merged 1 commit into
masterfrom
tempdir-migration

Conversation

@folbricht

Copy link
Copy Markdown
Owner

Replaces manual temp file/dir management (os.CreateTemp / tempfile.New + io.Copy + Close + defer os.Remove) with paths inside t.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 in TestExtract and the dst/seed files in TestSeed/TestSelfSeedInPlace become os.WriteFile into a single per-test temp dir; the bytes and io imports drop out.
  • selfseed_test.go, sparse-file_test.go: target files are passed as nonexistent paths — safe because AssembleFile and NewSparseFile both open with O_CREATE (TestExtract already exercised the nonexistent-file case with out1).
  • make_test.go: no longer uses github.com/folbricht/tempfile for 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 one os.WriteFile per test.
  • cmd/desync/chop_test.go: the "invalid store" case used filepath.Join(os.TempDir(), "desync"), which would behave differently if /tmp/desync happened to exist; it now uses a guaranteed-nonexistent path under t.TempDir().

Error checks on touched lines were converted to require per the project convention. go vet ./... and go test ./... pass.

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.
@folbricht folbricht merged commit aaed7a2 into master Jun 11, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant