5 changed files with 94 additions and 4 deletions
@ -0,0 +1,84 @@ |
|||||||
|
use std::path::{Path, PathBuf}; |
||||||
|
|
||||||
|
pub fn remove_extension(file_path: &str) -> String { |
||||||
|
let path = Path::new(file_path); |
||||||
|
|
||||||
|
match path.file_stem() { |
||||||
|
Some(stem) => { |
||||||
|
let mut new_path = PathBuf::from(stem); |
||||||
|
|
||||||
|
if let Some(parent) = path.parent() { |
||||||
|
new_path = parent.join(stem); |
||||||
|
} |
||||||
|
|
||||||
|
new_path.to_string_lossy().into_owned() |
||||||
|
} |
||||||
|
None => file_path.to_string() |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
pub fn remove_path_and_get_filename(file_path: &str) -> Option<String> { |
||||||
|
let path = Path::new(file_path); |
||||||
|
|
||||||
|
path.file_name() |
||||||
|
.map(|name| name.to_string_lossy().into_owned()) |
||||||
|
} |
||||||
|
|
||||||
|
pub fn get_directory_path(file_path: &str) -> Option<String> { |
||||||
|
let path = Path::new(file_path); |
||||||
|
|
||||||
|
path.parent() |
||||||
|
.map(|p| p.to_string_lossy().into_owned()) |
||||||
|
} |
||||||
|
|
||||||
|
#[cfg(test)] |
||||||
|
mod tests { |
||||||
|
use std::env; |
||||||
|
use super::*; |
||||||
|
use serial_test::serial; |
||||||
|
use crate::env::{get, set}; |
||||||
|
|
||||||
|
fn setup() { |
||||||
|
env::remove_var("RUST_PROFILE"); |
||||||
|
} |
||||||
|
|
||||||
|
fn teardown() { |
||||||
|
env::remove_var("RUST_PROFILE"); |
||||||
|
} |
||||||
|
|
||||||
|
#[test] |
||||||
|
#[serial] |
||||||
|
fn test_remove_extension() { |
||||||
|
setup(); |
||||||
|
let a = "apm-bigdata/flower-device/1111|2222|3333|4444.csv"; |
||||||
|
let b= remove_extension(a); |
||||||
|
|
||||||
|
//println!("테스트 중: value = {}", b);
|
||||||
|
assert!(b.eq("apm-bigdata/flower-device/1111|2222|3333|4444")); |
||||||
|
teardown(); |
||||||
|
} |
||||||
|
|
||||||
|
#[test] |
||||||
|
#[serial] |
||||||
|
fn test_remove_path_and_get_filename() { |
||||||
|
setup(); |
||||||
|
let a = "apm-bigdata/flower-device/1111|2222|3333|4444.csv"; |
||||||
|
let b= remove_path_and_get_filename(a).unwrap(); |
||||||
|
|
||||||
|
//println!("테스트 중: value = {}", b);
|
||||||
|
assert!(b.eq("1111|2222|3333|4444.csv")); |
||||||
|
teardown(); |
||||||
|
} |
||||||
|
|
||||||
|
#[test] |
||||||
|
#[serial] |
||||||
|
fn test_get_directory_path() { |
||||||
|
setup(); |
||||||
|
let a = "apm-bigdata/flower-device/1111|2222|3333|4444.csv"; |
||||||
|
let b= get_directory_path(a).unwrap(); |
||||||
|
|
||||||
|
println!("테스트 중: value = {}", b); |
||||||
|
assert!(b.eq("apm-bigdata/flower-device")); |
||||||
|
teardown(); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue