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

kaspar030 / laze / 13500942781

24 Feb 2025 02:55PM UTC coverage: 82.342% (+0.005%) from 82.337%
13500942781

Pull #652

github

web-flow
Merge cd39b5faa into ff8556d85
Pull Request #652: feat(task): Extend with working_directory option

13 of 14 new or added lines in 2 files covered. (92.86%)

4 existing lines in 1 file now uncovered.

3572 of 4338 relevant lines covered (82.34%)

104.42 hits per line

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

81.93
/src/model/task.rs
1
use std::path::Path;
2

3
use anyhow::{Error, Result};
4
use thiserror::Error;
5

6
use crate::nested_env;
7
use crate::serde_bool_helpers::{default_as_false, default_as_true};
8
use crate::EXIT_ON_SIGINT;
9

10
use super::shared::VarExportSpec;
11

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

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

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

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

8✔
51
            let cmd = cmd.replace("$$", "$");
8✔
52
            if verbose > 0 {
8✔
53
                command.arg("-x");
×
54
            }
8✔
55

56
            if let Some(working_directory) = &self.working_directory {
8✔
NEW
57
                command.current_dir(start_dir.join(working_directory));
×
58
            } else {
8✔
59
                command.current_dir(start_dir);
8✔
60
            }
8✔
61

62
            command.arg("-c");
8✔
63

64
            // handle "export:" (export laze variables to task shell environment)
65
            if let Some(export) = &self.export {
8✔
66
                for entry in export {
15✔
67
                    let VarExportSpec { variable, content } = entry;
12✔
68
                    if let Some(val) = content {
12✔
69
                        command.env(variable, val);
12✔
70
                    }
12✔
71
                }
72
            }
5✔
73

74
            if let Some(args) = args {
8✔
75
                command.arg(cmd.clone() + " " + &join(args).to_owned());
8✔
76
            } else {
8✔
UNCOV
77
                command.arg(cmd);
×
78
            }
×
79

80
            if self.ignore_ctrl_c {
8✔
UNCOV
81
                EXIT_ON_SIGINT
×
82
                    .get()
×
83
                    .unwrap()
×
84
                    .clone()
×
85
                    .store(false, std::sync::atomic::Ordering::SeqCst);
×
86
            }
8✔
87

88
            // run command, wait for status
89
            let status = command.status().expect("executing command");
8✔
90

8✔
91
            if self.ignore_ctrl_c {
8✔
UNCOV
92
                EXIT_ON_SIGINT
×
93
                    .get()
×
94
                    .unwrap()
×
95
                    .clone()
×
96
                    .store(true, std::sync::atomic::Ordering::SeqCst);
×
97
            }
8✔
98

99
            if !status.success() {
8✔
UNCOV
100
                return Err(anyhow!("task failed"));
×
101
            }
8✔
102
        }
103
        Ok(())
5✔
104
    }
5✔
105

106
    fn _with_env(&self, env: &im::HashMap<&String, String>, do_eval: bool) -> Result<Task, Error> {
20✔
107
        let expand = |s| {
30✔
108
            if do_eval {
30✔
109
                nested_env::expand_eval(s, env, nested_env::IfMissing::Empty)
15✔
110
            } else {
111
                nested_env::expand(s, env, nested_env::IfMissing::Ignore)
15✔
112
            }
113
        };
30✔
114

115
        Ok(Task {
116
            cmd: self
20✔
117
                .cmd
20✔
118
                .iter()
20✔
119
                .map(expand)
20✔
120
                .collect::<Result<Vec<String>, _>>()?,
20✔
121
            export: if do_eval {
20✔
122
                self.expand_export(env)
10✔
123
            } else {
124
                self.export.clone()
10✔
125
            },
126
            working_directory: self.working_directory.as_ref().map(expand).transpose()?,
20✔
127
            ..(*self).clone()
20✔
128
        })
129
    }
20✔
130

131
    /// This is called early when loading the yaml files.
132
    /// It will not evaluate expressions, and pass-through variables that are not
133
    /// found in `env`.
134
    pub fn with_env(&self, env: &im::HashMap<&String, String>) -> Result<Task, Error> {
10✔
135
        self._with_env(env, false)
10✔
136
    }
10✔
137

138
    /// This is called to generate the final task.
139
    /// It will evaluate expressions, and variables that are not
140
    /// found in `env` will be replaced with the empty string.
141
    pub fn with_env_eval(&self, env: &im::HashMap<&String, String>) -> Result<Task, Error> {
10✔
142
        self._with_env(env, true)
10✔
143
    }
10✔
144

145
    fn expand_export(&self, env: &im::HashMap<&String, String>) -> Option<Vec<VarExportSpec>> {
10✔
146
        VarExportSpec::expand(self.export.as_ref(), env)
10✔
147
    }
10✔
148
}
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