-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
swpfile.rs
83 lines (64 loc) · 1.59 KB
/
swpfile.rs
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use std::fs::File;
use std::io::{BufRead, BufReader, Result};
use std::path::Path;
pub fn parse_swpfile<P: AsRef<Path>>(dir: P, filename : P) -> Result<Vec<String>> {
let file = File::open(dir.as_ref().join(filename))?;
let reader = BufReader::new(file);
let mut paths = Vec::new();
for line in reader.lines() {
let line = line?.trim().to_owned();
if line.len() < 1 {
continue;
}
if line.starts_with('#') {
continue;
}
if paths.contains(&line) {
continue;
}
paths.push(line);
}
Ok(paths)
}
#[cfg(test)]
mod test {
use super::*;
use crate::utils::test_utils;
#[test]
fn empty_file() {
test_utils::with_temp_dir(|dir| {
std::fs::write(dir.join(".swpfile"), "")
.expect("Could not write test file");
let dirs = parse_swpfile(dir, Path::new(".swpfile"))
.expect("Error while reading .swpfile");
assert_eq!(dirs.len(), 0);
});
}
#[test]
fn swpfile() {
const FILE_CONTENTS: &'static str = r"
# comment
target
test-directory/
duplicate
duplicate
";
test_utils::with_temp_dir(|dir| {
std::fs::write(dir.join(".swpfile"), FILE_CONTENTS)
.expect("Could not write test file");
let dirs = parse_swpfile(dir, Path::new(".swpfile"))
.expect("Error while reading .swpfile");
assert_eq!(dirs.len(), 3);
});
}
#[test]
fn swpfile_cleanuprc() {
test_utils::with_temp_dir(|dir| {
std::fs::write(dir.join(".cleanuprc"), "target")
.expect("Could not write test file");
let dirs = parse_swpfile(dir, Path::new(".cleanuprc"))
.expect("Error while reading .cleanuprc");
assert_eq!(dirs.len(), 1);
});
}
}