Skip to content

Commit ed306a6

Browse files
committed
feat: add quicktest support + examples
1 parent f974240 commit ed306a6

11 files changed

+2020
-37
lines changed

example-codspeed/cli/runner.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ func main() {
8787
{
8888
Name: "BenchmarkWithSlogAssert",
8989
F: compat.BenchmarkWithSlogAssert,
90+
}, {
91+
Name: "BenchmarkQuicktest",
92+
F: compat.BenchmarkQuicktest,
9093
},
9194
}
9295

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package compat
2+
3+
import (
4+
testing "github.com/CodSpeedHQ/codspeed-go/compat/testing"
5+
qt "github.com/CodSpeedHQ/codspeed-go/pkg/quicktest"
6+
)
7+
8+
func TestQuicktest(t *testing.T) {
9+
t.Run("numbers", func(t *testing.T) {
10+
c := qt.New(t)
11+
12+
c.Assert("hello world", qt.Contains, "world")
13+
c.Assert([]int{3, 5, 7, 99}, qt.Contains, 7)
14+
c.Assert([]int{3, 5, 8}, qt.All(qt.Not(qt.Equals)), 0)
15+
})
16+
}
17+
18+
func BenchmarkQuicktest(b *testing.B) {
19+
for b.Loop() {
20+
c := qt.New(b)
21+
c.Assert("hello world", qt.Contains, "world")
22+
c.Assert([]int{3, 5, 7, 99}, qt.Contains, 7)
23+
c.Assert([]int{3, 5, 8}, qt.All(qt.Not(qt.Equals)), 0)
24+
}
25+
}

example-codspeed/go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@ go 1.24.3
44

55
require github.com/CodSpeedHQ/codspeed-go v0.1.2
66

7+
require (
8+
github.com/google/go-cmp v0.7.0 // indirect
9+
github.com/kr/pretty v0.3.1 // indirect
10+
github.com/kr/text v0.2.0 // indirect
11+
github.com/rogpeppe/go-internal v1.9.0 // indirect
12+
)
13+
714
replace github.com/CodSpeedHQ/codspeed-go => ..

example-codspeed/go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
2+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
3+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
4+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
5+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
6+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
7+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
8+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
9+
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
10+
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=

example/compat/quicktest_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package example
2+
3+
import (
4+
"testing"
5+
6+
qt "github.com/frankban/quicktest"
7+
)
8+
9+
func TestQuicktest(t *testing.T) {
10+
t.Run("numbers", func(t *testing.T) {
11+
c := qt.New(t)
12+
13+
c.Assert("hello world", qt.Contains, "world")
14+
c.Assert([]int{3, 5, 7, 99}, qt.Contains, 7)
15+
c.Assert([]int{3, 5, 8}, qt.All(qt.Not(qt.Equals)), 0)
16+
})
17+
}
18+
19+
func BenchmarkQuicktest(b *testing.B) {
20+
for b.Loop() {
21+
c := qt.New(b)
22+
c.Assert("hello world", qt.Contains, "world")
23+
c.Assert([]int{3, 5, 7, 99}, qt.Contains, 7)
24+
c.Assert([]int{3, 5, 8}, qt.All(qt.Not(qt.Equals)), 0)
25+
}
26+
}

example/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ module example
33
go 1.24.3
44

55
require (
6+
github.com/frankban/quicktest v1.14.6
67
github.com/stretchr/testify v1.11.1
78
github.com/thejerf/slogassert v0.3.4
89
)
910

1011
require (
1112
github.com/davecgh/go-spew v1.1.1 // indirect
13+
github.com/google/go-cmp v0.5.9 // indirect
14+
github.com/kr/pretty v0.3.1 // indirect
15+
github.com/kr/text v0.2.0 // indirect
1216
github.com/pmezard/go-difflib v1.0.0 // indirect
17+
github.com/rogpeppe/go-internal v1.9.0 // indirect
1318
gopkg.in/yaml.v3 v3.0.1 // indirect
1419
)

example/go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
12
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
23
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
5+
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
6+
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
7+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
8+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
9+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
10+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
11+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
12+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
313
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
414
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
15+
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
16+
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
517
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
618
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
719
github.com/thejerf/slogassert v0.3.4 h1:VoTsXixRbXMrRSSxDjYTiEDCM4VWbsYPW5rB/hX24kM=

go-runner/src/builder/discovery.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl GoPackage {
7878
};
7979

8080
let mut benchmarks = Vec::new();
81-
'file_loop: for file in test_go_files.iter().sorted() {
81+
for file in test_go_files.iter().sorted() {
8282
assert!(file.ends_with("_test.go"));
8383

8484
let file_path = self.dir.join(file);
@@ -93,20 +93,6 @@ impl GoPackage {
9393
}
9494
};
9595

