foundry_compilers_core/utils/
mod.rs

1//! Utility functions
2
3use crate::error::{SolcError, SolcIoError};
4use alloy_primitives::{hex, keccak256};
5use cfg_if::cfg_if;
6use semver::{Version, VersionReq};
7use serde::{de::DeserializeOwned, Serialize};
8use std::{
9    fs,
10    io::Write,
11    ops::Range,
12    path::{Component, Path, PathBuf},
13    sync::LazyLock as Lazy,
14};
15
16#[cfg(feature = "regex")]
17mod re;
18#[cfg(feature = "regex")]
19pub use re::*;
20
21#[cfg(feature = "walkdir")]
22mod wd;
23#[cfg(feature = "walkdir")]
24pub use wd::*;
25
26/// Extensions acceptable by solc compiler.
27pub const SOLC_EXTENSIONS: &[&str] = &["sol", "yul"];
28
29/// Support for configuring the EVM version
30/// <https://blog.soliditylang.org/2018/03/08/solidity-0.4.21-release-announcement/>
31pub const BYZANTIUM_SOLC: Version = Version::new(0, 4, 21);
32
33/// Bug fix for configuring the EVM version with Constantinople
34/// <https://blog.soliditylang.org/2018/03/08/solidity-0.4.21-release-announcement/>
35pub const CONSTANTINOPLE_SOLC: Version = Version::new(0, 4, 22);
36
37/// Petersburg support
38/// <https://blog.soliditylang.org/2019/03/05/solidity-0.5.5-release-announcement/>
39pub const PETERSBURG_SOLC: Version = Version::new(0, 5, 5);
40
41/// Istanbul support
42/// <https://blog.soliditylang.org/2019/12/09/solidity-0.5.14-release-announcement/>
43pub const ISTANBUL_SOLC: Version = Version::new(0, 5, 14);
44
45/// Berlin support
46/// <https://blog.soliditylang.org/2021/06/10/solidity-0.8.5-release-announcement/>
47pub const BERLIN_SOLC: Version = Version::new(0, 8, 5);
48
49/// London support
50/// <https://blog.soliditylang.org/2021/08/11/solidity-0.8.7-release-announcement/>
51pub const LONDON_SOLC: Version = Version::new(0, 8, 7);
52
53/// Paris support
54/// <https://blog.soliditylang.org/2023/02/01/solidity-0.8.18-release-announcement/>
55pub const PARIS_SOLC: Version = Version::new(0, 8, 18);
56
57/// Shanghai support
58/// <https://blog.soliditylang.org/2023/05/10/solidity-0.8.20-release-announcement/>
59pub const SHANGHAI_SOLC: Version = Version::new(0, 8, 20);
60
61/// Cancun support
62/// <https://soliditylang.org/blog/2024/01/26/solidity-0.8.24-release-announcement/>
63pub const CANCUN_SOLC: Version = Version::new(0, 8, 24);
64
65/// Prague support
66/// <https://soliditylang.org/blog/2024/09/04/solidity-0.8.27-release-announcement>
67pub const PRAGUE_SOLC: Version = Version::new(0, 8, 27);
68
69// `--base-path` was introduced in 0.6.9 <https://github.com/ethereum/solidity/releases/tag/v0.6.9>
70pub static SUPPORTS_BASE_PATH: Lazy<VersionReq> =
71    Lazy::new(|| VersionReq::parse(">=0.6.9").unwrap());
72
73// `--include-path` was introduced in 0.8.8 <https://github.com/ethereum/solidity/releases/tag/v0.8.8>
74pub static SUPPORTS_INCLUDE_PATH: Lazy<VersionReq> =
75    Lazy::new(|| VersionReq::parse(">=0.8.8").unwrap());
76
77/// Move a range by a specified offset
78pub fn range_by_offset(range: &Range<usize>, offset: isize) -> Range<usize> {
79    Range {
80        start: offset.saturating_add(range.start as isize) as usize,
81        end: offset.saturating_add(range.end as isize) as usize,
82    }
83}
84
85/// Returns the source name for the given source path, the ancestors of the root path.
86///
87/// `/Users/project/sources/contract.sol` -> `sources/contracts.sol`
88pub fn source_name<'a>(source: &'a Path, root: &Path) -> &'a Path {
89    strip_prefix(source, root)
90}
91
92/// Strips `root` from `source` and returns the relative path.
93pub fn strip_prefix<'a>(source: &'a Path, root: &Path) -> &'a Path {
94    source.strip_prefix(root).unwrap_or(source)
95}
96
97/// Strips `root` from `source` and returns the relative path.
98pub fn strip_prefix_owned(source: PathBuf, root: &Path) -> PathBuf {
99    source.strip_prefix(root).map(Path::to_path_buf).unwrap_or(source)
100}
101
102/// Attempts to determine if the given source is a local, relative import.
103pub fn is_local_source_name(libs: &[impl AsRef<Path>], source: impl AsRef<Path>) -> bool {
104    resolve_library(libs, source.as_ref()).is_none()
105}
106
107/// Canonicalize the path, platform-agnostic.
108///
109/// On windows this will ensure the path only consists of `/` separators.
110pub fn canonicalize(path: impl AsRef<Path>) -> Result<PathBuf, SolcIoError> {
111    let path = path.as_ref();
112    let res = dunce::canonicalize(path);
113    #[cfg(windows)]
114    let res = res.map(|p| {
115        use path_slash::PathBufExt;
116        PathBuf::from(p.to_slash_lossy().as_ref())
117    });
118    res.map_err(|err| SolcIoError::new(err, path))
119}
120
121/// Returns a normalized Solidity file path for the given import path based on the specified
122/// directory.
123///
124/// This function resolves `./` and `../`, but, unlike [`canonicalize`], it does not resolve
125/// symbolic links.
126///
127/// The function returns an error if the normalized path does not exist in the file system.
128///
129/// See also: <https://docs.soliditylang.org/en/v0.8.23/path-resolution.html>
130pub fn normalize_solidity_import_path(
131    directory: &Path,
132    import_path: &Path,
133) -> Result<PathBuf, SolcIoError> {
134    let original = directory.join(import_path);
135    let cleaned = clean_solidity_path(&original);
136
137    // this is to align the behavior with `canonicalize`
138    let normalized = dunce::simplified(&cleaned);
139    #[cfg(windows)]
140    let normalized = {
141        use path_slash::PathExt;
142        PathBuf::from(normalized.to_slash_lossy().as_ref())
143    };
144    #[cfg(not(windows))]
145    let normalized = PathBuf::from(normalized);
146
147    // checks if the path exists without reading its content and obtains an io error if it doesn't.
148    let _ = normalized.metadata().map_err(|err| SolcIoError::new(err, original))?;
149    Ok(normalized)
150}
151
152// This function lexically cleans the given path.
153//
154// It performs the following transformations for the path:
155//
156// * Resolves references (current directories (`.`) and parent (`..`) directories).
157// * Reduces repeated separators to a single separator (e.g., from `//` to `/`).
158//
159// This transformation is lexical, not involving the file system, which means it does not account
160// for symlinks. This approach has a caveat. For example, consider a filesystem-accessible path
161// `a/b/../c.sol` passed to this function. It returns `a/c.sol`. However, if `b` is a symlink,
162// `a/c.sol` might not be accessible in the filesystem in some environments. Despite this, it's
163// unlikely that this will pose a problem for our intended use.
164//
165// # How it works
166//
167// The function splits the given path into components, where each component roughly corresponds to a
168// string between separators. It then iterates over these components (starting from the leftmost
169// part of the path) to reconstruct the path. The following steps are applied to each component:
170//
171// * If the component is a current directory, it's removed.
172// * If the component is a parent directory, the following rules are applied:
173//     * If the preceding component is a normal, then both the preceding normal component and the
174//       parent directory component are removed. (Examples of normal components include `a` and `b`
175//       in `a/b`.)
176//     * Otherwise (if there is no preceding component, or if the preceding component is a parent,
177//       root, or prefix), it remains untouched.
178// * Otherwise, the component remains untouched.
179//
180// Finally, the processed components are reassembled into a path.
181fn clean_solidity_path(original_path: &Path) -> PathBuf {
182    let mut new_path = Vec::new();
183
184    for component in original_path.components() {
185        match component {
186            Component::Prefix(..) | Component::RootDir | Component::Normal(..) => {
187                new_path.push(component);
188            }
189            Component::CurDir => {}
190            Component::ParentDir => {
191                if let Some(Component::Normal(..)) = new_path.last() {
192                    new_path.pop();
193                } else {
194                    new_path.push(component);
195                }
196            }
197        }
198    }
199
200    new_path.iter().collect()
201}
202
203/// Returns the same path config but with canonicalized paths.
204///
205/// This will take care of potential symbolic linked directories.
206/// For example, the tempdir library is creating directories hosted under `/var/`, which in OS X
207/// is a symbolic link to `/private/var/`. So if when we try to resolve imports and a path is
208/// rooted in a symbolic directory we might end up with different paths for the same file, like
209/// `private/var/.../Dapp.sol` and `/var/.../Dapp.sol`
210///
211/// This canonicalizes all the paths but does not treat non existing dirs as an error
212pub fn canonicalized(path: impl Into<PathBuf>) -> PathBuf {
213    let path = path.into();
214    canonicalize(&path).unwrap_or(path)
215}
216
217/// Returns the path to the library if the source path is in fact determined to be a library path,
218/// and it exists.
219/// Note: this does not handle relative imports or remappings.
220pub fn resolve_library(libs: &[impl AsRef<Path>], source: impl AsRef<Path>) -> Option<PathBuf> {
221    let source = source.as_ref();
222    let comp = source.components().next()?;
223    match comp {
224        Component::Normal(first_dir) => {
225            // attempt to verify that the root component of this source exists under a library
226            // folder
227            for lib in libs {
228                let lib = lib.as_ref();
229                let contract = lib.join(source);
230                if contract.exists() {
231                    // contract exists in <lib>/<source>
232                    return Some(contract);
233                }
234                // check for <lib>/<first_dir>/src/name.sol
235                let contract = lib
236                    .join(first_dir)
237                    .join("src")
238                    .join(source.strip_prefix(first_dir).expect("is first component"));
239                if contract.exists() {
240                    return Some(contract);
241                }
242            }
243            None
244        }
245        Component::RootDir => Some(source.into()),
246        _ => None,
247    }
248}
249
250/// Tries to find an absolute import like `src/interfaces/IConfig.sol` in `cwd`, moving up the path
251/// until the `root` is reached.
252///
253/// If an existing file under `root` is found, this returns the path up to the `import` path and the
254/// normalized `import` path itself:
255///
256/// For example for following layout:
257///
258/// ```text
259/// <root>/mydependency/
260/// ├── src (`cwd`)
261/// │   ├── interfaces
262/// │   │   ├── IConfig.sol
263/// ```
264/// and `import` as `src/interfaces/IConfig.sol` and `cwd` as `src` this will return
265/// (`<root>/mydependency/`, `<root>/mydependency/src/interfaces/IConfig.sol`)
266pub fn resolve_absolute_library(
267    root: &Path,
268    cwd: &Path,
269    import: &Path,
270) -> Option<(PathBuf, PathBuf)> {
271    let mut parent = cwd.parent()?;
272    while parent != root {
273        if let Ok(import) = normalize_solidity_import_path(parent, import) {
274            return Some((parent.to_path_buf(), import));
275        }
276        parent = parent.parent()?;
277    }
278    None
279}
280
281/// Returns the 36 char (deprecated) fully qualified name placeholder
282///
283/// If the name is longer than 36 char, then the name gets truncated,
284/// If the name is shorter than 36 char, then the name is filled with trailing `_`
285pub fn library_fully_qualified_placeholder(name: &str) -> String {
286    name.chars().chain(std::iter::repeat('_')).take(36).collect()
287}
288
289/// Returns the library hash placeholder as `$hex(library_hash(name))$`
290pub fn library_hash_placeholder(name: impl AsRef<[u8]>) -> String {
291    let mut s = String::with_capacity(34 + 2);
292    s.push('$');
293    s.push_str(hex::Buffer::<17, false>::new().format(&library_hash(name)));
294    s.push('$');
295    s
296}
297
298/// Returns the library placeholder for the given name
299/// The placeholder is a 34 character prefix of the hex encoding of the keccak256 hash of the fully
300/// qualified library name.
301///
302/// See also <https://docs.soliditylang.org/en/develop/using-the-compiler.html#library-linking>
303pub fn library_hash(name: impl AsRef<[u8]>) -> [u8; 17] {
304    let hash = keccak256(name);
305    hash[..17].try_into().unwrap()
306}
307
308/// Find the common ancestor, if any, between the given paths
309///
310/// # Examples
311///
312/// ```
313/// use foundry_compilers_core::utils::common_ancestor_all;
314/// use std::path::{Path, PathBuf};
315///
316/// let baz = Path::new("/foo/bar/baz");
317/// let bar = Path::new("/foo/bar/bar");
318/// let foo = Path::new("/foo/bar/foo");
319/// let common = common_ancestor_all([baz, bar, foo]).unwrap();
320/// assert_eq!(common, Path::new("/foo/bar").to_path_buf());
321/// ```
322pub fn common_ancestor_all<I, P>(paths: I) -> Option<PathBuf>
323where
324    I: IntoIterator<Item = P>,
325    P: AsRef<Path>,
326{
327    let mut iter = paths.into_iter();
328    let mut ret = iter.next()?.as_ref().to_path_buf();
329    for path in iter {
330        if let Some(r) = common_ancestor(&ret, path.as_ref()) {
331            ret = r;
332        } else {
333            return None;
334        }
335    }
336    Some(ret)
337}
338
339/// Finds the common ancestor of both paths
340///
341/// # Examples
342///
343/// ```
344/// use foundry_compilers_core::utils::common_ancestor;
345/// use std::path::{Path, PathBuf};
346///
347/// let foo = Path::new("/foo/bar/foo");
348/// let bar = Path::new("/foo/bar/bar");
349/// let ancestor = common_ancestor(foo, bar).unwrap();
350/// assert_eq!(ancestor, Path::new("/foo/bar"));
351/// ```
352pub fn common_ancestor(a: &Path, b: &Path) -> Option<PathBuf> {
353    let a = a.components();
354    let b = b.components();
355    let mut ret = PathBuf::new();
356    let mut found = false;
357    for (c1, c2) in a.zip(b) {
358        if c1 == c2 {
359            ret.push(c1);
360            found = true;
361        } else {
362            break;
363        }
364    }
365    if found {
366        Some(ret)
367    } else {
368        None
369    }
370}
371
372/// Returns the right subpath in a dir
373///
374/// Returns `<root>/<fave>` if it exists or `<root>/<alt>` does not exist,
375/// Returns `<root>/<alt>` if it exists and `<root>/<fave>` does not exist.
376pub fn find_fave_or_alt_path(root: &Path, fave: &str, alt: &str) -> PathBuf {
377    let p = root.join(fave);
378    if !p.exists() {
379        let alt = root.join(alt);
380        if alt.exists() {
381            return alt;
382        }
383    }
384    p
385}
386
387cfg_if! {
388    if #[cfg(any(feature = "async", feature = "svm-solc"))] {
389        use tokio::runtime::{Handle, Runtime};
390
391        #[derive(Debug)]
392        pub enum RuntimeOrHandle {
393            Runtime(Runtime),
394            Handle(Handle),
395        }
396
397        impl Default for RuntimeOrHandle {
398            fn default() -> Self {
399                Self::new()
400            }
401        }
402
403        impl RuntimeOrHandle {
404            pub fn new() -> Self {
405                match Handle::try_current() {
406                    Ok(handle) => Self::Handle(handle),
407                    Err(_) => Self::Runtime(Runtime::new().expect("Failed to start runtime")),
408                }
409            }
410
411            pub fn block_on<F: std::future::Future>(&self, f: F) -> F::Output {
412                match &self {
413                    Self::Runtime(runtime) => runtime.block_on(f),
414                    Self::Handle(handle) => tokio::task::block_in_place(|| handle.block_on(f)),
415                }
416            }
417        }
418    }
419}
420
421/// Creates a new named tempdir.
422#[cfg(any(test, feature = "project-util", feature = "test-utils"))]
423pub fn tempdir(name: &str) -> Result<tempfile::TempDir, SolcIoError> {
424    tempfile::Builder::new().prefix(name).tempdir().map_err(|err| SolcIoError::new(err, name))
425}
426
427/// Reads the json file and deserialize it into the provided type.
428pub fn read_json_file<T: DeserializeOwned>(path: &Path) -> Result<T, SolcError> {
429    // See: https://github.com/serde-rs/json/issues/160
430    let s = fs::read_to_string(path).map_err(|err| SolcError::io(err, path))?;
431    serde_json::from_str(&s).map_err(Into::into)
432}
433
434/// Writes serializes the provided value to JSON and writes it to a file.
435pub fn write_json_file<T: Serialize>(
436    value: &T,
437    path: &Path,
438    capacity: usize,
439) -> Result<(), SolcError> {
440    let file = fs::File::create(path).map_err(|err| SolcError::io(err, path))?;
441    let mut writer = std::io::BufWriter::with_capacity(capacity, file);
442    serde_json::to_writer(&mut writer, value)?;
443    writer.flush().map_err(|e| SolcError::io(e, path))
444}
445
446/// Creates the parent directory of the `file` and all its ancestors if it does not exist.
447///
448/// See [`fs::create_dir_all()`].
449pub fn create_parent_dir_all(file: &Path) -> Result<(), SolcError> {
450    if let Some(parent) = file.parent() {
451        fs::create_dir_all(parent).map_err(|err| {
452            SolcError::msg(format!(
453                "Failed to create artifact parent folder \"{}\": {}",
454                parent.display(),
455                err
456            ))
457        })?;
458    }
459    Ok(())
460}
461
462#[cfg(any(test, feature = "test-utils"))]
463// <https://doc.rust-lang.org/rust-by-example/std_misc/fs.html>
464pub fn touch(path: &std::path::Path) -> std::io::Result<()> {
465    match std::fs::OpenOptions::new().create(true).write(true).truncate(false).open(path) {
466        Ok(_) => Ok(()),
467        Err(e) => Err(e),
468    }
469}
470
471#[cfg(any(test, feature = "test-utils"))]
472pub fn mkdir_or_touch(tmp: &std::path::Path, paths: &[&str]) {
473    for path in paths {
474        if let Some(parent) = Path::new(path).parent() {
475            std::fs::create_dir_all(tmp.join(parent)).unwrap();
476        }
477        if path.ends_with(".sol") {
478            let path = tmp.join(path);
479            touch(&path).unwrap();
480        } else {
481            let path: PathBuf = tmp.join(path);
482            std::fs::create_dir_all(path).unwrap();
483        }
484    }
485}
486
487#[cfg(test)]
488mod tests {
489    pub use super::*;
490    pub use std::fs::{create_dir_all, File};
491
492    #[test]
493    fn can_create_parent_dirs_with_ext() {
494        let tmp_dir = tempdir("out").unwrap();
495        let path = tmp_dir.path().join("IsolationModeMagic.sol/IsolationModeMagic.json");
496        create_parent_dir_all(&path).unwrap();
497        assert!(path.parent().unwrap().is_dir());
498    }
499
500    #[test]
501    fn can_create_parent_dirs_versioned() {
502        let tmp_dir = tempdir("out").unwrap();
503        let path = tmp_dir.path().join("IVersioned.sol/IVersioned.0.8.16.json");
504        create_parent_dir_all(&path).unwrap();
505        assert!(path.parent().unwrap().is_dir());
506        let path = tmp_dir.path().join("IVersioned.sol/IVersioned.json");
507        create_parent_dir_all(&path).unwrap();
508        assert!(path.parent().unwrap().is_dir());
509    }
510
511    #[test]
512    fn can_determine_local_paths() {
513        assert!(is_local_source_name(&[""], "./local/contract.sol"));
514        assert!(is_local_source_name(&[""], "../local/contract.sol"));
515        assert!(!is_local_source_name(&[""], "/ds-test/test.sol"));
516
517        let tmp_dir = tempdir("contracts").unwrap();
518        let dir = tmp_dir.path().join("ds-test");
519        create_dir_all(&dir).unwrap();
520        File::create(dir.join("test.sol")).unwrap();
521
522        assert!(!is_local_source_name(&[tmp_dir.path()], "ds-test/test.sol"));
523    }
524
525    #[test]
526    fn can_normalize_solidity_import_path() {
527        let dir = tempfile::tempdir().unwrap();
528        let dir_path = dir.path();
529
530        // File structure:
531        //
532        // `dir_path`
533        // └── src (`cwd`)
534        //     ├── Token.sol
535        //     └── common
536        //         └── Burnable.sol
537
538        fs::create_dir_all(dir_path.join("src/common")).unwrap();
539        fs::write(dir_path.join("src/Token.sol"), "").unwrap();
540        fs::write(dir_path.join("src/common/Burnable.sol"), "").unwrap();
541
542        // assume that the import path is specified in Token.sol
543        let cwd = dir_path.join("src");
544
545        assert_eq!(
546            normalize_solidity_import_path(&cwd, "./common/Burnable.sol".as_ref()).unwrap(),
547            dir_path.join("src/common/Burnable.sol"),
548        );
549
550        assert!(normalize_solidity_import_path(&cwd, "./common/Pausable.sol".as_ref()).is_err());
551    }
552
553    // This test is exclusive to unix because creating a symlink is a privileged action on Windows.
554    // https://doc.rust-lang.org/std/os/windows/fs/fn.symlink_dir.html#limitations
555    #[test]
556    #[cfg(unix)]
557    fn can_normalize_solidity_import_path_symlink() {
558        let dir = tempfile::tempdir().unwrap();
559        let dir_path = dir.path();
560
561        // File structure:
562        //
563        // `dir_path`
564        // ├── dependency
565        // │   └── Math.sol
566        // └── project
567        //     ├── node_modules
568        //     │   └── dependency -> symlink to actual 'dependency' directory
569        //     └── src (`cwd`)
570        //         └── Token.sol
571
572        fs::create_dir_all(dir_path.join("project/src")).unwrap();
573        fs::write(dir_path.join("project/src/Token.sol"), "").unwrap();
574        fs::create_dir(dir_path.join("project/node_modules")).unwrap();
575
576        fs::create_dir(dir_path.join("dependency")).unwrap();
577        fs::write(dir_path.join("dependency/Math.sol"), "").unwrap();
578
579        std::os::unix::fs::symlink(
580            dir_path.join("dependency"),
581            dir_path.join("project/node_modules/dependency"),
582        )
583        .unwrap();
584
585        // assume that the import path is specified in Token.sol
586        let cwd = dir_path.join("project/src");
587
588        assert_eq!(
589            normalize_solidity_import_path(&cwd, "../node_modules/dependency/Math.sol".as_ref())
590                .unwrap(),
591            dir_path.join("project/node_modules/dependency/Math.sol"),
592        );
593    }
594
595    #[test]
596    fn can_clean_solidity_path() {
597        let clean_solidity_path = |s: &str| clean_solidity_path(s.as_ref());
598        assert_eq!(clean_solidity_path("a"), PathBuf::from("a"));
599        assert_eq!(clean_solidity_path("./a"), PathBuf::from("a"));
600        assert_eq!(clean_solidity_path("../a"), PathBuf::from("../a"));
601        assert_eq!(clean_solidity_path("/a/"), PathBuf::from("/a"));
602        assert_eq!(clean_solidity_path("//a"), PathBuf::from("/a"));
603        assert_eq!(clean_solidity_path("a/b"), PathBuf::from("a/b"));
604        assert_eq!(clean_solidity_path("a//b"), PathBuf::from("a/b"));
605        assert_eq!(clean_solidity_path("/a/b"), PathBuf::from("/a/b"));
606        assert_eq!(clean_solidity_path("a/./b"), PathBuf::from("a/b"));
607        assert_eq!(clean_solidity_path("a/././b"), PathBuf::from("a/b"));
608        assert_eq!(clean_solidity_path("/a/../b"), PathBuf::from("/b"));
609        assert_eq!(clean_solidity_path("a/./../b/."), PathBuf::from("b"));
610        assert_eq!(clean_solidity_path("a/b/c"), PathBuf::from("a/b/c"));
611        assert_eq!(clean_solidity_path("a/b/../c"), PathBuf::from("a/c"));
612        assert_eq!(clean_solidity_path("a/b/../../c"), PathBuf::from("c"));
613        assert_eq!(clean_solidity_path("a/b/../../../c"), PathBuf::from("../c"));
614        assert_eq!(
615            clean_solidity_path("a/../b/../../c/./Token.sol"),
616            PathBuf::from("../c/Token.sol")
617        );
618    }
619
620    #[test]
621    fn can_find_ancestor() {
622        let a = Path::new("/foo/bar/bar/test.txt");
623        let b = Path::new("/foo/bar/foo/example/constract.sol");
624        let expected = Path::new("/foo/bar");
625        assert_eq!(common_ancestor(a, b).unwrap(), expected.to_path_buf())
626    }
627
628    #[test]
629    fn no_common_ancestor_path() {
630        let a = Path::new("/foo/bar");
631        let b = Path::new("./bar/foo");
632        assert!(common_ancestor(a, b).is_none());
633    }
634
635    #[test]
636    fn can_find_all_ancestor() {
637        let a = Path::new("/foo/bar/foo/example.txt");
638        let b = Path::new("/foo/bar/foo/test.txt");
639        let c = Path::new("/foo/bar/bar/foo/bar");
640        let expected = Path::new("/foo/bar");
641        let paths = vec![a, b, c];
642        assert_eq!(common_ancestor_all(paths).unwrap(), expected.to_path_buf())
643    }
644}