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
#[macro_use]
extern crate failure;

use std::path::PathBuf;
use std::process::Command;

use failure::Error;

pub struct Composer {
    path: PathBuf,
}

impl Composer {
    pub fn new<P>(path: P) -> Self
    where
        P: Into<PathBuf>,
    {
        Composer {
            path: path.into().join("composer.json"),
        }
    }

    pub fn is_available(&self) -> bool {
        self.path.exists()
    }

    fn ensure_available(&self) -> Result<(), Error> {
        if !self.is_available() {
            format_err!("no composer executable found");
        }
        Ok(())
    }

    pub fn install(&self) -> Result<(), Error> {
        self.ensure_available()?;

        Command::new("composer").arg("install").output()?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_is_not_available_in_the_root_dir() {
        let composer = Composer::new(".");

        assert!(!composer.is_available());
    }

    #[test]
    fn it_is_available_in_the_examples_dir() {
        let composer = Composer::new("examples");

        assert!(composer.is_available());
    }

    #[test]
    fn it_installs_deps() {
        let composer = Composer::new("examples");

        composer.install().unwrap();
    }
}