Skip to content

Commit

Permalink
Add version bump and revert tests
Browse files Browse the repository at this point in the history
Two tests are added to `version.rs`, testing version bump and version revert functionalities. The new tests cover the whole process of version change in a package, from the initial version to the updated version, as well as the reverting back to the original version.
  • Loading branch information
Barsik-sus committed Apr 8, 2024
1 parent 6082812 commit cf927e6
Showing 1 changed file with 148 additions and 1 deletion.
149 changes: 148 additions & 1 deletion module/move/willbe/tests/inc/entity/version.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
use crate::the_module::version::Version;
use crate::*;

use std::path::{ Path, PathBuf };
use std::str::FromStr;
use std::io::Write;
use assert_fs::prelude::*;
use the_module::
{
CrateDir,
Manifest,
version::Version,
_path::AbsolutePath,
package::Package,
version::{ BumpOptions, version_bump, version_revert },
};

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 )
}

#[ test ]
fn patch()
Expand Down Expand Up @@ -78,3 +99,129 @@ fn major_with_patches()
// Assert
assert_eq!( "2.0.0", &new_version.to_string() );
}

#[ test ]
fn package_version_bump()
{
// Arrange
let c = package_path( "c" );
let temp = assert_fs::TempDir::new().unwrap();
let temp_module = temp.child( "module" );
std::fs::create_dir( &temp_module ).unwrap();
temp_module.child( "c" ).copy_from( &c, &[ "**" ] ).unwrap();
let c_temp_path = temp_module.join( "c" );
let c_temp_absolute_path = AbsolutePath::try_from( c_temp_path ).unwrap();
let c_temp_crate_dir = CrateDir::try_from( c_temp_absolute_path.clone() ).unwrap();
let c_package = Package::try_from( c_temp_absolute_path.clone() ).unwrap();
let version = c_package.version().unwrap();

let root_manifest_path = temp.join( "Cargo.toml" );
let mut cargo_toml = std::fs::File::create( &root_manifest_path ).unwrap();
let root_manifest_absolute_path = AbsolutePath::try_from( root_manifest_path.as_path() ).unwrap();
write!( cargo_toml, r#"
[workspace]
resolver = "2"
members = [
"module/*",
]
[workspace.dependencies.test_experimental_c]
version = "{version}"
path = "module/c"
default-features = true
"# ).unwrap();
let version = Version::try_from( &version ).unwrap();
let bumped_version = version.clone().bump();

// Act
let options = BumpOptions
{
crate_dir : c_temp_crate_dir,
old_version : version.clone(),
new_version : bumped_version.clone(),
dependencies : vec![ CrateDir::try_from( root_manifest_absolute_path.parent().unwrap() ).unwrap() ],
dry : false,
};
let bump_report = version_bump( options ).unwrap();

// Assert
assert_eq!( Some( version.to_string() ), bump_report.old_version );
assert_eq!( Some( bumped_version.to_string() ), bump_report.new_version );
assert_eq!
(
{
let mut v = vec![ root_manifest_absolute_path.clone(), c_temp_absolute_path.join( "Cargo.toml" ) ];
v.sort();
v
},
{
let mut v = bump_report.changed_files;
v.sort();
v
}
);
let c_package = Package::try_from( c_temp_absolute_path.clone() ).unwrap();
let name = c_package.name().unwrap();
assert_eq!( bumped_version.to_string(), c_package.version().unwrap() );
let mut root_manifest = Manifest::try_from( root_manifest_absolute_path ).unwrap();
root_manifest.load().unwrap();
let data = root_manifest.data();
let current_version_item = data.get( "workspace" ).and_then( | w | w.get( "dependencies" ) ).and_then( | d | d.get( &name ) ).and_then( | p | p.get( "version" ) ).unwrap();
let current_version = current_version_item.as_str().unwrap();
assert_eq!( &bumped_version.to_string(), current_version );
}

#[ test ]
fn package_version_bump_revert()
{
// Arrange
let c = package_path( "c" );
let temp = assert_fs::TempDir::new().unwrap();
let temp_module = temp.child( "module" );
std::fs::create_dir( &temp_module ).unwrap();
temp_module.child( "c" ).copy_from( &c, &[ "**" ] ).unwrap();
let c_temp_path = temp_module.join( "c" );
let c_temp_absolute_path = AbsolutePath::try_from( c_temp_path ).unwrap();
let c_temp_crate_dir = CrateDir::try_from( c_temp_absolute_path.clone() ).unwrap();
let c_package = Package::try_from( c_temp_absolute_path.clone() ).unwrap();
let version = c_package.version().unwrap();

let root_manifest_path = temp.join( "Cargo.toml" );
let mut cargo_toml = std::fs::File::create( &root_manifest_path ).unwrap();
let root_manifest_absolute_path = AbsolutePath::try_from( root_manifest_path.as_path() ).unwrap();
write!( cargo_toml, r#"
[workspace]
resolver = "2"
members = [
"module/*",
]
[workspace.dependencies.test_experimental_c]
version = "{version}"
path = "module/c"
default-features = true
"# ).unwrap();
let version = Version::try_from( &version ).unwrap();
let bumped_version = version.clone().bump();

// Act
let options = BumpOptions
{
crate_dir : c_temp_crate_dir,
old_version : version.clone(),
new_version : bumped_version.clone(),
dependencies : vec![ CrateDir::try_from( root_manifest_absolute_path.parent().unwrap() ).unwrap() ],
dry : false,
};
let bump_report = version_bump( options ).unwrap();
version_revert( &bump_report ).unwrap();

// Assert
let c_package = Package::try_from( c_temp_absolute_path.clone() ).unwrap();
let name = c_package.name().unwrap();
assert_eq!( version.to_string(), c_package.version().unwrap() );
let mut root_manifest = Manifest::try_from( root_manifest_absolute_path ).unwrap();
root_manifest.load().unwrap();
let data = root_manifest.data();
let current_version_item = data.get( "workspace" ).and_then( | w | w.get( "dependencies" ) ).and_then( | d | d.get( &name ) ).and_then( | p | p.get( "version" ) ).unwrap();
let current_version = current_version_item.as_str().unwrap();
assert_eq!( &version.to_string(), current_version );
}

0 comments on commit cf927e6

Please sign in to comment.