Repository: dbrgn/tealdeer (rust)

Issue:
Include custom pages and Patches directory in `--show-paths` option
Quote from PR #142
> We should also include the custom pages directory in the new --show-paths option output, but that's probably best done in a separate PR, in order not to hold up this one.

Acceptance Criteria:
* Using `--show-paths` includes the location of the custom_pages directory in the output
* Tests are created / updated for new and changed code

Pull request:
Custom pages in show paths
A simpler version of #184 that doesn't change the config structure or file lookup logic.

Fixes #182.

Patch:
diff --git a/src/config.rs b/src/config.rs
index d1b040b..32e902e 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -173,7 +173,10 @@ impl Default for RawDirectoriesConfig {
     fn default() -> Self {
         Self {
             custom_pages_dir: get_app_root(AppDataType::UserData, &crate::APP_INFO)
-                .map(|path| path.join("pages"))
+                .map(|path| {
+                    // Note: The `join("")` call ensures that there's a trailing slash
+                    path.join("pages").join("")
+                })
                 .ok(),
         }
     }
diff --git a/src/main.rs b/src/main.rs
index 4ee6d2d..d23e029 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -257,7 +257,7 @@ fn show_config_path(enable_styles: bool) {
 }
 
 /// Show file paths
-fn show_paths() {
+fn show_paths(config: &Config) {
     let config_dir = get_config_dir().map_or_else(
         |e| format!("[Error: {}]", e),
         |(mut path, source)| {
@@ -289,13 +289,21 @@ fn show_paths() {
             path.push(""); // Trailing path separator
             path.into_os_string()
                 .into_string()
-                .unwrap_or_else(|_| String::from("[Invalid]"))
+                .unwrap_or_else(|_| "[Invalid]".to_string())
         },
     );
-    println!("Config dir:  {}", config_dir);
-    println!("Config path: {}", config_path);
-    println!("Cache dir:   {}", cache_dir);
-    println!("Pages dir:   {}", pages_dir);
+    let custom_pages_dir = config.directories.custom_pages_dir.as_deref().map_or_else(
+        || "[None]".to_string(),
+        |path| {
+            path.to_str()
+                .map_or_else(|| "[Invalid]".to_string(), ToString::to_string)
+        },
+    );
+    println!("Config dir:       {}", config_dir);
+    println!("Config path:      {}", config_path);
+    println!("Cache dir:        {}", cache_dir);
+    println!("Pages dir:        {}", pages_dir);
+    println!("Custom pages dir: {}", custom_pages_dir);
 }
 
 /// Create seed config file and exit
@@ -416,14 +424,6 @@ fn main() {
         );
         show_config_path(enable_styles);
     }
-    if args.show_paths {
-        show_paths();
-    }
-
-    // Create a basic config and exit
-    if args.seed_config {
-        create_config_and_exit(enable_styles);
-    }
 
     // Look up config file, if none is found fall back to default config.
     let config = match Config::load(enable_styles) {
@@ -438,10 +438,21 @@ fn main() {
         }
     };
 
+    // Set up pager
     if args.pager || config.display.use_pager {
         configure_pager(enable_styles);
     }
 
+    // Show various paths
+    if args.show_paths {
+        show_paths(&config);
+    }
+
+    // Create a basic config and exit
+    if args.seed_config {
+        create_config_and_exit(enable_styles);
+    }
+
     // Specify target OS
     let platform: PlatformType = args.platform.unwrap_or_else(PlatformType::current);
 


Test patch:
diff --git a/tests/lib.rs b/tests/lib.rs
index de6d346..dd03135 100644
--- a/tests/lib.rs
+++ b/tests/lib.rs
@@ -284,17 +284,18 @@ fn test_setup_seed_config() {
 fn test_show_paths() {
     let testenv = TestEnv::new();
 
+    // Show general commands
     testenv
         .command()
         .args(["--show-paths"])
         .assert()
         .success()
         .stdout(contains(format!(
-            "Config dir:  {}",
+            "Config dir:       {}",
             testenv.config_dir.path().to_str().unwrap(),
         )))
         .stdout(contains(format!(
-            "Config path: {}",
+            "Config path:      {}",
             testenv
                 .config_dir
                 .path()
@@ -303,11 +304,11 @@ fn test_show_paths() {
                 .unwrap(),
         )))
         .stdout(contains(format!(
-            "Cache dir:   {}",
+            "Cache dir:        {}",
             testenv.cache_dir.path().to_str().unwrap(),
         )))
         .stdout(contains(format!(
-            "Pages dir:   {}",
+            "Pages dir:        {}",
             testenv
                 .cache_dir
                 .path()
@@ -315,6 +316,23 @@ fn test_show_paths() {
                 .to_str()
                 .unwrap(),
         )));
+
+    // Set custom pages directory
+    testenv.write_config(format!(
+        "[directories]\ncustom_pages_dir = '{}'",
+        testenv.custom_pages_dir.path().to_str().unwrap()
+    ));
+
+    // Now ensure that this path is contained in the output
+    testenv
+        .command()
+        .args(["--show-paths"])
+        .assert()
+        .success()
+        .stdout(contains(format!(
+            "Custom pages dir: {}",
+            testenv.custom_pages_dir.path().to_str().unwrap(),
+        )));
 }
 
 #[test]