Skip to content

Commit e549bed

Browse files
committed
fix: cleanup temporary files during tests
1 parent 6b3652b commit e549bed

3 files changed

Lines changed: 28 additions & 15 deletions

File tree

go-runner/src/builder/templater.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,23 @@ struct TemplateData {
2525
///
2626
/// # Returns
2727
///
28-
/// The path to the generated runner.go file. This should be passed to the `build_binary` function to build
29-
/// the binary that will execute the benchmarks.
30-
pub fn run<P: AsRef<Path>>(package: &BenchmarkPackage, profile_dir: P) -> anyhow::Result<PathBuf> {
28+
/// - `TempDir`: The temporary directory containing the modified Go project. This directory will be automatically deleted when dropped (only in tests).
29+
/// - `PathBuf`: The path to the generated runner.go file. This should be passed to the `build_binary` function to build
30+
/// the binary that will execute the benchmarks.
31+
pub fn run<P: AsRef<Path>>(
32+
package: &BenchmarkPackage,
33+
profile_dir: P,
34+
) -> anyhow::Result<(TempDir, PathBuf)> {
3135
// Create a temporary target directory for building the modified Go project.
32-
// NOTE: We don't want to spend time cleanup any temporary files since the code is only
36+
let mut target_dir = TempDir::new()?;
37+
38+
// We don't want to spend time cleanup any temporary files since the code is only
3339
// run on CI servers which clean up themselves.
34-
let target_dir = TempDir::new()?.keep();
40+
// However, when running tests we don't want to fill the disk with temporary files, which
41+
// can cause the tests to fail due to lack of disk space.
42+
if cfg!(not(test)) {
43+
target_dir.disable_cleanup(true);
44+
}
3545

3646
// 1. Copy the whole git repository to a build directory
3747
let git_root = if let Ok(git_dir) = utils::get_parent_git_repo_path(&package.module.dir) {
@@ -60,7 +70,7 @@ pub fn run<P: AsRef<Path>>(package: &BenchmarkPackage, profile_dir: P) -> anyhow
6070
relative_package_path,
6171
};
6272
fs::write(
63-
target_dir.join("go-runner.metadata"),
73+
target_dir.path().join("go-runner.metadata"),
6474
serde_json::to_string_pretty(&metadata)?,
6575
)
6676
.context("Failed to write go-runner.metadata file")?;
@@ -81,7 +91,7 @@ pub fn run<P: AsRef<Path>>(package: &BenchmarkPackage, profile_dir: P) -> anyhow
8191
// 2. Patch the imports and package of the test files
8292
// - Renames package declarations (to support main package tests and external tests)
8393
// - Fixes imports to use our compat packages (e.g., testing/quicktest/testify)
84-
let package_path = target_dir.join(relative_package_path);
94+
let package_path = target_dir.path().join(relative_package_path);
8595
let test_file_paths: Vec<PathBuf> = files.iter().map(|f| package_path.join(f)).collect();
8696

8797
// If we have external tests (e.g. "package {pkg}_test") they have to be
@@ -102,15 +112,18 @@ pub fn run<P: AsRef<Path>>(package: &BenchmarkPackage, profile_dir: P) -> anyhow
102112
// Find the module directory by getting the relative path from git root
103113
let module_dir = Path::new(&package.module.dir)
104114
.strip_prefix(&git_root)
105-
.map(|relative_module_path| target_dir.join(relative_module_path))
115+
.map(|relative_module_path| target_dir.path().join(relative_module_path))
106116
.unwrap_or_else(|_| {
107117
// Fall back to target_dir if we can't calculate relative path
108-
target_dir.to_path_buf()
118+
target_dir.path().to_path_buf()
109119
});
110120
patcher::install_codspeed_dependency(&module_dir)?;
111121

112122
// 4. Handle test files differently based on whether they're external or internal tests
113-
let codspeed_dir = target_dir.join(relative_package_path).join("codspeed");
123+
let codspeed_dir = target_dir
124+
.path()
125+
.join(relative_package_path)
126+
.join("codspeed");
114127
fs::create_dir_all(&codspeed_dir).context("Failed to create codspeed directory")?;
115128

116129
if package.is_external_test_package() {
@@ -119,7 +132,7 @@ pub fn run<P: AsRef<Path>>(package: &BenchmarkPackage, profile_dir: P) -> anyhow
119132
// They're now package main and will be built from the subdirectory
120133
debug!("Handling external test package - moving files to codspeed/ subdirectory");
121134
for file in files {
122-
let src_path = target_dir.join(relative_package_path).join(file);
135+
let src_path = target_dir.path().join(relative_package_path).join(file);
123136
// Rename _test.go to _codspeed.go so it's not treated as a test file
124137
let dst_filename = file.replace("_test.go", "_codspeed.go");
125138
let dst_path = codspeed_dir.join(&dst_filename);
@@ -132,7 +145,7 @@ pub fn run<P: AsRef<Path>>(package: &BenchmarkPackage, profile_dir: P) -> anyhow
132145
// For internal test packages: rename _test.go to _codspeed.go in place
133146
debug!("Handling internal test package - renaming files in place");
134147
for file in files {
135-
let old_path = target_dir.join(relative_package_path).join(file);
148+
let old_path = target_dir.path().join(relative_package_path).join(file);
136149
let new_path = old_path.with_file_name(
137150
old_path
138151
.file_name()
@@ -162,5 +175,5 @@ pub fn run<P: AsRef<Path>>(package: &BenchmarkPackage, profile_dir: P) -> anyhow
162175
let runner_path = codspeed_dir.join("runner.go");
163176
fs::write(&runner_path, rendered).context("Failed to write runner.go file")?;
164177

165-
Ok(runner_path)
178+
Ok((target_dir, runner_path))
166179
}

go-runner/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn run_benchmarks<P: AsRef<Path>>(
3939
// 2. Generate codspeed runners, build binaries, and execute them
4040
for package in &packages {
4141
info!("Generating custom runner for package: {}", package.name);
42-
let runner_path = builder::templater::run(package, &profile_dir)?;
42+
let (_target_dir, runner_path) = builder::templater::run(package, &profile_dir)?;
4343

4444
info!("Building binary for package: {}", package.name);
4545

go-runner/tests/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::path::Path;
44
/// Helper function to run a single package with arguments
55
pub fn run_package_with_args(package: &BenchmarkPackage, args: &[&str]) -> anyhow::Result<String> {
66
let profile_dir = tempfile::TempDir::new()?;
7-
let runner_path = builder::templater::run(package, profile_dir.as_ref())?;
7+
let (_dir, runner_path) = builder::templater::run(package, profile_dir.as_ref())?;
88
let binary_path = builder::build_binary(&runner_path)?;
99
runner::run_with_stdout(&binary_path, args)
1010
}

0 commit comments

Comments
 (0)