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

kaspar030 / laze / 13500486698

24 Feb 2025 02:33PM UTC coverage: 82.35%. First build
13500486698

Pull #652

github

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

10 of 11 new or added lines in 2 files covered. (90.91%)

3574 of 4340 relevant lines covered (82.35%)

104.37 hits per line

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

82.35
/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::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<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
            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✔
77
                command.arg(cmd);
×
78
            }
×
79

80
            if self.ignore_ctrl_c {
8✔
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✔
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✔
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
        Ok(Task {
20✔
108
            cmd: self
20✔
109
                .cmd
20✔
110
                .iter()
20✔
111
                .map(|cmd| {
30✔
112
                    if do_eval {
30✔
113
                        nested_env::expand_eval(cmd, env, nested_env::IfMissing::Empty)
15✔
114
                    } else {
115
                        nested_env::expand(cmd, env, nested_env::IfMissing::Ignore)
15✔
116
                    }
117
                })
30✔
118
                .collect::<Result<Vec<String>, _>>()?,
20✔
119
            export: if do_eval {
20✔
120
                self.expand_export(env)
10✔
121
            } else {
122
                self.export.clone()
10✔
123
            },
124
            working_directory: if do_eval {
20✔
125
                self.working_directory.clone().map(|wd| PathBuf::from(nested_env::expand_eval(wd.to_str().unwrap(), env, nested_env::IfMissing::Ignore).unwrap()))
10✔
126
            } else {
127
                self.working_directory.clone().map(|wd| PathBuf::from(nested_env::expand(wd.to_str().unwrap(), env, nested_env::IfMissing::Ignore).unwrap()))
10✔
128
            },
129
            ..(*self).clone()
20✔
130
        })
131
    }
20✔
132

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

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

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