• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kaspar030 / laze / 13764247969

10 Mar 2025 12:08PM UTC coverage: 82.161% (-0.1%) from 82.274%
13764247969

push

github

web-flow
feat(cli): make laze print verbose task commands (instead of `shell -x`) (#670)

2 of 9 new or added lines in 1 file covered. (22.22%)

3574 of 4350 relevant lines covered (82.16%)

104.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.4
/src/model/task.rs
1
use std::ffi::OsStr;
2
use std::path::Path;
3

4
use anyhow::{Error, Result};
5
use itertools::Itertools;
6
use thiserror::Error;
7

8
use crate::nested_env;
9
use crate::serde_bool_helpers::{default_as_false, default_as_true};
10
use crate::EXIT_ON_SIGINT;
11

12
use super::shared::VarExportSpec;
13

14
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
15
#[serde(deny_unknown_fields)]
16
pub struct Task {
17
    pub cmd: Vec<String>,
18
    pub help: Option<String>,
19
    pub required_vars: Option<Vec<String>>,
20
    pub required_modules: Option<Vec<String>>,
21
    pub export: Option<Vec<VarExportSpec>>,
22
    #[serde(default = "default_as_true")]
23
    pub build: bool,
24
    #[serde(default = "default_as_false")]
25
    pub ignore_ctrl_c: bool,
26
    pub workdir: Option<String>,
27
}
28

29
#[derive(Error, Debug, Serialize, Deserialize)]
30
pub enum TaskError {
31
    #[error("required variable `{var}` not set")]
32
    RequiredVarMissing { var: String },
33
    #[error("required module `{module}` not selected")]
34
    RequiredModuleMissing { module: String },
35
}
36

37
impl Task {
38
    pub fn build_app(&self) -> bool {
5✔
39
        self.build
5✔
40
    }
5✔
41

42
    pub fn execute(
5✔
43
        &self,
5✔
44
        start_dir: &Path,
5✔
45
        args: Option<&Vec<&str>>,
5✔
46
        verbose: u8,
5✔
47
    ) -> Result<(), Error> {
5✔
48
        for cmd in &self.cmd {
13✔
49
            use shell_words::join;
5✔
50
            use std::process::Command;
5✔
51

52
            let mut command = if cfg!(target_family = "windows") {
8✔
53
                let mut cmd = Command::new("cmd");
×
54
                cmd.arg("/C");
×
55
                cmd
×
56
            } else {
57
                let mut sh = Command::new("sh");
8✔
58
                sh.arg("-c");
8✔
59
                sh
8✔
60
            };
61

62
            let cmd = cmd.replace("$$", "$");
8✔
63

64
            if let Some(working_directory) = &self.workdir {
8✔
65
                // This includes support for absolute working directories through .join
×
66
                command.current_dir(start_dir.join(working_directory));
×
67
            } else {
8✔
68
                command.current_dir(start_dir);
8✔
69
            }
8✔
70

71
            // handle "export:" (export laze variables to task shell environment)
72
            if let Some(export) = &self.export {
8✔
73
                for entry in export {
15✔
74
                    let VarExportSpec { variable, content } = entry;
12✔
75
                    if let Some(val) = content {
12✔
76
                        command.env(variable, val);
12✔
77
                    }
12✔
78
                }
79
            }
5✔
80

81
            if let Some(args) = args {
8✔
82
                command.arg(cmd.clone() + " " + &join(args).to_owned());
8✔
83
            } else {
8✔
84
                command.arg(cmd);
×
85
            }
×
86

87
            if verbose > 0 {
8✔
NEW
88
                let command_with_args = command
×
NEW
89
                    .get_args()
×
NEW
90
                    .skip(1)
×
NEW
91
                    .map(OsStr::to_string_lossy)
×
NEW
92
                    .collect_vec();
×
NEW
93

×
NEW
94
                println!("laze: executing `{}`", command_with_args.join(" "));
×
95
            }
8✔
96

97
            if self.ignore_ctrl_c {
8✔
98
                EXIT_ON_SIGINT
×
99
                    .get()
×
100
                    .unwrap()
×
101
                    .clone()
×
102
                    .store(false, std::sync::atomic::Ordering::SeqCst);
×
103
            }
8✔
104

105
            // run command, wait for status
106
            let status = command.status().expect("executing command");
8✔
107

8✔
108
            if self.ignore_ctrl_c {
8✔
109
                EXIT_ON_SIGINT
×
110
                    .get()
×
111
                    .unwrap()
×
112
                    .clone()
×
113
                    .store(true, std::sync::atomic::Ordering::SeqCst);
×
114
            }
8✔
115

116
            if !status.success() {
8✔
117
                return Err(anyhow!("task failed"));
×
118
            }
8✔
119
        }
120
        Ok(())
5✔
121
    }
5✔
122

123
    fn _with_env(&self, env: &im::HashMap<&String, String>, do_eval: bool) -> Result<Task, Error> {
20✔
124
        let expand = |s| {
30✔
125
            if do_eval {
30✔
126
                nested_env::expand_eval(s, env, nested_env::IfMissing::Empty)
15✔
127
            } else {
128
                nested_env::expand(s, env, nested_env::IfMissing::Ignore)
15✔
129
            }
130
        };
30✔
131

132
        Ok(Task {
133
            cmd: self
20✔
134
                .cmd
20✔
135
                .iter()
20✔
136
                .map(expand)
20✔
137
                .collect::<Result<Vec<String>, _>>()?,
20✔
138
            export: if do_eval {
20✔
139
                self.expand_export(env)
10✔
140
            } else {
141
                self.export.clone()
10✔
142
            },
143
            workdir: self.workdir.as_ref().map(expand).transpose()?,
20✔
144
            ..(*self).clone()
20✔
145
        })
146
    }
20✔
147

148
    /// This is called early when loading the yaml files.
149
    /// It will not evaluate expressions, and pass-through variables that are not
150
    /// found in `env`.
151
    pub fn with_env(&self, env: &im::HashMap<&String, String>) -> Result<Task, Error> {
10✔
152
        self._with_env(env, false)
10✔
153
    }
10✔
154

155
    /// This is called to generate the final task.
156
    /// It will evaluate expressions, and variables that are not
157
    /// found in `env` will be replaced with the empty string.
158
    pub fn with_env_eval(&self, env: &im::HashMap<&String, String>) -> Result<Task, Error> {
10✔
159
        self._with_env(env, true)
10✔
160
    }
10✔
161

162
    fn expand_export(&self, env: &im::HashMap<&String, String>) -> Option<Vec<VarExportSpec>> {
10✔
163
        VarExportSpec::expand(self.export.as_ref(), env)
10✔
164
    }
10✔
165
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc