Skip to content

Commit 691449e

Browse files
committed
use lazy_static to cache rustfmt's binary path
1 parent 6344e56 commit 691449e

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

src/cargo-fmt/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ fn execute() -> i32 {
102102
if opts.version {
103103
return handle_command_status(get_rustfmt_info(&[String::from("--version")]));
104104
}
105-
if opts.rustfmt_options.iter().any(|s| is_status_options(s.as_str())) {
105+
if opts
106+
.rustfmt_options
107+
.iter()
108+
.any(|s| is_status_options(s.as_str()))
109+
{
106110
return handle_command_status(get_rustfmt_info(&opts.rustfmt_options));
107111
}
108112

src/test/mod.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -903,29 +903,34 @@ impl Drop for TempFile {
903903
}
904904
}
905905

906-
fn rustfmt() -> PathBuf {
907-
let mut me = env::current_exe().expect("failed to get current executable");
908-
// Chop of the test name.
909-
me.pop();
910-
// Chop off `deps`.
911-
me.pop();
912-
913-
// If we run `cargo test --release`, we might only have a release build.
914-
if cfg!(release) {
915-
// `../release/`
916-
me.pop();
917-
me.push("release");
918-
}
919-
me.push("rustfmt");
920-
assert!(
921-
me.is_file() || me.with_extension("exe").is_file(),
922-
if cfg!(release) {
923-
"no rustfmt bin, try running `cargo build --release` before testing"
924-
} else {
925-
"no rustfmt bin, try running `cargo build` before testing"
926-
}
927-
);
928-
me
906+
fn rustfmt() -> &'static Path {
907+
lazy_static! {
908+
static ref RUSTFMT_PATH: PathBuf = {
909+
let mut me = env::current_exe().expect("failed to get current executable");
910+
// Chop of the test name.
911+
me.pop();
912+
// Chop off `deps`.
913+
me.pop();
914+
915+
// If we run `cargo test --release`, we might only have a release build.
916+
if cfg!(release) {
917+
// `../release/`
918+
me.pop();
919+
me.push("release");
920+
}
921+
me.push("rustfmt");
922+
assert!(
923+
me.is_file() || me.with_extension("exe").is_file(),
924+
if cfg!(release) {
925+
"no rustfmt bin, try running `cargo build --release` before testing"
926+
} else {
927+
"no rustfmt bin, try running `cargo build` before testing"
928+
}
929+
);
930+
me
931+
};
932+
}
933+
&RUSTFMT_PATH
929934
}
930935

931936
#[test]

0 commit comments

Comments
 (0)