Skip to content

Commit ba42390

Browse files
simpkinsfacebook-github-bot
authored andcommitted
fix hg status path relativization in tests on Windows
Summary: Fix the `test_status()` function to properly canonicalize the input paths, so that it works successfully on Windows. Previously this function would fail on Windows as some paths would end up as UNC-style paths and some would be plain paths, causing the equality comparison to fail even though the paths were equivalent. This canonicalizes the repo path so that they both use the same format. Reviewed By: quark-zju Differential Revision: D20662921 fbshipit-source-id: fdd36bac755f9694b4a482615d3dca43ff21e05e
1 parent 4aaa6c7 commit ba42390

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

eden/scm/lib/edenfs-client/src/path_relativizer.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ pub struct PathRelativizer {
8686

8787
/// Utility for computing a relativized path for a file in an Hg repository given the user's cwd
8888
/// and specified value for --repository/-R, if any.
89+
///
90+
/// Note: the caller is responsible for normalizing the repo_root and cwd parameters ahead of time.
91+
/// If these are specified in different formats (e.g., on Windows one is a UNC path and the other
92+
/// is not), then this function will not produce expected results. Unfortunately the Rust library
93+
/// does not provide a mechanism for normalizing paths. The best thing for callers to do for now
94+
/// is probably to call Path::canonicalize() if they expect that the paths do exist on disk.
8995
impl PathRelativizer {
9096
/// `cwd` corresponds to getcwd(2) while `repo_root` is the absolute path specified via
9197
/// --repository/-R, or failing that, the Hg repo that contains `cwd`.
@@ -95,7 +101,7 @@ impl PathRelativizer {
95101

96102
fn new_impl(cwd: &Path, repo_root: &Path) -> PathRelativizer {
97103
use self::PathRelativizerConfig::*;
98-
let config = if cwd.starts_with(repo_root) {
104+
let config = if cwd.starts_with(&repo_root) {
99105
CwdUnderRepo {
100106
relative_cwd: relativize(repo_root, cwd),
101107
}
@@ -144,6 +150,14 @@ mod test {
144150
check("/foo/bar/baz", "/foo/BAR/BAZ", "../../BAR/BAZ");
145151
}
146152

153+
#[test]
154+
fn relativize_platform_absolute_paths() {
155+
// This test with Windows-style absolute paths on Windows, and Unix-style path on Unix
156+
let cwd = Path::new(".").canonicalize().unwrap();
157+
let result = relativize(&cwd, &cwd.join("a").join("b"));
158+
assert_eq!(result, Path::new("a").join("b"));
159+
}
160+
147161
#[test]
148162
fn relativize_path_from_repo_when_cwd_is_repo_root() {
149163
let repo_root = Path::new("/home/zuck/tfb");

eden/scm/lib/edenfs-client/src/status.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,7 @@ mod test {
813813
/// focuses on exercising the display logic under various scenarios.
814814
fn test_status(test_case: StatusTestCase<'_>) {
815815
let repo_root = generate_fixture(test_case.files);
816+
let repo_root_path = repo_root.path().canonicalize().unwrap();
816817
let mut all_args: Vec<String> = vec![
817818
"--cwd".to_owned(),
818819
repo_root.path().to_str().unwrap().to_string(),
@@ -835,7 +836,7 @@ mod test {
835836

836837
let relativizer = HgStatusPathRelativizer::new(
837838
print_config.root_relative,
838-
PathRelativizer::new(hg_args.cwd, repo_root.path()),
839+
PathRelativizer::new(hg_args.cwd, repo_root_path),
839840
);
840841
let tin = "".as_bytes();
841842
let tout = Vec::new();

0 commit comments

Comments
 (0)