Skip to content

Commit 265da88

Browse files
committed
Fix name of special directories
1 parent c7d3399 commit 265da88

File tree

3 files changed

+92
-4
lines changed

3 files changed

+92
-4
lines changed

src/fs_tree_builder.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use super::{
44
reporter::{error_report::Operation::*, ErrorReport, Event, Reporter},
55
size::Size,
66
tree_builder::{Info, TreeBuilder},
7+
utils::path_name,
78
};
89
use pipe_trait::Pipe;
910
use std::{
@@ -42,10 +43,7 @@ where
4243
} = builder;
4344

4445
TreeBuilder::<PathBuf, OsStringDisplay, Data, _, _> {
45-
name: root.file_name().map_or_else(
46-
|| ".".pipe(OsStringDisplay::os_string_from),
47-
OsStringDisplay::os_string_from,
48-
),
46+
name: path_name(&root),
4947

5048
path: root,
5149

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![deny(warnings)]
22

3+
mod utils;
4+
35
pub mod app;
46
pub mod args;
57
pub mod bytes_format;

src/utils.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use super::os_string_display::OsStringDisplay;
2+
use std::path::{Component::*, Path};
3+
4+
/// Get file name or directory name of a path.
5+
pub fn path_name(path: &Path) -> OsStringDisplay {
6+
match path.components().last() {
7+
None | Some(CurDir) => OsStringDisplay::os_string_from("."),
8+
Some(RootDir) => OsStringDisplay::os_string_from("/"),
9+
Some(Normal(name)) => OsStringDisplay::os_string_from(name),
10+
Some(Prefix(prefix)) => OsStringDisplay::os_string_from(prefix.as_os_str()),
11+
Some(ParentDir) => OsStringDisplay::os_string_from(path),
12+
}
13+
}
14+
15+
#[cfg(test)]
16+
use pretty_assertions::assert_eq;
17+
#[cfg(test)]
18+
use std::path::PathBuf;
19+
20+
#[test]
21+
fn empty() {
22+
let actual = path_name(&PathBuf::new());
23+
let expected = OsStringDisplay::os_string_from(".");
24+
assert_eq!(actual, expected);
25+
}
26+
27+
#[test]
28+
fn current_dir() {
29+
let actual = path_name(&PathBuf::from("."));
30+
let expected = OsStringDisplay::os_string_from(".");
31+
assert_eq!(actual, expected);
32+
}
33+
34+
#[cfg(unix)]
35+
#[test]
36+
fn root_dir() {
37+
let actual = path_name(&PathBuf::from("/"));
38+
let expected = OsStringDisplay::os_string_from("/");
39+
assert_eq!(actual, expected);
40+
}
41+
42+
#[cfg(windows)]
43+
#[test]
44+
fn root_dir() {
45+
let actual = path_name(&PathBuf::from("C:\\"));
46+
let expected = OsStringDisplay::os_string_from("c:");
47+
assert_eq!(actual, expected);
48+
}
49+
50+
#[cfg(unix)]
51+
#[test]
52+
fn normal_relative() {
53+
let actual = path_name(&PathBuf::from("abc/def/ghi"));
54+
let expected = OsStringDisplay::os_string_from("ghi");
55+
assert_eq!(actual, expected);
56+
}
57+
58+
#[cfg(unix)]
59+
#[test]
60+
fn normal_absolute() {
61+
let actual = path_name(&PathBuf::from("/abc/def/ghi"));
62+
let expected = OsStringDisplay::os_string_from("ghi");
63+
assert_eq!(actual, expected);
64+
}
65+
66+
#[cfg(unix)]
67+
#[test]
68+
fn normal_trailing_separator() {
69+
let actual = path_name(&PathBuf::from("abc/def/ghi/"));
70+
let expected = OsStringDisplay::os_string_from("ghi");
71+
assert_eq!(actual, expected);
72+
}
73+
74+
#[cfg(unix)]
75+
#[test]
76+
fn parent_dir() {
77+
let actual = path_name(&PathBuf::from(".."));
78+
let expected = OsStringDisplay::os_string_from("..");
79+
assert_eq!(actual, expected);
80+
}
81+
82+
#[cfg(unix)]
83+
#[test]
84+
fn grandparent_dir() {
85+
let actual = path_name(&PathBuf::from("../.."));
86+
let expected = OsStringDisplay::os_string_from("../..");
87+
assert_eq!(actual, expected);
88+
}

0 commit comments

Comments
 (0)