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

kaspar030 / laze / 13500104900

24 Feb 2025 02:15PM UTC coverage: 82.337% (-0.1%) from 82.44%
13500104900

push

github

web-flow
refactor: Use register_conditional_shutdown (#648)

2 of 12 new or added lines in 2 files covered. (16.67%)

3566 of 4331 relevant lines covered (82.34%)

104.57 hits per line

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

81.82
/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
}
25

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

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

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

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

56
            // handle "export:" (export laze variables to task shell environment)
57
            if let Some(export) = &self.export {
8✔
58
                for entry in export {
15✔
59
                    let VarExportSpec { variable, content } = entry;
12✔
60
                    if let Some(val) = content {
12✔
61
                        command.env(variable, val);
12✔
62
                    }
12✔
63
                }
64
            }
5✔
65

66
            if let Some(args) = args {
8✔
67
                command.arg(cmd.clone() + " " + &join(args).to_owned());
8✔
68
            } else {
8✔
69
                command.arg(cmd);
×
70
            }
×
71

72
            if self.ignore_ctrl_c {
8✔
NEW
73
                EXIT_ON_SIGINT
×
NEW
74
                    .get()
×
NEW
75
                    .unwrap()
×
NEW
76
                    .clone()
×
NEW
77
                    .store(false, std::sync::atomic::Ordering::SeqCst);
×
78
            }
8✔
79

80
            // run command, wait for status
81
            let status = command.status().expect("executing command");
8✔
82

8✔
83
            if self.ignore_ctrl_c {
8✔
NEW
84
                EXIT_ON_SIGINT
×
NEW
85
                    .get()
×
NEW
86
                    .unwrap()
×
NEW
87
                    .clone()
×
NEW
88
                    .store(true, std::sync::atomic::Ordering::SeqCst);
×
89
            }
8✔
90

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

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

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

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

134
    fn expand_export(&self, env: &im::HashMap<&String, String>) -> Option<Vec<VarExportSpec>> {
10✔
135
        VarExportSpec::expand(self.export.as_ref(), env)
10✔
136
    }
10✔
137
}
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