diff --git a/.gitlab/CI/check.gitlab-ci.yml b/.gitlab/CI/check.gitlab-ci.yml index 8f465e1be7..60f0e8425f 100644 --- a/.gitlab/CI/check.gitlab-ci.yml +++ b/.gitlab/CI/check.gitlab-ci.yml @@ -6,7 +6,7 @@ code-quality: script: - ln -s /dockercache/target target - rm -r target/debug/incremental/* || echo "all good" # TMP FIX FOR 2021-03-22-nightly - - cargo clippy --all-targets --locked --features="bin_csv,bin_bot" -- -D warnings + - cargo clippy --all-targets --locked --features="bin_csv,bin_bot,asset_tweak" -- -D warnings - cargo fmt --all -- --check security: diff --git a/common/assets/Cargo.toml b/common/assets/Cargo.toml index edb60a77fc..a3e40b43f8 100644 --- a/common/assets/Cargo.toml +++ b/common/assets/Cargo.toml @@ -14,5 +14,7 @@ dot_vox = "4.0" image = { version = "0.23.12", default-features = false, features = ["png"] } tracing = "0.1" -[dev-dependencies] -serial_test = "0.5" +serial_test = {version = "0.5", optional = true} + +[features] +asset_tweak = ["serial_test"] diff --git a/common/assets/src/lib.rs b/common/assets/src/lib.rs index 4d35b3318a..65c679eaf1 100644 --- a/common/assets/src/lib.rs +++ b/common/assets/src/lib.rs @@ -261,6 +261,7 @@ impl Compound for Directory { } #[warn(clippy::pedantic)] +#[cfg(feature = "asset_tweak")] pub mod asset_tweak { // Return path to repository by searching 10 directories back use super::{find_root, fs, Asset, AssetExt, Path, RonLoader}; @@ -280,8 +281,6 @@ pub mod asset_tweak { } #[must_use] - /// NOTE: Don't use it in production code, it's debug only - /// /// # Usage /// Create file with content which represent tweaked value /// @@ -299,13 +298,11 @@ pub mod asset_tweak { /// /// # Panics /// 1) If given `asset_specifier` does not exists + /// 2) If asseet is broken pub fn tweak_expect(specifier: &str) -> T where T: Clone + Sized + Send + Sync + 'static + DeserializeOwned, { - if cfg!(not(any(debug_assertions, test))) { - tracing::warn!("AssetTweaker used in release build!"); - } let asset_specifier: &str = &format!("tweak.{}", specifier); let handle = as AssetExt>::load_expect(asset_specifier); let AssetTweakWrapper(value) = handle.read().clone(); @@ -313,11 +310,13 @@ pub mod asset_tweak { } #[must_use] - /// NOTE: Don't use it in production code, it's debug only - /// /// # Usage - /// Will create file "assets/tweak/{specifier}.ron" if not exists. - /// If exists will read a value from such file. + /// Will create file "assets/tweak/{specifier}.ron" if not exists + /// and return passed `value`. + /// If file exists will read a value from such file. + /// + /// In release builds (if `debug_assertions` == false) just returns passed + /// `value` /// /// Example if you want to tweak integer value /// ```no_run @@ -338,8 +337,7 @@ pub mod asset_tweak { where T: Clone + Sized + Send + Sync + 'static + DeserializeOwned + Serialize, { - if cfg!(not(any(debug_assertions, test))) { - tracing::warn!("AssetTweaker used in release build!"); + if cfg!(not(debug_assertions)) { return value; } @@ -365,143 +363,140 @@ pub mod asset_tweak { value } } -} -#[cfg(test)] -mod tests { - use super::{ - asset_tweak::{tweak_expect, tweak_expect_or_create}, - find_root, - }; - use serial_test::serial; - use std::{ - convert::AsRef, - fmt::Debug, - fs::{self, File}, - io::Write, - path::Path, - }; + #[cfg(test)] + mod tests { + use super::{find_root, tweak_expect, tweak_expect_or_create}; + use serial_test::serial; + use std::{ + convert::AsRef, + fmt::Debug, + fs::{self, File}, + io::Write, + path::Path, + }; - struct DirectoryGuard

- where - P: AsRef, - { - dir: P, - } - - impl

DirectoryGuard

- where - P: AsRef, - { - fn create(dir: P) -> Self { - fs::create_dir_all(&dir).expect("failed to create directory"); - Self { dir } - } - } - - impl

Drop for DirectoryGuard

- where - P: AsRef, - { - fn drop(&mut self) { fs::remove_dir(&self.dir).expect("failed to remove directory"); } - } - - struct FileGuard

- where - P: AsRef + Debug, - { - file: P, - } - - impl

FileGuard

- where - P: AsRef + Debug, - { - fn create(file: P) -> (Self, File) { - let f = - File::create(&file).unwrap_or_else(|_| panic!("failed to create file {:?}", &file)); - (Self { file }, f) + struct DirectoryGuard

+ where + P: AsRef, + { + dir: P, } - fn hold(file: P) -> Self { Self { file } } - } - - impl

Drop for FileGuard

- where - P: AsRef + Debug, - { - fn drop(&mut self) { - fs::remove_file(&self.file) - .unwrap_or_else(|_| panic!("failed to create file {:?}", &self.file)); + impl

DirectoryGuard

+ where + P: AsRef, + { + fn create(dir: P) -> Self { + fs::create_dir_all(&dir).expect("failed to create directory"); + Self { dir } + } } - } - #[test] - #[serial] - fn test_tweaked_string() { - let root = find_root().expect("failed to discover repository_root"); - let tweak_dir = root.join("assets/tweak/"); - let _dir_guard = DirectoryGuard::create(tweak_dir.clone()); + impl

Drop for DirectoryGuard

+ where + P: AsRef, + { + fn drop(&mut self) { fs::remove_dir(&self.dir).expect("failed to remove directory"); } + } - // define test files - let from_int = tweak_dir.join("__test_int_tweak.ron"); - let from_string = tweak_dir.join("__test_string_tweak.ron"); - let from_map = tweak_dir.join("__test_map_tweak.ron"); + struct FileGuard

+ where + P: AsRef + Debug, + { + file: P, + } - // setup fs guards - let (_file_guard1, mut file1) = FileGuard::create(from_int); - let (_file_guard2, mut file2) = FileGuard::create(from_string); - let (_file_guard3, mut file3) = FileGuard::create(from_map); + impl

FileGuard

+ where + P: AsRef + Debug, + { + fn create(file: P) -> (Self, File) { + let f = File::create(&file) + .unwrap_or_else(|_| panic!("failed to create file {:?}", &file)); + (Self { file }, f) + } - // write to file and check result - file1 - .write_all(b"(5)") - .expect("failed to write to the file"); - let x = tweak_expect::("__test_int_tweak"); - assert_eq!(x, 5); + fn hold(file: P) -> Self { Self { file } } + } - // write to file and check result - file2 - .write_all(br#"("Hello Zest")"#) - .expect("failed to write to the file"); - let x = tweak_expect::("__test_string_tweak"); - assert_eq!(x, "Hello Zest".to_owned()); + impl

Drop for FileGuard

+ where + P: AsRef + Debug, + { + fn drop(&mut self) { + fs::remove_file(&self.file) + .unwrap_or_else(|_| panic!("failed to create file {:?}", &self.file)); + } + } - // write to file and check result - file3 - .write_all( - br#" + #[test] + #[serial] + fn test_tweaked_string() { + let root = find_root().expect("failed to discover repository_root"); + let tweak_dir = root.join("assets/tweak/"); + let _dir_guard = DirectoryGuard::create(tweak_dir.clone()); + + // define test files + let from_int = tweak_dir.join("__test_int_tweak.ron"); + let from_string = tweak_dir.join("__test_string_tweak.ron"); + let from_map = tweak_dir.join("__test_map_tweak.ron"); + + // setup fs guards + let (_file_guard1, mut file1) = FileGuard::create(from_int); + let (_file_guard2, mut file2) = FileGuard::create(from_string); + let (_file_guard3, mut file3) = FileGuard::create(from_map); + + // write to file and check result + file1 + .write_all(b"(5)") + .expect("failed to write to the file"); + let x = tweak_expect::("__test_int_tweak"); + assert_eq!(x, 5); + + // write to file and check result + file2 + .write_all(br#"("Hello Zest")"#) + .expect("failed to write to the file"); + let x = tweak_expect::("__test_string_tweak"); + assert_eq!(x, "Hello Zest".to_owned()); + + // write to file and check result + file3 + .write_all( + br#" ({ "wow": 4, "such": 5, }) "#, - ) - .expect("failed to write to the file"); - let x: std::collections::HashMap = tweak_expect("__test_map_tweak"); - let mut map = std::collections::HashMap::new(); - map.insert("wow".to_owned(), 4); - map.insert("such".to_owned(), 5); - assert_eq!(x, map); - } + ) + .expect("failed to write to the file"); + let x: std::collections::HashMap = tweak_expect("__test_map_tweak"); + let mut map = std::collections::HashMap::new(); + map.insert("wow".to_owned(), 4); + map.insert("such".to_owned(), 5); + assert_eq!(x, map); + } - #[test] - #[serial] - fn test_tweaked_create() { - let root = find_root().expect("failed to discover repository_root"); - let tweak_dir = root.join("assets/tweak/"); - let test_path1 = tweak_dir.join("__test_int_create.ron"); - let _file_guard1 = FileGuard::hold(&test_path1); - let x = tweak_expect_or_create("__test_int_create", 5); - assert_eq!(x, 5); - assert!(test_path1.is_file()); + #[test] + #[serial] + fn test_tweaked_create() { + let root = find_root().expect("failed to discover repository_root"); + let tweak_dir = root.join("assets/tweak/"); + let test_path1 = tweak_dir.join("__test_int_create.ron"); + let _file_guard1 = FileGuard::hold(&test_path1); + let x = tweak_expect_or_create("__test_int_create", 5); + assert_eq!(x, 5); + assert!(test_path1.is_file()); - // Test that file has stronger priority - let test_path2 = tweak_dir.join("__test_priority.ron"); - let (_file_guard2, mut file) = FileGuard::create(&test_path2); - file.write_all(b"(10)") - .expect("failed to write to the file"); - let x = tweak_expect_or_create("__test_priority", 6); - assert_eq!(x, 10); + // Test that file has stronger priority + let test_path2 = tweak_dir.join("__test_priority.ron"); + let (_file_guard2, mut file) = FileGuard::create(&test_path2); + file.write_all(b"(10)") + .expect("failed to write to the file"); + let x = tweak_expect_or_create("__test_priority", 6); + assert_eq!(x, 10); + } } }