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