Skip to content

Commit b5255fa

Browse files
authored
refactor: Move from Lazy to OnceCell (#88)
This is prep for exploring a way to drop once_cell without an MSRV bump
2 parents 8e1cf0a + cace998 commit b5255fa

6 files changed

Lines changed: 66 additions & 60 deletions

File tree

crates/libtest2-mimic/tests/testsuite/all_passing.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
fn test_cmd() -> snapbox::cmd::Command {
2-
static BIN: once_cell::sync::Lazy<(std::path::PathBuf, std::path::PathBuf)> =
3-
once_cell::sync::Lazy::new(|| {
4-
let package_root = crate::util::new_test(
5-
r#"
2+
static BIN: once_cell::sync::OnceCell<(std::path::PathBuf, std::path::PathBuf)> =
3+
once_cell::sync::OnceCell::new();
4+
let (bin, current_dir) = BIN.get_or_init(|| {
5+
let package_root = crate::util::new_test(
6+
r#"
67
fn main() {
78
use libtest2_mimic::Trial;
89
libtest2_mimic::Harness::with_env()
@@ -14,12 +15,12 @@ fn main() {
1415
.main();
1516
}
1617
"#,
17-
false,
18-
);
19-
let bin = crate::util::compile_test(&package_root);
20-
(bin, package_root)
21-
});
22-
snapbox::cmd::Command::new(&BIN.0).current_dir(&BIN.1)
18+
false,
19+
);
20+
let bin = crate::util::compile_test(&package_root);
21+
(bin, package_root)
22+
});
23+
snapbox::cmd::Command::new(bin).current_dir(current_dir)
2324
}
2425

2526
fn check(args: &[&str], single: &str, parallel: &str) {

crates/libtest2-mimic/tests/testsuite/mixed_bag.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
fn test_cmd() -> snapbox::cmd::Command {
2-
static BIN: once_cell::sync::Lazy<(std::path::PathBuf, std::path::PathBuf)> =
3-
once_cell::sync::Lazy::new(|| {
4-
let package_root = crate::util::new_test(
5-
r#"
2+
static BIN: once_cell::sync::OnceCell<(std::path::PathBuf, std::path::PathBuf)> =
3+
once_cell::sync::OnceCell::new();
4+
let (bin, current_dir) = BIN.get_or_init(|| {
5+
let package_root = crate::util::new_test(
6+
r#"
67
fn main() {
78
use libtest2_mimic::Trial;
89
use libtest2_mimic::RunError;
@@ -35,12 +36,12 @@ fn main() {
3536
.main();
3637
}
3738
"#,
38-
false,
39-
);
40-
let bin = crate::util::compile_test(&package_root);
41-
(bin, package_root)
42-
});
43-
snapbox::cmd::Command::new(&BIN.0).current_dir(&BIN.1)
39+
false,
40+
);
41+
let bin = crate::util::compile_test(&package_root);
42+
(bin, package_root)
43+
});
44+
snapbox::cmd::Command::new(bin).current_dir(current_dir)
4445
}
4546

4647
fn check(args: &[&str], code: i32, single: &str, parallel: &str) {

crates/libtest2-mimic/tests/testsuite/panic.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
fn test_cmd() -> snapbox::cmd::Command {
2-
static BIN: once_cell::sync::Lazy<(std::path::PathBuf, std::path::PathBuf)> =
3-
once_cell::sync::Lazy::new(|| {
4-
let package_root = crate::util::new_test(
5-
r#"
2+
static BIN: once_cell::sync::OnceCell<(std::path::PathBuf, std::path::PathBuf)> =
3+
once_cell::sync::OnceCell::new();
4+
let (bin, current_dir) = BIN.get_or_init(|| {
5+
let package_root = crate::util::new_test(
6+
r#"
67
fn main() {
78
use libtest2_mimic::Trial;
89
libtest2_mimic::Harness::with_env()
@@ -13,12 +14,12 @@ fn main() {
1314
.main();
1415
}
1516
"#,
16-
false,
17-
);
18-
let bin = crate::util::compile_test(&package_root);
19-
(bin, package_root)
20-
});
21-
snapbox::cmd::Command::new(&BIN.0).current_dir(&BIN.1)
17+
false,
18+
);
19+
let bin = crate::util::compile_test(&package_root);
20+
(bin, package_root)
21+
});
22+
snapbox::cmd::Command::new(bin).current_dir(current_dir)
2223
}
2324

2425
fn check(args: &[&str], code: i32, single: &str, parallel: &str) {

crates/libtest2/tests/testsuite/all_passing.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
fn test_cmd() -> snapbox::cmd::Command {
2-
static BIN: once_cell::sync::Lazy<(std::path::PathBuf, std::path::PathBuf)> =
3-
once_cell::sync::Lazy::new(|| {
4-
let package_root = crate::util::new_test(
5-
r#"
2+
static BIN: once_cell::sync::OnceCell<(std::path::PathBuf, std::path::PathBuf)> =
3+
once_cell::sync::OnceCell::new();
4+
let (bin, current_dir) = BIN.get_or_init(|| {
5+
let package_root = crate::util::new_test(
6+
r#"
67
libtest2::libtest2_main!(foo, bar, barro);
78
89
fn foo(_state: &libtest2::State) -> libtest2::RunResult {
@@ -17,12 +18,12 @@ fn barro(_state: &libtest2::State) -> libtest2::RunResult {
1718
Ok(())
1819
}
1920
"#,
20-
false,
21-
);
22-
let bin = crate::util::compile_test(&package_root);
23-
(bin, package_root)
24-
});
25-
snapbox::cmd::Command::new(&BIN.0).current_dir(&BIN.1)
21+
false,
22+
);
23+
let bin = crate::util::compile_test(&package_root);
24+
(bin, package_root)
25+
});
26+
snapbox::cmd::Command::new(bin).current_dir(current_dir)
2627
}
2728

2829
fn check(args: &[&str], single: &str, parallel: &str) {

crates/libtest2/tests/testsuite/mixed_bag.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
fn test_cmd() -> snapbox::cmd::Command {
2-
static BIN: once_cell::sync::Lazy<(std::path::PathBuf, std::path::PathBuf)> =
3-
once_cell::sync::Lazy::new(|| {
4-
let package_root = crate::util::new_test(
5-
r#"
2+
static BIN: once_cell::sync::OnceCell<(std::path::PathBuf, std::path::PathBuf)> =
3+
once_cell::sync::OnceCell::new();
4+
let (bin, current_dir) = BIN.get_or_init(|| {
5+
let package_root = crate::util::new_test(
6+
r#"
67
libtest2::libtest2_main!(cat, dog, fox, bunny, frog, owl, fly, bear);
78
89
fn cat(_state: &libtest2::State) -> libtest2::RunResult {
@@ -42,12 +43,12 @@ fn bear(state: &libtest2::State) -> libtest2::RunResult {
4243
Err(libtest2::RunError::fail("no honey"))
4344
}
4445
"#,
45-
false,
46-
);
47-
let bin = crate::util::compile_test(&package_root);
48-
(bin, package_root)
49-
});
50-
snapbox::cmd::Command::new(&BIN.0).current_dir(&BIN.1)
46+
false,
47+
);
48+
let bin = crate::util::compile_test(&package_root);
49+
(bin, package_root)
50+
});
51+
snapbox::cmd::Command::new(bin).current_dir(current_dir)
5152
}
5253

5354
fn check(args: &[&str], code: i32, single: &str, parallel: &str) {

crates/libtest2/tests/testsuite/panic.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
fn test_cmd() -> snapbox::cmd::Command {
2-
static BIN: once_cell::sync::Lazy<(std::path::PathBuf, std::path::PathBuf)> =
3-
once_cell::sync::Lazy::new(|| {
4-
let package_root = crate::util::new_test(
5-
r#"
2+
static BIN: once_cell::sync::OnceCell<(std::path::PathBuf, std::path::PathBuf)> =
3+
once_cell::sync::OnceCell::new();
4+
let (bin, current_dir) = BIN.get_or_init(|| {
5+
let package_root = crate::util::new_test(
6+
r#"
67
libtest2::libtest2_main!(passes, panics);
78
89
fn passes(_state: &libtest2::State) -> libtest2::RunResult {
@@ -13,12 +14,12 @@ fn panics(_state: &libtest2::State) -> libtest2::RunResult {
1314
panic!("uh oh")
1415
}
1516
"#,
16-
false,
17-
);
18-
let bin = crate::util::compile_test(&package_root);
19-
(bin, package_root)
20-
});
21-
snapbox::cmd::Command::new(&BIN.0).current_dir(&BIN.1)
17+
false,
18+
);
19+
let bin = crate::util::compile_test(&package_root);
20+
(bin, package_root)
21+
});
22+
snapbox::cmd::Command::new(bin).current_dir(current_dir)
2223
}
2324

2425
fn check(args: &[&str], code: i32, single: &str, parallel: &str) {

0 commit comments

Comments
 (0)