File: access.rs

package info (click to toggle)
rust-findutils 0.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,112 kB
  • sloc: sh: 129; python: 31; makefile: 9
file content (58 lines) | stat: -rw-r--r-- 1,496 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2022 Tavian Barnes
//
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

use faccess::PathExt;

use super::{Matcher, MatcherIO, WalkEntry};

/// Matcher for -{read,writ,execut}able.
pub enum AccessMatcher {
    Readable,
    Writable,
    Executable,
}

impl Matcher for AccessMatcher {
    fn matches(&self, file_info: &WalkEntry, _: &mut MatcherIO) -> bool {
        let path = file_info.path();

        match self {
            Self::Readable => path.readable(),
            Self::Writable => path.writable(),
            Self::Executable => path.executable(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::find::matchers::tests::get_dir_entry_for;
    use crate::find::tests::FakeDependencies;

    #[test]
    fn access_matcher() {
        let file_info = get_dir_entry_for("test_data/simple", "abbbc");
        let deps = FakeDependencies::new();

        assert!(
            AccessMatcher::Readable.matches(&file_info, &mut deps.new_matcher_io()),
            "file should be readable"
        );

        /*assert!(
            AccessMatcher::Writable.matches(&file_info, &mut deps.new_matcher_io()),
            "file should be writable"
        );*/

        #[cfg(unix)]
        assert!(
            !AccessMatcher::Executable.matches(&file_info, &mut deps.new_matcher_io()),
            "file should not be executable"
        );
    }
}