diff --git a/Cargo.toml b/Cargo.toml index 7f2e837..6dced69 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,9 +29,9 @@ toml = "0.4" uuid = { version = "0.7", features = ["v4"] } [dev-dependencies] -assert_fs = "0.9" +assert_fs = "0.11" lazy_static = "1" -predicates = "0.9" +predicates = "1.0" tempfile = "3" sxd-document = "0.3" sxd-xpath = "0.4" diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 84bc3ec..6a600bc 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,9 +1,14 @@ +extern crate assert_fs; extern crate sxd_document; extern crate sxd_xpath; -extern crate tempfile; +// extern crate tempfile; -use std::fs::File; -use std::io::Read; +use assert_fs::prelude::*; + +use assert_fs::TempDir; +use std::fs; +use std::fs::{File, OpenOptions}; +use std::io::{Read, Write}; use std::path::Path; use std::process::Command; use self::sxd_document::parser; @@ -12,6 +17,9 @@ use self::sxd_xpath::{Context, Factory}; #[allow(dead_code)] pub const TARGET_NAME: &str = "target"; +#[allow(dead_code)] +pub const PACKAGE_NAME: &str = "cargowixtest"; + /// Create a new cargo project/package for a binary project in a temporary /// directory. /// @@ -34,18 +42,22 @@ pub const TARGET_NAME: &str = "target"; /// This will panic if a temporary directory fails to be created or if cargo /// fails to create the project/package. #[allow(dead_code)] -pub fn create_test_package() -> tempfile::TempDir { +// pub fn create_test_package() -> tempfile::TempDir { +pub fn create_test_package() -> TempDir { // Use a prefix because the default `.tmp` is an invalid name for a Cargo package. // // Cannot use dashes. WiX Toolset only allows A-Z, a-z, digits, underscores (_), or periods (.) // for attribute IDs. - let temp_dir = tempfile::Builder::new().prefix("cargo_wix_test_").tempdir().unwrap(); + // let temp_dir = tempfile::Builder::new().prefix("cargo_wix_test_").tempdir().unwrap(); + let temp_dir = TempDir::new().unwrap(); let cargo_init_status = Command::new("cargo") .arg("init") .arg("--bin") .arg("--quiet") .arg("--vcs") .arg("none") + .arg("--name") + .arg(PACKAGE_NAME) .arg(temp_dir.path()) .status() .expect("Creation of test Cargo package"); @@ -53,6 +65,59 @@ pub fn create_test_package() -> tempfile::TempDir { temp_dir } +/// Create a new cargo project/package for a project with multiple binaries in a +/// temporary directory. See the [create_test_package] function for more +/// information. +/// +/// Following creation of the project, the manifest file (Cargo.toml) is +/// modified to include multiple `[[bin]]` sections for multiple binaries. The +/// original `main.rs` file that is created for the first binary is copied for +/// each of the other binaries. A total of three (3) binaries will be created +/// and added to the manifest file. +/// +/// [create_test_package]: fn.create_test_package.html +/// +/// # Panics +/// +/// This will panic if a temporary directory fails to be created or if cargo +/// fails to create the project/package. +/// +/// It will also panic if it cannot modify the manifest file (Cargo.toml) or the +/// project layout for multiple binaries. +#[allow(dead_code)] +pub fn create_test_package_multiple_binaries() -> assert_fs::TempDir { + let package = create_test_package(); + let package_manifest = package.child("Cargo.toml"); + let package_src = package.child("src"); + { + let mut cargo_toml_handle = OpenOptions::new() + .read(true) + .append(true) + .open(package_manifest.path()) + .unwrap(); + cargo_toml_handle.write_all( +r#"[[bin]] +name = "main1" +path = "src/main1.rs" + +[[bin]] +name = "main2" +path = "src/main2.rs" + +[[bin]] +name = "main3" +path = "src/main3.rs" +"#.as_bytes() + ).unwrap(); + } + let package_original_main = package_src.child("main.rs"); + fs::copy(package_original_main.path(), package_src.child("main1.rs").path()).unwrap(); + fs::copy(package_original_main.path(), package_src.child("main2.rs").path()).unwrap(); + fs::copy(package_original_main.path(), package_src.child("main3.rs").path()).unwrap(); + fs::remove_file(package_original_main.path()).unwrap(); + package +} + /// Evaluates an XPath expression for a WiX Source file. /// /// This registers the WiX XML namespace with the `wix` prefix. So, XPath diff --git a/tests/create.rs b/tests/create.rs index a9df2bd..3511136 100644 --- a/tests/create.rs +++ b/tests/create.rs @@ -24,7 +24,7 @@ mod common; use assert_fs::prelude::*; use predicates::prelude::*; -use common::TARGET_NAME; +use common::{PACKAGE_NAME, TARGET_NAME}; use std::env; use std::fs::{self, File}; use std::io::{Read, Write}; @@ -47,7 +47,7 @@ fn default_works() { let original_working_directory = env::current_dir().unwrap(); let package = common::create_test_package(); let expected_msi_file = TARGET_WIX_DIR.join(format!( - "{}-0.1.0-x86_64.msi", package.path().file_name().and_then(|o| o.to_str()).unwrap() + "{}-0.1.0-x86_64.msi", PACKAGE_NAME )); env::set_current_dir(package.path()).unwrap(); initialize::Execution::default().run().unwrap(); @@ -63,7 +63,7 @@ fn init_with_package_section_fields_works() { let original_working_directory = env::current_dir().unwrap(); let package = common::create_test_package(); let expected_msi_file = TARGET_WIX_DIR.join(format!( - "{}-0.1.0-x86_64.msi", package.path().file_name().and_then(|o| o.to_str()).unwrap() + "{}-0.1.0-x86_64.msi", PACKAGE_NAME )); env::set_current_dir(package.path()).unwrap(); let package_manifest = package.child("Cargo.toml"); @@ -106,7 +106,7 @@ fn init_with_all_options_works() { let original_working_directory = env::current_dir().unwrap(); let package = common::create_test_package(); let expected_msi_file = TARGET_WIX_DIR.join(format!( - "{}-0.1.0-x86_64.msi", package.path().file_name().and_then(|o| o.to_str()).unwrap() + "{}-0.1.0-x86_64.msi", PACKAGE_NAME )); env::set_current_dir(package.path()).unwrap(); let bin_example_path = package.path().join("bin").join("Example.exe"); @@ -161,7 +161,7 @@ fn init_with_banner_option_works() { let original_working_directory = env::current_dir().unwrap(); let package = common::create_test_package(); let expected_msi_file = TARGET_WIX_DIR.join(format!( - "{}-0.1.0-x86_64.msi", package.path().file_name().and_then(|o| o.to_str()).unwrap() + "{}-0.1.0-x86_64.msi", PACKAGE_NAME )); env::set_current_dir(package.path()).unwrap(); let banner_path = package.path().join("img").join("Banner.bmp"); @@ -186,7 +186,7 @@ fn init_with_binary_option_works() { let original_working_directory = env::current_dir().unwrap(); let package = common::create_test_package(); let expected_msi_file = TARGET_WIX_DIR.join(format!( - "{}-0.1.0-x86_64.msi", package.path().file_name().and_then(|o| o.to_str()).unwrap() + "{}-0.1.0-x86_64.msi", PACKAGE_NAME )); env::set_current_dir(package.path()).unwrap(); let bin_example_path = package.path().join("bin").join("Example.exe"); @@ -211,7 +211,7 @@ fn init_with_description_option_works() { let original_working_directory = env::current_dir().unwrap(); let package = common::create_test_package(); let expected_msi_file = TARGET_WIX_DIR.join(format!( - "{}-0.1.0-x86_64.msi", package.path().file_name().and_then(|o| o.to_str()).unwrap() + "{}-0.1.0-x86_64.msi", PACKAGE_NAME )); env::set_current_dir(package.path()).unwrap(); initialize::Builder::new() @@ -231,7 +231,7 @@ fn init_with_dialog_option_works() { let original_working_directory = env::current_dir().unwrap(); let package = common::create_test_package(); let expected_msi_file = TARGET_WIX_DIR.join(format!( - "{}-0.1.0-x86_64.msi", package.path().file_name().and_then(|o| o.to_str()).unwrap() + "{}-0.1.0-x86_64.msi", PACKAGE_NAME )); env::set_current_dir(package.path()).unwrap(); let dialog_path = package.path().join("img").join("Dialog.bmp"); @@ -257,7 +257,7 @@ fn init_with_eula_in_cwd_works() { let original_working_directory = env::current_dir().unwrap(); let package = common::create_test_package(); let expected_msi_file = TARGET_WIX_DIR.join(format!( - "{}-0.1.0-x86_64.msi", package.path().file_name().and_then(|o| o.to_str()).unwrap() + "{}-0.1.0-x86_64.msi", PACKAGE_NAME )); env::set_current_dir(package.path()).unwrap(); let package_eula = package.child(EULA_FILE); @@ -282,7 +282,7 @@ fn init_with_eula_in_docs_works() { let original_working_directory = env::current_dir().unwrap(); let package = common::create_test_package(); let expected_msi_file = TARGET_WIX_DIR.join(format!( - "{}-0.1.0-x86_64.msi", package.path().file_name().and_then(|o| o.to_str()).unwrap() + "{}-0.1.0-x86_64.msi", PACKAGE_NAME )); env::set_current_dir(package.path()).unwrap(); let package_docs = package.child("docs"); @@ -308,7 +308,7 @@ fn init_with_help_url_option_works() { let original_working_directory = env::current_dir().unwrap(); let package = common::create_test_package(); let expected_msi_file = TARGET_WIX_DIR.join(format!( - "{}-0.1.0-x86_64.msi", package.path().file_name().and_then(|o| o.to_str()).unwrap() + "{}-0.1.0-x86_64.msi", PACKAGE_NAME )); env::set_current_dir(package.path()).unwrap(); initialize::Builder::new() @@ -329,7 +329,7 @@ fn init_with_license_in_cwd_works() { let original_working_directory = env::current_dir().unwrap(); let package = common::create_test_package(); let expected_msi_file = TARGET_WIX_DIR.join(format!( - "{}-0.1.0-x86_64.msi", package.path().file_name().and_then(|o| o.to_str()).unwrap() + "{}-0.1.0-x86_64.msi", PACKAGE_NAME )); env::set_current_dir(package.path()).unwrap(); let package_license = package.child(LICENSE_FILE); @@ -354,7 +354,7 @@ fn init_with_license_in_docs_works() { let original_working_directory = env::current_dir().unwrap(); let package = common::create_test_package(); let expected_msi_file = TARGET_WIX_DIR.join(format!( - "{}-0.1.0-x86_64.msi", package.path().file_name().and_then(|o| o.to_str()).unwrap() + "{}-0.1.0-x86_64.msi", PACKAGE_NAME )); env::set_current_dir(package.path()).unwrap(); let package_docs = package.child("docs"); @@ -380,7 +380,7 @@ fn init_with_manufacturer_option_works() { let original_working_directory = env::current_dir().unwrap(); let package = common::create_test_package(); let expected_msi_file = TARGET_WIX_DIR.join(format!( - "{}-0.1.0-x86_64.msi", package.path().file_name().and_then(|o| o.to_str()).unwrap() + "{}-0.1.0-x86_64.msi", PACKAGE_NAME )); env::set_current_dir(package.path()).unwrap(); initialize::Builder::new() @@ -400,7 +400,7 @@ fn init_with_product_icon_option_works() { let original_working_directory = env::current_dir().unwrap(); let package = common::create_test_package(); let expected_msi_file = TARGET_WIX_DIR.join(format!( - "{}-0.1.0-x86_64.msi", package.path().file_name().and_then(|o| o.to_str()).unwrap() + "{}-0.1.0-x86_64.msi", PACKAGE_NAME )); env::set_current_dir(package.path()).unwrap(); let product_icon_path = package.path().join("img").join("Product.ico"); @@ -425,7 +425,7 @@ fn init_with_product_name_option_works() { let original_working_directory = env::current_dir().unwrap(); let package = common::create_test_package(); let expected_msi_file = TARGET_WIX_DIR.join(format!( - "{}-0.1.0-x86_64.msi", package.path().file_name().and_then(|o| o.to_str()).unwrap() + "{}-0.1.0-x86_64.msi", PACKAGE_NAME )); env::set_current_dir(package.path()).unwrap(); initialize::Builder::new() @@ -453,7 +453,7 @@ fn input_works() { fs::create_dir(output.parent().unwrap()).unwrap(); fs::create_dir(&output).unwrap(); let expected_msi_file = TARGET_WIX_DIR.join(format!( - "{}-0.1.0-x86_64.msi", package.path().file_name().and_then(|o| o.to_str()).unwrap() + "{}-0.1.0-x86_64.msi", PACKAGE_NAME )); let mut toml: Value = { let mut cargo_toml_handle = File::open(package_manifest.path()).unwrap(); diff --git a/tests/initialize.rs b/tests/initialize.rs index 1c624d0..6e9d369 100644 --- a/tests/initialize.rs +++ b/tests/initialize.rs @@ -25,6 +25,7 @@ mod common; use assert_fs::prelude::*; use predicates::prelude::*; +use assert_fs::fixture::ChildPath; use std::env; use std::fs::{self, File}; use std::io::{Read, Write}; @@ -166,7 +167,8 @@ fn input_works() { fn output_works() { let original_working_directory = env::current_dir().unwrap(); let package = common::create_test_package(); - let output = tempfile::Builder::new().prefix("cargo_wix_test_output_").tempdir().unwrap(); + let temp_dir = tempfile::Builder::new().prefix("cargo_wix_test_output_").tempdir().unwrap(); + let output = ChildPath::new(temp_dir.path()); env::set_current_dir(package.path()).unwrap(); let result = Builder::default() .output(output.path().to_str())