Skip to content

Commit

Permalink
Refactor StrictPath parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Apr 7, 2024
1 parent 9801f60 commit a7239db
Show file tree
Hide file tree
Showing 9 changed files with 603 additions and 747 deletions.
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ pub fn run(sub: Subcommand, no_manifest_update: bool, try_manifest_update: bool)
game,
name,
&roots,
&StrictPath::from_std_path_buf(&app_dir()),
&StrictPath::from(app_dir()),
&launchers,
&filter,
&wine_prefix,
Expand Down
10 changes: 7 additions & 3 deletions src/cli/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ fn parse_strict_path(path: &str) -> Result<StrictPath, std::io::Error> {
}

fn parse_existing_strict_path(path: &str) -> Result<StrictPath, std::io::Error> {
let sp = StrictPath::new(path.to_owned());
std::fs::canonicalize(sp.interpret())?;
let cwd = StrictPath::cwd();
let sp = StrictPath::relative(path.to_owned(), Some(cwd.raw()));
sp.metadata()?;
Ok(sp)
}

Expand Down Expand Up @@ -912,7 +913,10 @@ mod tests {
try_manifest_update: false,
sub: Some(Subcommand::Restore {
preview: true,
path: Some(StrictPath::new(s("tests/backup"))),
path: Some(StrictPath::relative(
s("tests/backup"),
Some(StrictPath::cwd().interpret()),
)),
force: true,
api: true,
sort: Some(CliSort::Name),
Expand Down
112 changes: 46 additions & 66 deletions src/cli/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,14 +547,6 @@ mod tests {
testing::s,
};

fn drive() -> String {
if cfg!(target_os = "windows") {
StrictPath::new(s("foo")).render()[..2].to_string()
} else {
s("")
}
}

#[test]
fn can_render_in_standard_mode_with_minimal_input() {
let mut reporter = Reporter::standard();
Expand All @@ -566,15 +558,12 @@ mod tests {
&DuplicateDetector::default(),
);
assert_eq!(
format!(
r#"
r#"
Overall:
Games: 0
Size: 0 B
Location: {}/dev/null
"#,
&drive()
)
Location: /dev/null
"#
.trim_end(),
reporter.render(&StrictPath::new(s("/dev/null")))
)
Expand Down Expand Up @@ -631,8 +620,8 @@ Overall:
assert_eq!(
r#"
foo [100.00 KiB]:
- <drive>/file1
- [FAILED] <drive>/file2
- /file1
- [FAILED] /file2
- [FAILED] HKEY_CURRENT_USER/Key1
- HKEY_CURRENT_USER/Key2
- HKEY_CURRENT_USER/Key3
Expand All @@ -641,10 +630,9 @@ foo [100.00 KiB]:
Overall:
Games: 1
Size: 100.00 KiB / 150.00 KiB
Location: <drive>/dev/null
Location: /dev/null
"#
.trim()
.replace("<drive>", &drive()),
.trim(),
reporter.render(&StrictPath::new(s("/dev/null")))
);
}
Expand Down Expand Up @@ -708,18 +696,17 @@ Overall:
assert_eq!(
r#"
foo [1 B]:
- <drive>/file1
- /file1
bar [3 B]:
- <drive>/file2
- /file2
Overall:
Games: 2
Size: 4 B
Location: <drive>/dev/null
Location: /dev/null
"#
.trim()
.replace("<drive>", &drive()),
.trim(),
reporter.render(&StrictPath::new(s("/dev/null")))
);
}
Expand All @@ -734,20 +721,20 @@ Overall:
game_name: s("foo"),
found_files: hash_set! {
ScannedFile {
path: StrictPath::new(format!("{}/backup/file1", drive())),
path: StrictPath::new(s("/backup/file1")),
size: 102_400,
hash: "1".to_string(),
original_path: Some(StrictPath::new(format!("{}/original/file1", drive()))),
original_path: Some(StrictPath::new(s("/original/file1"))),
ignored: false,
change: Default::default(),
container: None,
redirected: None,
},
ScannedFile {
path: StrictPath::new(format!("{}/backup/file2", drive())),
path: StrictPath::new(s("/backup/file2")),
size: 51_200,
hash: "2".to_string(),
original_path: Some(StrictPath::new(format!("{}/original/file2", drive()))),
original_path: Some(StrictPath::new(s("/original/file2"))),
ignored: false,
change: Default::default(),
container: None,
Expand All @@ -764,16 +751,15 @@ Overall:
assert_eq!(
r#"
foo [150.00 KiB]:
- <drive>/original/file1
- <drive>/original/file2
- /original/file1
- /original/file2
Overall:
Games: 1
Size: 150.00 KiB
Location: <drive>/dev/null
Location: /dev/null
"#
.trim()
.replace("<drive>", &drive()),
.trim(),
reporter.render(&StrictPath::new(s("/dev/null")))
);
}
Expand Down Expand Up @@ -818,16 +804,15 @@ Overall:
assert_eq!(
r#"
foo [100.00 KiB] [DUPLICATES]:
- [DUPLICATED] <drive>/file1
- [DUPLICATED] /file1
- [DUPLICATED] HKEY_CURRENT_USER/Key1
Overall:
Games: 1
Size: 100.00 KiB
Location: <drive>/dev/null
Location: /dev/null
"#
.trim()
.replace("<drive>", &drive()),
.trim(),
reporter.render(&StrictPath::new(s("/dev/null")))
);
}
Expand Down Expand Up @@ -876,21 +861,20 @@ Overall:
assert_eq!(
r#"
foo [4 B] [Δ]:
- [Δ] <drive>/different
- [+] <drive>/new
- <drive>/same
- <drive>/unknown
- [Δ] /different
- [+] /new
- /same
- /unknown
bar [1 B] [+]:
- [+] <drive>/brand-new
- [+] /brand-new
Overall:
Games: 2 [+1] [Δ1]
Size: 5 B
Location: <drive>/dev/null
Location: /dev/null
"#
.trim()
.replace("<drive>", &drive()),
.trim(),
reporter.render(&StrictPath::new(s("/dev/null")))
);
}
Expand Down Expand Up @@ -980,11 +964,11 @@ Overall:
"decision": "Processed",
"change": "Same",
"files": {
"<drive>/file1": {
"/file1": {
"change": "Unknown",
"bytes": 100
},
"<drive>/file2": {
"/file2": {
"failed": true,
"change": "Unknown",
"bytes": 50
Expand All @@ -1011,8 +995,7 @@ Overall:
}
}
"#
.trim()
.replace("<drive>", &drive()),
.trim(),
reporter.render(&StrictPath::new(s("/dev/null")))
);
}
Expand All @@ -1027,20 +1010,20 @@ Overall:
game_name: s("foo"),
found_files: hash_set! {
ScannedFile {
path: StrictPath::new(format!("{}/backup/file1", drive())),
path: StrictPath::new(s("/backup/file1")),
size: 100,
hash: "1".to_string(),
original_path: Some(StrictPath::new(format!("{}/original/file1", drive()))),
original_path: Some(StrictPath::new(s("/original/file1"))),
ignored: false,
change: Default::default(),
container: None,
redirected: None,
},
ScannedFile {
path: StrictPath::new(format!("{}/backup/file2", drive())),
path: StrictPath::new(s("/backup/file2")),
size: 50,
hash: "2".to_string(),
original_path: Some(StrictPath::new(format!("{}/original/file2", drive()))),
original_path: Some(StrictPath::new(s("/original/file2"))),
ignored: false,
change: Default::default(),
container: None,
Expand Down Expand Up @@ -1073,11 +1056,11 @@ Overall:
"decision": "Processed",
"change": "Same",
"files": {
"<drive>/original/file1": {
"/original/file1": {
"change": "Unknown",
"bytes": 100
},
"<drive>/original/file2": {
"/original/file2": {
"change": "Unknown",
"bytes": 50
}
Expand All @@ -1087,8 +1070,7 @@ Overall:
}
}
"#
.trim()
.replace("<drive>", &drive()),
.trim(),
reporter.render(&StrictPath::new(s("/dev/null")))
);
}
Expand Down Expand Up @@ -1149,7 +1131,7 @@ Overall:
"decision": "Processed",
"change": "Same",
"files": {
"<drive>/file1": {
"/file1": {
"change": "Unknown",
"bytes": 100,
"duplicatedBy": [
Expand All @@ -1169,8 +1151,7 @@ Overall:
}
}
"#
.trim()
.replace("<drive>", &drive()),
.trim(),
reporter.render(&StrictPath::new(s("/dev/null")))
);
}
Expand Down Expand Up @@ -1218,19 +1199,19 @@ Overall:
"decision": "Processed",
"change": "Different",
"files": {
"<drive>/different": {
"/different": {
"change": "Different",
"bytes": 1
},
"<drive>/new": {
"/new": {
"change": "New",
"bytes": 1
},
"<drive>/same": {
"/same": {
"change": "Same",
"bytes": 1
},
"<drive>/unknown": {
"/unknown": {
"change": "Unknown",
"bytes": 1
}
Expand All @@ -1240,8 +1221,7 @@ Overall:
}
}
"#
.trim()
.replace("<drive>", &drive()),
.trim(),
reporter.render(&StrictPath::new(s("/dev/null")))
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ impl App {
&game,
&key,
&roots,
&StrictPath::from_std_path_buf(&app_dir()),
&StrictPath::from(app_dir()),
&launchers,
&filter,
&None,
Expand Down
Loading

0 comments on commit a7239db

Please sign in to comment.