Skip to content
Myr

Implement multiple suffixes extension support with tests (#214)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Martin Larralde · committed by David Peter · parent b1bb60e · Browse files · 4 files+71 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+71 added−5 removed

  • tests:+22 added−0 removed
  • other:+49 added−5 removed

4 files+71 added−5 removed

modified:src/main.rs+2 added-1 removed
23 unmodified lines
24mod app;25mod exec;26mod internal;27mod output;28mod utils;29mod walk;3031#[cfg(windows)]32mod windows;
119 unmodified lines
152 })153 .collect(),154 },155 extensions: matches.values_of("extension").map(|exts| {155 exts.map(|e| e.trim_left_matches('.').to_lowercase())156 exts.map(|e| String::from(".") + &e.trim_left_matches('.'))157 .collect()158 }),159 command,160 exclude_patterns: matches
14 unmodified lines
added:src/utils.rs+45 added
1// Copyright (c) 2017 fd developers2// Licensed under the Apache License, Version 2.03// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>4// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,5// at your option. All files in the project carrying such6// notice may not be copied, modified, or distributed except7// according to those terms.89use std::path::Path;10use std::iter::Iterator;1112/// Determine if an os string ends with any of the given extensions (case insensitive).13pub fn path_has_any_extension<'a, I>(path: &Path, exts: I) -> bool14where15 I: 'a + Iterator<Item = &'a String> + Clone,16{17 // TODO: remove these two lines when we drop support for Rust version < 1.23.18 #[allow(unused_imports)]19 use std::ascii::AsciiExt;2021 if let Some(ref name) = path.file_name() {22 if let Some(ref name_str) = name.to_str() {23 exts.clone().any(|x| {24 let mut it = name_str.chars().rev();2526 if x.chars()27 .rev()28 .zip(&mut it)29 .all(|(a, b)| a.eq_ignore_ascii_case(&b))30 {31 match it.next() {32 Some('/') | None => false,33 _ => true,34 }35 } else {36 false37 }38 })39 } else {40 false41 }42 } else {43 false44 }45}
modified:src/walk.rs+2 added-4 removed
9 unmodified lines
1011use exec;12use fshelper;13use internal::{error, FdOptions, EXITCODE_SIGINT, MAX_BUFFER_LENGTH};14use utils::path_has_any_extension;15use output;1617use std::process;18use std::path::PathBuf;
195 unmodified lines
214 }215216 // Filter out unwanted extensions.217 if let Some(ref filter_exts) = config.extensions {217 let entry_ext = entry_path218 .extension()219 .map(|e| e.to_string_lossy().to_lowercase());220 if entry_ext.map_or(true, |ext| !filter_exts.contains(&ext)) {218 if !path_has_any_extension(entry_path, filter_exts.iter()) {219 return ignore::WalkState::Continue;220 }221 }222
31 unmodified lines
modified:tests/tests.rs+22 added
528 unmodified lines
529530 te.assert_output(&["--extension", ".foo", "a"], "a.foo");531532 te.assert_output(&["--extension", "foo2"], "one/two/C.Foo2");533534 let te2 = TestEnv::new(&[], &["spam.bar.baz", "egg.bar.baz", "yolk.bar.baz.sig"]);535536 te2.assert_output(537 &["--extension", ".bar.baz"],538 "spam.bar.baz539 egg.bar.baz",540 );541542 te2.assert_output(&["--extension", "sig"], "yolk.bar.baz.sig");543544 te2.assert_output(&["--extension", "bar.baz.sig"], "yolk.bar.baz.sig");545546 let te3 = TestEnv::new(&[], &["latin1.e\u{301}xt", "smiley.☻"]);547548 te3.assert_output(&["--extension", "☻"], "smiley.☻");549550 te3.assert_output(&["--extension", ".e\u{301}xt"], "latin1.e\u{301}xt");551552 let te4 = TestEnv::new(&[], &[".hidden", "test.hidden"]);553554 te4.assert_output(&["--hidden", "--extension", ".hidden"], "test.hidden");555}556557/// Symlinks misc558#[test]
235 unmodified lines