96-
// Check for unsupported imports
97-
const UNSUPPORTED_IMPORTS: &[(&str, &str)] =
98-
&[("github.com/frankban/quicktest", "quicktest")];
99-
for (import_path, import_name) in UNSUPPORTED_IMPORTS {
100-
if file
101-
.imports
102-
.iter()
103-
.any(|import| import.path.value.contains(import_path))
104-
{
105-
warn!("Skipping file with {import_name} import: {file_path:?}");
106-
continue 'file_loop;
107-
}
108-
}
109-
11096
// We can't import packages that are declared as `main`
11197
if file.pkg_name.name == "main" {
11298
warn!("Skipping file with main package: {file_path:?}");

go-runner/src/builder/patcher.rs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,29 @@ use std::fs;
55
use std::path::{Path, PathBuf};
66
use std::process::Command;
77

8+
fn codspeed_go_version() -> anyhow::Result<String> {
9+
if std::env::var("CODSPEED_LOCAL_GO_PKG").is_err() && cfg!(not(test)) {
10+
return Ok(format!("v{}", env!("CARGO_PKG_VERSION")));
11+
}
12+
13+
// When running in GitHub Actions, we always want to use the latest
14+
// codspeed-go package. For this, we have to use the current branch.
15+
if std::env::var("GITHUB_ACTIONS").is_ok() {
16+
return Ok(std::env::var("GITHUB_HEAD_REF")?);
17+
}
18+
19+
// Locally, run `git rev-parse --abbrev-ref HEAD` to get the current branch name
20+
let output = Command::new("git")
21+
.args(["rev-parse", "--abbrev-ref", "HEAD"])
22+
.output()
23+
.context("Failed to execute 'git rev-parse' command")?;
24+
if !output.status.success() {
25+
let stderr = String::from_utf8_lossy(&output.stderr);
26+
bail!("Failed to get current git branch: {}", stderr);
27+
}
28+
Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
29+
}
30+
831
pub fn replace_pkg<P: AsRef<Path>>(folder: P) -> anyhow::Result<()> {
932
let codspeed_root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
1033
let replace_arg = format!(
@@ -55,8 +78,10 @@ pub fn patch_imports<P: AsRef<Path>>(
5578
debug!("Patched {patched_files} files");
5679

5780
// 2. Update the go module to use the codspeed package
58-
let version = format!("v{}", env!("CARGO_PKG_VERSION"));
59-
let pkg = format!("github.com/CodSpeedHQ/codspeed-go@{version}");
81+
let pkg = format!(
82+
"github.com/CodSpeedHQ/codspeed-go@{}",
83+
codspeed_go_version()?
84+
);
6085
debug!("Installing {pkg}");
6186

6287
let mut cmd: Command = Command::new("go");
@@ -72,9 +97,20 @@ pub fn patch_imports<P: AsRef<Path>>(
7297
let stderr = String::from_utf8_lossy(&output.stderr);
7398
bail!("Failed to install codspeed-go dependency: {}", stderr);
7499
}
75-
76100
debug!("Successfully installed codspeed-go dependency");
77101

102+
// Run 'go mod tidy' to resolve transitive dependencies
103+
let output = Command::new("go")
104+
.args(["mod", "tidy"])
105+
.current_dir(folder)
106+
.output()
107+
.context("Failed to execute 'go mod tidy' command")?;
108+
if !output.status.success() {
109+
let stderr = String::from_utf8_lossy(&output.stderr);
110+
bail!("Failed to run 'go mod tidy': {}", stderr);
111+
}
112+
debug!("Ran 'go mod tidy' successfully");
113+
78114
// Ensure we have the latest codspeed-go package installed. Just
79115
// use the local one which might contain uncommitted changes.
80116
if std::env::var("CODSPEED_LOCAL_GO_PKG").is_ok() || cfg!(test) {
@@ -114,6 +150,11 @@ pub fn patch_go_source(source: &str) -> anyhow::Result<String> {
114150
"github.com/thejerf/slogassert",
115151
"\"github.com/CodSpeedHQ/codspeed-go/pkg/slogassert\"",
116152
)?;
153+
let source = replace_import(
154+
source,
155+
"github.com/frankban/quicktest",
156+
"\"github.com/CodSpeedHQ/codspeed-go/pkg/quicktest\"",
157+
)?;
117158

118159
Ok(source)
119160
}

go-runner/src/snapshots/codspeed_go_runner__integration_tests__assert_results_snapshots@example.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,17 @@ expression: results
235235
"type": "walltime"
236236
},
237237
"benchmarks": [
238+
{
239+
"name": "BenchmarkQuicktest",
240+
"uri": "example/compat/quicktest_test.go::BenchmarkQuicktest",
241+
"config": {
242+
"warmup_time_ns": null,
243+
"min_round_time_ns": null,
244+
"max_time_ns": null,
245+
"max_rounds": null
246+
},
247+
"stats": "[stats]"
248+
},
238249
{
239250
"name": "BenchmarkTestifyWithNew",
240251
"uri": "example/compat/testify_test.go::BenchmarkTestifyWithNew",

0 commit comments

Comments
 (0)