Add CI workflow for formatting, lints and tests
5 files+58−90/5 viewed
| 1 | + | name: CI | |
| 2 | + | on: | |
| 3 | + | push: | |
| 4 | + | branches: [main] | |
| 5 | + | pull_request: | |
| 6 | + | merge_group: | |
| 7 | + | jobs: | |
| 8 | + | check: | |
| 9 | + | runs-on: ubuntu-latest | |
| 10 | + | steps: | |
| 11 | + | - uses: actions/checkout@v7 | |
| 12 | + | - uses: dtolnay/rust-toolchain@stable | |
| 13 | + | with: | |
| 14 | + | components: rustfmt, clippy | |
| 15 | + | - uses: actions/cache@v6 | |
| 16 | + | with: | |
| 17 | + | path: | | |
| 18 | + | ~/.cargo/registry | |
| 19 | + | ~/.cargo/git | |
| 20 | + | target | |
| 21 | + | key: cargo-${{ runner.os }}-${{ hashFiles('Cargo.lock') }} | |
| 22 | + | restore-keys: cargo-${{ runner.os }}- | |
| 23 | + | - name: Formatting | |
| 24 | + | run: cargo fmt --check | |
| 25 | + | - name: Lints | |
| 26 | + | run: cargo clippy --all-targets -- -D warnings | |
| 27 | + | - name: Tests | |
| 28 | + | run: cargo test |
| 83 | 83 | cargo test | |
| 84 | 84 | ``` | |
| 85 | 85 | ||
| 86 | + | Every pull request runs formatting, lints and tests. | |
| 87 | + | ||
| 86 | 88 | ## Troubleshooting | |
| 87 | 89 | ||
| 88 | 90 | **`error: --times value must be a number, got "abc"`** |
| 98 | 98 | let value = args | |
| 99 | 99 | .next() | |
| 100 | 100 | .ok_or_else(|| "--lang requires a value".to_owned())?; | |
| 101 | − | options.lang = Lang::parse(&value).ok_or_else(|| { | |
| 102 | − | format!("--lang value must be one of en, es, fr, got {value:?}") | |
| 103 | − | })?; | |
| 101 | + | options.lang = Lang::parse(&value) | |
| 102 | + | .ok_or_else(|| format!("--lang value must be one of en, es, fr, got {value:?}"))?; | |
| 104 | 103 | } else if !arg.starts_with("--") && options.name.is_none() { | |
| 105 | 104 | options.name = Some(arg); | |
| 106 | 105 | } | |
| 196 | 195 | ||
| 197 | 196 | #[test] | |
| 198 | 197 | fn parse_args_name() { | |
| 199 | − | assert_eq!(parse_args(args(&["Ada"])).unwrap().name.as_deref(), Some("Ada")); | |
| 198 | + | assert_eq!( | |
| 199 | + | parse_args(args(&["Ada"])).unwrap().name.as_deref(), | |
| 200 | + | Some("Ada") | |
| 201 | + | ); | |
| 200 | 202 | } | |
| 201 | 203 | ||
| 202 | 204 | #[test] | |
| 203 | 205 | fn parse_args_skips_leading_flag() { | |
| 204 | 206 | assert_eq!( | |
| 205 | − | parse_args(args(&["--unknown", "Ada"])).unwrap().name.as_deref(), | |
| 207 | + | parse_args(args(&["--unknown", "Ada"])) | |
| 208 | + | .unwrap() | |
| 209 | + | .name | |
| 210 | + | .as_deref(), | |
| 206 | 211 | Some("Ada") | |
| 207 | 212 | ); | |
| 208 | 213 | } |
| 23 | 23 | .or_else(|| env::var("USER").ok()) | |
| 24 | 24 | .unwrap_or_else(|| "world".to_owned()); | |
| 25 | 25 | let message = greeting_in(&name, options.lang); | |
| 26 | − | let message = if options.loud { loud(&message) } else { message }; | |
| 27 | − | let line = if options.shout { shout(&message) } else { message }; | |
| 26 | + | let message = if options.loud { | |
| 27 | + | loud(&message) | |
| 28 | + | } else { | |
| 29 | + | message | |
| 30 | + | }; | |
| 31 | + | let line = if options.shout { | |
| 32 | + | shout(&message) | |
| 33 | + | } else { | |
| 34 | + | message | |
| 35 | + | }; | |
| 28 | 36 | for _ in 0..options.times { | |
| 29 | 37 | println!("{line}"); | |
| 30 | 38 | } |
| 58 | 58 | fn lang_fr_greets_in_french() { | |
| 59 | 59 | let output = run(&["--lang", "fr", "Luc"]); | |
| 60 | 60 | assert!(output.status.success()); | |
| 61 | − | assert_eq!(String::from_utf8(output.stdout).unwrap(), "Bonjour, Luc !\n"); | |
| 61 | + | assert_eq!( | |
| 62 | + | String::from_utf8(output.stdout).unwrap(), | |
| 63 | + | "Bonjour, Luc !\n" | |
| 64 | + | ); | |
| 62 | 65 | } | |
| 63 | 66 | ||
| 64 | 67 | #[test] | |
| 83 | 86 | let output = run(&["--lang"]); | |
| 84 | 87 | assert_eq!(output.status.code(), Some(2)); | |
| 85 | 88 | let stderr = String::from_utf8(output.stderr).unwrap(); | |
| 86 | − | assert!(stderr.contains("--lang requires a value"), "stderr was {stderr:?}"); | |
| 89 | + | assert!( | |
| 90 | + | stderr.contains("--lang requires a value"), | |
| 91 | + | "stderr was {stderr:?}" | |
| 92 | + | ); | |
| 87 | 93 | } | |
| 88 | 94 | ||
| 89 | 95 | #[test] |