Skip to content
Open
Changes from 3 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
19 changes: 13 additions & 6 deletions pkg/minikube/machine/filesync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ func TestAssetsFromDir(t *testing.T) {
vmPath: "/",
},
}
var testDirs = make([]string, 0)

var testDirs []string
defer func() {
for _, testDir := range testDirs {
err := os.RemoveAll(testDir)
if err != nil {
if err := os.RemoveAll(testDir); err != nil {
t.Logf("got unexpected error removing test dir: %v", err)
}
}
Expand All @@ -98,7 +98,6 @@ func TestAssetsFromDir(t *testing.T) {
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
testDir := testutil.MakeTempDir(t)

testDirs = append(testDirs, testDir)
testFileBaseDir := filepath.Join(testDir, test.baseDir)
want := make(map[string]string)
Expand Down Expand Up @@ -128,9 +127,17 @@ func TestAssetsFromDir(t *testing.T) {
}

actualFiles, err := assetsFromDir(testFileBaseDir, test.vmPath, test.flatten)

t.Cleanup(func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must be called before calling assetsFromDir().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please clarify more on this because the current placement looks fine to me since:

  • assetsFromDir opens the files (via assets.NewFileAsset) and returns []assets.CopyableFile. We don’t have anything to close until you have that slice.
  • We register t.Cleanup immediately after obtaining actualFiles. That’s the earliest possible point we can close them reliably.
  • t.Cleanup runs after the subtest finishes (even if it fails), so descriptors are released in all cases.
  • Moving the cleanup registration “before” the call would be impossible (the slice doesn’t exist yet) unless we refactored assetsFromDir to accept a callback, which is unnecessary for a test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if assetsFromDir fails, we don't have anything to cleanup? Can return incomplete results and fail?

If it either return slice or fail without opening anything we are fine with current code. If it can open some files and fail, we can either clean up in assetsFromDir, or here by registring the cleanup before we call:

var actualFiles []Xxx
t.Cleanup(...)
actualFiles = ...

But it will probably better to cleanup in assetsFromDir() or a test helper using it. Test helpers should get a t *testing.T argument and handle cleanup and test failures internally so the test code is simpler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I introduced a collectAssets(t, root, dest, flatten) test helper that:

  • Calls assetsFromDir once and immediately registers a t.Cleanup to close any FileAsset descriptors it opened (including partial slices if the walk fails).
  • Keeps error handling + cleanup localized so each subtest only sets up files and validates the mapping. This addresses the earlier concern about calling cleanup “early enough” without modifying production code. Moving cleanup into assetsFromDir would change its resource lifecycle semantics for other callers, so keeping it in a test helper preserves current behavior while preventing leaks on Windows. Let me know if you’d prefer an additional error-path test; happy to add one if we can deterministically trigger a walk failure.

for _, f := range actualFiles {
if cerr := f.Close(); cerr != nil {
t.Logf("warning: closing asset %s failed: %v", f.GetSourcePath(), cerr)
}
}
})

if err != nil {
t.Errorf("got unexpected error adding minikube dir assets: %v", err)
return
t.Fatalf("got unexpected error adding minikube dir assets: %v", err)
}

got := make(map[string]string)
Expand Down