Skip to main content

clitest_lib/
util.rs

1use std::{
2    borrow::Cow,
3    ffi::OsStr,
4    path::{Component, Path, PathBuf, Prefix},
5};
6
7use keepcalm::SharedGlobalMut;
8use serde::Serialize;
9use tempfile::TempDir;
10
11static CANONICAL_TEMP_DIR: SharedGlobalMut<PathBuf> = SharedGlobalMut::new_lazy(|| {
12    let tmp = if cfg!(target_vendor = "apple") {
13        Path::new("/tmp").to_owned()
14    } else {
15        std::env::temp_dir()
16    };
17    match dunce::canonicalize(&tmp) {
18        Ok(canonical) => canonical,
19        Err(_) => tmp,
20    }
21});
22
23static CANONICAL_CWD: SharedGlobalMut<Option<PathBuf>> = SharedGlobalMut::new_lazy(|| {
24    let cwd = std::env::current_dir().ok()?;
25    match dunce::canonicalize(&cwd) {
26        Ok(canonical) => Some(canonical),
27        Err(_) => Some(cwd),
28    }
29});
30
31static CANONICAL_HOME_DIR: SharedGlobalMut<Option<PathBuf>> = SharedGlobalMut::new_lazy(|| {
32    dirs::home_dir().map(|home| dunce::canonicalize(&home).unwrap_or(home))
33});
34
35#[derive(Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
36pub struct NicePathBuf {
37    path: PathBuf,
38}
39
40impl serde::Serialize for NicePathBuf {
41    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
42    where
43        S: serde::Serializer,
44    {
45        serializer.serialize_str(&self.path.display().to_string())
46    }
47}
48
49impl<'de> serde::Deserialize<'de> for NicePathBuf {
50    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
51    where
52        D: serde::Deserializer<'de>,
53    {
54        let s = String::deserialize(deserializer)?;
55        Ok(Self::new(&s))
56    }
57}
58
59impl From<&'_ NicePathBuf> for NicePathBuf {
60    fn from(path: &NicePathBuf) -> Self {
61        path.clone()
62    }
63}
64
65impl From<&'_ Path> for NicePathBuf {
66    fn from(path: &Path) -> Self {
67        NicePathBuf::new(path)
68    }
69}
70
71impl AsRef<Path> for NicePathBuf {
72    fn as_ref(&self) -> &Path {
73        &self.path
74    }
75}
76
77impl NicePathBuf {
78    pub fn new(path: impl AsRef<Path>) -> Self {
79        Self {
80            path: path.as_ref().to_path_buf(),
81        }
82    }
83
84    pub fn exists(&self) -> std::io::Result<bool> {
85        std::fs::exists(&self.path)
86    }
87
88    pub fn join(&self, other: impl AsRef<Path>) -> Self {
89        Self {
90            path: self.path.join(other.as_ref()),
91        }
92    }
93
94    pub fn create_dir_all(&self) -> std::io::Result<()> {
95        std::fs::create_dir_all(&self.path)
96    }
97
98    pub fn remove_dir_all(&self) -> std::io::Result<()> {
99        std::fs::remove_dir_all(&self.path)
100    }
101
102    pub fn parent(&self) -> Option<NicePathBuf> {
103        self.path.parent().map(NicePathBuf::new)
104    }
105
106    pub fn cwd() -> NicePathBuf {
107        let cwd = std::env::current_dir().expect("Couldn't get current directory");
108        cwd.into()
109    }
110
111    /// Returns a string that can be used in the environment to refer to this
112    /// path.
113    ///
114    /// In the case where this path may be accessed via multiple routes, we will
115    /// choose the shortest (ie: /tmp on macOS rather than /private/tmp).
116    pub fn env_string(&self) -> String {
117        let path = &self.path;
118        let canonical = canonicalize_path(path);
119        if cfg!(target_vendor = "apple") {
120            if let Ok(tmp) = canonical.strip_prefix(CANONICAL_TEMP_DIR.read()) {
121                format!("/tmp/{}", tmp.display())
122            } else {
123                canonical.display().to_string()
124            }
125        } else {
126            canonical.display().to_string()
127        }
128    }
129}
130
131impl From<PathBuf> for NicePathBuf {
132    fn from(path: PathBuf) -> Self {
133        Self { path }
134    }
135}
136
137impl From<String> for NicePathBuf {
138    fn from(path: String) -> Self {
139        Self {
140            path: PathBuf::from(path),
141        }
142    }
143}
144
145impl std::fmt::Display for NicePathBuf {
146    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
147        write_pretty_path(false, &self.path, f)
148    }
149}
150
151impl std::fmt::Debug for NicePathBuf {
152    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
153        write_pretty_path(true, &self.path, f)
154    }
155}
156
157pub struct NiceTempDir {
158    path: TempDir,
159}
160
161impl Default for NiceTempDir {
162    fn default() -> Self {
163        Self::new()
164    }
165}
166
167impl NiceTempDir {
168    pub fn new() -> Self {
169        let path = if cfg!(target_vendor = "apple") {
170            tempfile::Builder::new()
171                .tempdir_in("/tmp")
172                .expect("Couldn't create tempdir")
173        } else {
174            tempfile::tempdir().expect("Couldn't create tempdir")
175        };
176        debug_assert!(path.path().is_absolute());
177        debug_assert!(matches!(std::fs::exists(path.path()), Ok(true)));
178        Self { path }
179    }
180
181    pub fn exists(&self) -> Result<bool, std::io::Error> {
182        std::fs::exists(self.path.path())
183    }
184
185    pub fn remove_dir_all(self) -> std::io::Result<()> {
186        self.path.close()
187    }
188
189    pub fn join(&self, other: impl AsRef<Path>) -> NicePathBuf {
190        NicePathBuf::new(self.path.path().join(other.as_ref()))
191    }
192
193    pub fn file_name(&self) -> Option<&OsStr> {
194        self.path.path().file_name()
195    }
196
197    pub fn env_string(&self) -> String {
198        NicePathBuf::from(self).env_string()
199    }
200}
201
202impl std::fmt::Display for NiceTempDir {
203    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
204        write!(f, "{}", NicePathBuf::new(self.path.path()))
205    }
206}
207
208impl From<&'_ NiceTempDir> for NicePathBuf {
209    fn from(tempdir: &NiceTempDir) -> Self {
210        NicePathBuf::new(tempdir.path.path())
211    }
212}
213
214/// Best effort to canonicalize a path.
215fn canonicalize_path(path: &Path) -> Cow<'_, Path> {
216    if let Ok(path) = dunce::canonicalize(path) {
217        return path.into();
218    }
219
220    let mut components = path.components();
221    let Some(last) = components.next_back() else {
222        return path.into();
223    };
224
225    let mut rest = PathBuf::from(last.as_os_str());
226
227    // Walk up the path, canonicalizing each component and taking the first
228    // component that exists.
229    let mut path = path;
230    while let Some(parent) = path.parent() {
231        if let Ok(mut path) = dunce::canonicalize(parent) {
232            for component in rest.components() {
233                match component {
234                    Component::ParentDir => {
235                        if let Some(parent) = path.parent() {
236                            path = parent.to_path_buf();
237                        }
238                    }
239                    Component::CurDir => {}
240                    _ => {
241                        path = path.join(component.as_os_str());
242                    }
243                }
244            }
245            return path.into();
246        }
247
248        path = parent;
249        let mut components = path.components();
250        let Some(last) = components.next_back() else {
251            return path.into();
252        };
253
254        rest = PathBuf::from(last.as_os_str()).join(rest);
255    }
256
257    path.into()
258}
259
260fn write_pretty_path(
261    debug: bool,
262    path: &Path,
263    f: &mut std::fmt::Formatter<'_>,
264) -> std::fmt::Result {
265    let tmp = &*CANONICAL_TEMP_DIR.read();
266    let home = &*CANONICAL_HOME_DIR.read();
267    let cwd = &*CANONICAL_CWD.read();
268
269    let mut canon_path = canonicalize_path(path);
270
271    // On Apple, we can strip the /private prefix from the path for display purposes
272    if cfg!(target_vendor = "apple")
273        && canon_path.is_absolute()
274        && let Ok(without_private) = canon_path.strip_prefix("/private")
275    {
276        canon_path = Path::new("/").join(without_private).into();
277    }
278
279    // If the path is relative, we can try strip the cwd from its canonical
280    // version to eliminate any relative paths.
281    if let Some(cwd) = cwd
282        && let Ok(path) = canon_path.strip_prefix(cwd)
283    {
284        if debug {
285            write_debug_path(f, path)?;
286        } else {
287            write!(f, "./{}", path.display())?;
288        }
289        return Ok(());
290    }
291
292    // Unlikely, but just print the path if we're not on unix or windows
293    if !cfg!(unix) && !cfg!(windows) {
294        if debug {
295            write_debug_path(f, path)?;
296        } else {
297            write!(f, "{}", path.display())?;
298        }
299        return Ok(());
300    }
301
302    // If the path is in tmp, try to prettify it
303    if let Ok(path) = canon_path.strip_prefix(tmp) {
304        if cfg!(unix) {
305            let path = Path::new("/tmp").join(path);
306            if debug {
307                write_debug_path(f, &path)?;
308            } else {
309                write!(f, "{}", path.display())?;
310            }
311        } else if cfg!(windows) {
312            let path = Path::new("%TEMP%").join(path);
313            if debug {
314                write_debug_path(f, &path)?;
315            } else {
316                write!(f, "{}", path.display())?;
317            }
318        }
319        return Ok(());
320    }
321
322    // Skip out here in debug mode
323    if debug {
324        // On Windows, we can strip the \\?\ prefix from the path for display purposes
325        if cfg!(windows)
326            && let Some(Component::Prefix(prefix)) = canon_path.components().next()
327        {
328            // This is a backslash explosion in debug mode...
329            if let Prefix::VerbatimDisk(_) = prefix.kind() {
330                return f.write_str(&format!("<{}>", canon_path.display()).replace(r"\\?\", ""));
331            }
332        }
333
334        write_debug_path(f, &canon_path)?;
335        return Ok(());
336    }
337
338    // If the path is in home, try to prettify it
339    if let Some(home) = home
340        && let Ok(path) = canon_path.strip_prefix(home)
341    {
342        if cfg!(unix) {
343            write!(f, "~/{}", path.display())?;
344        } else if cfg!(windows) {
345            write!(f, "%USERPROFILE%\\{}", path.display())?;
346        }
347        return Ok(());
348    }
349
350    // On Windows, we can strip the \\?\ prefix from the path for display purposes
351    if cfg!(windows)
352        && let Some(Component::Prefix(prefix)) = canon_path.components().next()
353        && let Prefix::VerbatimDisk(_) = prefix.kind()
354    {
355        return write!(
356            f,
357            "{}",
358            canon_path.display().to_string().replace(r"\\?\", "")
359        );
360    }
361
362    write!(f, "{}", canon_path.display())
363}
364
365fn write_debug_path(f: &mut std::fmt::Formatter<'_>, path: &Path) -> std::fmt::Result {
366    if cfg!(windows) {
367        write!(f, "<{}>", path.display())
368    } else {
369        write!(f, "{path:?}")
370    }
371}
372
373#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, derive_more::Error, derive_more::Display)]
374pub enum ShellParseError {
375    #[display("unmatched quote ({_0})")]
376    UnmatchedQuote(#[error(not(source))] char),
377    #[display("invalid hex escape ({_0})")]
378    InvalidHexEscape(#[error(not(source))] char),
379}
380
381/// A single bit of a shell-ish string.
382#[derive(derive_more::Debug, Clone, Hash, Eq, PartialEq, PartialOrd, Ord)]
383pub enum ShellBit {
384    /// A literal string that does not participate in expansion. Comes from
385    /// `'string'`.
386    #[debug("{_0:?}")]
387    Literal(String),
388    /// A string that is (possibly) quoted and participates in expansion. Comes
389    /// from `"string"` or `string`.
390    #[debug("{_0:?}")]
391    Quoted(String),
392}
393
394impl PartialEq<str> for ShellBit {
395    fn eq(&self, other: &str) -> bool {
396        match self {
397            ShellBit::Literal(s) => s == other,
398            ShellBit::Quoted(s) => s == other,
399        }
400    }
401}
402
403impl PartialEq<&'_ str> for ShellBit {
404    fn eq(&self, other: &&str) -> bool {
405        match self {
406            ShellBit::Literal(s) => s == other,
407            ShellBit::Quoted(s) => s == other,
408        }
409    }
410}
411
412impl std::fmt::Display for ShellBit {
413    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
414        match self {
415            ShellBit::Literal(s) => f.write_str(s),
416            ShellBit::Quoted(s) => f.write_str(s),
417        }
418    }
419}
420
421impl Serialize for ShellBit {
422    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
423    where
424        S: serde::Serializer,
425    {
426        match self {
427            ShellBit::Literal(s) => serializer.serialize_str(s),
428            ShellBit::Quoted(s) => serializer.serialize_str(s),
429        }
430    }
431}
432
433/// Split a shell-ish string into a vector of strings.
434pub fn shell_split(input: &str) -> Result<Vec<ShellBit>, ShellParseError> {
435    let mut result = Vec::new();
436    let mut in_string = None;
437    let mut in_escape = false;
438    let mut in_hex_escape = 0;
439    let mut hex_accum = 0;
440    let mut accum = String::new();
441
442    for c in input.chars() {
443        match in_hex_escape {
444            2 => {
445                in_hex_escape = 1;
446                if c.is_ascii_hexdigit() {
447                    hex_accum = c.to_digit(16).unwrap();
448                    continue;
449                } else {
450                    return Err(ShellParseError::InvalidHexEscape(c));
451                }
452            }
453            1 => {
454                in_hex_escape = 0;
455                if c.is_ascii_hexdigit() {
456                    hex_accum = hex_accum * 16 + c.to_digit(16).unwrap();
457                    accum.push(char::from_u32(hex_accum).unwrap());
458                    continue;
459                } else {
460                    return Err(ShellParseError::InvalidHexEscape(c));
461                }
462            }
463            _ => {}
464        }
465
466        if in_escape {
467            in_escape = false;
468            match c {
469                // alert (BEL)
470                'a' => accum.push('\x07'),
471                // backspace
472                'b' => accum.push('\x08'),
473                // form feed
474                'f' => accum.push('\x0c'),
475                // new line
476                'n' => accum.push('\n'),
477                // carriage return
478                'r' => accum.push('\r'),
479                // horizontal tab
480                't' => accum.push('\t'),
481                // vertical tab
482                'v' => accum.push('\x0b'),
483                // escape
484                'e' => accum.push('\x1b'),
485                // null
486                '0' => accum.push('\0'),
487
488                '"' => accum.push('"'),
489                'x' => in_hex_escape = 2,
490                _ => {
491                    accum.push('\\');
492                    accum.push(c);
493                }
494            }
495            continue;
496        }
497
498        if let Some(string_char) = in_string {
499            if string_char == '\'' {
500                if c == string_char {
501                    in_string = None;
502                    result.push(ShellBit::Literal(std::mem::take(&mut accum)));
503                } else {
504                    accum.push(c);
505                }
506            } else if c == '\\' {
507                in_escape = true;
508            } else if c == string_char {
509                in_string = None;
510                if c == '"' {
511                    result.push(ShellBit::Quoted(std::mem::take(&mut accum)));
512                }
513            } else {
514                accum.push(c);
515            }
516        } else if c == '\\' {
517            in_escape = true;
518        } else if c == '"' || c == '\'' {
519            in_string = Some(c);
520        } else if c == ' ' {
521            if accum.is_empty() {
522                continue;
523            }
524            result.push(ShellBit::Quoted(std::mem::take(&mut accum)));
525        } else {
526            accum.push(c);
527        }
528    }
529    if let Some(string_char) = in_string {
530        return Err(ShellParseError::UnmatchedQuote(string_char));
531    }
532
533    if !accum.is_empty() {
534        result.push(ShellBit::Quoted(std::mem::take(&mut accum)));
535    }
536
537    Ok(result)
538}
539
540#[cfg(test)]
541mod tests {
542    use super::*;
543
544    #[cfg(unix)]
545    #[test]
546    fn test_nice_path_buf_tmp_unix() {
547        let path = NicePathBuf::new(Path::new("/tmp/hello.world"));
548
549        assert_eq!("/tmp/hello.world", format!("{path}"));
550        assert_eq!("\"/tmp/hello.world\"", format!("{path:?}"));
551
552        let path = NicePathBuf::new(Path::new("//tmp//hello.world"));
553
554        assert_eq!("/tmp/hello.world", format!("{path}"));
555        assert_eq!("\"/tmp/hello.world\"", format!("{path:?}"));
556
557        let path = NicePathBuf::new(Path::new("//does-not-exist-anywhere/..//tmp//hello.world"));
558
559        assert_eq!("/tmp/hello.world", format!("{path}"));
560        assert_eq!("\"/tmp/hello.world\"", format!("{path:?}"));
561
562        let path = NicePathBuf::new(
563            Path::new("/tmp")
564                .canonicalize()
565                .unwrap()
566                .join("hello.world"),
567        );
568
569        assert_eq!("/tmp/hello.world", format!("{path}"));
570        assert_eq!("\"/tmp/hello.world\"", format!("{path:?}"));
571
572        // Test partial canonicalization
573        let temp_dir = NiceTempDir::new();
574        let path = temp_dir.join("a/b/c/d");
575
576        let name = temp_dir.file_name().unwrap().to_string_lossy();
577
578        assert_eq!(format!("/tmp/{name}/a/b/c/d"), format!("{}", path));
579        assert_eq!(format!("\"/tmp/{name}/a/b/c/d\""), format!("{:?}", path));
580    }
581
582    #[cfg(windows)]
583    #[test]
584    fn test_nice_path_buf_tmp_windows() {
585        let tmp = std::env::temp_dir();
586        let tmp = tmp.join("hello.world");
587
588        let path = NicePathBuf::new(&tmp);
589
590        assert_eq!(r"%TEMP%\hello.world", format!("{}", path));
591        assert_eq!(r"<%TEMP%\hello.world>", format!("{:?}", path));
592
593        let path = NicePathBuf::new(
594            &std::env::temp_dir()
595                .canonicalize()
596                .unwrap()
597                .join("hello.world"),
598        );
599
600        assert_eq!(r"%TEMP%\hello.world", format!("{}", path));
601        assert_eq!(r"<%TEMP%\hello.world>", format!("{:?}", path));
602
603        let path = NicePathBuf::new(r#"C:\directory"#);
604
605        assert_eq!(r"C:\directory", format!("{}", path));
606        assert_eq!(r"<C:\directory>", format!("{:?}", path));
607    }
608
609    #[test]
610    fn test_shell_split() {
611        assert_eq!(format!("{:?}", shell_split("").unwrap()), r#"[]"#);
612        assert_eq!(format!("{:?}", shell_split("a").unwrap()), r#"["a"]"#);
613        assert_eq!(
614            format!("{:?}", shell_split("a b").unwrap()),
615            r#"["a", "b"]"#
616        );
617        assert_eq!(
618            format!("{:?}", shell_split("a b c").unwrap()),
619            r#"["a", "b", "c"]"#
620        );
621        assert_eq!(
622            format!("{:?}", shell_split("a 'b' c").unwrap()),
623            r#"["a", "b", "c"]"#
624        );
625        assert_eq!(
626            format!("{:?}", shell_split("a 'b c' d").unwrap()),
627            r#"["a", "b c", "d"]"#
628        );
629        assert_eq!(
630            format!("{:?}", shell_split(r#"a "b" c"#).unwrap()),
631            r#"["a", "b", "c"]"#
632        );
633        assert_eq!(
634            format!("{:?}", shell_split(r#"a "b c" d"#).unwrap()),
635            r#"["a", "b c", "d"]"#
636        );
637        assert_eq!(
638            format!("{:?}", shell_split(r#"a "b\"c" d"#).unwrap()),
639            r#"["a", "b\"c", "d"]"#
640        );
641        assert_eq!(
642            format!("{:?}", shell_split(r#"a "b\'c" d"#).unwrap()),
643            r#"["a", "b\\'c", "d"]"#
644        );
645        assert_eq!(
646            format!("{:?}", shell_split(r#"a "b\nc" d"#).unwrap()),
647            r#"["a", "b\nc", "d"]"#
648        );
649        assert_eq!(
650            format!("{:?}", shell_split(r#"a "a\\b" d"#).unwrap()),
651            r#"["a", "a\\\\b", "d"]"#
652        );
653        assert_eq!(
654            format!("{:?}", shell_split(r#"a 'a\\b' d"#).unwrap()),
655            r#"["a", "a\\\\b", "d"]"#
656        );
657    }
658
659    #[test]
660    fn test_shell_split_errors() {
661        assert_eq!(
662            shell_split("a 'b").unwrap_err(),
663            ShellParseError::UnmatchedQuote('\'')
664        );
665        assert_eq!(
666            shell_split("a \"b c").unwrap_err(),
667            ShellParseError::UnmatchedQuote('"')
668        );
669        assert_eq!(
670            shell_split("a '").unwrap_err(),
671            ShellParseError::UnmatchedQuote('\'')
672        );
673    }
674}