Skip to content

Commit df0ed28

Browse files
committed
fix: cleanup temporary files during tests
1 parent 4b2789b commit df0ed28

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

go-runner/src/builder/templater.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,20 @@ 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.
36+
let mut target_dir = TempDir::new()?;
37+
3238
// NOTE: 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, avoid filling the disk with temporary files of tests (otherwise CI fails).
41+
target_dir.disable_cleanup(cfg!(not(test)));
3542

3643
// 1. Copy the whole git repository to a build directory
3744
let git_root = if let Ok(git_dir) = utils::get_parent_git_repo_path(&package.module.dir) {
@@ -60,7 +67,7 @@ pub fn run<P: AsRef<Path>>(package: &BenchmarkPackage, profile_dir: P) -> anyhow
6067
relative_package_path,
6168
};
6269
fs::write(
63-
target_dir.join("go-runner.metadata"),
70+
target_dir.path().join("go-runner.metadata"),
6471
serde_json::to_string_pretty(&metadata)?,
6572
)
6673
.context("Failed to write go-runner.metadata file")?;
@@ -81,7 +88,7 @@ pub fn run<P: AsRef<Path>>(package: &BenchmarkPackage, profile_dir: P) -> anyhow
8188
// 2. Patch the imports and package of the test files
8289
// - Renames package declarations (to support main package tests and external tests)
8390
// - Fixes imports to use our compat packages (e.g., testing/quicktest/testify)
84-
let package_path = target_dir.join(relative_package_path);
91+
let package_path = target_dir.path().join(relative_package_path);
8592
let test_file_paths: Vec<PathBuf> = files.iter().map(|f| package_path.join(f)).collect();
8693

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

112119
// 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");
120+
let codspeed_dir = target_dir
121+
.path()
122+
.join(relative_package_path)
123+
.join("codspeed");
114124
fs::create_dir_all(&codspeed_dir).context("Failed to create codspeed directory")?;
115125

116126
if package.is_external_test_package() {
@@ -119,7 +129,7 @@ pub fn run<P: AsRef<Path>>(package: &BenchmarkPackage, profile_dir: P) -> anyhow
119129
// They're now package main and will be built from the subdirectory
120130
debug!("Handling external test package - moving files to codspeed/ subdirectory");
121131
for file in files {
122-
let src_path = target_dir.join(relative_package_path).join(file);
132+
let src_path = target_dir.path().join(relative_package_path).join(file);
123133
// Rename _test.go to _codspeed.go so it's not treated as a test file
124134
let dst_filename = file.replace("_test.go", "_codspeed.go");
125135
let dst_path = codspeed_dir.join(&dst_filename);
@@ -132,7 +142,7 @@ pub fn run<P: AsRef<Path>>(package: &BenchmarkPackage, profile_dir: P) -> anyhow
132142
// For internal test packages: rename _test.go to _codspeed.go in place
133143
debug!("Handling internal test package - renaming files in place");
134144
for file in files {
135-
let old_path = target_dir.join(relative_package_path).join(file);
145+
let old_path = target_dir.path().join(relative_package_path).join(file);
136146
let new_path = old_path.with_file_name(
137147
old_path
138148
.file_name()
@@ -162,5 +172,5 @@ pub fn run<P: AsRef<Path>>(package: &BenchmarkPackage, profile_dir: P) -> anyhow
162172
let runner_path = codspeed_dir.join("runner.go");
163173
fs::write(&runner_path, rendered).context("Failed to write runner.go file")?;
164174

165-
Ok(runner_path)
175+
Ok((target_dir, runner_path))
166176
}

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)