Skip to content

Commit f155d6d

Browse files
committed
refactor get_binary_version
1 parent 5db8964 commit f155d6d

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

check_diff/src/lib.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ impl RustfmtRunner {
7575
.args(args)
7676
.output()
7777
}
78+
79+
fn get_binary_version(&self) -> Result<String, CheckDiffError> {
80+
let Ok(command) = Command::new(&self.binary_path)
81+
.env("LD_LIB_PATH", &self.ld_library_path)
82+
.args(["--version"])
83+
.output()
84+
else {
85+
return Err(CheckDiffError::FailedBinaryVersioning(
86+
self.binary_path.to_path_buf(),
87+
));
88+
};
89+
90+
let binding = String::from_utf8(command.stdout)?;
91+
let binary_version = binding.trim();
92+
return Ok(binary_version.to_string());
93+
}
7894
}
7995

8096
/// Clone a git repository
@@ -198,23 +214,9 @@ pub fn get_cargo_version() -> Result<String, CheckDiffError> {
198214
return Ok(cargo_version);
199215
}
200216

201-
pub fn get_binary_version(binary: &Path, ld_lib_path: &String) -> Result<String, CheckDiffError> {
202-
let Ok(command) = Command::new(binary)
203-
.env("LD_LIB_PATH", ld_lib_path)
204-
.args(["--version"])
205-
.output()
206-
else {
207-
return Err(CheckDiffError::FailedBinaryVersioning(binary.to_path_buf()));
208-
};
209-
210-
let binary_version = String::from_utf8(command.stdout)?;
211-
212-
return Ok(binary_version);
213-
}
214-
215217
/// Obtains the ld_lib path and then builds rustfmt from source
216218
/// If that operation succeeds, the source is then copied to the output path specified
217-
pub fn build_rustfmt_from_src(binary_path: &Path) -> Result<RustfmtRunner, CheckDiffError> {
219+
pub fn build_rustfmt_from_src(binary_path: PathBuf) -> Result<RustfmtRunner, CheckDiffError> {
218220
//Because we're building standalone binaries we need to set `LD_LIBRARY_PATH` so each
219221
// binary can find it's runtime dependencies.
220222
// See https://github.com/rust-lang/rustfmt/issues/5675
@@ -235,7 +237,7 @@ pub fn build_rustfmt_from_src(binary_path: &Path) -> Result<RustfmtRunner, Check
235237

236238
return Ok(RustfmtRunner {
237239
ld_library_path: ld_lib_path,
238-
binary_path: binary_path.into(),
240+
binary_path,
239241
});
240242
}
241243

@@ -252,33 +254,31 @@ pub fn compile_rustfmt(
252254
const RUSTFMT_REPO: &str = "https://github.com/rust-lang/rustfmt.git";
253255

254256
clone_git_repo(RUSTFMT_REPO, dest)?;
257+
change_directory_to_path(dest)?;
255258
git_remote_add(remote_repo_url.as_str())?;
256259
git_fetch(feature_branch.as_str())?;
257260

258261
let cargo_version = get_cargo_version()?;
259262
info!("Compiling with {}", cargo_version);
260-
let rustfmt_binary = dest.join("/rustfmt");
261-
let src_runner = build_rustfmt_from_src(&rustfmt_binary)?;
263+
let src_runner = build_rustfmt_from_src(dest.join("/rustfmt"))?;
262264

263265
let should_detach = commit_hash.is_some();
264266
git_switch(
265267
commit_hash.unwrap_or(feature_branch).as_str(),
266268
should_detach,
267269
)?;
268-
let feature_binary = dest.join("/feature_rustfmt");
269270

270-
let feature_runner = build_rustfmt_from_src(&feature_binary)?;
271+
let feature_runner = build_rustfmt_from_src(dest.join("/feature_rustfmt"))?;
271272

272273
info!(
273274
"\nRuntime dependencies for rustfmt -- LD_LIBRARY_PATH: {}",
274275
&feature_runner.ld_library_path
275276
);
276277

277-
let rustfmt_version = get_binary_version(&rustfmt_binary, &feature_runner.ld_library_path)?;
278+
let rustfmt_version = src_runner.get_binary_version()?;
278279
info!("\nRUSFMT_BIN {}\n", rustfmt_version);
279280

280-
let feature_binary_version =
281-
get_binary_version(&feature_binary, &(feature_runner.ld_library_path))?;
281+
let feature_binary_version = feature_runner.get_binary_version()?;
282282
info!("FEATURE_BIN {}\n", feature_binary_version);
283283

284284
return Ok(CheckDiffRunners {

0 commit comments

Comments
 (0)