Skip to main content

kawauso_process/invocation/
working_directory.rs

1//! The directory in which an external command runs
2
3use typed_fields::path;
4
5path!(
6    /// The directory in which an external command runs
7    ///
8    /// Some commands work on the directory that they run in. A build tool
9    /// reads the manifest of the project in that directory, and a formatter
10    /// reads the configuration file that it finds there. Such a command
11    /// carries the directory that it needs.
12    ///
13    /// A command without a working directory runs where the process runs.
14    /// Most commands need no directory of their own, which is why an
15    /// invocation does not require one.
16    WorkingDirectory
17);
18
19/// Creates a working directory from a path that the application built at
20/// runtime
21impl From<std::path::PathBuf> for WorkingDirectory {
22    fn from(path: std::path::PathBuf) -> Self {
23        Self::new(path)
24    }
25}
26
27#[cfg(test)]
28mod tests {
29    // An assertion in a test panics by design. A `# Panics` section on every
30    // test would repeat that and give the reader no information.
31    #![allow(clippy::missing_panics_doc)]
32
33    use super::*;
34
35    // An application that runs a command in the project that it found holds
36    // a `PathBuf`, and the working directory takes it without a borrow.
37    #[test]
38    fn from_path_buf_keeps_the_path() {
39        let directory = WorkingDirectory::from(std::path::PathBuf::from("crates/example"));
40
41        assert_eq!(directory.get(), std::path::Path::new("crates/example"));
42    }
43}