|
| 1 | +use std::sync::LazyLock; |
| 2 | + |
| 3 | +use regex::{Regex, RegexBuilder}; |
| 4 | + |
1 | 5 | use crate::github::FileDiff;
|
2 | 6 |
|
3 | 7 | const SUBMODULE_WARNING_MSG: &str = "Some commits in this PR modify **submodules**.";
|
4 | 8 |
|
| 9 | +static SUBPROJECT_COMMIT_RE: LazyLock<Regex> = LazyLock::new(|| { |
| 10 | + RegexBuilder::new(r"^\+Subproject commit [a-zA-Z0-9]+$") |
| 11 | + .multi_line(true) |
| 12 | + .build() |
| 13 | + .unwrap() |
| 14 | +}); |
| 15 | + |
5 | 16 | /// Returns a message if the PR modifies a git submodule.
|
6 | 17 | pub(super) fn modifies_submodule(diff: &[FileDiff]) -> Option<String> {
|
7 |
| - let re = regex::Regex::new(r"\+Subproject\scommit\s").unwrap(); |
8 |
| - if diff.iter().any(|fd| re.is_match(&fd.patch)) { |
| 18 | + if diff |
| 19 | + .iter() |
| 20 | + .any(|fd| SUBPROJECT_COMMIT_RE.is_match(&fd.patch)) |
| 21 | + { |
9 | 22 | Some(SUBMODULE_WARNING_MSG.to_string())
|
10 | 23 | } else {
|
11 | 24 | None
|
12 | 25 | }
|
13 | 26 | }
|
| 27 | + |
| 28 | +#[test] |
| 29 | +fn no_submodule_update() { |
| 30 | + let filediff = FileDiff { |
| 31 | + filename: "src/lib.rs".to_string(), |
| 32 | + patch: "@@ -1 +1 @@\ |
| 33 | + -let mut my_var = 5;\ |
| 34 | + +let mut my_var = \"tmp\";" |
| 35 | + .to_string(), |
| 36 | + }; |
| 37 | + |
| 38 | + assert_eq!(modifies_submodule(&[filediff]), None) |
| 39 | +} |
| 40 | + |
| 41 | +#[test] |
| 42 | +fn simple_submodule_update() { |
| 43 | + // Taken from https://api.github.com/repos/rust-lang/rust/compare/5af801b687e6e8b860ae970e725c8b9a3820d0ce...d6c4ab81be200855df856468ddedde057958441a |
| 44 | + let filediff = FileDiff { |
| 45 | + filename: "src/tools/rustc-perf".to_string(), |
| 46 | + patch: "@@ -1 +1 @@\n\ |
| 47 | + -Subproject commit c0f3b53c8e5de87714d18a5f42998859302ae03a\n\ |
| 48 | + +Subproject commit 8158f78f738715c060d230351623a7f7cc01bf97" |
| 49 | + .to_string(), |
| 50 | + }; |
| 51 | + |
| 52 | + assert_eq!( |
| 53 | + modifies_submodule(&[filediff]), |
| 54 | + Some(SUBMODULE_WARNING_MSG.to_string()) |
| 55 | + ) |
| 56 | +} |
| 57 | + |
| 58 | +#[test] |
| 59 | +fn no_submodule_update_tricky_case() { |
| 60 | + let filediff = FileDiff { |
| 61 | + filename: "src/tools.sh".to_string(), |
| 62 | + patch: "@@ -1 +1 @@\ |
| 63 | + -let mut subproject_commit = 5;\ |
| 64 | + +let mut subproject_commit = \"+Subproject commit \";" |
| 65 | + .to_string(), |
| 66 | + }; |
| 67 | + |
| 68 | + assert_eq!(modifies_submodule(&[filediff]), None) |
| 69 | +} |
0 commit comments