From d661f601e4bb2b3ffcbb1db5bd11a51c6cf8abc1 Mon Sep 17 00:00:00 2001
From: chenjiayi <chenjiayi22@huawei.com>
Date: Fri, 17 Nov 2023 01:15:00 +0800
Subject: [PATCH 068/103] test(devmaster): test exception scenarios on parsing
 rules

---
 exts/devmaster/src/lib/rules/rules_load.rs | 38 ++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/exts/devmaster/src/lib/rules/rules_load.rs b/exts/devmaster/src/lib/rules/rules_load.rs
index cd90c271..19b7aaa4 100644
--- a/exts/devmaster/src/lib/rules/rules_load.rs
+++ b/exts/devmaster/src/lib/rules/rules_load.rs
@@ -1725,10 +1725,13 @@ impl RuleToken {
 #[cfg(test)]
 mod tests {
     use crate::config::*;
+    use basic::fs_util::touch_file;
     use log::init_log;
     use log::Level;
 
     use super::*;
+    use std::fs::create_dir_all;
+    use std::fs::remove_dir_all;
     use std::{fs, path::Path, thread::JoinHandle};
 
     fn create_test_rules_dir(dir: &'static str) {
@@ -2099,4 +2102,39 @@ SYMLINK += \"test111111\"",
         );
         assert_eq!(token.read().unwrap().as_ref().unwrap().value, "0");
     }
+
+    #[test]
+    fn test_parse_rules() {
+        create_dir_all("/tmp/devmaster/rules").unwrap();
+
+        /* Normal rule file. */
+        touch_file(
+            "/tmp/devmaster/rules/00-a.rules",
+            false,
+            Some(0o777),
+            None,
+            None,
+        )
+        .unwrap();
+        /* Skip parsing the file with invalid suffix. */
+        touch_file("/tmp/devmaster/rules/01-b", false, Some(0o777), None, None).unwrap();
+        /* Failed to parse the file as it is not readable. */
+        touch_file(
+            "/tmp/devmaster/rules/02-c.rules",
+            false,
+            Some(0o222),
+            None,
+            None,
+        )
+        .unwrap();
+
+        let rules = Rules::new(
+            vec!["/tmp/devmaster/rules".to_string()],
+            ResolveNameTime::Never,
+        );
+
+        Rules::parse_rules(Arc::new(RwLock::new(rules)));
+
+        remove_dir_all("/tmp/devmaster").unwrap();
+    }
 }
-- 
2.33.0

