aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/mozart/tests/common/mod.rs
blob: bec467794c1e679437c55746944346b6b92507c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use std::fs;
use std::path::{Path, PathBuf};
use tempfile::TempDir;

/// Returns a `Command` configured to run the `mozart` binary.
pub fn mozart_cmd() -> assert_cmd::Command {
    assert_cmd::cargo::cargo_bin_cmd!("mozart")
}

/// Returns the absolute path to `tests/fixtures/<name>`.
pub fn fixture_dir(name: &str) -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("fixtures")
        .join(name)
}

/// A temporary project directory that is cleaned up when dropped.
pub struct TestProject {
    #[allow(dead_code)]
    pub dir: TempDir,
}

impl TestProject {
    /// Returns the path to the temp directory root.
    #[allow(dead_code)]
    pub fn path(&self) -> &Path {
        self.dir.path()
    }
}

/// Create a temporary project with just a `composer.json`.
#[allow(dead_code)]
pub fn setup_temp_project(composer_json: &str) -> TestProject {
    let dir = TempDir::new().expect("Failed to create temp dir");
    fs::write(dir.path().join("composer.json"), composer_json)
        .expect("Failed to write composer.json");
    TestProject { dir }
}

/// Create a temporary project with both `composer.json` and `composer.lock`.
#[allow(dead_code)]
pub fn setup_temp_project_with_lock(composer_json: &str, composer_lock: &str) -> TestProject {
    let dir = TempDir::new().expect("Failed to create temp dir");
    fs::write(dir.path().join("composer.json"), composer_json)
        .expect("Failed to write composer.json");
    fs::write(dir.path().join("composer.lock"), composer_lock)
        .expect("Failed to write composer.lock");
    TestProject { dir }
}

/// Copy an entire fixture directory to a new temp directory.
#[allow(dead_code)]
pub fn copy_fixture_to_temp(fixture_name: &str) -> TestProject {
    let src = fixture_dir(fixture_name);
    let dir = TempDir::new().expect("Failed to create temp dir");
    copy_dir_recursive(&src, dir.path()).expect("Failed to copy fixture");
    TestProject { dir }
}

fn copy_dir_recursive(src: &Path, dst: &Path) -> anyhow::Result<()> {
    for entry in fs::read_dir(src)? {
        let entry = entry?;
        let file_type = entry.file_type()?;
        let dst_path = dst.join(entry.file_name());
        if file_type.is_dir() {
            fs::create_dir_all(&dst_path)?;
            copy_dir_recursive(&entry.path(), &dst_path)?;
        } else {
            fs::copy(entry.path(), dst_path)?;
        }
    }
    Ok(())
}