Skip to content

Commit

Permalink
fix: resolve path issues on Windows
Browse files Browse the repository at this point in the history
Running `cargo test` on Windows gets flaky when calling `canonicalize()`
on Path. This is because on Windows, the disk pathing for an absolute
path starts with `//?/C:/foo/bar/`. See Rust GitHub issue and discussion
here [1]. To resolve, add a helper function that remove the `//?/` from
the canonical path.

[1] - rust-lang/rust#42869
  • Loading branch information
n-dusan committed Jan 1, 2024
1 parent 6328783 commit dbb82ce
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/utils/archive.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//! The archive module contains structs for interacting with a Stele archive

use std::path::{Path, PathBuf};

use super::paths::fix_unc_path;
use anyhow::Context;
use std::path::{Path, PathBuf};

/// given a &Path `path`, return the path to the containing archive.
///
/// # Errors
/// Error if the path doesn't exist or isn't inside a Stele archive.
pub fn find_archive_path(path: &Path) -> anyhow::Result<PathBuf> {
let abs_path = path.canonicalize()?;
let abs_path = fix_unc_path(&path.canonicalize()?);
for working_path in abs_path.ancestors() {
if working_path.join(".stelae").exists() {
return Ok(working_path.to_owned());
Expand Down
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub mod archive;
pub mod cli;
pub mod git;
pub mod http;
pub mod paths;
17 changes: 17 additions & 0 deletions src/utils/paths.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! Utility functions for working with paths
use std::path::{Path, PathBuf};
/// On Windows removes the `\\?\\` prefix to UNC paths.
/// For other OS'es just turns the `Path` into a `PathBuf`
#[must_use]
pub fn fix_unc_path(res: &Path) -> PathBuf {
if cfg!(windows) {
let res_str = res.display().to_string();
if res_str.starts_with(r#"\\?"#) {
PathBuf::from(res_str.replace(r#"\\?\"#, ""))
} else {
res.to_path_buf()
}
} else {
res.to_path_buf()
}
}
11 changes: 3 additions & 8 deletions tests/archive_testtools/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::{bail, Result};
use lazy_static::lazy_static;
use std::path::{Path, PathBuf};
use stelae::utils::paths::fix_unc_path;

lazy_static! {
static ref SCRIPT_PATH: PathBuf = {
Expand All @@ -11,21 +12,16 @@ lazy_static! {
}

pub fn execute_script(script_name: &str, mut script_result_directory: PathBuf) -> Result<()> {
let script_absolute_path = SCRIPT_PATH.join(script_name).canonicalize()?;
let script_absolute_path = fix_unc_path(&SCRIPT_PATH.join(script_name).canonicalize()?);
let env_path = std::env::current_dir()?.join(script_name);
dbg!(&env_path);
dbg!(&script_absolute_path);
dbg!(&script_result_directory);
let mut cmd = std::process::Command::new(&script_absolute_path);
let output = match configure_command(&mut cmd, &script_result_directory).output() {
Ok(out) => out,
Err(err)
if err.kind() == std::io::ErrorKind::PermissionDenied || err.raw_os_error() == Some(193) /* windows */ =>
{
dbg!("Running script with bash");
cmd = std::process::Command::new("bash");
let output = configure_command(cmd.arg(script_absolute_path), &script_result_directory).output().unwrap();
dbg!(&output);
let output = configure_command(cmd.arg(&script_absolute_path), &script_result_directory).output().unwrap();
output
}
Err(err) => return Err(err.into()),
Expand Down Expand Up @@ -54,5 +50,4 @@ fn configure_command<'a>(
.env("GIT_COMMITTER_DATE", "2000-01-02 00:00:00 +0000")
.env("GIT_COMMITTER_EMAIL", "committer@openlawlib.org")
.env("GIT_COMMITTER_NAME", "committer")

}

0 comments on commit dbb82ce

Please sign in to comment.