mk_lib/schema/command/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use super::TaskContext;
use serde::Deserialize;

mod container_run;
mod local_run;
mod task_run;

#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum CommandRunner {
  ContainerRun(container_run::ContainerRun),
  LocalRun(local_run::LocalRun),
  TaskRun(task_run::TaskRun),
}

impl CommandRunner {
  pub fn execute(&self, context: &mut TaskContext) -> anyhow::Result<()> {
    match self {
      CommandRunner::ContainerRun(container_run) => container_run.execute(context),
      CommandRunner::LocalRun(local_run) => local_run.execute(context),
      CommandRunner::TaskRun(task_run) => task_run.execute(context),
    }
  }
}

#[cfg(test)]
mod test {
  use super::*;

  #[test]
  fn test_command_1() -> anyhow::Result<()> {
    {
      let yaml = "
        command: 'echo \"Hello, World!\"'
        ignore_errors: false
        verbose: false
      ";
      let command = serde_yaml::from_str::<CommandRunner>(yaml)?;

      if let CommandRunner::LocalRun(local_run) = command {
        assert_eq!(local_run.command, "echo \"Hello, World!\"");
        assert_eq!(local_run.shell, "sh");
        assert_eq!(local_run.work_dir, None);
        assert!(!local_run.ignore_errors);
        assert!(!local_run.verbose);
      } else {
        panic!("Expected CommandRunner::LocalRun");
      }

      Ok(())
    }
  }

  #[test]
  fn test_command_2() -> anyhow::Result<()> {
    {
      let yaml = "
        command: 'echo \"Hello, World!\"'
      ";
      let command = serde_yaml::from_str::<CommandRunner>(yaml)?;

      if let CommandRunner::LocalRun(local_run) = command {
        assert_eq!(local_run.command, "echo \"Hello, World!\"");
        assert_eq!(local_run.shell, "sh");
        assert_eq!(local_run.work_dir, None);
        assert!(!local_run.ignore_errors);
        assert!(local_run.verbose);
      } else {
        panic!("Expected CommandRunner::LocalRun");
      }

      Ok(())
    }
  }

  #[test]
  fn test_command_3() -> anyhow::Result<()> {
    {
      let yaml = "
        command: 'echo \"Hello, World!\"'
        ignore_errors: true
      ";
      let command = serde_yaml::from_str::<CommandRunner>(yaml)?;
      if let CommandRunner::LocalRun(local_run) = command {
        assert_eq!(local_run.command, "echo \"Hello, World!\"");
        assert_eq!(local_run.shell, "sh");
        assert_eq!(local_run.work_dir, None);
        assert!(local_run.ignore_errors);
        assert!(local_run.verbose);
      } else {
        panic!("Expected CommandRunner::LocalRun");
      }

      Ok(())
    }
  }

  #[test]
  fn test_command_4() -> anyhow::Result<()> {
    {
      let yaml = "
        command: 'echo \"Hello, World!\"'
        verbose: false
      ";
      let command = serde_yaml::from_str::<CommandRunner>(yaml)?;
      if let CommandRunner::LocalRun(local_run) = command {
        assert_eq!(local_run.command, "echo \"Hello, World!\"");
        assert_eq!(local_run.shell, "sh");
        assert_eq!(local_run.work_dir, None);
        assert!(!local_run.ignore_errors);
        assert!(!local_run.verbose);
      } else {
        panic!("Expected CommandRunner::LocalRun");
      }

      Ok(())
    }
  }

  #[test]
  fn test_command_5() -> anyhow::Result<()> {
    {
      let yaml = "
        command: 'echo \"Hello, World!\"'
        work_dir: /tmp
      ";
      let command = serde_yaml::from_str::<CommandRunner>(yaml)?;
      if let CommandRunner::LocalRun(local_run) = command {
        assert_eq!(local_run.command, "echo \"Hello, World!\"");
        assert_eq!(local_run.shell, "sh");
        assert_eq!(local_run.work_dir, Some("/tmp".into()));
        assert!(!local_run.ignore_errors);
        assert!(local_run.verbose);
      } else {
        panic!("Expected CommandRunner::LocalRun");
      }

      Ok(())
    }
  }
}