path resolution w.r.t root
This commit is contained in:
parent
b7336fc0b1
commit
26d40b9993
3 changed files with 69 additions and 17 deletions
|
@ -3,20 +3,23 @@ use cache_buster::Files;
|
|||
fn main() {
|
||||
let files = Files::load();
|
||||
|
||||
assert!(file_exists("../dist/log-out.svg", &files));
|
||||
assert!(file_exists(
|
||||
assert!(get_full_path_runner("../dist/log-out.svg", &files));
|
||||
assert!(get_full_path_runner(
|
||||
"../dist/a/b/c/d/s/d/svg/credit-card.svg",
|
||||
&files
|
||||
));
|
||||
|
||||
assert!(!file_exists("dist/log-out.svg", &files));
|
||||
assert!(!file_exists("dist/a/b/c/d/s/d/svg/credit-card.svg", &files));
|
||||
assert!(!get_full_path_runner("dist/log-out.svg", &files));
|
||||
assert!(!get_full_path_runner(
|
||||
"dist/a/b/c/d/s/d/svg/credit-card.svg",
|
||||
&files
|
||||
));
|
||||
}
|
||||
|
||||
fn file_exists(path: &str, files: &Files) -> bool {
|
||||
fn get_full_path_runner(path: &str, files: &Files) -> bool {
|
||||
use std::path::Path;
|
||||
|
||||
if let Some(file) = files.get(path) {
|
||||
if let Some(file) = files.get_full_path(path) {
|
||||
Path::new(file).exists()
|
||||
} else {
|
||||
false
|
||||
|
|
|
@ -54,7 +54,7 @@ impl Buster {
|
|||
//
|
||||
// doesn't process files for which mime is not resolved
|
||||
pub fn try_hash(&self) -> Result<Files, Error> {
|
||||
let mut file_map: Files = Files::default();
|
||||
let mut file_map: Files = Files::new(&self.result);
|
||||
for entry in WalkDir::new(&self.source)
|
||||
.follow_links(self.follow_links)
|
||||
.into_iter()
|
||||
|
@ -92,7 +92,7 @@ impl Buster {
|
|||
// panics when mimetypes are detected. This way you'll know which files are ignored
|
||||
// from processing
|
||||
pub fn hash(&self) -> Result<Files, Error> {
|
||||
let mut file_map: Files = Files::default();
|
||||
let mut file_map: Files = Files::new(&self.result);
|
||||
|
||||
for entry in WalkDir::new(&self.source)
|
||||
.follow_links(self.follow_links)
|
||||
|
|
67
src/map.rs
67
src/map.rs
|
@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize};
|
|||
|
||||
const ENV_VAR_NAME: &str = "CACHE_BUSTER_FILE_MAP";
|
||||
|
||||
#[derive(Debug, PartialEq, Default, Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
|
||||
pub struct Files {
|
||||
pub map: HashMap<String, String>,
|
||||
base_dir: String,
|
||||
|
@ -26,7 +26,15 @@ impl Files {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get<'a>(&'a self, path: &'a str) -> Option<&'a String> {
|
||||
pub fn get<'a>(&'a self, path: &'a str) -> Option<&'a str> {
|
||||
if let Some(path) = self.map.get(path) {
|
||||
Some(&path[self.base_dir.len()..])
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_full_path<'a>(&'a self, path: &'a str) -> Option<&'a String> {
|
||||
self.map.get(path)
|
||||
}
|
||||
|
||||
|
@ -70,7 +78,7 @@ mod tests {
|
|||
use std::path::Path;
|
||||
|
||||
#[test]
|
||||
fn get_works() {
|
||||
fn get_full_path_works() {
|
||||
let types = vec![
|
||||
mime::IMAGE_PNG,
|
||||
mime::IMAGE_SVG,
|
||||
|
@ -90,19 +98,22 @@ mod tests {
|
|||
config.init().unwrap();
|
||||
let files = config.hash().unwrap();
|
||||
|
||||
assert!(file_exists("./dist/log-out.svg", &files));
|
||||
assert!(file_exists(
|
||||
assert!(get_full_path_runner("./dist/log-out.svg", &files));
|
||||
assert!(get_full_path_runner(
|
||||
"./dist/a/b/c/d/s/d/svg/credit-card.svg",
|
||||
&files
|
||||
));
|
||||
|
||||
assert!(!file_exists("dist/log-out.svg", &files));
|
||||
assert!(!file_exists("dist/a/b/c/d/s/d/svg/credit-card.svg", &files));
|
||||
assert!(!get_full_path_runner("dist/log-out.svg", &files));
|
||||
assert!(!get_full_path_runner(
|
||||
"dist/a/b/c/d/s/d/svg/credit-card.svg",
|
||||
&files
|
||||
));
|
||||
cleanup(&config);
|
||||
}
|
||||
|
||||
fn file_exists(path: &str, files: &Files) -> bool {
|
||||
if let Some(file) = files.get(path) {
|
||||
fn get_full_path_runner(path: &str, files: &Files) -> bool {
|
||||
if let Some(file) = files.get_full_path(path) {
|
||||
Path::new(file).exists()
|
||||
} else {
|
||||
false
|
||||
|
@ -138,4 +149,42 @@ mod tests {
|
|||
|
||||
cleanup(&config);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_works() {
|
||||
let types = vec![
|
||||
mime::IMAGE_PNG,
|
||||
mime::IMAGE_SVG,
|
||||
mime::IMAGE_JPEG,
|
||||
mime::IMAGE_GIF,
|
||||
];
|
||||
|
||||
let config = BusterBuilder::default()
|
||||
.source("./dist")
|
||||
.result("/tmp/prod5")
|
||||
.mime_types(types)
|
||||
.copy(true)
|
||||
.follow_links(true)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
config.init().unwrap();
|
||||
let files = config.hash().unwrap();
|
||||
|
||||
assert!(get_runner("./dist/log-out.svg", &files));
|
||||
assert!(get_runner("./dist/a/b/c/d/s/d/svg/credit-card.svg", &files));
|
||||
|
||||
assert!(!get_runner("dist/log-out.svg", &files));
|
||||
assert!(!get_runner("dist/a/b/c/d/s/d/svg/credit-card.svg", &files));
|
||||
cleanup(&config);
|
||||
}
|
||||
|
||||
fn get_runner(path: &str, files: &Files) -> bool {
|
||||
if let Some(file) = files.get(path) {
|
||||
let path = Path::new(&files.base_dir).join(&file[1..]);
|
||||
path.exists()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue