Skip to content

Commit ac7faca

Browse files
authored
acc: auto clean up old wheels (#2759)
## Changes Automatically clean up old wheels instead of aborting the tests. ## Tests Manually: ``` ~/work/cli-main/acceptance/build/darwin_arm64 % cp -r databricks_bundles-0.249.0-py3-none-any.whl databricks_bundles-0.255.0-py3-none-any.whl ~/work/cli-main/acceptance/selftest/basic % testme -v + go test ../.. -run ^TestAccept$/^selftest$/^basic$ -v === RUN TestAccept ... acceptance_test.go:879: Cleaning up /Users/denis.bilenko/work/cli-main/acceptance/build/darwin_arm64/databricks_bundles-0.249.0-py3-none-any.whl acceptance_test.go:741: [uv build --no-cache -q --wheel --out-dir /Users/denis.bilenko/work/cli-main/acceptance/build/darwin_arm64] took 408.369459ms acceptance_test.go:879: Cleaning up /Users/denis.bilenko/work/cli-main/acceptance/build/darwin_arm64/databricks_bundles-0.255.0-py3-none-any.whl ```
1 parent bac7b49 commit ac7faca

File tree

1 file changed

+51
-20
lines changed

1 file changed

+51
-20
lines changed

acceptance/acceptance_test.go

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,11 @@ func testAccept(t *testing.T, InprocessMode bool, singleTest string) int {
101101
// Download terraform and provider and create config; this also creates build directory.
102102
RunCommand(t, []string{"python3", filepath.Join(cwd, "install_terraform.py"), "--targetdir", buildDir}, ".")
103103

104-
wheelPath, err := buildDatabricksBundlesWheel(t, buildDir)
105-
require.NoError(t, err)
106-
t.Setenv("DATABRICKS_BUNDLES_WHEEL", wheelPath)
107-
repls.SetPath(wheelPath, "[DATABRICKS_BUNDLES_WHEEL]")
104+
wheelPath := buildDatabricksBundlesWheel(t, buildDir)
105+
if wheelPath != "" {
106+
t.Setenv("DATABRICKS_BUNDLES_WHEEL", wheelPath)
107+
repls.SetPath(wheelPath, "[DATABRICKS_BUNDLES_WHEEL]")
108+
}
108109

109110
coverDir := os.Getenv("CLI_GOCOVERDIR")
110111

@@ -841,31 +842,61 @@ func getNodeTypeID(cloudEnv string) string {
841842
}
842843

843844
// buildDatabricksBundlesWheel builds the databricks-bundles wheel and returns the path to the wheel.
844-
// It's used to cache the wheel build between acceptance tests, because one build takes ~10 seconds.
845-
func buildDatabricksBundlesWheel(t *testing.T, buildDir string) (string, error) {
845+
func buildDatabricksBundlesWheel(t *testing.T, buildDir string) string {
846+
// Clean up directory, remove all but the latest wheel
847+
// Doing this avoids ambiguity if the build command below does not touch any whl files,
848+
// because it considers it already good. However, we would not know which one it considered good,
849+
// so we prepare here by keeping only one.
850+
_ = prepareWheelBuildDirectory(t, buildDir)
851+
846852
RunCommand(t, []string{"uv", "build", "--no-cache", "-q", "--wheel", "--out-dir", buildDir}, "../experimental/python")
847853

848-
files, err := os.ReadDir(buildDir)
849-
if err != nil {
850-
return "", fmt.Errorf("failed to read directory %s: %s", buildDir, err)
854+
latestWheel := prepareWheelBuildDirectory(t, buildDir)
855+
if latestWheel == "" {
856+
// Many tests don't need the wheel, so continue there rather than hard fail
857+
t.Errorf("databricks-bundles wheel not found in %s", buildDir)
851858
}
852859

853-
// we can't control output file name, so we have to search for it
860+
return latestWheel
861+
}
862+
863+
// Find all possible whl files in 'dir' and clean up all but the one with most recent mtime
864+
// Return that full path to the wheel with most recent mtime (that was not cleaned up)
865+
func prepareWheelBuildDirectory(t *testing.T, dir string) string {
866+
var wheels []string
867+
868+
files, err := os.ReadDir(dir)
869+
require.NoError(t, err)
870+
871+
var latestWheel string
872+
var latestTime time.Time
854873

855-
var wheelName string
874+
// First pass: find the latest wheel
856875
for _, file := range files {
857-
if strings.HasPrefix(file.Name(), "databricks_bundles-") && strings.HasSuffix(file.Name(), ".whl") {
858-
if wheelName != "" {
859-
return "", fmt.Errorf("multiple wheels found: %s and %s", wheelName, file.Name())
860-
} else {
861-
wheelName = file.Name()
876+
name := file.Name()
877+
if strings.HasPrefix(name, "databricks_bundles-") && strings.HasSuffix(name, ".whl") {
878+
info, err := file.Info()
879+
require.NoError(t, err)
880+
name = filepath.Join(dir, name)
881+
wheels = append(wheels, name)
882+
if info.ModTime().After(latestTime) {
883+
latestWheel = name
884+
latestTime = info.ModTime()
862885
}
863886
}
864887
}
865888

866-
if wheelName != "" {
867-
return filepath.Join(buildDir, wheelName), nil
868-
} else {
869-
return "", fmt.Errorf("databricks-bundles wheel not found in %s", buildDir)
889+
// Second pass: delete all wheels except the latest
890+
for _, wheel := range wheels {
891+
if wheel != latestWheel {
892+
err := os.Remove(wheel)
893+
if err == nil {
894+
t.Logf("Cleaning up %s", wheel)
895+
} else {
896+
t.Errorf("Cleaning up %s failed: %s", wheel, err)
897+
}
898+
}
870899
}
900+
901+
return latestWheel
871902
}

0 commit comments

Comments
 (0)