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

kaspar030 / laze / 13443171818

20 Feb 2025 07:29PM UTC coverage: 82.39%. First build
13443171818

Pull #652

github

web-flow
Merge 3b20d98d0 into b120a3a87
Pull Request #652: feat(task): Extend with working_directory option

9 of 11 new or added lines in 2 files covered. (81.82%)

3579 of 4344 relevant lines covered (82.39%)

104.3 hits per line

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

89.74
/src/model/task.rs
1
use std::path::{Path, PathBuf};
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::IGNORE_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<PathBuf>,
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
            let mut start_dir = start_dir.to_path_buf();
8✔
57

58
            if let Some(working_directory) = &self.working_directory {
8✔
NEW
59
                let working_directory = join(std::iter::once(working_directory.to_str().unwrap()));
×
NEW
60
                start_dir = start_dir.join(working_directory);
×
61
            }
8✔
62
            command.current_dir(&start_dir);
8✔
63
            command.arg("-c");
8✔
64

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

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

81
            if self.ignore_ctrl_c {
8✔
82
                IGNORE_SIGINT.store(true, std::sync::atomic::Ordering::SeqCst);
×
83
            }
8✔
84

85
            // run command, wait for status
86
            let status = command.status().expect("executing command");
8✔
87

8✔
88
            if self.ignore_ctrl_c {
8✔
89
                IGNORE_SIGINT.store(false, std::sync::atomic::Ordering::SeqCst);
×
90
            }
8✔
91

92
            if !status.success() {
8✔
93
                return Err(anyhow!("task failed"));
×
94
            }
8✔
95
        }
96
        Ok(())
5✔
97
    }
5✔
98

99
    fn _with_env(&self, env: &im::HashMap<&String, String>, do_eval: bool) -> Result<Task, Error> {
20✔
100
        Ok(Task {
20✔
101
            cmd: self
20✔
102
                .cmd
20✔
103
                .iter()
20✔
104
                .map(|cmd| {
30✔
105
                    if do_eval {
30✔
106
                        nested_env::expand_eval(cmd, env, nested_env::IfMissing::Empty)
15✔
107
                    } else {
108
                        nested_env::expand(cmd, env, nested_env::IfMissing::Ignore)
15✔
109
                    }
110
                })
30✔
111
                .collect::<Result<Vec<String>, _>>()?,
20✔
112
            export: if do_eval {
20✔
113
                self.expand_export(env)
10✔
114
            } else {
115
                self.export.clone()
10✔
116
            },
117
            working_directory: if do_eval {
20✔
118
                self.working_directory.clone().map(|wd| PathBuf::from(nested_env::expand_eval(wd.to_str().unwrap(), env, nested_env::IfMissing::Ignore).unwrap()))
10✔
119
            } else {
120
                self.working_directory.clone().map(|wd| PathBuf::from(nested_env::expand(wd.to_str().unwrap(), env, nested_env::IfMissing::Ignore).unwrap()))
10✔
121
            },
122
            ..(*self).clone()
20✔
123
        })
124
    }
20✔
125

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

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

140
    fn expand_export(&self, env: &im::HashMap<&String, String>) -> Option<Vec<VarExportSpec>> {
10✔
141
        VarExportSpec::expand(self.export.as_ref(), env)
10✔
142
    }
10✔
143
}
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