Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kyclark committed Aug 13, 2021
1 parent 71dac13 commit 9e668d6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
33 changes: 23 additions & 10 deletions 11_fortuner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,15 @@ fn parse_u64(val: &str) -> MyResult<u64> {
}

// --------------------------------------------------
fn read_fortunes(paths: &[PathBuf], pattern: &Option<Regex>) -> MyResult<Vec<Fortune>> {
fn read_fortunes(
paths: &[PathBuf],
pattern: &Option<Regex>,
) -> MyResult<Vec<Fortune>> {
let mut fortunes = vec![];
let mut buffer = vec![];

let is_match = |text: &str| pattern.as_ref().map_or(true, |re| re.is_match(text));
let is_match =
|text: &str| pattern.as_ref().map_or(true, |re| re.is_match(text));

for path in paths {
let source = path.file_name().unwrap().to_string_lossy().into_owned();
Expand Down Expand Up @@ -159,7 +163,10 @@ fn find_files(sources: &[String]) -> MyResult<Vec<PathBuf>> {
WalkDir::new(source)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file() && e.path().extension() != Some(dat))
.filter(|e| {
e.file_type().is_file()
&& e.path().extension() != Some(dat)
})
.map(|e| Into::into(e.path())),
//.map(|e| PathBuf::from(e.path())),
//.map(|e| e.path().into()),
Expand Down Expand Up @@ -188,7 +195,9 @@ fn pick_fortune(fortunes: &[Fortune], seed: &Option<u64>) -> Option<String> {
// --------------------------------------------------
#[cfg(test)]
mod tests {
use super::{find_files, parse_u64, pick_fortune, read_fortunes, Fortune};
use super::{
find_files, parse_u64, pick_fortune, read_fortunes, Fortune,
};
use regex::Regex;
use std::path::PathBuf;

Expand All @@ -210,13 +219,13 @@ mod tests {
#[test]
fn test_find_files() {
// Verify that the function finds a file known to exist
let res = find_files(&vec!["./tests/inputs/fortunes".to_string()]);
let res = find_files(&["./tests/inputs/fortunes".to_string()]);
assert!(res.is_ok());

let files = res.unwrap();
assert_eq!(files.len(), 1);
assert_eq!(
files.iter().nth(0).unwrap().to_string_lossy(),
files.get(0).unwrap().to_string_lossy(),
"./tests/inputs/fortunes"
);

Expand All @@ -227,6 +236,8 @@ mod tests {
// Finds all the input files, excludes ".dat"
let res = find_files(&["./tests/inputs".to_string()]);
assert!(res.is_ok());

// Check number and order of files
let files = res.unwrap();
assert_eq!(files.len(), 5);
let first = files.get(0).unwrap().display().to_string();
Expand All @@ -238,7 +249,8 @@ mod tests {
#[test]
fn test_read_fortunes() {
// Parses all the fortunes without a filter
let res = read_fortunes(&[PathBuf::from("./tests/inputs/fortunes")], &None);
let res =
read_fortunes(&[PathBuf::from("./tests/inputs/fortunes")], &None);
assert!(res.is_ok());

if let Ok(fortunes) = res {
Expand Down Expand Up @@ -270,7 +282,7 @@ mod tests {
#[test]
fn test_pick_fortune() {
// Create a vector of fortunes
let fortunes = vec![
let fortunes = &[
Fortune {
source: "fortunes".to_string(),
text: "You cannot achieve the impossible without \
Expand All @@ -279,7 +291,8 @@ mod tests {
},
Fortune {
source: "fortunes".to_string(),
text: "Assumption is the mother of all screw-ups.".to_string(),
text: "Assumption is the mother of all screw-ups."
.to_string(),
},
Fortune {
source: "fortunes".to_string(),
Expand All @@ -289,7 +302,7 @@ mod tests {

// Pick a fortune with a seed
assert_eq!(
pick_fortune(&fortunes, &Some(1)).unwrap(),
pick_fortune(fortunes, &Some(1)).unwrap(),
"Neckties strangle clear thinking.".to_string()
);
}
Expand Down
17 changes: 16 additions & 1 deletion 13_lsr/set-test-perms.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
#!/usr/bin/env bash

chmod 600 tests/inputs/fox.txt
set -u

if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
printf "Usage: %s DIR\n" $(basename "$0")
exit 0
fi

DIR=${1:-$PWD}

chmod 755 ${DIR}/tests/inputs/dir
chmod 600 ${DIR}/tests/inputs/fox.txt
chmod 644 ${DIR}/tests/inputs/.hidden ${DIR}/tests/inputs/empty.txt \
${DIR}/tests/inputs/bustle.txt ${DIR}/tests/inputs/dir/.gitkeep \
${DIR}/tests/inputs/dir/spiders.txt

echo "Done, fixed files in \"$DIR\"."
6 changes: 3 additions & 3 deletions 13_lsr/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ fn run_short(arg: &str) -> TestResult {
}

// --------------------------------------------------
fn run_long(arg: &str, permissions: &str, size: &str) -> TestResult {
fn run_long(filename: &str, permissions: &str, size: &str) -> TestResult {
let cmd = Command::cargo_bin(PRG)?
.args(&["--long", arg])
.args(&["--long", filename])
.assert()
.success();
let stdout = String::from_utf8(cmd.get_output().stdout.clone())?;
let parts: Vec<_> = stdout.split_whitespace().collect();
assert_eq!(parts.get(0).unwrap(), &permissions);
assert_eq!(parts.get(4).unwrap(), &size);
assert_eq!(parts.last().unwrap(), &arg);
assert_eq!(parts.last().unwrap(), &filename);
Ok(())
}

Expand Down

0 comments on commit 9e668d6

Please sign in to comment.