Skip to content

Commit

Permalink
test: add test error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
effervescentia committed Apr 14, 2024
1 parent 51c7957 commit a74aca1
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions engine/src/resolve/file_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,70 +49,79 @@ where
mod tests {
use super::FileCache;
use crate::{resolve::Resolver, FileSystem};
use std::{fs, path::Path};
use std::{fs, path::Path, thread::sleep, time::Duration};
use tempfile::tempdir;

const TARGET_FILE: &str = "target_file.txt";
const FILE_CONTENTS: &str = "what's in the box?!";
const STALE_DATA: &str = "stale data";
const FRESH_DATA: &str = "fresh data";

#[test]
fn resolve_from_source() {
let cache_dir = tempdir().unwrap();

let source_dir = tempdir().unwrap();
let source = FileSystem(source_dir.path());
source.write(TARGET_FILE, FILE_CONTENTS);
source.write(TARGET_FILE, FRESH_DATA);

let mut file_cache = FileCache(FileSystem(cache_dir.path()), source);

assert_eq!(
file_cache.resolve(Path::new(TARGET_FILE)),
Some(FILE_CONTENTS.to_owned())
Some(FRESH_DATA.to_owned()),
"data should come from the source file"
);
assert_eq!(
fs::read_to_string(cache_dir.path().join(TARGET_FILE)).unwrap(),
FILE_CONTENTS.to_owned()
FRESH_DATA.to_owned(),
"file should be copied to the cache"
);
}

#[test]
fn resolve_from_fresh_cache() {
let source_dir = tempdir().unwrap();
let source = FileSystem(source_dir.path());
source.write(TARGET_FILE, FILE_CONTENTS);
source.write(TARGET_FILE, STALE_DATA);

sleep(Duration::from_millis(50));

let cache_dir = tempdir().unwrap();
let cache = FileSystem(cache_dir.path());
cache.write(TARGET_FILE, FILE_CONTENTS);
cache.write(TARGET_FILE, FRESH_DATA);

let mut file_cache = FileCache(cache, source);

assert_eq!(
file_cache.resolve(Path::new(TARGET_FILE)),
Some(FILE_CONTENTS.to_owned())
Some(FRESH_DATA.to_owned()),
"data should come from the cached file"
);
}

#[ignore = "failing on ci"]
#[test]
fn resolve_from_stale_cache() {
let cache_dir = tempdir().unwrap();
let cache = FileSystem(cache_dir.path());
cache.write(TARGET_FILE, "stale contents");
cache.write(TARGET_FILE, STALE_DATA);

sleep(Duration::from_millis(50));

let source_dir = tempdir().unwrap();
let source = FileSystem(source_dir.path());
source.write(TARGET_FILE, FILE_CONTENTS);
source.write(TARGET_FILE, FRESH_DATA);

let mut file_cache = FileCache(cache, FileSystem(source_dir.path()));

assert_eq!(
file_cache.resolve(Path::new(TARGET_FILE)),
Some(FILE_CONTENTS.to_owned())
Some(FRESH_DATA.to_owned()),
"data should come from the source file"
);
assert_eq!(
fs::read_to_string(cache_dir.path().join(TARGET_FILE)).unwrap(),
FILE_CONTENTS.to_owned()
FRESH_DATA.to_owned(),
"cache file should be overwritten"
);
}

Expand All @@ -122,10 +131,13 @@ mod tests {

let source_dir = tempdir().unwrap();
let source = FileSystem(source_dir.path());
source.write(TARGET_FILE, FILE_CONTENTS);
source.write(TARGET_FILE, FRESH_DATA);

let file_cache = FileCache(FileSystem(cache_dir.path()), source);

assert!(file_cache.last_modified(Path::new(TARGET_FILE)).is_some());
assert!(
file_cache.last_modified(Path::new(TARGET_FILE)).is_some(),
"should get the last modified date"
);
}
}

0 comments on commit a74aca1

Please sign in to comment.