Forráskód Böngészése

Add tests for file extraction from diffs

Mark Rousskov 3 éve
szülő
commit
0ccf3ee53e
2 módosított fájl, 99 hozzáadás és 30 törlés
  1. 85 0
      src/github.rs
  2. 14 30
      src/handlers/autolabel.rs

+ 85 - 0
src/github.rs

@@ -779,6 +779,21 @@ pub struct CommitBase {
     sha: String,
 }
 
+pub fn files_changed(diff: &str) -> Vec<&str> {
+    let mut files = Vec::new();
+    for line in diff.lines() {
+        // mostly copied from highfive
+        if line.starts_with("diff --git ") {
+            files.push(
+                line[line.find(" b/").unwrap()..]
+                    .strip_prefix(" b/")
+                    .unwrap(),
+            );
+        }
+    }
+    files
+}
+
 impl IssuesEvent {
     /// Returns the diff in this event, for Open and Synchronize events for now.
     pub async fn diff_between(&self, client: &GithubClient) -> anyhow::Result<Option<String>> {
@@ -1331,3 +1346,73 @@ pub trait IssuesQuery {
         client: &'a GithubClient,
     ) -> anyhow::Result<Vec<crate::actions::IssueDecorator>>;
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn extract_one_file() {
+        let input = r##"\
+diff --git a/triagebot.toml b/triagebot.toml
+index fb9cee43b2d..b484c25ea51 100644
+--- a/triagebot.toml
++++ b/triagebot.toml
+@@ -114,6 +114,15 @@ trigger_files = [
+        "src/tools/rustdoc-themes",
+    ]
++[autolabel."T-compiler"]
++trigger_files = [
++    # Source code
++    "compiler",
++
++    # Tests
++    "src/test/ui",
++]
++
+    [notify-zulip."I-prioritize"]
+    zulip_stream = 245100 # #t-compiler/wg-prioritization/alerts
+    topic = "#{number} {title}"
+         "##;
+        assert_eq!(files_changed(input), vec!["triagebot.toml".to_string()]);
+    }
+
+    #[test]
+    fn extract_several_files() {
+        let input = r##"\
+diff --git a/library/stdarch b/library/stdarch
+index b70ae88ef2a..cfba59fccd9 160000
+--- a/library/stdarch
++++ b/library/stdarch
+@@ -1 +1 @@
+-Subproject commit b70ae88ef2a6c83acad0a1e83d5bd78f9655fd05
++Subproject commit cfba59fccd90b3b52a614120834320f764ab08d1
+diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs
+index 1fe4aa9023e..f0330f1e424 100644
+--- a/src/librustdoc/clean/types.rs
++++ b/src/librustdoc/clean/types.rs
+@@ -2322,3 +2322,4 @@ impl SubstParam {
+        if let Self::Lifetime(lt) = self { Some(lt) } else { None }
+    }
+}
++
+diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs
+index c58310947d2..3b0854d4a9b 100644
+--- a/src/librustdoc/core.rs
++++ b/src/librustdoc/core.rs
+@@ -591,3 +591,4 @@ fn from(idx: u32) -> Self {
+        ImplTraitParam::ParamIndex(idx)
+    }
+}
++
+"##;
+        assert_eq!(
+            files_changed(input),
+            vec![
+                "library/stdarch".to_string(),
+                "src/librustdoc/clean/types.rs".to_string(),
+                "src/librustdoc/core.rs".to_string(),
+            ]
+        )
+    }
+}

+ 14 - 30
src/handlers/autolabel.rs

@@ -1,6 +1,6 @@
 use crate::{
     config::AutolabelConfig,
-    github::{IssuesAction, IssuesEvent, Label},
+    github::{files_changed, IssuesAction, IssuesEvent, Label},
     handlers::Context,
 };
 use tracing as log;
@@ -23,38 +23,22 @@ pub(super) async fn parse_input(
             })
             .unwrap_or_default()
         {
-            let mut files = Vec::new();
-            for line in diff.lines() {
-                // mostly copied from highfive
-                if line.starts_with("diff --git ") {
-                    files.push(
-                        line[line.find(" b/").unwrap()..]
-                            .strip_prefix(" b/")
-                            .unwrap(),
-                    );
-                }
-            }
+            let files = files_changed(&diff);
             let mut autolabels = Vec::new();
-            for trigger_file in files {
-                if trigger_file.is_empty() {
-                    // TODO: when would this be true?
-                    continue;
-                }
-                for (label, cfg) in config.labels.iter() {
-                    if cfg
-                        .trigger_files
-                        .iter()
-                        .any(|f| trigger_file.starts_with(f))
-                    {
-                        autolabels.push(Label {
-                            name: label.to_owned(),
-                        });
-                    }
-                }
-                if !autolabels.is_empty() {
-                    return Ok(Some(AutolabelInput { labels: autolabels }));
+            for (label, cfg) in config.labels.iter() {
+                if cfg
+                    .trigger_files
+                    .iter()
+                    .any(|f| files.iter().any(|diff_file| diff_file.starts_with(f)))
+                {
+                    autolabels.push(Label {
+                        name: label.to_owned(),
+                    });
                 }
             }
+            if !autolabels.is_empty() {
+                return Ok(Some(AutolabelInput { labels: autolabels }));
+            }
         }
     }