Skip to content

Commit 1011b80

Browse files
committed
Add test for loader.rs
1 parent 9d0b771 commit 1011b80

File tree

5 files changed

+65
-40
lines changed

5 files changed

+65
-40
lines changed

src/hash.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use std::result::Result;
88
use std::str::FromStr;
99
use std::u64;
1010

11-
const FILE_SEPARATOR: &'static str = "\x1C";
12-
const GROUP_SEPARATOR: &'static str = "\x1D";
11+
const FILE_SEPARATOR: &str = "\x1C";
12+
const GROUP_SEPARATOR: &str = "\x1D";
1313

1414
#[derive(Debug, Clone)]
1515
pub struct Source {
@@ -25,7 +25,7 @@ pub struct SourceFile {
2525

2626
impl Ord for SourceFile {
2727
fn cmp(&self, other: &Self) -> Ordering {
28-
return self.name.cmp(&other.name);
28+
self.name.cmp(&other.name)
2929
}
3030
}
3131

@@ -41,7 +41,7 @@ impl PartialEq for SourceFile {
4141
}
4242
}
4343

44-
#[derive(Debug, PartialEq, Clone)]
44+
#[derive(Debug, PartialEq, Eq, Clone)]
4545
pub struct Hash {
4646
pub hash: u64,
4747
}
@@ -52,22 +52,15 @@ struct WrongInputSize;
5252

5353
impl Source {
5454
pub fn new(dir: String) -> Self {
55-
Source {
56-
dir: dir,
57-
files: vec![],
58-
}
55+
Source { dir, files: vec![] }
5956
}
6057

61-
pub fn add_file(&mut self, name: String, contents: String) -> Result<(), Error> {
62-
self.files.push(SourceFile {
63-
name: name.to_string(),
64-
contents: contents.to_string(),
65-
});
66-
Ok(())
58+
pub fn add_file(&mut self, name: String, contents: String) {
59+
self.files.push(SourceFile { name, contents })
6760
}
6861

6962
pub fn hash(&self) -> Result<u64, Error> {
70-
if self.files.len() == 0 {
63+
if self.files.is_empty() {
7164
return Ok(0);
7265
}
7366
let mut hasher = VarBlake2b::new(8)?;
@@ -94,7 +87,7 @@ impl FromStr for Hash {
9487
if key.len() != 16 {
9588
return Err(WrongInputSize {}.into());
9689
}
97-
let hash = u64::from_str_radix(&key, 16)?;
90+
let hash = u64::from_str_radix(key, 16)?;
9891
Ok(Hash { hash })
9992
}
10093
}

src/loader.rs

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use crate::hash::Source;
22

3-
use std::fs::{self, File};
4-
use std::io::{prelude::*, ErrorKind};
3+
use std::fs;
4+
use std::io::ErrorKind;
55
use std::path::PathBuf;
6-
use std::string::String;
76

87
use failure::Error;
98

10-
pub const DEFAULT_RELATIVE_COMPONENT: &'static str = ".shadowenv.d";
9+
pub const DEFAULT_RELATIVE_COMPONENT: &str = ".shadowenv.d";
1110

1211
/// Search upwards the filesystem branch starting with `at` and then its ancestors looking
1312
/// for a file or directory named `relative_component`.
@@ -16,41 +15,71 @@ pub fn find_root(at: PathBuf, relative_component: &str) -> Result<Option<PathBuf
1615
let dirpath = curr.join(relative_component);
1716

1817
match fs::read_dir(&dirpath) {
19-
Ok(_) => return Ok(Some(std::fs::canonicalize(dirpath)?)),
18+
Ok(_) => return Ok(Some(fs::canonicalize(dirpath)?)),
2019
Err(ref e) if e.kind() == ErrorKind::NotFound => (),
2120
Err(e) => return Err(e.into()),
2221
}
2322
}
24-
return Ok(None);
23+
Ok(None)
2524
}
2625

2726
/// Load all .lisp files in the directory pointed by `dirpath` storing their names and contents as
2827
/// `SourceFiles` inside a `Source` struct.
2928
///
3029
/// Note that this function assumes that the dirpath is trusted.
3130
pub fn load(dirpath: PathBuf) -> Result<Option<Source>, Error> {
32-
let mut source = Source::new(dirpath.parent().unwrap().to_str().unwrap().to_string());
33-
34-
for entry in fs::read_dir(dirpath)? {
35-
if let Ok(entry) = entry {
36-
let path = entry.path();
37-
if path.is_file() {
38-
// TODO: there HAS to be a better way to do this.
39-
let basename = path.file_name().unwrap().to_str().unwrap().to_string();
40-
if !basename.ends_with(".lisp") {
41-
continue;
42-
}
43-
let mut file = File::open(&path)?;
44-
let mut contents = String::new();
45-
file.read_to_string(&mut contents)?;
46-
// TODO: surely there's a better way to do this.
47-
source.add_file(basename, contents)?;
31+
let mut source = Source::new(dirpath.parent().unwrap().to_string_lossy().to_string());
32+
33+
for entry in fs::read_dir(dirpath)?.flatten() {
34+
let path = entry.path();
35+
if path.is_file() {
36+
// TODO: there HAS to be a better way to do this.
37+
let basename = path.file_name().unwrap().to_string_lossy().to_string();
38+
if !basename.ends_with(".lisp") {
39+
continue;
4840
}
41+
let contents = fs::read_to_string(&path)?;
42+
source.add_file(basename, contents);
4943
}
5044
}
5145

52-
if source.files.len() == 0 {
46+
if source.files.is_empty() {
5347
return Ok(None);
5448
}
55-
return Ok(Some(source));
49+
Ok(Some(source))
50+
}
51+
52+
#[cfg(test)]
53+
mod tests {
54+
use super::*;
55+
use crate::hash::SourceFile;
56+
57+
#[test]
58+
fn test_load() {
59+
let path: PathBuf = [env!("CARGO_MANIFEST_DIR"), "tests", "fixtures", "simple"]
60+
.iter()
61+
.collect();
62+
let res = load(path);
63+
let source = res.unwrap().unwrap();
64+
assert_eq!(source.files.len(), 2, "it should contain 2 files");
65+
let mut files = source.files.clone();
66+
files.sort_by(|a, b| a.name.cmp(&b.name));
67+
68+
let expected = vec![
69+
SourceFile {
70+
name: "550_dev_ruby.lisp".to_string(),
71+
contents: r#"(provide "ruby" "3.1.2")
72+
"#
73+
.to_string(),
74+
},
75+
SourceFile {
76+
name: "585_dev_rust.lisp".to_string(),
77+
contents: r#"(provide "rust" "stable")
78+
"#
79+
.to_string(),
80+
},
81+
];
82+
83+
assert_eq!(files, expected)
84+
}
5685
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(provide "ruby" "3.1.2")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(provide "rust" "stable")

tests/fixtures/simple/not_lisp.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is not lisp.

0 commit comments

Comments
 (0)