input.rs
This file
Path
src/exec/input.rs
87 lines · 2.8 KiB · Rust
Last changed by
Fix test on Windowssharkdpbefore adoption95eae00
History of this fileTake it
The walk stopped after 200 commits: lines older than 8c197d2 point at it.
Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
lines 1–2why, one step back1use std::ffi::{OsStr, OsString};2use std::path::{Path, PathBuf};Update to new lscolors versionsharkdpbefore adoption8c197d2
line 3why, one step back3Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
line 4why, one step back4use crate::filesystem::strip_current_dir;Update to new lscolors versionsharkdpbefore adoption8c197d2
line 5why, one step back5Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
lines 6–8why, one step back6/// Removes the parent component of the path7pub fn basename(path: &Path) -> &OsStr {8 path.file_name().unwrap_or(path.as_os_str())Update to new lscolors versionsharkdpbefore adoption8c197d2
lines 9–11why, one step back9}1011/// Removes the extension from the pathReimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
lines 12–14why, one step back12pub fn remove_extension(path: &Path) -> OsString {13 let dirname = dirname(path);14 let stem = path.file_stem().unwrap_or(path.as_os_str());Update to new lscolors versionsharkdpbefore adoption8c197d2
line 15why, one step back15Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
line 16why, one step back16 let path = PathBuf::from(dirname).join(stem);Update to new lscolors versionsharkdpbefore adoption8c197d2
line 17why, one step back17Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
line 18why, one step back18 strip_current_dir(&path).to_owned().into_os_string()Update to new lscolors versionsharkdpbefore adoption8c197d2
lines 19–21why, one step back19}2021/// Removes the basename from the path.Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
lines 22–31why, one step back22pub fn dirname(path: &Path) -> OsString {23 path.parent()24 .map(|p| {25 if p == OsStr::new("") {26 OsString::from(".")27 } else {28 p.as_os_str().to_owned()29 }30 })31 .unwrap_or(path.as_os_str().to_owned())Update to new lscolors versionsharkdpbefore adoption8c197d2
lines 32–34why, one step back32}3334#[cfg(test)]macronize repetitive tests in exec/inputAlexandru Macoveibefore adoptiond8bd5f9
line 35why, one step back35mod path_tests {Update to new lscolors versionsharkdpbefore adoption8c197d2
line 36why, one step back36 use super::*;Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
line 37why, one step back37 use std::path::MAIN_SEPARATOR;Update to new lscolors versionsharkdpbefore adoption8c197d2
lines 38–42why, one step back3839 fn correct(input: &str) -> String {40 input.replace('/', &MAIN_SEPARATOR.to_string())41 }42macronize repetitive tests in exec/inputAlexandru Macoveibefore adoptiond8bd5f9
lines 43–47why, one step back43 macro_rules! func_tests {44 ($($name:ident: $func:ident for $input:expr => $output:expr)+) => {45 $(46 #[test]47 fn $name() {Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
lines 48–50why, one step back48 let input_path = PathBuf::from(&correct($input));49 let output_string = OsString::from(correct($output));50 assert_eq!($func(&input_path), output_string);macronize repetitive tests in exec/inputAlexandru Macoveibefore adoptiond8bd5f9
lines 51–53why, one step back51 }52 )+53 }Update to new lscolors versionsharkdpbefore adoption8c197d2
lines 54–55why, one step back54 }55macronize repetitive tests in exec/inputAlexandru Macoveibefore adoptiond8bd5f9
lines 56–61why, one step back56 func_tests! {57 remove_ext_simple: remove_extension for "foo.txt" => "foo"58 remove_ext_dir: remove_extension for "dir/foo.txt" => "dir/foo"59 hidden: remove_extension for ".foo" => ".foo"60 remove_ext_utf8: remove_extension for "💖.txt" => "💖"61 remove_ext_empty: remove_extension for "" => ""Update to new lscolors versionsharkdpbefore adoption8c197d2
line 62why, one step back62macronize repetitive tests in exec/inputAlexandru Macoveibefore adoptiond8bd5f9
lines 63–67why, one step back63 basename_simple: basename for "foo.txt" => "foo.txt"64 basename_dir: basename for "dir/foo.txt" => "foo.txt"65 basename_empty: basename for "" => ""66 basename_utf8_0: basename for "💖/foo.txt" => "foo.txt"67 basename_utf8_1: basename for "dir/💖.txt" => "💖.txt"Update to new lscolors versionsharkdpbefore adoption8c197d2
line 68why, one step back68macronize repetitive tests in exec/inputAlexandru Macoveibefore adoptiond8bd5f9
lines 69–72why, one step back69 dirname_simple: dirname for "foo.txt" => "."70 dirname_dir: dirname for "dir/foo.txt" => "dir"71 dirname_utf8_0: dirname for "💖/foo.txt" => "💖"72 dirname_utf8_1: dirname for "dir/💖.txt" => "dir"Update to new lscolors versionsharkdpbefore adoption8c197d2
lines 73–75why, one step back73 }7475 #[test]Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
line 76why, one step back76 #[cfg(windows)]macronize repetitive tests in exec/inputAlexandru Macoveibefore adoptiond8bd5f9
line 77why, one step back77 fn dirname_root() {Fix test on Windowssharkdpbefore adoption95eae00
line 78why, one step back78 assert_eq!(dirname(&PathBuf::from("C:")), OsString::from("C:"));Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
lines 79–85why, one step back79 assert_eq!(dirname(&PathBuf::from("\\")), OsString::from("\\"));80 }8182 #[test]83 #[cfg(not(windows))]84 fn dirname_root() {85 assert_eq!(dirname(&PathBuf::from("/")), OsString::from("/"));Update to new lscolors versionsharkdpbefore adoption8c197d2
lines 86–87why, one step back86 }87}