diff --git a/.gitignore b/.gitignore index ba67566..b079e77 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ prod tarpaulin-report.html actix-example/target actix-example/prod +prod56/ diff --git a/CHANGELOG.md b/CHANGELOG.md index b919dc9..f2787f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,17 +1,32 @@ +## 0.2.0 + +### Changed: + +- `Files::new()` takes a `&str`: Earlier versions were using + environment variables to pass filemap information from `build.rs` + component to program code but this proved to be unreliable. Starting + with `0.2.0`, `cache_buster` will write filemap to + `CACHE_BUSTER_DATA_FILE`(`./src/cache_buster_data.json`) and the user + is requested to read and pass the value to `File::new()` + ## 0.1.1 ### Added: + - Optional route prefix to `Processor` ### Changed: + - `Files::load()` became `Files::new()` ### Removed: + - Some methods on `Files` were for internal use only but they had a public API, they were modified to private. ## 0.1.0 ### Added: + - `SHA-256`-based cache-buster - runtime filemap loading diff --git a/Cargo.lock b/Cargo.lock index 6163925..e77b9fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -21,7 +21,7 @@ dependencies = [ [[package]] name = "cache-buster" -version = "0.1.1" +version = "0.2.0" dependencies = [ "data-encoding", "derive_builder", diff --git a/Cargo.toml b/Cargo.toml index edc437b..354549f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cache-buster" -version = "0.1.1" +version = "0.2.0" authors = ["realaravinth "] edition = "2018" license = "MIT OR Apache-2.0" @@ -22,7 +22,7 @@ mime = "0.3.16" sha2 = "0.9.3" -derive_builder = "0.10.0" +derive_builder = "0.10.2" data-encoding = "2.3.2" walkdir = "2" diff --git a/actix-example/src/cache_buster_data.json b/actix-example/src/cache_buster_data.json new file mode 100644 index 0000000..ea306a9 --- /dev/null +++ b/actix-example/src/cache_buster_data.json @@ -0,0 +1 @@ +{"map":{"../dist/shield-off.svg":"./prod/shield-off.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.svg","../dist/svg/bell.svg":"./prod/svg/bell.9DA292704EE9907EFDB870F4510C97336977CA27FBFAAD83CF46F8E22D3828F7.svg","../dist/file.svg":"./prod/file.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.svg","../dist/svg/tag.svg":"./prod/svg/tag.E0BC111B8E81BBFC62B6A9E7E4AC162B7085A6543D995B7A0030CB7632901BD4.svg","../dist/a/b/c/d/s/d/svg/10.svg":"./prod/a/b/c/d/s/d/svg/10.DF612AFE367A7B31410F2F6CD3C7B515B0F1889C0107EA695D840DFFA492E07D.svg","../dist/svg/log-out.svg":"./prod/svg/log-out.92AB4384FD41D9AFE4735C480361BB64789CD767B7DD0FF3C6F56287B3D4498E.svg","../dist/svg/menu.svg":"./prod/svg/menu.A2C4DD00686F5D23F78885AC4CE3E075FCA78DFBDEA70407667FBBD9801B7A75.svg","../dist/a/b/c/d/s/d/svg/5.svg":"./prod/a/b/c/d/s/d/svg/5.DF612AFE367A7B31410F2F6CD3C7B515B0F1889C0107EA695D840DFFA492E07D.svg","../dist/a/b/c/d/s/d/svg/log-out.svg":"./prod/a/b/c/d/s/d/svg/log-out.92AB4384FD41D9AFE4735C480361BB64789CD767B7DD0FF3C6F56287B3D4498E.svg","../dist/a/b/c/d/s/d/svg/menu.svg":"./prod/a/b/c/d/s/d/svg/menu.A2C4DD00686F5D23F78885AC4CE3E075FCA78DFBDEA70407667FBBD9801B7A75.svg","../dist/svg/settings.svg":"./prod/svg/settings.910C6241743C9C694141971BE8E1C4016A1A5BF203E4E9D676D4CE93BD518F4C.svg","../dist/a/b/c/d/s/d/svg/filter.svg":"./prod/a/b/c/d/s/d/svg/filter.6D5FBD96BA2E2020663AAC4994A991295917D73F3592C07EE103647B655A2275.svg","../dist/svg/moon.svg":"./prod/svg/moon.1E151D68949CA3B2DC7DE34BC25B7586E4175AC3BA7F56DDBB34227334EF7155.svg","../dist/a/b/c/d/s/d/svg/tag.svg":"./prod/a/b/c/d/s/d/svg/tag.E0BC111B8E81BBFC62B6A9E7E4AC162B7085A6543D995B7A0030CB7632901BD4.svg","../dist/bell.svg":"./prod/bell.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.svg","../dist/svg/shield.svg":"./prod/svg/shield.13AFE15DCB4882B4A940CFDC3E2088A733CD4E6F97F25B211D87C7C9D6DBA2B6.svg","../dist/a/b/c/d/s/d/svg/shield-off.svg":"./prod/a/b/c/d/s/d/svg/shield-off.85394A6AD92D550F8EBA72AAB095E078E7A0E3359DF81174532C8D1AF53B5876.svg","../dist/a/b/c/d/s/d/svg/eye.svg":"./prod/a/b/c/d/s/d/svg/eye.9DE4D24D3C9B055D02B94A8AD65E8C0C644852381FDD131A64448B6DA7859167.svg","../dist/file-text.svg":"./prod/file-text.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.svg","../dist/shield.svg":"./prod/shield.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.svg","../dist/a/b/c/d/s/d/svg/toggle-left.svg":"./prod/a/b/c/d/s/d/svg/toggle-left.E421950C5922E84015F0A86F272AE5637A2ED96E267D2C962543F5994E5D1172.svg","../dist/a/b/c/d/s/d/svg/moon.svg":"./prod/a/b/c/d/s/d/svg/moon.1E151D68949CA3B2DC7DE34BC25B7586E4175AC3BA7F56DDBB34227334EF7155.svg","../dist/tag.svg":"./prod/tag.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.svg","../dist/a/b/c/d/s/d/svg/globe.svg":"./prod/a/b/c/d/s/d/svg/globe.44C2A069EBD637663E938ECE7B8E4EC2A8BDE049A8A044EC68D9CB69AE8C592E.svg","../dist/a/b/c/d/s/d/svg/7.svg":"./prod/a/b/c/d/s/d/svg/7.DF612AFE367A7B31410F2F6CD3C7B515B0F1889C0107EA695D840DFFA492E07D.svg","../dist/a/b/c/d/s/d/svg/2.svg":"./prod/a/b/c/d/s/d/svg/2.DF612AFE367A7B31410F2F6CD3C7B515B0F1889C0107EA695D840DFFA492E07D.svg","../dist/svg/home.svg":"./prod/svg/home.28C26C2D3E4013D24D755A589A80D8DD5C49DA5397032E3F09B76BC3A2C314ED.svg","../dist/a/b/c/d/s/d/svg/home.svg":"./prod/a/b/c/d/s/d/svg/home.28C26C2D3E4013D24D755A589A80D8DD5C49DA5397032E3F09B76BC3A2C314ED.svg","../dist/a/b/c/d/s/d/svg/settings.svg":"./prod/a/b/c/d/s/d/svg/settings.910C6241743C9C694141971BE8E1C4016A1A5BF203E4E9D676D4CE93BD518F4C.svg","../dist/a/b/c/d/s/d/svg/help-circle.svg":"./prod/a/b/c/d/s/d/svg/help-circle.BE230ABD2E05EB05EF6C5B7D04D35A3A43637EF1E046DEF3D244425609B99F81.svg","../dist/log-out.svg":"./prod/log-out.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.svg","../dist/filter.svg":"./prod/filter.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.svg","../dist/a/b/c/d/s/d/svg/toggle-right.svg":"./prod/a/b/c/d/s/d/svg/toggle-right.15BF49887941593CE3FE09FA73E3CAF1F4B1E8ABCB42A23D85B4FCBC24FDF5AA.svg","../dist/a/b/c/d/s/d/svg/8.svg":"./prod/a/b/c/d/s/d/svg/8.DF612AFE367A7B31410F2F6CD3C7B515B0F1889C0107EA695D840DFFA492E07D.svg","../dist/credit-card.svg":"./prod/credit-card.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.svg","../dist/a/b/c/d/s/d/svg/file-text.svg":"./prod/a/b/c/d/s/d/svg/file-text.CF57DF252051E7E81C240D36AF1DB8A9DDAF282F9A5E8C338408FE88A6545A02.svg","../dist/menu.svg":"./prod/menu.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.svg","../dist/a/b/c/d/s/d/svg/3.svg":"./prod/a/b/c/d/s/d/svg/3.DF612AFE367A7B31410F2F6CD3C7B515B0F1889C0107EA695D840DFFA492E07D.svg","../dist/svg/shield-off.svg":"./prod/svg/shield-off.85394A6AD92D550F8EBA72AAB095E078E7A0E3359DF81174532C8D1AF53B5876.svg","../dist/a/b/c/d/s/d/svg/user.svg":"./prod/a/b/c/d/s/d/svg/user.B164ECD2C4A09DC5189F1F252487E2AC6A33646BEA67AF9C528CDA61FE5E146F.svg","../dist/a/b/c/d/s/d/svg/shield.svg":"./prod/a/b/c/d/s/d/svg/shield.13AFE15DCB4882B4A940CFDC3E2088A733CD4E6F97F25B211D87C7C9D6DBA2B6.svg","../dist/svg/file-text.svg":"./prod/svg/file-text.CF57DF252051E7E81C240D36AF1DB8A9DDAF282F9A5E8C338408FE88A6545A02.svg","../dist/eye-off.svg":"./prod/eye-off.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.svg","../dist/svg/eye.svg":"./prod/svg/eye.9DE4D24D3C9B055D02B94A8AD65E8C0C644852381FDD131A64448B6DA7859167.svg","../dist/toggle-right.svg":"./prod/toggle-right.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.svg","../dist/moon.svg":"./prod/moon.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.svg","../dist/toggle-left.svg":"./prod/toggle-left.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.svg","../dist/svg/github.svg":"./prod/svg/github.FA9EB1C66F548EC2C7598B94BA6A17275E1EA383D42B6C83351A2388C773E621.svg","../dist/a/b/c/d/s/d/svg/4.svg":"./prod/a/b/c/d/s/d/svg/4.DF612AFE367A7B31410F2F6CD3C7B515B0F1889C0107EA695D840DFFA492E07D.svg","../dist/svg/help-circle.svg":"./prod/svg/help-circle.BE230ABD2E05EB05EF6C5B7D04D35A3A43637EF1E046DEF3D244425609B99F81.svg","../dist/a/b/c/d/s/d/svg/6.svg":"./prod/a/b/c/d/s/d/svg/6.DF612AFE367A7B31410F2F6CD3C7B515B0F1889C0107EA695D840DFFA492E07D.svg","../dist/globe.svg":"./prod/globe.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.svg","../dist/svg/toggle-left.svg":"./prod/svg/toggle-left.E421950C5922E84015F0A86F272AE5637A2ED96E267D2C962543F5994E5D1172.svg","../dist/settings.svg":"./prod/settings.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.svg","../dist/a/b/c/d/s/d/svg/credit-card.svg":"./prod/a/b/c/d/s/d/svg/credit-card.DF612AFE367A7B31410F2F6CD3C7B515B0F1889C0107EA695D840DFFA492E07D.svg","../dist/svg/toggle-right.svg":"./prod/svg/toggle-right.15BF49887941593CE3FE09FA73E3CAF1F4B1E8ABCB42A23D85B4FCBC24FDF5AA.svg","../dist/home.svg":"./prod/home.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.svg","../dist/svg/globe.svg":"./prod/svg/globe.44C2A069EBD637663E938ECE7B8E4EC2A8BDE049A8A044EC68D9CB69AE8C592E.svg","../dist/github.svg":"./prod/github.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.svg","../dist/eye.svg":"./prod/eye.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.svg","../dist/user.svg":"./prod/user.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.svg","../dist/svg/file.svg":"./prod/svg/file.F9E376D9F78FFD918D8D592A8B2D97EAAC14E638B5A7AE3C58DDB075375D8E0E.svg","../dist/help-circle.svg":"./prod/help-circle.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.svg","../dist/svg/credit-card.svg":"./prod/svg/credit-card.DF612AFE367A7B31410F2F6CD3C7B515B0F1889C0107EA695D840DFFA492E07D.svg","../dist/a/b/c/d/s/d/svg/github.svg":"./prod/a/b/c/d/s/d/svg/github.FA9EB1C66F548EC2C7598B94BA6A17275E1EA383D42B6C83351A2388C773E621.svg","../dist/svg/filter.svg":"./prod/svg/filter.6D5FBD96BA2E2020663AAC4994A991295917D73F3592C07EE103647B655A2275.svg","../dist/svg/eye-off.svg":"./prod/svg/eye-off.939360B335D1D35B57C3E2070129D14ABB168E4AC137B5BE4F6F8BD450B712F5.svg","../dist/a/b/c/d/s/d/svg/1.svg":"./prod/a/b/c/d/s/d/svg/1.DF612AFE367A7B31410F2F6CD3C7B515B0F1889C0107EA695D840DFFA492E07D.svg","../dist/a/b/c/d/s/d/svg/file.svg":"./prod/a/b/c/d/s/d/svg/file.F9E376D9F78FFD918D8D592A8B2D97EAAC14E638B5A7AE3C58DDB075375D8E0E.svg","../dist/icon.png":"./prod/icon.99378E293369ED6C5DC48A825BFF345A442988AEAFD48CC5EA399EDC87A3CBFA.png","../dist/a/b/c/d/s/d/svg/9.svg":"./prod/a/b/c/d/s/d/svg/9.DF612AFE367A7B31410F2F6CD3C7B515B0F1889C0107EA695D840DFFA492E07D.svg","../dist/svg/user.svg":"./prod/svg/user.B164ECD2C4A09DC5189F1F252487E2AC6A33646BEA67AF9C528CDA61FE5E146F.svg"},"base_dir":"./prod"} \ No newline at end of file diff --git a/actix-example/src/main.rs b/actix-example/src/main.rs index 33f4bda..4a229f6 100644 --- a/actix-example/src/main.rs +++ b/actix-example/src/main.rs @@ -1,7 +1,8 @@ use cache_buster::Files; +use cache_buster::CACHE_BUSTER_DATA_FILE; fn main() { - let files = Files::new(); + let files = Files::new(CACHE_BUSTER_DATA_FILE); assert!(get_full_path_runner("../dist/log-out.svg", &files)); assert!(get_full_path_runner( diff --git a/src/filemap.rs b/src/filemap.rs index 21e0081..1869c5a 100644 --- a/src/filemap.rs +++ b/src/filemap.rs @@ -10,19 +10,17 @@ //! //! ```no_run //! use cache_buster::Files; +//! use cache_buster::CACHE_BUSTER_DATA_FILE; //! //! fn main(){ -//! let files = Files::new(); +//! let files = Files::new(CACHE_BUSTER_DATA_FILE); //! } //! ``` use std::collections::HashMap; -use std::env; use serde::{Deserialize, Serialize}; -const ENV_VAR_NAME: &str = "CACHE_BUSTER_FILE_MAP"; - /// Filemap struct /// /// maps original names to generated names @@ -35,10 +33,8 @@ pub struct Files { impl Files { /// Load filemap in main program. Should be called from main program - pub fn new() -> Self { - let env = env::var(ENV_VAR_NAME) - .expect("unable to read env var, might be a bug in lib. Please report on GitHub"); - let res: Files = serde_json::from_str(&env).unwrap(); + pub fn new(map: &str) -> Self { + let res: Files = serde_json::from_str(&map).unwrap(); res } @@ -48,7 +44,8 @@ impl Files { /// output `/test.randomhash.svg`. For full path, see [get_full_path][Self::get_full_path]. 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()..]) + //Some(&path[self.base_dir.len()..]) + Some(&path) } else { None } @@ -65,14 +62,17 @@ impl Files { #[cfg(test)] mod tests { - use crate::processor::tests::cleanup; + use std::fs; + + use crate::processor::tests::{cleanup, delete_file, runner as processor_runner}; use crate::processor::*; + use crate::CACHE_BUSTER_DATA_FILE; use super::*; use std::path::Path; - #[test] fn get_full_path_works() { + delete_file(); let types = vec![ mime::IMAGE_PNG, mime::IMAGE_SVG, @@ -82,7 +82,7 @@ mod tests { let config = BusterBuilder::default() .source("./dist") - .result("/tmp/prod2") + .result("/tmp/prodsd2") .mime_types(types) .copy(true) .follow_links(true) @@ -91,7 +91,9 @@ mod tests { config.process().unwrap(); - let files = Files::new(); + let map = fs::read_to_string(CACHE_BUSTER_DATA_FILE).unwrap(); + let files = Files::new(&map); + 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", @@ -114,8 +116,8 @@ mod tests { } } - #[test] fn get_works() { + delete_file(); let types = vec![ mime::IMAGE_PNG, mime::IMAGE_SVG, @@ -134,7 +136,8 @@ mod tests { config.process().unwrap(); - let files = Files::new(); + let map = fs::read_to_string(CACHE_BUSTER_DATA_FILE).unwrap(); + let files = Files::new(&map); assert!(get_runner("./dist/log-out.svg", &files)); assert!(get_runner("./dist/a/b/c/d/s/d/svg/credit-card.svg", &files)); @@ -146,10 +149,19 @@ mod tests { 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..]); + // let path = Path::new(&files.base_dir).join(&file[1..]); + println!("{}", &file); + let path = Path::new(&file); path.exists() } else { false } } + + #[test] + pub fn runner() { + get_works(); + get_full_path_works(); + processor_runner(); + } } diff --git a/src/lib.rs b/src/lib.rs index 958e526..0e471bd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,5 +36,6 @@ pub use processor::BusterBuilder; pub mod filemap; pub use filemap::Files; -/// env var to which filemap is written during compilation -pub const ENV_VAR_NAME: &str = "CACHE_BUSTER_FILE_MAP"; +/// file to which filemap is written during compilation +/// include this to `.gitignore` +pub const CACHE_BUSTER_DATA_FILE: &str = "./src/cache_buster_data.json"; diff --git a/src/processor.rs b/src/processor.rs index 998fd05..21c334e 100644 --- a/src/processor.rs +++ b/src/processor.rs @@ -47,7 +47,7 @@ use derive_builder::Builder; use serde::{Deserialize, Serialize}; use walkdir::WalkDir; -use crate::ENV_VAR_NAME; +use crate::*; /// Configuration for setting up cache-busting #[derive(Debug, Clone, Builder)] @@ -77,7 +77,6 @@ impl Buster { fs::remove_dir_all(&self.result).unwrap(); } - println!("{}", &self.result); fs::create_dir(&self.result).unwrap(); self.create_dir_structure(Path::new(&self.source))?; Ok(()) @@ -93,53 +92,7 @@ impl Buster { /// Processes files. /// - /// If MIME types are uncommon, then use this funtion - /// as it won't panic when a weird MIM is encountered. - /// - /// Otherwise, use [process][Self::process] - /// - /// Note: it omits processing uncommon MIME types - pub fn try_process(&self) -> Result<(), Error> { - self.init()?; - let mut file_map: Files = Files::new(&self.result); - for entry in WalkDir::new(&self.source) - .follow_links(self.follow_links) - .into_iter() - { - let entry = entry?; - let path = entry.path(); - let path = Path::new(&path); - - for mime_type in self.mime_types.iter() { - if let Some(file_mime) = mime_guess::from_path(path).first() { - if &file_mime == mime_type { - let contents = Self::read_to_string(&path).unwrap(); - let hash = Self::hasher(&contents); - let new_name = format!( - "{}.{}.{}", - path.file_stem().unwrap().to_str().unwrap(), - hash, - path.extension().unwrap().to_str().unwrap() - ); - self.copy(path, &new_name); - - let (source, destination) = self.gen_map(path, &&new_name); - let _ = file_map.add( - source.to_str().unwrap().into(), - destination.to_str().unwrap().into(), - ); - } - } - } - } - file_map.to_env(); - Ok(()) - } - - /// Processes files. - /// - /// If MIME types are common, then use this funtion - /// as it will panic when a weird MIM is encountered. + /// Panics when a weird MIME is encountered. pub fn process(&self) -> Result<(), Error> { // panics when mimetypes are detected. This way you'll know which files are ignored // from processing @@ -206,7 +159,7 @@ impl Buster { result = &self.result[1..]; } let destination = Path::new(prefix) - .join(&self.result[1..]) + .join(&result) .join(rel_location) .join(name); @@ -281,25 +234,33 @@ impl Files { /// This crate uses compile-time environment variables to transfer /// data to the main program. This funtction sets that variable fn to_env(&self) { - println!( - "cargo:rustc-env={}={}", - ENV_VAR_NAME, - serde_json::to_string(&self).unwrap() - ); + let json = serde_json::to_string(&self).unwrap(); + // println!("cargo:rustc-env={}={}", ENV_VAR_NAME, json); + let res = Path::new(CACHE_BUSTER_DATA_FILE); + if res.exists() { + fs::remove_file(&res).unwrap(); + } + + // const PREFIX: &str = r##"pub const FILE_MAP: &str = r#" "##; + // const POSTFIX: &str = r##""#;"##; + + // let content = format!("#[allow(dead_code)]\n{}{}{}", &PREFIX, &json, &POSTFIX); + + // fs::write(CACHE_BUSTER_DATA_FILE, content).unwrap(); + fs::write(CACHE_BUSTER_DATA_FILE, &json).unwrap(); // needed for testing load() // if the above statement fails(println), then something's broken // with the rust compiler. So not really worried about that. - #[cfg(test)] - std::env::set_var(ENV_VAR_NAME, serde_json::to_string(&self).unwrap()); + // #[cfg(test)] + // std::env::set_var(ENV_VAR_NAME, serde_json::to_string(&self).unwrap()); } #[cfg(test)] /// Load filemap in main program. Should be called from main program fn load() -> Self { - let env = std::env::var(ENV_VAR_NAME) - .expect("unable to read env var, might be a bug in lib. Please report on GitHub"); - let res: Files = serde_json::from_str(&env).unwrap(); + let map = fs::read_to_string(CACHE_BUSTER_DATA_FILE).unwrap(); + let res: Files = serde_json::from_str(&map).unwrap(); res } } @@ -308,8 +269,8 @@ impl Files { pub mod tests { use super::*; - #[test] fn hasher_works() { + delete_file(); let types = vec![ mime::IMAGE_PNG, mime::IMAGE_SVG, @@ -339,42 +300,17 @@ pub mod tests { cleanup(&config); } - #[test] - fn try_process_works() { - let types = vec![ - mime::IMAGE_PNG, - mime::IMAGE_SVG, - mime::IMAGE_JPEG, - mime::IMAGE_GIF, - ]; - - let config = BusterBuilder::default() - .source("./dist") - .result("/tmp/prod") - .mime_types(types) - .copy(true) - .follow_links(true) - .build() - .unwrap(); - - config.process().unwrap(); - let mut files = Files::load(); - - for (k, v) in files.map.drain() { - let src = Path::new(&k); - let dest = Path::new(&v); - - assert_eq!(src.exists(), dest.exists()); - } - - cleanup(&config); - } - pub fn cleanup(config: &Buster) { let _ = fs::remove_dir_all(&config.result); + delete_file(); } - #[test] + + pub fn delete_file() { + let _ = fs::remove_file(&CACHE_BUSTER_DATA_FILE); + } + fn prefix_works() { + delete_file(); let types = vec![ mime::IMAGE_PNG, mime::IMAGE_SVG, @@ -398,7 +334,6 @@ pub mod tests { if let Some(prefix) = &config.prefix { for (k, v) in files.map.drain() { let src = Path::new(&k); - println!("{}", &v); let dest = Path::new(&v[prefix.len()..]); assert_eq!(src.exists(), dest.exists()); @@ -407,4 +342,9 @@ pub mod tests { cleanup(&config); } + + pub fn runner() { + prefix_works(); + hasher_works(); + } }