Skip to main content

cuno_core/
program.rs

1//! Starting a program cuno built: what `cuno run` and the test runner both
2//! decide the same way (`docs/en/specs/architecture.md`, "What goes in
3//! `cuno-core`"). How its standard streams are handled is the caller's:
4//! `cuno run` leaves them connected, the test runner captures them.
5
6use std::ffi::OsString;
7use std::path::Path;
8use std::process::Command;
9
10use crate::Env;
11
12/// The command that runs `exe` with `args`: with exactly the variables of
13/// `env`, the environment cuno was given rather than the compiler's, and
14/// the project `root` as its working directory (`docs/en/specs/commands.md`,
15/// "`cuno run`"; `docs/en/specs/testing.md`, "Execution").
16pub fn command(exe: &Path, args: &[OsString], env: &Env, root: &Path) -> Command {
17    let mut command = Command::new(exe);
18    command
19        .args(args)
20        .env_clear()
21        .envs(env.vars())
22        .current_dir(root);
23    command
24}
25
26/// `path` as cuno shows a program it runs: relative to `cwd` when it lies
27/// there, as it is otherwise.
28pub fn shown<'a>(path: &'a Path, cwd: &Path) -> &'a Path {
29    path.strip_prefix(cwd).unwrap_or(path)
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35    use crate::NameCase;
36    use std::ffi::OsStr;
37
38    /// The command runs the program with its arguments in the root, with
39    /// the variables given and nothing else.
40    ///
41    /// Mutation: leave out `env_clear`, the variables, the arguments, or
42    /// the working directory, or use another; this test fails.
43    #[test]
44    fn a_program_runs_in_the_root_with_the_environment_given() {
45        let env = Env::from_pairs([("ONLY".into(), "1".into())], NameCase::Insensitive);
46        let args = [OsString::from("--exact"), OsString::from("t")];
47        let cmd = command(Path::new("C:/p/app.exe"), &args, &env, Path::new("C:/p"));
48        assert_eq!(cmd.get_program(), OsStr::new("C:/p/app.exe"));
49        assert_eq!(cmd.get_args().collect::<Vec<_>>(), ["--exact", "t"]);
50        assert_eq!(cmd.get_current_dir(), Some(Path::new("C:/p")));
51        let vars: Vec<_> = cmd.get_envs().collect();
52        assert_eq!(vars, [(OsStr::new("ONLY"), Some(OsStr::new("1")))]);
53        // `env_clear` leaves no trace in `get_envs`; a program that prints
54        // its environment shows it took effect.
55        let (shell, list): (OsString, &[&str]) = match std::env::var_os("ComSpec") {
56            Some(cmd) => (cmd, &["/C", "set"]),
57            None => ("/usr/bin/env".into(), &[]),
58        };
59        let list: Vec<OsString> = list.iter().map(OsString::from).collect();
60        let printed = command(Path::new(&shell), &list, &env, &std::env::temp_dir())
61            .output()
62            .unwrap()
63            .stdout;
64        let printed = String::from_utf8_lossy(&printed);
65        assert!(printed.contains("ONLY=1"), "{printed}");
66        assert!(!printed.contains("CARGO_PKG_NAME="), "{printed}");
67    }
68
69    /// A path in the current directory is shown relative to it, and one
70    /// elsewhere as it is.
71    ///
72    /// Mutation: show every path as it is, or strip another prefix; this
73    /// test fails.
74    #[test]
75    fn a_program_is_shown_relative_to_the_current_directory() {
76        let cwd = Path::new("C:/p");
77        assert_eq!(
78            shown(Path::new("C:/p/target/debug/app.exe"), cwd),
79            Path::new("target/debug/app.exe")
80        );
81        assert_eq!(
82            shown(Path::new("D:/other/app.exe"), cwd),
83            Path::new("D:/other/app.exe")
84        );
85    }
86}