Skip to content

Commit

Permalink
Merge pull request #1 from codeitlikemiley/config_path
Browse files Browse the repository at this point in the history
add fallback path
  • Loading branch information
codeitlikemiley authored Dec 18, 2023
2 parents d241d4f + b1f9faf commit c332455
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
name: Rust Build and Test

on:
push:
pull_request:
branches: [ main ]
tags:
- 'v*'

jobs:
build:
Expand Down
19 changes: 11 additions & 8 deletions src/helpers/expand_home_dir.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use dirs_next::home_dir;
use std::path::PathBuf;

pub fn expand_home_dir(path: &str) -> Option<PathBuf> {
if path.starts_with("~/") {
home_dir().map(|mut home| {
pub fn expand_home_dir(path: &str) -> PathBuf {
if path.starts_with("~") {
if let Some(mut home) = home_dir() {
home.push(&path[2..]);
home
})
} else {
// Fallback: use a temporary directory or another suitable default
PathBuf::from("/tmp/snip.json")
}
} else {
Some(PathBuf::from(path))
PathBuf::from(path)
}
}

Expand All @@ -21,12 +24,12 @@ mod tests {
let home = home_dir().unwrap();
assert_eq!(
expand_home_dir("~/test"),
Some(home.join("test")),
home.join("test"),
"Failed to expand home directory"
);
assert_eq!(
expand_home_dir("/test"),
Some(PathBuf::from("/test")),
PathBuf::from("/test"),
"Failed to expand home directory"
);
}
Expand All @@ -35,7 +38,7 @@ mod tests {
fn test_providing_pull_path_on_expand_home_dir() {
assert_eq!(
expand_home_dir("/Users/uriah/Code/rustacean/src"),
Some(PathBuf::from("/Users/uriah/Code/rustacean/src")),
PathBuf::from("/Users/uriah/Code/rustacean/src"),
"Failed to expand home directory"
)
}
Expand Down
11 changes: 8 additions & 3 deletions src/helpers/get_app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ use crate::helpers::expand_home_dir::expand_home_dir;

pub fn get_app_config() -> String {
dotenv::dotenv().ok();

let config_path = match std::env::var("SNIP_CONFIG_PATH") {
Ok(path) => path,
Err(_) => {
// Set Default Config Path
let config_path = expand_home_dir(DEFAULT_CONFIG_PATH)
.expect("Failed to find home directory")
.to_string_lossy()
.into_owned();
config_path
Expand All @@ -20,16 +19,22 @@ pub fn get_app_config() -> String {
#[cfg(test)]
mod tests {
use super::*;
use std::io::{stdout, Write};

#[test]
#[ignore]
fn test_env_var_is_not_set_then_use_default_config_path() {
// Arrange
std::env::remove_var("SNIP_CONFIG_PATH");

let expected = expand_home_dir(DEFAULT_CONFIG_PATH)
.expect("Failed to find home directory")
.to_string_lossy()
.into_owned();

// Act
let actual = get_app_config();
// For debugging purpose on CI
writeln!(stdout(), "DEBUGGER ENV CONFIG PATH: {}", actual).unwrap();
// Assert
assert_eq!(actual, expected);
}
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ async fn main() -> Result<()> {
Ok(cfg) => cfg,
Err(_) => {
let default_snippet_path = expand_home_dir(DEFAULT_SNIPPET_PATH)
.expect("Failed to find home directory")
.to_string_lossy()
.into_owned();
let new_config = SnipConfig {
Expand Down
5 changes: 1 addition & 4 deletions src/models/snip_config_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ impl SnipConfig {
}

pub fn update_path(&mut self, new_path: String) {
self.path = expand_home_dir(&new_path)
.expect("Failed to find home directory")
.to_string_lossy()
.into_owned();
self.path = expand_home_dir(&new_path).to_string_lossy().into_owned();
}
}

0 comments on commit c332455

Please sign in to comment.