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 components = path.components();
221    let Some(last) = components.last() 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 components = path.components();
250        let Some(last) = components.last() 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") && canon_path.is_absolute() {
273        if let Ok(without_private) = canon_path.strip_prefix("/private") {
274            canon_path = Path::new("/").join(without_private).into();
275        }
276    }
277
278    // If the path is relative, we can try strip the cwd from its canonical
279    // version to eliminate any relative paths.
280    if let Some(cwd) = cwd {
281        if let Ok(path) = canon_path.strip_prefix(cwd) {
282            if debug {
283                write_debug_path(f, path)?;
284            } else {
285                write!(f, "{}", path.display())?;
286            }
287            return Ok(());
288        }
289    }
290
291    // Unlikely, but just print the path if we're not on unix or windows
292    if !cfg!(unix) && !cfg!(windows) {
293        if debug {
294            write_debug_path(f, path)?;
295        } else {
296            write!(f, "{}", path.display())?;
297        }
298        return Ok(());
299    }
300
301    // If the path is in tmp, try to prettify it
302    if let Ok(path) = canon_path.strip_prefix(tmp) {
303        if cfg!(unix) {
304            let path = Path::new("/tmp").join(path);
305            if debug {
306                write_debug_path(f, &path)?;
307            } else {
308                write!(f, "{}", path.display())?;
309            }
310        } else if cfg!(windows) {
311            let path = Path::new("%TEMP%").join(path);
312            if debug {
313                write_debug_path(f, &path)?;
314            } else {
315                write!(f, "{}", path.display())?;
316            }
317        }
318        return Ok(());
319    }
320
321    // Skip out here in debug mode
322    if debug {
323        // On Windows, we can strip the \\?\ prefix from the path for display purposes
324        if cfg!(windows) {
325            if let Some(Component::Prefix(prefix)) = canon_path.components().next() {
326                // This is a backslash explosion in debug mode...
327                if let Prefix::VerbatimDisk(_) = prefix.kind() {
328                    return f
329                        .write_str(&format!("<{}>", canon_path.display()).replace(r"\\?\", ""));
330                }
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        if let Ok(path) = canon_path.strip_prefix(home) {
341            if cfg!(unix) {
342                write!(f, "~/{}", path.display())?;
343            } else if cfg!(windows) {
344                write!(f, "%USERPROFILE%\\{}", path.display())?;
345            }
346            return Ok(());
347        }
348    }
349
350    // On Windows, we can strip the \\?\ prefix from the path for display purposes
351    if cfg!(windows) {
352        if let Some(Component::Prefix(prefix)) = canon_path.components().next() {
353            if let Prefix::VerbatimDisk(_) = prefix.kind() {
354                return write!(
355                    f,
356                    "{}",
357                    canon_path.display().to_string().replace(r"\\?\", "")
358                );
359            }
360        }
361    }
362
363    write!(f, "{}", canon_path.display())
364}
365
366fn write_debug_path(f: &mut std::fmt::Formatter<'_>, path: &Path) -> std::fmt::Result {
367    if cfg!(windows) {
368        write!(f, "<{}>", path.display())
369    } else {
370        write!(f, "{path:?}")
371    }
372}
373
374#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, derive_more::Error, derive_more::Display)]
375pub enum ShellParseError {
376    #[display("unmatched quote ({_0})")]
377    UnmatchedQuote(#[error(not(source))] char),
378    #[display("invalid hex escape ({_0})")]
379    InvalidHexEscape(#[error(not(source))] char),
380}
381
382/// A single bit of a shell-ish string.
383#[derive(derive_more::Debug, Clone, Hash, Eq, PartialEq, PartialOrd, Ord)]
384pub enum ShellBit {
385    /// A literal string that does not participate in expansion. Comes from
386    /// `'string'`.
387    #[debug("{_0:?}")]
388    Literal(String),
389    /// A string that is (possibly) quoted and participates in expansion. Comes
390    /// from `"string"` or `string`.
391    #[debug("{_0:?}")]
392    Quoted(String),
393}
394
395impl PartialEq<str> for ShellBit {
396    fn eq(&self, other: &str) -> bool {
397        match self {
398            ShellBit::Literal(s) => s == other,
399            ShellBit::Quoted(s) => s == other,
400        }
401    }
402}
403
404impl PartialEq<&'_ str> for ShellBit {
405    fn eq(&self, other: &&str) -> bool {
406        match self {
407            ShellBit::Literal(s) => s == other,
408            ShellBit::Quoted(s) => s == other,
409        }
410    }
411}
412
413impl ShellBit {
414    pub fn to_string(&self) -> String {
415        match self {
416            ShellBit::Literal(s) => s.clone(),
417            ShellBit::Quoted(s) => s.clone(),
418        }
419    }
420}
421
422impl Serialize for ShellBit {
423    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
424    where
425        S: serde::Serializer,
426    {
427        // TODO
428        match self {
429            ShellBit::Literal(s) => serializer.serialize_str(s),
430            ShellBit::Quoted(s) => serializer.serialize_str(s),
431        }
432    }
433}
434
435/// Split a shell-ish string into a vector of strings.
436pub fn shell_split(input: &str) -> Result<Vec<ShellBit>, ShellParseError> {
437    let mut result = Vec::new();
438    let mut in_string = None;
439    let mut in_escape = false;
440    let mut in_hex_escape = 0;
441    let mut hex_accum = 0;
442    let mut accum = String::new();
443
444    for c in input.chars() {
445        match in_hex_escape {
446            2 => {
447                in_hex_escape = 1;
448                if c.is_ascii_hexdigit() {
449                    hex_accum = c.to_digit(16).unwrap();
450                    continue;
451                } else {
452                    return Err(ShellParseError::InvalidHexEscape(c));
453                }
454            }
455            1 => {
456                in_hex_escape = 0;
457                if c.is_ascii_hexdigit() {
458                    hex_accum = hex_accum * 16 + c.to_digit(16).unwrap();
459                    accum.push(char::from_u32(hex_accum).unwrap());
460                    continue;
461                } else {
462                    return Err(ShellParseError::InvalidHexEscape(c));
463                }
464            }
465            _ => {}
466        }
467
468        if in_escape {
469            in_escape = false;
470            match c {
471                // alert (BEL)
472                'a' => accum.push('\x07'),
473                // backspace
474                'b' => accum.push('\x08'),
475                // form feed
476                'f' => accum.push('\x0c'),
477                // new line
478                'n' => accum.push('\n'),
479                // carriage return
480                'r' => accum.push('\r'),
481                // horizontal tab
482                't' => accum.push('\t'),
483                // vertical tab
484                'v' => accum.push('\x0b'),
485                // escape
486                'e' => accum.push('\x1b'),
487                // null
488                '0' => accum.push('\0'),
489
490                '"' => accum.push('"'),
491                'x' => in_hex_escape = 2,
492                _ => {
493                    accum.push('\\');
494                    accum.push(c);
495                }
496            }
497            continue;
498        }
499
500        if let Some(string_char) = in_string {
501            if string_char == '\'' {
502                if c == string_char {
503                    in_string = None;
504                    result.push(ShellBit::Literal(std::mem::take(&mut accum)));
505                } else {
506                    accum.push(c);
507                }
508            } else if c == '\\' {
509                in_escape = true;
510            } else if c == string_char {
511                in_string = None;
512                if c == '"' {
513                    result.push(ShellBit::Quoted(std::mem::take(&mut accum)));
514                }
515            } else {
516                accum.push(c);
517            }
518        } else if c == '\\' {
519            in_escape = true;
520        } else if c == '"' || c == '\'' {
521            in_string = Some(c);
522        } else if c == ' ' {
523            if accum.is_empty() {
524                continue;
525            }
526            result.push(ShellBit::Quoted(std::mem::take(&mut accum)));
527        } else {
528            accum.push(c);
529        }
530    }
531    if let Some(string_char) = in_string {
532        return Err(ShellParseError::UnmatchedQuote(string_char));
533    }
534
535    if !accum.is_empty() {
536        result.push(ShellBit::Quoted(std::mem::take(&mut accum)));
537    }
538
539    Ok(result)
540}
541
542#[cfg(test)]
543mod tests {
544    use super::*;
545
546    #[cfg(unix)]
547    #[test]
548    fn test_nice_path_buf_tmp_unix() {
549        let path = NicePathBuf::new(Path::new("/tmp/hello.world"));
550
551        assert_eq!("/tmp/hello.world", format!("{}", path));
552        assert_eq!("\"/tmp/hello.world\"", format!("{:?}", path));
553
554        let path = NicePathBuf::new(Path::new("//tmp//hello.world"));
555
556        assert_eq!("/tmp/hello.world", format!("{}", path));
557        assert_eq!("\"/tmp/hello.world\"", format!("{:?}", path));
558
559        let path = NicePathBuf::new(Path::new("//does-not-exist-anywhere/..//tmp//hello.world"));
560
561        assert_eq!("/tmp/hello.world", format!("{}", path));
562        assert_eq!("\"/tmp/hello.world\"", format!("{:?}", path));
563
564        let path = NicePathBuf::new(
565            Path::new("/tmp")
566                .canonicalize()
567                .unwrap()
568                .join("hello.world"),
569        );
570
571        assert_eq!("/tmp/hello.world", format!("{}", path));
572        assert_eq!("\"/tmp/hello.world\"", format!("{:?}", path));
573
574        // Test partial canonicalization
575        let temp_dir = NiceTempDir::new();
576        let path = temp_dir.join("a/b/c/d");
577
578        let name = temp_dir.file_name().unwrap().to_string_lossy();
579
580        assert_eq!(format!("/tmp/{name}/a/b/c/d"), format!("{}", path));
581        assert_eq!(format!("\"/tmp/{name}/a/b/c/d\""), format!("{:?}", path));
582    }
583
584    #[cfg(windows)]
585    #[test]
586    fn test_nice_path_buf_tmp_windows() {
587        let tmp = std::env::temp_dir();
588        let tmp = tmp.join("hello.world");
589
590        let path = NicePathBuf::new(&tmp);
591
592        assert_eq!(r"%TEMP%\hello.world", format!("{}", path));
593        assert_eq!(r"<%TEMP%\hello.world>", format!("{:?}", path));
594
595        let path = NicePathBuf::new(
596            &std::env::temp_dir()
597                .canonicalize()
598                .unwrap()
599                .join("hello.world"),
600        );
601
602        assert_eq!(r"%TEMP%\hello.world", format!("{}", path));
603        assert_eq!(r"<%TEMP%\hello.world>", format!("{:?}", path));
604
605        let path = NicePathBuf::new(r#"C:\directory"#);
606
607        assert_eq!(r"C:\directory", format!("{}", path));
608        assert_eq!(r"<C:\directory>", format!("{:?}", path));
609    }
610
611    #[test]
612    fn test_shell_split() {
613        assert_eq!(format!("{:?}", shell_split("").unwrap()), r#"[]"#);
614        assert_eq!(format!("{:?}", shell_split("a").unwrap()), r#"["a"]"#);
615        assert_eq!(
616            format!("{:?}", shell_split("a b").unwrap()),
617            r#"["a", "b"]"#
618        );
619        assert_eq!(
620            format!("{:?}", shell_split("a b c").unwrap()),
621            r#"["a", "b", "c"]"#
622        );
623        assert_eq!(
624            format!("{:?}", shell_split("a 'b' c").unwrap()),
625            r#"["a", "b", "c"]"#
626        );
627        assert_eq!(
628            format!("{:?}", shell_split("a 'b c' d").unwrap()),
629            r#"["a", "b c", "d"]"#
630        );
631        assert_eq!(
632            format!("{:?}", shell_split(r#"a "b" c"#).unwrap()),
633            r#"["a", "b", "c"]"#
634        );
635        assert_eq!(
636            format!("{:?}", shell_split(r#"a "b c" d"#).unwrap()),
637            r#"["a", "b c", "d"]"#
638        );
639        assert_eq!(
640            format!("{:?}", shell_split(r#"a "b\"c" d"#).unwrap()),
641            r#"["a", "b\"c", "d"]"#
642        );
643        assert_eq!(
644            format!("{:?}", shell_split(r#"a "b\'c" d"#).unwrap()),
645            r#"["a", "b\\'c", "d"]"#
646        );
647        assert_eq!(
648            format!("{:?}", shell_split(r#"a "b\nc" d"#).unwrap()),
649            r#"["a", "b\nc", "d"]"#
650        );
651        assert_eq!(
652            format!("{:?}", shell_split(r#"a "a\\b" d"#).unwrap()),
653            r#"["a", "a\\\\b", "d"]"#
654        );
655        assert_eq!(
656            format!("{:?}", shell_split(r#"a 'a\\b' d"#).unwrap()),
657            r#"["a", "a\\\\b", "d"]"#
658        );
659    }
660
661    #[test]
662    fn test_shell_split_errors() {
663        assert_eq!(
664            shell_split("a 'b").unwrap_err(),
665            ShellParseError::UnmatchedQuote('\'')
666        );
667        assert_eq!(
668            shell_split("a \"b c").unwrap_err(),
669            ShellParseError::UnmatchedQuote('"')
670        );
671        assert_eq!(
672            shell_split("a '").unwrap_err(),
673            ShellParseError::UnmatchedQuote('\'')
674        );
675    }
676}