| 1 | //! Workflows as people write them for GitHub (`tests/corpus/valid`), each |
| 2 | //! of which g1t reads, and workflows GitHub refuses |
| 3 | //! (`tests/corpus/invalid`), each of which g1t refuses too, with the |
| 4 | //! problem its first line names (`# error: ...`). Every file was written |
| 5 | //! for this test; the refused ones follow the cases in GitHub's own parser |
| 6 | //! tests (actions/languageservices, `workflow-parser/testdata/reader`). |
| 7 | |
| 8 | use std::path::{Path, PathBuf}; |
| 9 | |
| 10 | use g1t_actions::workflow; |
| 11 | |
| 12 | fn files(kind: &str) -> Vec<(String, String)> { |
| 13 | let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/corpus").join(kind); |
| 14 | let mut files: Vec<(String, String)> = std::fs::read_dir(&dir) |
| 15 | .unwrap() |
| 16 | .map(|entry| entry.unwrap().path()) |
| 17 | .filter(|path| path.extension().and_then(|e| e.to_str()) == Some("yml")) |
| 18 | .map(|path: PathBuf| (name(&path), std::fs::read_to_string(&path).unwrap())) |
| 19 | .collect(); |
| 20 | files.sort(); |
| 21 | files |
| 22 | } |
| 23 | |
| 24 | fn name(path: &Path) -> String { |
| 25 | path.file_name().unwrap().to_string_lossy().into_owned() |
| 26 | } |
| 27 | |
| 28 | #[test] |
| 29 | fn every_github_workflow_reads() { |
| 30 | let files = files("valid"); |
| 31 | assert!(files.len() >= 40, "{}", files.len()); |
| 32 | let mut problems = Vec::new(); |
| 33 | for (name, source) in &files { |
| 34 | if let Err(problem) = workflow::parse(source) { |
| 35 | problems.push(format!("{name}: {problem}")); |
| 36 | } |
| 37 | } |
| 38 | assert!(problems.is_empty(), "{}", problems.join("\n")); |
| 39 | } |
| 40 | |
| 41 | #[test] |
| 42 | fn what_github_refuses_is_refused() { |
| 43 | let files = files("invalid"); |
| 44 | assert!(files.len() >= 40, "{}", files.len()); |
| 45 | let mut problems = Vec::new(); |
| 46 | for (name, source) in &files { |
| 47 | let expected = source |
| 48 | .lines() |
| 49 | .next() |
| 50 | .and_then(|line| line.strip_prefix("# error: ")) |
| 51 | .unwrap_or_else(|| panic!("{name}: its first line is `# error: <what the problem says>`")); |
| 52 | match workflow::parse(source) { |
| 53 | Ok(_) => problems.push(format!("{name}: reads, and should not")), |
| 54 | Err(problem) if !problem.contains(expected) => problems.push(format!("{name}: says {problem:?}, not {expected:?}")), |
| 55 | Err(_) => {} |
| 56 | } |
| 57 | } |
| 58 | assert!(problems.is_empty(), "{}", problems.join("\n")); |
| 59 | } |