]> git.r.bdr.sh - rbdr/blog/commitdiff
Add tests for configuration
authorRuben Beltran del Rio <redacted>
Sun, 5 Jan 2025 20:30:54 +0000 (21:30 +0100)
committerRuben Beltran del Rio <redacted>
Sun, 5 Jan 2025 20:30:54 +0000 (21:30 +0100)
src/configuration.rs

index dd9a6a9b9f14889aa27aa4b9ce302cd778bc2497..a117f296555c5e759f8874ed8b7448909befc83b 100644 (file)
@@ -91,3 +91,168 @@ impl Configuration {
         .join(path)
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use std::env;
+
+    use test_utilities::*;
+
+    #[test]
+    fn test_reads_configuration_from_configuration_variable_if_set() {
+        let test_dir = setup_test_dir();
+        env::set_var("TEST_BLOG_CONFIG_DIRECTORY", &test_dir);
+
+        let config_directory = Configuration::directory(
+            "TEST_BLOG_CONFIG_DIRECTORY",
+            "XDG_CONFIG_HOME",
+            ".config",
+            "beepboop",
+        );
+
+        assert_eq!(config_directory, test_dir.join("beepboop"));
+        cleanup_test_dir(&test_dir);
+    }
+
+    #[test]
+    fn test_reads_configuration_from_default_location_if_configuration_variable_not_set() {
+        let test_dir = setup_test_dir();
+        env::set_var("TEST_XDG_CONFIG_HOME", &test_dir);
+
+        let config_directory = Configuration::directory(
+            "UNDEFINED_BLOG_CONFIG_DIRECTORY",
+            "TEST_XDG_CONFIG_HOME",
+            ".config",
+            "beepboop",
+        );
+
+        assert_eq!(config_directory, test_dir.join("beepboop"));
+        cleanup_test_dir(&test_dir);
+    }
+
+    #[test]
+    fn test_reads_configuration_from_home_if_configuration_variable_not_set() {
+        let path = PathBuf::from(std::env::var("HOME").unwrap());
+
+        let config_directory = Configuration::directory(
+            "UNDEFINED_BLOG_CONFIG_DIRECTORY",
+            "UNDEFINED_FAKE_XDG_CONFIG_HOME",
+            ".config",
+            "beepboop",
+        );
+
+        assert_eq!(config_directory, path.join(".config/beepboop"));
+    }
+
+    #[test]
+    fn test_sets_correct_configuration_directories() {
+        let default_configuration = Configuration::new();
+
+        let test_dir = setup_test_dir();
+        env::set_var("BLOG_CONFIG_DIRECTORY", &test_dir.join("config"));
+        env::set_var("BLOG_DATA_DIRECTORY", &test_dir.join("data"));
+        env::set_var("BLOG_OUTPUT_DIRECTORY", &test_dir.join("output"));
+        env::set_var("BLOG_MAX_POSTS", "99");
+        let override_configuration = Configuration::new();
+
+        // Ensure our overrides were applied
+
+        assert_eq!(
+            override_configuration.config_directory,
+            test_dir.join("config/blog")
+        );
+        assert_eq!(
+            override_configuration.output_directory,
+            test_dir.join("output/blog")
+        );
+        assert_eq!(
+            override_configuration.data_directory,
+            test_dir.join("data/blog")
+        );
+        assert_eq!(override_configuration.max_posts, 99);
+
+        assert_eq!(
+            override_configuration.remote_config,
+            test_dir.join("config/blog/blogremote")
+        );
+
+        assert_eq!(
+            override_configuration.blog_output_directory,
+            test_dir.join("output/blog/blog")
+        );
+        assert_eq!(
+            override_configuration.archive_output_directory,
+            test_dir.join("output/blog/archive")
+        );
+
+        assert_eq!(
+            override_configuration.posts_directory,
+            test_dir.join("data/blog/posts")
+        );
+        assert_eq!(
+            override_configuration.archive_directory,
+            test_dir.join("data/blog/archive")
+        );
+        assert_eq!(
+            override_configuration.static_directory,
+            test_dir.join("data/blog/static")
+        );
+        assert_eq!(
+            override_configuration.templates_directory,
+            test_dir.join("data/blog/templates")
+        );
+
+        // Ensure all the defaults are different from defaults
+
+        assert_ne!(
+            default_configuration.config_directory,
+            override_configuration.config_directory
+        );
+        assert_ne!(
+            default_configuration.output_directory,
+            override_configuration.output_directory
+        );
+        assert_ne!(
+            default_configuration.data_directory,
+            override_configuration.data_directory
+        );
+        assert_ne!(
+            default_configuration.max_posts,
+            override_configuration.max_posts
+        );
+
+        assert_ne!(
+            default_configuration.remote_config,
+            override_configuration.remote_config
+        );
+
+        assert_ne!(
+            default_configuration.blog_output_directory,
+            override_configuration.blog_output_directory
+        );
+        assert_ne!(
+            default_configuration.archive_output_directory,
+            override_configuration.archive_output_directory
+        );
+
+        assert_ne!(
+            default_configuration.posts_directory,
+            override_configuration.posts_directory
+        );
+        assert_ne!(
+            default_configuration.archive_directory,
+            override_configuration.archive_directory
+        );
+        assert_ne!(
+            default_configuration.static_directory,
+            override_configuration.static_directory
+        );
+        assert_ne!(
+            default_configuration.templates_directory,
+            override_configuration.templates_directory
+        );
+
+        cleanup_test_dir(&test_dir);
+    }
+}