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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
extern crate duct_sh;
#[macro_use]
extern crate failure;
#[macro_use]
extern crate structopt;
#[cfg(test)]
#[macro_use]
extern crate maplit;
#[macro_use]
extern crate serde_derive;
extern crate serde_yaml;

use std::collections::BTreeMap;
use std::fs::File;
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};

use console::Style;
use dialoguer::theme::ColorfulTheme;
use dialoguer::Confirmation;
use duct_sh::sh_dangerous;
use failure::Error;
use indicatif::{ProgressBar, ProgressStyle};

// Increment the version number every time the version changes. I can't figure out how to
// break this out into its own const, see https://github.com/rust-lang/rust/issues/52393.
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "schema_version")]
enum VersionedCheckListList {
    #[serde(rename = "2")] // increment here
    Current(CheckListList),
}

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
struct CheckListList(BTreeMap<String, CheckList>);

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
struct CheckList {
    #[serde(default)]
    automated: Vec<String>,
    #[serde(default)]
    manual: Vec<String>,
}

impl CheckListList {
    /// Creates a CheckListList from a file
    fn from_file(path: &Path) -> Result<CheckListList, Error> {
        let file = match File::open(&path) {
            Err(e) => bail!("couldn't open file {}: {}", path.display(), e),
            Ok(file) => file,
        };
        match CheckListList::from_reader(file) {
            Err(e) => bail!("couldn't open file {}: {}", path.display(), e),
            Ok(s) => Ok(s),
        }
    }

    /// Creates a CheckListList from a Reader
    fn from_reader<R: Read>(input: R) -> Result<CheckListList, Error> {
        match serde_yaml::from_reader::<_, VersionedCheckListList>(input) {
            Err(e) => bail!("couldn't parse yaml: {}", e),
            Ok(s) => match s {
                VersionedCheckListList::Current(s) => Ok(s),
            },
        }
    }
}

/// Asks a yes/no question to the user, returning the response
fn ask_question(prompt: &str) -> Result<bool, Error> {
    let mut theme = ColorfulTheme::default();
    theme.no_style = Style::new().red();
    Ok(Confirmation::with_theme(&theme)
        .with_text(&prompt)
        .interact()?)
}

/// Asks a formatted yes/no question to the user, returning the response
fn ask_formatted_question(prefix: &str, prompt: &str) -> Result<bool, Error> {
    ask_question(&format!("{}{}?", prefix, prompt))
}

/// Prompt the user with a list of yes/no questions, stopping if any are false
fn question_loop(checklist: &CheckList) -> Result<bool, Error> {
    let success = Style::new().green();
    let failure = Style::new().red();
    let mut seen = false;
    for item in &checklist.manual {
        if !seen {
            seen = true;
        } else {
            println!("Great! Continuing...")
        }
        if !ask_formatted_question(&"Have you: ", &item)? {
            println!("\nmanual tests: {}", failure.apply_to("failed"));
            return Ok(false);
        }
    }

    println!("\nmanual tests: {}", success.apply_to("passed"));
    Ok(true)
}

/// Run a list of commands, stopping if any fail
fn shell_loop(checklist: &CheckList) -> Result<bool, Error> {
    let success = Style::new().green();
    let failure = Style::new().red();
    let sty = ProgressStyle::default_bar()
        .template("{bar:40.green/white} {pos:>2}/{len:7} {wide_msg:.blue}");
    let b = ProgressBar::new(checklist.automated.len() as u64);
    b.set_style(sty.clone());
    let progress_bar = scopeguard::guard(b, |b| {
        b.finish_and_clear();
    });
    for item in &checklist.automated {
        progress_bar.set_message(&item);
        let command_res = sh_dangerous(item)
            .stdout_capture()
            .stderr_capture()
            .unchecked()
            .run()?;
        if !command_res.status.success() {
            progress_bar.finish_and_clear();
            println!("\nautomated tests: {}", failure.apply_to("failed"));
            println!("{} running: {}\n", failure.apply_to("error"), item);
            io::stdout().write_all(&command_res.stdout)?;
            io::stderr().write_all(&command_res.stderr)?;
            return Ok(false);
        }
        progress_bar.inc(1);
    }

    progress_bar.finish_and_clear();
    println!("\nautomated tests: {}", success.apply_to("passed"));
    Ok(true)
}

#[derive(Debug, StructOpt)]
#[structopt(
    name = "checklist",
    about = "Run through a checklist",
    raw(setting = "structopt::clap::AppSettings::ColoredHelp")
)]
pub struct Opt {
    #[structopt(
        parse(from_os_str),
        default_value = ".checklist.yml",
        long = "checklist",
        help = "location of the checklist YAML"
    )]
    checklist: PathBuf,
}

/// Run automated tests and ask if manual tests have been done
pub fn run(opts: &Opt) -> Result<(), Error> {
    let success = Style::new().green();
    let failure = Style::new().red();
    let checklists = CheckListList::from_file(&opts.checklist)?;
    if let Some(checklist) = checklists.0.get("committing") {
        if shell_loop(&checklist)? && question_loop(&checklist)? {
            println!("{}", success.apply_to("all clear!"));
        } else {
            println!(
                "{} please fix and start again",
                failure.apply_to("aborting")
            );
        }
    }
    Ok(())
}

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

    #[test]
    fn test_correct_yaml() {
        let t = assert_fs::TempDir::new().unwrap();
        let temp = scopeguard::guard(t, |t| {
            t.close().unwrap();
        });
        temp.child(".checklist.yml")
            .write_str(
                "schema_version: 2\ncommitting:\n  automated: []\n  manual:\n    - test",
            )
            .unwrap();
        assert_eq!(
            CheckListList::from_file(temp.child(".checklist.yml").path()).unwrap(),
            CheckListList(btreemap! {
                String::from("committing") => CheckList{
                    manual: vec![String::from("test")],
                    automated: vec![],
                },
            }),
        );
    }

    #[test]
    fn test_defaults() {
        let t = assert_fs::TempDir::new().unwrap();
        let temp = scopeguard::guard(t, |t| {
            t.close().unwrap();
        });
        temp.child(".checklist.yml")
            .write_str("schema_version: 2\ncommitting:\n  manual: []")
            .unwrap();
        assert_eq!(
            CheckListList::from_file(temp.child(".checklist.yml").path()).unwrap(),
            CheckListList(btreemap! {
                String::from("committing") => CheckList{
                    manual: vec![],
                    automated: vec![],
                },
            }),
        );
    }

    #[test]
    fn test_incorrect_yaml() {
        let t = assert_fs::TempDir::new().unwrap();
        let temp = scopeguard::guard(t, |t| {
            t.close().unwrap();
        });
        temp.child(".checklist.yml").write_str("beep beep").unwrap();
        assert!(CheckListList::from_file(temp.child(".checklist.yml").path()).is_err())
    }

    #[test]
    fn test_incorrect_schema_version() {
        let t = assert_fs::TempDir::new().unwrap();
        let temp = scopeguard::guard(t, |t| {
            t.close().unwrap();
        });
        temp.child(".checklist.yml")
            .write_str("schema_version: bananas\ncommitting:\n- test")
            .unwrap();
        assert!(CheckListList::from_file(temp.child(".checklist.yml").path()).is_err())
    }

    #[test]
    fn test_invalid_filename() {
        let t = assert_fs::TempDir::new().unwrap();
        let temp = scopeguard::guard(t, |t| {
            t.close().unwrap();
        });
        assert!(CheckListList::from_file(temp.child("does_not_exist").path()).is_err())
    }
}