From df428a99e92fdf9fc6f7d4fbb334848fbb488cf3 Mon Sep 17 00:00:00 2001 From: Barsik Date: Mon, 8 Apr 2024 13:14:41 +0300 Subject: [PATCH] Update and refactor publish_need test cases in module entity. This commit removes the existing 'publish_need.rs' file and introduces the 'diff.rs' file into the 'entity' module, containing updated and more accurate test cases. Also, unnecessary import statements were removed from several module files, improving the overall code cleanliness. --- .../action/readme_modules_headers_renew.rs | 2 +- module/move/willbe/src/entity/features.rs | 1 - module/move/willbe/src/entity/manifest.rs | 17 ++- module/move/willbe/tests/inc/entity/diff.rs | 98 +++++++++++++ module/move/willbe/tests/inc/entity/mod.rs | 7 +- .../willbe/tests/inc/entity/publish_need.rs | 134 ------------------ module/move/willbe/tests/inc/package.rs | 18 +-- 7 files changed, 126 insertions(+), 151 deletions(-) create mode 100644 module/move/willbe/tests/inc/entity/diff.rs delete mode 100644 module/move/willbe/tests/inc/entity/publish_need.rs diff --git a/module/move/willbe/src/action/readme_modules_headers_renew.rs b/module/move/willbe/src/action/readme_modules_headers_renew.rs index 39237fcb12..2be1eab6a8 100644 --- a/module/move/willbe/src/action/readme_modules_headers_renew.rs +++ b/module/move/willbe/src/action/readme_modules_headers_renew.rs @@ -12,7 +12,7 @@ mod private use std::borrow::Cow; use std::fs::{ OpenOptions }; use std::io::{ Read, Seek, SeekFrom, Write }; - use std::path::{Path, PathBuf}; + use std::path::PathBuf; use convert_case::{ Case, Casing }; use regex::Regex; use crate::action::readme_health_table_renew::find_example_file; diff --git a/module/move/willbe/src/entity/features.rs b/module/move/willbe/src/entity/features.rs index a292b131b1..81c1452180 100644 --- a/module/move/willbe/src/entity/features.rs +++ b/module/move/willbe/src/entity/features.rs @@ -2,7 +2,6 @@ mod private { use crate::*; use std::collections::{ BTreeSet, HashSet }; - use error_tools::err; // aaa : for Petro : don't use cargo_metadata and Package directly, use facade // aaa : ✅ use error_tools::for_app::{ bail, Result }; diff --git a/module/move/willbe/src/entity/manifest.rs b/module/move/willbe/src/entity/manifest.rs index 619a5bab8c..0b5405bf48 100644 --- a/module/move/willbe/src/entity/manifest.rs +++ b/module/move/willbe/src/entity/manifest.rs @@ -7,7 +7,7 @@ pub( crate ) mod private { io::{ self, Read }, fs, - path::Path, + path::{ Path, PathBuf }, }; use wtools::error:: { @@ -53,6 +53,21 @@ pub( crate ) mod private Ok( Self( crate_dir_path ) ) } } + + impl TryFrom< PathBuf > for CrateDir + { + type Error = CrateDirError; + + fn try_from( crate_dir_path : PathBuf ) -> Result< Self, Self::Error > + { + if !crate_dir_path.join( "Cargo.toml" ).exists() + { + return Err( CrateDirError::Validation( "The path is not a crate directory path".into() ) ); + } + + Ok( Self( AbsolutePath::try_from( crate_dir_path ).unwrap() ) ) + } + } impl CrateDir { diff --git a/module/move/willbe/tests/inc/entity/diff.rs b/module/move/willbe/tests/inc/entity/diff.rs new file mode 100644 index 0000000000..b3362ee64d --- /dev/null +++ b/module/move/willbe/tests/inc/entity/diff.rs @@ -0,0 +1,98 @@ +use crate::*; + +use std::path::{ Path, PathBuf }; +use assert_fs::{ TempDir, prelude::* }; +use crates_tools::CrateArchive; +use the_module::*; +use _path::AbsolutePath; +use package::Package; +use diff::crate_diff; +use the_module::version::{ Version, BumpOptions, version_bump }; + +const TEST_MODULE_PATH : &str = "../../test/"; + +#[ test ] +fn no_changes() +{ + let tmp = &TempDir::new().unwrap(); + let package_path = package_path( "c" ); + + let left = prepare( tmp, "left", &package_path ); + let left_crate = crate_file_path( &left ); + let left_archive = CrateArchive::read( &left_crate ).unwrap(); + + let right = prepare( tmp, "right", &package_path ); + let right_crate = crate_file_path( &right ); + let right_archive = CrateArchive::read( &right_crate ).unwrap(); + + let has_changes = crate_diff( &left_archive, &right_archive ).exclude( diff::PUBLISH_IGNORE_LIST ).has_changes(); + + assert!( !has_changes ); +} + +#[ test ] +fn with_changes() +{ + let tmp = &TempDir::new().unwrap(); + let package_path = package_path( "c" ); + + let left = + { + let left = prepare( tmp, "left", &package_path ); + let left_crate = crate_file_path( &left ); + CrateArchive::read( &left_crate ).unwrap() + }; + + let right = + { + let right = prepare( tmp, "right", &package_path ); + + let absolute = AbsolutePath::try_from( right.as_path() ).unwrap(); + let right_package = Package::try_from( absolute ).unwrap(); + let right_version = Version::try_from( &right_package.version().unwrap() ).unwrap(); + + let bump_options = BumpOptions + { + crate_dir : CrateDir::try_from( right.clone() ).unwrap(), + old_version : right_version.clone(), + new_version : right_version.bump(), + dependencies : vec![], + dry : false, + }; + version_bump( bump_options ).unwrap(); + + let right_crate = crate_file_path( &right ); + CrateArchive::read( &right_crate ).unwrap() + }; + + let has_changes = crate_diff( &left, &right ).exclude( diff::PUBLISH_IGNORE_LIST ).has_changes(); + + assert!( has_changes ); +} + +fn package_path< P : AsRef< Path > >( path : P ) -> PathBuf +{ + let root_path = Path::new( env!( "CARGO_MANIFEST_DIR" ) ).join( TEST_MODULE_PATH ); + root_path.join( path ) +} + +fn prepare( tmp : &TempDir, name : &str, manifest_dir_path : &Path ) -> PathBuf +{ + let dir = tmp.child( name ); + dir.create_dir_all().unwrap(); + dir.copy_from( manifest_dir_path, &[ "**" ] ).unwrap(); + + dir.to_path_buf() +} + +fn crate_file_path( manifest_dir_path : &Path ) -> PathBuf +{ + _ = cargo::pack( cargo::PackOptions::former().path( manifest_dir_path ).dry( false ).form() ).expect( "Failed to package a package" ); + + let absolute = AbsolutePath::try_from( manifest_dir_path ).unwrap(); + let package = Package::try_from( absolute ).unwrap(); + manifest_dir_path + .join( "target" ) + .join( "package" ) + .join( format!( "{}-{}.crate", package.name().unwrap(), package.version().unwrap() ) ) +} diff --git a/module/move/willbe/tests/inc/entity/mod.rs b/module/move/willbe/tests/inc/entity/mod.rs index d86862bb87..58ee035a97 100644 --- a/module/move/willbe/tests/inc/entity/mod.rs +++ b/module/move/willbe/tests/inc/entity/mod.rs @@ -1,9 +1,6 @@ use super::*; +pub mod dependencies; +pub mod diff; pub mod features; - pub mod version; - -pub mod publish_need; - -pub mod dependencies; \ No newline at end of file diff --git a/module/move/willbe/tests/inc/entity/publish_need.rs b/module/move/willbe/tests/inc/entity/publish_need.rs deleted file mode 100644 index 59f4a97828..0000000000 --- a/module/move/willbe/tests/inc/entity/publish_need.rs +++ /dev/null @@ -1,134 +0,0 @@ -use super::*; - -use std:: -{ - io::Write, - path::{ Path, PathBuf }, -}; - -use assert_fs::prelude::*; -use the_module:: -{ - package::{ publish_need, Package }, - _path::AbsolutePath, - manifest, - version, - cargo -}; - -const TEST_MODULE_PATH : &str = "../../test/"; - -fn package_path< P : AsRef< Path > >( path : P ) -> PathBuf -{ - let root_path = Path::new( env!( "CARGO_MANIFEST_DIR" ) ).join( TEST_MODULE_PATH ); - root_path.join( path ) -} - -fn package< P : AsRef< Path > >( path : P ) -> Package -{ - let path = path.as_ref(); - _ = cargo::pack( cargo::PackOptions::former().path( path.to_path_buf() ).dry( false ).form() ).expect( "Failed to package a package" ); - let absolute = AbsolutePath::try_from( path ).unwrap(); - - Package::try_from( absolute ).unwrap() -} - -// published the same as local -#[ test ] -fn no_changes() -{ - // Arrange - // aaa : for Bohdan : make helper function returning package_path. reuse it for all relevant tests - // aaa : use `package_path` function - let package_path = package_path( "c" ); - - _ = cargo::pack( cargo::PackOptions::former().path( package_path.clone() ).dry( false ).form() ).expect( "Failed to package a package" ); - let absolute = AbsolutePath::try_from( package_path ).unwrap(); - let package = Package::try_from( absolute ).unwrap(); - - // Act - let publish_needed = publish_need( &package, None ).unwrap(); - - // Assert - assert!( !publish_needed ); -} - -// version bumped => publish required -#[ test ] -fn with_changes() -{ - // Arrange - let package_path = package_path( "c" ); - - let temp = assert_fs::TempDir::new().unwrap(); - temp.copy_from( &package_path, &[ "**" ] ).unwrap(); - - let absolute = AbsolutePath::try_from( temp.as_ref() ).unwrap(); - let mut manifest = manifest::open( absolute ).unwrap(); - version::bump( &mut manifest, false ).unwrap(); - - _ = cargo::pack( cargo::PackOptions::former().path( temp.path().to_path_buf() ).dry( false ).form() ).expect( "Failed to package a package" ); - - let absolute = AbsolutePath::try_from( temp.as_ref() ).unwrap(); - let package = Package::try_from( absolute ).unwrap(); - - // Act - let publish_needed = publish_need( &package, None ).unwrap(); - - // Assert - assert!( publish_needed ); -} - -// c(update) -> b(re-publish) -> a(re-publish) -#[ test ] -fn cascade_with_changes() -{ - let abc = [ "a", "b", "c" ].into_iter().map( package_path ).map( package ).collect::< Vec< _ > >(); - let [ a, b, c ] = abc.as_slice() else { unreachable!() }; - if ![ c, b, a ].into_iter().inspect( | x | { dbg!( x.name().unwrap() ); } ).map( | a | publish_need( a, None ) ).inspect( | x | { dbg!(x); } ).all( | p | !p.expect( "There was an error verifying whether the package needs publishing or not" ) ) - { - panic!( "The packages must be up-to-dated" ); - } - let temp = assert_fs::TempDir::new().unwrap(); - let temp_module = temp.child( "module" ); - std::fs::create_dir( &temp_module ).unwrap(); - temp_module.child( "a" ).copy_from( a.manifest_path().parent().unwrap(), &[ "**" ] ).unwrap(); - temp_module.child( "b" ).copy_from( b.manifest_path().parent().unwrap(), &[ "**" ] ).unwrap(); - temp_module.child( "c" ).copy_from( c.manifest_path().parent().unwrap(), &[ "**" ] ).unwrap(); - let a_temp_path = temp_module.join( "a" ); - let b_temp_path = temp_module.join( "b" ); - let c_temp_path = temp_module.join( "c" ); - - let mut cargo_toml = std::fs::File::create( temp.join( "Cargo.toml" ) ).unwrap(); - write!( cargo_toml, r#" -[workspace] -resolver = "2" -members = [ - "module/*", -] -[workspace.dependencies.test_experimental_a] -version = "*" -path = "module/a" -default-features = true -[workspace.dependencies.test_experimental_b] -version = "*" -path = "module/b" -default-features = true -[workspace.dependencies.test_experimental_c] -version = "*" -path = "module/c" -default-features = true -"# ).unwrap(); - - let absolute = AbsolutePath::try_from( c_temp_path.join( "Cargo.toml" ) ).unwrap(); - let mut manifest = manifest::open( absolute ).unwrap(); - version::bump( &mut manifest, false ).unwrap(); - - let c_temp = package( c_temp_path ); - let b_temp = package( b_temp_path ); - let a_temp = package( a_temp_path ); - - assert!( publish_need( &c_temp, None ).unwrap() ); - assert!( publish_need( &b_temp, None ).unwrap() ); - assert!( publish_need( &a_temp, None ).unwrap() ); -} diff --git a/module/move/willbe/tests/inc/package.rs b/module/move/willbe/tests/inc/package.rs index 08b2f36f93..935069b5e6 100644 --- a/module/move/willbe/tests/inc/package.rs +++ b/module/move/willbe/tests/inc/package.rs @@ -1,12 +1,12 @@ -use super::*; -use the_module:: -{ - Workspace, - _path::AbsolutePath, - package::PublishPlan, -}; -use willbe::package::perform_packages_publish; - +// use super::*; +// use the_module:: +// { +// Workspace, +// _path::AbsolutePath, +// package::PublishPlan, +// }; +// use willbe::package::perform_packages_publish; +// // #[ test ] // fn plan_publish_many_packages() // {