Skip to content

Commit fafd312

Browse files
committed
fix(go-runner): patch all imports
1 parent fcec45e commit fafd312

10 files changed

+1155
-14
lines changed

go-runner/src/builder/patcher.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::prelude::*;
44
use std::fs;
5-
use std::path::{Path, PathBuf};
5+
use std::path::Path;
66
use std::process::Command;
77

88
pub fn replace_pkg<P: AsRef<Path>>(folder: P) -> anyhow::Result<()> {
@@ -29,21 +29,19 @@ pub fn replace_pkg<P: AsRef<Path>>(folder: P) -> anyhow::Result<()> {
2929
Ok(())
3030
}
3131

32-
pub fn patch_imports<P: AsRef<Path>>(
33-
folder: P,
34-
files_to_patch: Vec<PathBuf>,
35-
) -> anyhow::Result<()> {
32+
pub fn patch_imports<P: AsRef<Path>>(folder: P) -> anyhow::Result<()> {
3633
let folder = folder.as_ref();
3734
debug!("Patching imports in folder: {folder:?}");
3835

3936
// 1. Find all imports that match "testing" and replace them with codspeed equivalent
4037
let mut patched_files = 0;
41-
for go_file in files_to_patch {
38+
39+
let pattern = folder.join("**/*.go");
40+
for go_file in glob::glob(pattern.to_str().unwrap())?.filter_map(Result::ok) {
4241
let content =
4342
fs::read_to_string(&go_file).context(format!("Failed to read Go file: {go_file:?}"))?;
4443

4544
let patched_content = patch_go_source(&content)?;
46-
4745
if patched_content != content {
4846
fs::write(&go_file, patched_content)
4947
.context(format!("Failed to write patched Go file: {go_file:?}"))?;

go-runner/src/builder/templater.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,8 @@ pub fn run<P: AsRef<Path>>(
6868
))?;
6969
debug!("Relative package path: {relative_package_path:?}");
7070

71-
// 2. Find benchmark files and patch their imports
72-
let files_to_patch = package
73-
.benchmarks
74-
.iter()
75-
.map(|bench| target_dir.path().join(&bench.file_path))
76-
.collect::<Vec<_>>();
77-
patcher::patch_imports(&target_dir, files_to_patch)?;
71+
// 2. Patch the imports of all files with our own versions
72+
patcher::patch_imports(&target_dir)?;
7873

7974
// 3. Rename the _test.go files to _codspeed.go
8075
for file in files {

go-runner/src/integration_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ fn assert_results_snapshots(profile_dir: &Path, project_name: &str) {
7474
#[case::fuego("fuego")]
7575
#[case::cli_runtime("cli-runtime")]
7676
#[case::example("example")]
77+
#[case::example_with_helper("example-with-helper")]
7778
fn test_build_and_run(#[case] project_name: &str) {
7879
let project_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
7980
.join("testdata/projects")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
source: go-runner/src/integration_tests.rs
3+
expression: results
4+
---
5+
[
6+
{
7+
"creator": {
8+
"name": "codspeed-go",
9+
"version": "[version]",
10+
"pid": "[pid]"
11+
},
12+
"instrument": {
13+
"type": "walltime"
14+
},
15+
"benchmarks": [
16+
{
17+
"name": "BenchmarkFib",
18+
"uri": "go-runner/testdata/projects/example-with-helper/fib_test.go::BenchmarkFib",
19+
"config": {
20+
"warmup_time_ns": null,
21+
"min_round_time_ns": null,
22+
"max_time_ns": null,
23+
"max_rounds": null
24+
},
25+
"stats": "[stats]"
26+
}
27+
]
28+
}
29+
]

0 commit comments

Comments
 (0)