From 9da2a0edb0542777d8ea878954e05d09d4ecb0e4 Mon Sep 17 00:00:00 2001 From: Chris Henk Date: Sun, 17 Dec 2023 14:48:33 -0800 Subject: [PATCH 01/21] starting point --- .github/workflows/rust.yml | 28 ++++++++-------------------- src/lib.rs | 20 -------------------- 2 files changed, 8 insertions(+), 40 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index e36bc2b..c567e00 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -16,6 +16,14 @@ jobs: steps: - uses: actions/checkout@v3 + - name: Run sccache-cache only on non-release runs + if: github.event_name != 'release' && github.event_name != 'workflow_dispatch' + uses: mozilla-actions/sccache-action@v0.0.3 + - name: Set Rust caching env vars only on non-release runs + if: github.event_name != 'release' && github.event_name != 'workflow_dispatch' + run: | + echo "SCCACHE_GHA_ENABLED=true" >> $GITHUB_ENV + echo "RUSTC_WRAPPER=sccache" >> $GITHUB_ENV - name: Build run: cargo build --verbose - name: Format @@ -26,23 +34,3 @@ jobs: run: cargo test --verbose - name: Docs run: RUSTDOCFLAGS="-D warnings" cargo doc --no-deps - -# coverage: -# -# runs-on: ubuntu-latest -# -# steps: -# - uses: actions/checkout@v1 -# - uses: actions-rs/toolchain@v1 -# with: -# toolchain: nightly -# override: true -# - uses: actions-rs/cargo@v1 -# with: -# command: test -# args: --all-features --no-fail-fast -# env: -# CARGO_INCREMENTAL: '0' -# RUSTFLAGS: '-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests' -# RUSTDOCFLAGS: '-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests' -# - uses: actions-rs/grcov@v0.1 diff --git a/src/lib.rs b/src/lib.rs index 646b623..6ce7a9c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,19 +65,6 @@ pub fn copy_to_output(path: &str) -> Result<()> { } pub fn copy_to_output_for_build_type(path: &str, build_type: &str) -> Result<()> { - let mut cargo_cache_command = format!("cargo:rerun-if-changed={}", path).to_owned(); - - if std::fs::metadata(path).expect("Could not pull metadata for path").is_dir() { - cargo_cache_command.push_str("/*"); - } - - // Re-runs script if any files changed - println!("{}", cargo_cache_command); - copy_to_output_no_cargo(path, build_type) -} - -/// Advanced usage, leaves cargo commands up to you. -pub fn copy_to_output_no_cargo(path: &str, build_type: &str) -> Result<()> { let mut options = CopyOptions::new(); let mut out_path = get_project_root()?; out_path.push("target"); @@ -112,10 +99,3 @@ fn path_to_str(path: &Path) -> &str { pub fn copy_to_output_by_path_for_build_type(path: &Path, build_type: &str) -> Result<()> { copy_to_output_for_build_type(path_to_str(path), build_type) } - -/// Advanced usage, leaves cargo commands up to you. -pub fn copy_to_output_by_path_no_cargo(path: &Path, build_type: &str) -> Result<()> { - copy_to_output_no_cargo(path_to_str(path), build_type) -} - - From c107f97928ef3b58e9c5db78e6b43582baadee2f Mon Sep 17 00:00:00 2001 From: Chris Henk Date: Sun, 17 Dec 2023 16:18:20 -0800 Subject: [PATCH 02/21] basic copies working, workspaces, and custom targets --- build.rs | 6 ------ src/lib.rs | 2 +- tests/fake_crate/build.rs | 9 --------- tests/fake_crate/res/test.txt | 1 - tests/fake_workspace/Cargo.toml | 5 +++++ tests/{ => fake_workspace}/fake_crate/Cargo.toml | 2 +- tests/fake_workspace/fake_crate/build.rs | 12 ++++++++++++ .../res/nested/doublenested/seconddoublenested.txt | 0 .../fake_crate/res/nested/doublenested/test3.txt | 0 .../fake_crate/res/nested/secondnested.txt | 0 .../fake_crate/res/nested/test2.txt | 0 tests/{ => fake_workspace}/fake_crate/res/second.txt | 0 tests/fake_workspace/fake_crate/res/test.dat | 0 tests/fake_workspace/fake_crate/res/test.txt | 1 + tests/fake_workspace/fake_crate/src/lib.rs | 4 ++++ 15 files changed, 24 insertions(+), 18 deletions(-) delete mode 100644 build.rs delete mode 100644 tests/fake_crate/build.rs delete mode 100644 tests/fake_crate/res/test.txt create mode 100644 tests/fake_workspace/Cargo.toml rename tests/{ => fake_workspace}/fake_crate/Cargo.toml (58%) create mode 100644 tests/fake_workspace/fake_crate/build.rs rename tests/{ => fake_workspace}/fake_crate/res/nested/doublenested/seconddoublenested.txt (100%) rename tests/{ => fake_workspace}/fake_crate/res/nested/doublenested/test3.txt (100%) rename tests/{ => fake_workspace}/fake_crate/res/nested/secondnested.txt (100%) rename tests/{ => fake_workspace}/fake_crate/res/nested/test2.txt (100%) rename tests/{ => fake_workspace}/fake_crate/res/second.txt (100%) create mode 100644 tests/fake_workspace/fake_crate/res/test.dat create mode 100644 tests/fake_workspace/fake_crate/res/test.txt create mode 100644 tests/fake_workspace/fake_crate/src/lib.rs diff --git a/build.rs b/build.rs deleted file mode 100644 index 704c35a..0000000 --- a/build.rs +++ /dev/null @@ -1,6 +0,0 @@ -//use build_copy_to_output::copy_to_output; - -fn main() -> Result<(), Box> { - - Ok(()) -} diff --git a/src/lib.rs b/src/lib.rs index 6ce7a9c..a220b04 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,7 @@ //!
//! //! Provides a generalized implementation for a "post build copy" operation, which isn't -//! well-supported in rust at time of writing. This crate is a fork of +//! well-supported in rust at time of writing. This crate is inspired by //! https://github.com/prenwyn/copy_to_output that implements more managed helpers + addresses some //! of the missing scenarios (again, at time of writing). //! diff --git a/tests/fake_crate/build.rs b/tests/fake_crate/build.rs deleted file mode 100644 index a297da1..0000000 --- a/tests/fake_crate/build.rs +++ /dev/null @@ -1,9 +0,0 @@ -use build_copy_to_output::copy_to_output; - -fn main() -> Result<(), Box> { - // Bin-place resource files - copy_to_output("schemas").unwrap(); - copy_to_output("config.toml").unwrap(); - - Ok(()) -} diff --git a/tests/fake_crate/res/test.txt b/tests/fake_crate/res/test.txt deleted file mode 100644 index 566c7ba..0000000 --- a/tests/fake_crate/res/test.txt +++ /dev/null @@ -1 +0,0 @@ -Testing is neat. \ No newline at end of file diff --git a/tests/fake_workspace/Cargo.toml b/tests/fake_workspace/Cargo.toml new file mode 100644 index 0000000..3d9f9ca --- /dev/null +++ b/tests/fake_workspace/Cargo.toml @@ -0,0 +1,5 @@ +[workspace] + +members = [ + "fake_crate" +] \ No newline at end of file diff --git a/tests/fake_crate/Cargo.toml b/tests/fake_workspace/fake_crate/Cargo.toml similarity index 58% rename from tests/fake_crate/Cargo.toml rename to tests/fake_workspace/fake_crate/Cargo.toml index 6c5ec9a..5fe278d 100644 --- a/tests/fake_crate/Cargo.toml +++ b/tests/fake_workspace/fake_crate/Cargo.toml @@ -4,4 +4,4 @@ version = "1.0.0" edition = "2021" [build-dependencies] -build_copy_to_output = { path = "../Omnicopy-To-Outputbuild" } +omnicopy_to_output = { path = "../../../" } diff --git a/tests/fake_workspace/fake_crate/build.rs b/tests/fake_workspace/fake_crate/build.rs new file mode 100644 index 0000000..c7c2afe --- /dev/null +++ b/tests/fake_workspace/fake_crate/build.rs @@ -0,0 +1,12 @@ +use omnicopy_to_output::copy_to_output; + +fn main() -> Result<(), Box> { + // Bin-place resource files + copy_to_output("res/nested").unwrap(); + copy_to_output("res/empty").unwrap(); + copy_to_output("res/test.dat").unwrap(); + copy_to_output("res/test.txt").unwrap(); + copy_to_output("res/second.txt").unwrap(); + + Ok(()) +} diff --git a/tests/fake_crate/res/nested/doublenested/seconddoublenested.txt b/tests/fake_workspace/fake_crate/res/nested/doublenested/seconddoublenested.txt similarity index 100% rename from tests/fake_crate/res/nested/doublenested/seconddoublenested.txt rename to tests/fake_workspace/fake_crate/res/nested/doublenested/seconddoublenested.txt diff --git a/tests/fake_crate/res/nested/doublenested/test3.txt b/tests/fake_workspace/fake_crate/res/nested/doublenested/test3.txt similarity index 100% rename from tests/fake_crate/res/nested/doublenested/test3.txt rename to tests/fake_workspace/fake_crate/res/nested/doublenested/test3.txt diff --git a/tests/fake_crate/res/nested/secondnested.txt b/tests/fake_workspace/fake_crate/res/nested/secondnested.txt similarity index 100% rename from tests/fake_crate/res/nested/secondnested.txt rename to tests/fake_workspace/fake_crate/res/nested/secondnested.txt diff --git a/tests/fake_crate/res/nested/test2.txt b/tests/fake_workspace/fake_crate/res/nested/test2.txt similarity index 100% rename from tests/fake_crate/res/nested/test2.txt rename to tests/fake_workspace/fake_crate/res/nested/test2.txt diff --git a/tests/fake_crate/res/second.txt b/tests/fake_workspace/fake_crate/res/second.txt similarity index 100% rename from tests/fake_crate/res/second.txt rename to tests/fake_workspace/fake_crate/res/second.txt diff --git a/tests/fake_workspace/fake_crate/res/test.dat b/tests/fake_workspace/fake_crate/res/test.dat new file mode 100644 index 0000000..e69de29 diff --git a/tests/fake_workspace/fake_crate/res/test.txt b/tests/fake_workspace/fake_crate/res/test.txt new file mode 100644 index 0000000..35204a7 --- /dev/null +++ b/tests/fake_workspace/fake_crate/res/test.txt @@ -0,0 +1 @@ +Testing is neat.asdfasdfsdf \ No newline at end of file diff --git a/tests/fake_workspace/fake_crate/src/lib.rs b/tests/fake_workspace/fake_crate/src/lib.rs new file mode 100644 index 0000000..25f470f --- /dev/null +++ b/tests/fake_workspace/fake_crate/src/lib.rs @@ -0,0 +1,4 @@ + +pub fn hello_world() { + println!("Hello world!") +} \ No newline at end of file From 2606199256fb504a9abc09f68069bb3625328d8e Mon Sep 17 00:00:00 2001 From: Chris Henk Date: Sun, 17 Dec 2023 16:20:52 -0800 Subject: [PATCH 03/21] fix warning --- tests/fake_workspace/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/fake_workspace/Cargo.toml b/tests/fake_workspace/Cargo.toml index 3d9f9ca..af935e9 100644 --- a/tests/fake_workspace/Cargo.toml +++ b/tests/fake_workspace/Cargo.toml @@ -1,4 +1,5 @@ [workspace] +resolver = "2" members = [ "fake_crate" From 3e3d45e85566ae03b004a3af3e8717e9a8ef7eba Mon Sep 17 00:00:00 2001 From: Chris Henk Date: Sun, 17 Dec 2023 16:30:50 -0800 Subject: [PATCH 04/21] fix docs --- README.md | 2 +- src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index dd0e5ec..d279a85 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ # Omnicopy_to_output Provides a generalized implementation for a "post build copy" operation, which isn't well-supported in rust at time of -writing. This crate is a fork of https://github.com/prenwyn/copy_to_output that implements more managed helpers + +writing. This crate is inspired by https://github.com/prenwyn/copy_to_output, but implements more managed helpers + addresses some of the missing scenarios (again, at time of writing). If any scenarios are missing, please contribute! diff --git a/src/lib.rs b/src/lib.rs index a220b04..0e9c39c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,7 @@ //! //! Provides a generalized implementation for a "post build copy" operation, which isn't //! well-supported in rust at time of writing. This crate is inspired by -//! https://github.com/prenwyn/copy_to_output that implements more managed helpers + addresses some +//! https://github.com/prenwyn/copy_to_output, but implements more managed helpers + addresses some //! of the missing scenarios (again, at time of writing). //! //! As the name implies, the goal here is to provide coverage for all possible build scenarios as From 338c06e4f9ca88186335d0f397e520f6eb3b9650 Mon Sep 17 00:00:00 2001 From: Chris Henk Date: Sun, 17 Dec 2023 19:22:49 -0800 Subject: [PATCH 05/21] checkpoint --- Cargo.toml | 3 ++ src/lib.rs | 2 +- tests/build_tests.rs | 31 ++++++++++++ tests/common.rs | 0 tests/common/mod.rs | 84 +++++++++++++++++++++++++++++++ tests/fake_workspace/Cargo.toml | 2 +- tests/psuedo_integration_tests.rs | 18 ------- 7 files changed, 120 insertions(+), 20 deletions(-) create mode 100644 tests/build_tests.rs delete mode 100644 tests/common.rs create mode 100644 tests/common/mod.rs delete mode 100644 tests/psuedo_integration_tests.rs diff --git a/Cargo.toml b/Cargo.toml index 68aea77..7babea7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,6 @@ anyhow = { version = "1.0.75", features = [] } glob = "0.3" project-root = "0.2.2" build-target = "0.4.0" + +[dev-dependencies] +tempfile = "3.8.1" diff --git a/src/lib.rs b/src/lib.rs index 0e9c39c..fdb69ef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,7 +65,6 @@ pub fn copy_to_output(path: &str) -> Result<()> { } pub fn copy_to_output_for_build_type(path: &str, build_type: &str) -> Result<()> { - let mut options = CopyOptions::new(); let mut out_path = get_project_root()?; out_path.push("target"); @@ -78,6 +77,7 @@ pub fn copy_to_output_for_build_type(path: &str, build_type: &str) -> Result<()> out_path.push(build_type); // Overwrite existing files with same name + let mut options = CopyOptions::new(); options.overwrite = true; options.copy_inside = true; diff --git a/tests/build_tests.rs b/tests/build_tests.rs new file mode 100644 index 0000000..bdf6445 --- /dev/null +++ b/tests/build_tests.rs @@ -0,0 +1,31 @@ +use crate::common::{build_environment, build_environment_with_target, custom_test_target, fake_crate_in_tempdir, fake_workspace_in_tempdir, validate}; + +mod common; + +#[test] +fn build_crate() { + let environment = fake_crate_in_tempdir(); + build_environment(&environment); + validate(&environment); +} + +#[test] +fn build_crate_target_specified() { + let environment = fake_crate_in_tempdir(); + build_environment_with_target(&environment, custom_test_target()); + validate(&environment); +} + +#[test] +fn build_workspace() { + let environment = fake_workspace_in_tempdir(); + build_environment(&environment); + validate(&environment); +} + +#[test] +fn build_workspace_target_specified() { + let environment = fake_workspace_in_tempdir(); + build_environment_with_target(&environment, custom_test_target()); + validate(&environment); +} diff --git a/tests/common.rs b/tests/common.rs deleted file mode 100644 index e69de29..0000000 diff --git a/tests/common/mod.rs b/tests/common/mod.rs new file mode 100644 index 0000000..3c8279c --- /dev/null +++ b/tests/common/mod.rs @@ -0,0 +1,84 @@ +use std::fs; +use std::path::{Path, PathBuf}; +use fs_extra::copy_items; +use fs_extra::dir::CopyOptions; +use tempfile::TempDir; + +pub struct TestEnvironment { + _handle: TempDir, + pub path: PathBuf +} + +// Creates a copy of the fake crate in a temp dir and returns handle +pub fn fake_crate_in_tempdir() -> TestEnvironment { + let mut environment = fake_workspace_in_tempdir(); + + // In order for the fake crate to work as expected, kill the workspace file that owns it. + fs::remove_file(environment.path.join("../../Cargo.toml")).expect("Failed to delete workspace toml"); + + // We just reused the workspace builder, fake crate is nested just append to the path. + environment.path = environment.path.join("fake_crate"); + + environment +} + +pub fn fake_workspace_in_tempdir() -> TestEnvironment { + let dir = TempDir::new().expect("Failed to create temp dir"); + + let mut options = CopyOptions::new(); + options.overwrite = true; + options.copy_inside = true; + + copy_items(&[Path::new("./")], &dir.path(), &options).expect("Failed to copy"); + + let path = dir.path().join("../fake_workspace"); + + TestEnvironment { + _handle: dir, + path + } +} + +pub fn build_environment(environment: &TestEnvironment) { + std::process::Command::new("cargo") + .current_dir(environment.path.to_str().expect("Couldn't to_string environment path")) + .arg("build") + .output().expect("failed to execute process"); +} + +pub fn build_environment_with_target(environment: &TestEnvironment, target: String) { + std::process::Command::new("cargo") + .current_dir(environment.path.to_str().expect("Couldn't to_string environment path")) + .arg("build") + .arg(format!("--target {target}")) + .output().expect("failed to execute process"); +} + +pub fn validate(environment: &TestEnvironment) { + assert!(environment.path.join("empty").exists()); + assert!(environment.path.join("nested").exists()); + + assert!(environment.path.join("nested/doublenested").exists()); + assert!(environment.path.join("nested/doublenested/emptiest").exists()); + assert!(environment.path.join("nested/doublenested/seconddoublenested.txt").exists()); + assert!(environment.path.join("nested/doublenested/test3.txt").exists()); + + assert!(environment.path.join("nested/emptier").exists()); + assert!(environment.path.join("nested/secondnested.txt").exists()); + assert!(environment.path.join("nested/test2.txt").exists()); + + assert!(environment.path.join("second.txt").exists()); + assert!(environment.path.join("test.dat").exists()); + assert!(environment.path.join("test.txt").exists()); +} + +#[cfg(target_os = "linux")] +pub fn custom_test_target() -> String { + "x86_64-unknown-linux-gnu".to_string() +} + +// Just makes running the tests on a windows machine easier +#[cfg(target_os = "windows")] +pub fn custom_test_target() -> String { + "x86_64-pc-windows-msvc".to_string() +} diff --git a/tests/fake_workspace/Cargo.toml b/tests/fake_workspace/Cargo.toml index af935e9..c014233 100644 --- a/tests/fake_workspace/Cargo.toml +++ b/tests/fake_workspace/Cargo.toml @@ -3,4 +3,4 @@ resolver = "2" members = [ "fake_crate" -] \ No newline at end of file +] diff --git a/tests/psuedo_integration_tests.rs b/tests/psuedo_integration_tests.rs deleted file mode 100644 index 3f5950f..0000000 --- a/tests/psuedo_integration_tests.rs +++ /dev/null @@ -1,18 +0,0 @@ - -// These aren't actually integration tests, the point is to verify that target directory when -// an integration test is run is verified to work. - -#[test] -fn file_copy() { - assert_eq!(5, 5); -} - -#[test] -fn directory_copy() { - assert_eq!(5, 5); -} - -#[test] -fn directory_copy_recuses() { - assert_eq!(5, 5); -} From 8f986d4f64577e8a018279d6bd1c32aba8597068 Mon Sep 17 00:00:00 2001 From: Chris Henk Date: Sun, 17 Dec 2023 19:24:51 -0800 Subject: [PATCH 06/21] remove mod.rs; wasn't the issue --- tests/{common/mod.rs => common.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{common/mod.rs => common.rs} (100%) diff --git a/tests/common/mod.rs b/tests/common.rs similarity index 100% rename from tests/common/mod.rs rename to tests/common.rs From 636c84dbe35b8b06abc6ed17bd7041353ddc3d08 Mon Sep 17 00:00:00 2001 From: Chris Henk Date: Sun, 17 Dec 2023 19:25:20 -0800 Subject: [PATCH 07/21] revert test diff --- tests/fake_workspace/fake_crate/res/test.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fake_workspace/fake_crate/res/test.txt b/tests/fake_workspace/fake_crate/res/test.txt index 35204a7..566c7ba 100644 --- a/tests/fake_workspace/fake_crate/res/test.txt +++ b/tests/fake_workspace/fake_crate/res/test.txt @@ -1 +1 @@ -Testing is neat.asdfasdfsdf \ No newline at end of file +Testing is neat. \ No newline at end of file From eadf47a20e9d4d215872c177392ed9d2179cce07 Mon Sep 17 00:00:00 2001 From: Chris Henk Date: Sun, 17 Dec 2023 20:16:38 -0800 Subject: [PATCH 08/21] working tests --- tests/build_tests.rs | 8 +++---- tests/common.rs | 54 ++++++++++++++++++++++++++------------------ 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/tests/build_tests.rs b/tests/build_tests.rs index bdf6445..6c781c7 100644 --- a/tests/build_tests.rs +++ b/tests/build_tests.rs @@ -6,26 +6,26 @@ mod common; fn build_crate() { let environment = fake_crate_in_tempdir(); build_environment(&environment); - validate(&environment); + validate(&environment, None); } #[test] fn build_crate_target_specified() { let environment = fake_crate_in_tempdir(); build_environment_with_target(&environment, custom_test_target()); - validate(&environment); + validate(&environment, Some(custom_test_target())); } #[test] fn build_workspace() { let environment = fake_workspace_in_tempdir(); build_environment(&environment); - validate(&environment); + validate(&environment, None); } #[test] fn build_workspace_target_specified() { let environment = fake_workspace_in_tempdir(); build_environment_with_target(&environment, custom_test_target()); - validate(&environment); + validate(&environment, Some(custom_test_target())); } diff --git a/tests/common.rs b/tests/common.rs index 3c8279c..69a761f 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -14,7 +14,7 @@ pub fn fake_crate_in_tempdir() -> TestEnvironment { let mut environment = fake_workspace_in_tempdir(); // In order for the fake crate to work as expected, kill the workspace file that owns it. - fs::remove_file(environment.path.join("../../Cargo.toml")).expect("Failed to delete workspace toml"); + fs::remove_file(environment.path.join("Cargo.toml")).expect("Failed to delete workspace toml"); // We just reused the workspace builder, fake crate is nested just append to the path. environment.path = environment.path.join("fake_crate"); @@ -31,7 +31,7 @@ pub fn fake_workspace_in_tempdir() -> TestEnvironment { copy_items(&[Path::new("./")], &dir.path(), &options).expect("Failed to copy"); - let path = dir.path().join("../fake_workspace"); + let path = dir.path().join("tests").join("fake_workspace"); TestEnvironment { _handle: dir, @@ -50,35 +50,45 @@ pub fn build_environment_with_target(environment: &TestEnvironment, target: Stri std::process::Command::new("cargo") .current_dir(environment.path.to_str().expect("Couldn't to_string environment path")) .arg("build") - .arg(format!("--target {target}")) + .arg("--target") + .arg(target) .output().expect("failed to execute process"); } -pub fn validate(environment: &TestEnvironment) { - assert!(environment.path.join("empty").exists()); - assert!(environment.path.join("nested").exists()); +pub fn validate(environment: &TestEnvironment, target: Option) { + let base_path = environment.path.join("target"); - assert!(environment.path.join("nested/doublenested").exists()); - assert!(environment.path.join("nested/doublenested/emptiest").exists()); - assert!(environment.path.join("nested/doublenested/seconddoublenested.txt").exists()); - assert!(environment.path.join("nested/doublenested/test3.txt").exists()); + let base_path = if let Some(target) = target { + base_path.join(target) + } else { + base_path + }; - assert!(environment.path.join("nested/emptier").exists()); - assert!(environment.path.join("nested/secondnested.txt").exists()); - assert!(environment.path.join("nested/test2.txt").exists()); + let base_path = base_path.join("debug"); - assert!(environment.path.join("second.txt").exists()); - assert!(environment.path.join("test.dat").exists()); - assert!(environment.path.join("test.txt").exists()); -} + assert!(base_path.join("empty").exists()); + assert!(base_path.join("nested").exists()); -#[cfg(target_os = "linux")] -pub fn custom_test_target() -> String { - "x86_64-unknown-linux-gnu".to_string() + assert!(base_path.join("nested").join("doublenested").exists()); + assert!(base_path.join("nested").join("doublenested").join("emptiest").exists()); + assert!(base_path.join("nested").join("doublenested").join("seconddoublenested.txt").exists()); + assert!(base_path.join("nested").join("doublenested").join("test3.txt").exists()); + + assert!(base_path.join("nested").join("emptier").exists()); + assert!(base_path.join("nested").join("secondnested.txt").exists()); + assert!(base_path.join("nested").join("test2.txt").exists()); + + assert!(base_path.join("second.txt").exists()); + assert!(base_path.join("test.dat").exists()); + assert!(base_path.join("test.txt").exists()); } // Just makes running the tests on a windows machine easier -#[cfg(target_os = "windows")] pub fn custom_test_target() -> String { - "x86_64-pc-windows-msvc".to_string() + if cfg!(target_os = "windows") { + "x86_64-pc-windows-msvc".to_string() + } + else { + "x86_64-unknown-linux-gnu".to_string() + } } From 9cb8c45c0c864f35f1b2f7ddaac2f0a9a90a714d Mon Sep 17 00:00:00 2001 From: Chris Henk Date: Sun, 17 Dec 2023 20:19:28 -0800 Subject: [PATCH 09/21] format --- src/lib.rs | 12 +++++++-- tests/build_tests.rs | 5 +++- tests/cargo_cache_tests.rs | 1 - tests/common.rs | 52 ++++++++++++++++++++++++++------------ 4 files changed, 50 insertions(+), 20 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fdb69ef..62f2b84 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,7 +61,10 @@ use std::env; use std::path::Path; pub fn copy_to_output(path: &str) -> Result<()> { - copy_to_output_for_build_type(path, &env::var("PROFILE").expect("Could not load env:PROFILE")) + copy_to_output_for_build_type( + path, + &env::var("PROFILE").expect("Could not load env:PROFILE"), + ) } pub fn copy_to_output_for_build_type(path: &str, build_type: &str) -> Result<()> { @@ -70,7 +73,12 @@ pub fn copy_to_output_for_build_type(path: &str, build_type: &str) -> Result<()> // TODO: This is a hack, ideally we would plug into https://docs.rs/cargo/latest/cargo/core/compiler/enum.CompileKind.html let triple = build_target::target_triple().unwrap(); - if env::var_os("OUT_DIR").unwrap().to_str().unwrap().contains(&triple) { + if env::var_os("OUT_DIR") + .unwrap() + .to_str() + .unwrap() + .contains(&triple) + { out_path.push(triple) } diff --git a/tests/build_tests.rs b/tests/build_tests.rs index 6c781c7..b7b64c7 100644 --- a/tests/build_tests.rs +++ b/tests/build_tests.rs @@ -1,4 +1,7 @@ -use crate::common::{build_environment, build_environment_with_target, custom_test_target, fake_crate_in_tempdir, fake_workspace_in_tempdir, validate}; +use crate::common::{ + build_environment, build_environment_with_target, custom_test_target, fake_crate_in_tempdir, + fake_workspace_in_tempdir, validate, +}; mod common; diff --git a/tests/cargo_cache_tests.rs b/tests/cargo_cache_tests.rs index 65e16ac..24acfea 100644 --- a/tests/cargo_cache_tests.rs +++ b/tests/cargo_cache_tests.rs @@ -1,4 +1,3 @@ - // Verify cache clears occur by: // 1. Clone to temp dir and build // 2. Edit a resource file diff --git a/tests/common.rs b/tests/common.rs index 69a761f..5b95248 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -1,12 +1,12 @@ -use std::fs; -use std::path::{Path, PathBuf}; use fs_extra::copy_items; use fs_extra::dir::CopyOptions; +use std::fs; +use std::path::{Path, PathBuf}; use tempfile::TempDir; pub struct TestEnvironment { _handle: TempDir, - pub path: PathBuf + pub path: PathBuf, } // Creates a copy of the fake crate in a temp dir and returns handle @@ -33,26 +33,35 @@ pub fn fake_workspace_in_tempdir() -> TestEnvironment { let path = dir.path().join("tests").join("fake_workspace"); - TestEnvironment { - _handle: dir, - path - } + TestEnvironment { _handle: dir, path } } pub fn build_environment(environment: &TestEnvironment) { std::process::Command::new("cargo") - .current_dir(environment.path.to_str().expect("Couldn't to_string environment path")) + .current_dir( + environment + .path + .to_str() + .expect("Couldn't to_string environment path"), + ) .arg("build") - .output().expect("failed to execute process"); + .output() + .expect("failed to execute process"); } pub fn build_environment_with_target(environment: &TestEnvironment, target: String) { std::process::Command::new("cargo") - .current_dir(environment.path.to_str().expect("Couldn't to_string environment path")) + .current_dir( + environment + .path + .to_str() + .expect("Couldn't to_string environment path"), + ) .arg("build") .arg("--target") .arg(target) - .output().expect("failed to execute process"); + .output() + .expect("failed to execute process"); } pub fn validate(environment: &TestEnvironment, target: Option) { @@ -70,9 +79,21 @@ pub fn validate(environment: &TestEnvironment, target: Option) { assert!(base_path.join("nested").exists()); assert!(base_path.join("nested").join("doublenested").exists()); - assert!(base_path.join("nested").join("doublenested").join("emptiest").exists()); - assert!(base_path.join("nested").join("doublenested").join("seconddoublenested.txt").exists()); - assert!(base_path.join("nested").join("doublenested").join("test3.txt").exists()); + assert!(base_path + .join("nested") + .join("doublenested") + .join("emptiest") + .exists()); + assert!(base_path + .join("nested") + .join("doublenested") + .join("seconddoublenested.txt") + .exists()); + assert!(base_path + .join("nested") + .join("doublenested") + .join("test3.txt") + .exists()); assert!(base_path.join("nested").join("emptier").exists()); assert!(base_path.join("nested").join("secondnested.txt").exists()); @@ -87,8 +108,7 @@ pub fn validate(environment: &TestEnvironment, target: Option) { pub fn custom_test_target() -> String { if cfg!(target_os = "windows") { "x86_64-pc-windows-msvc".to_string() - } - else { + } else { "x86_64-unknown-linux-gnu".to_string() } } From 18aabc3a9ac96e987699b228fd4fd3f08c2c5e3f Mon Sep 17 00:00:00 2001 From: Chris Henk Date: Sun, 17 Dec 2023 20:21:08 -0800 Subject: [PATCH 10/21] debug workflow --- .github/workflows/rust.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index c567e00..dd483b0 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -30,6 +30,8 @@ jobs: run: cargo fmt --check - name: Clippy run: cargo clippy -- -D warnings + - name: Check + run: rustup show - name: Run tests run: cargo test --verbose - name: Docs From d6b936a62341c57f70d555305bf2923c1ba0930e Mon Sep 17 00:00:00 2001 From: Chris Henk Date: Mon, 18 Dec 2023 19:29:25 -0800 Subject: [PATCH 11/21] print debug --- tests/common.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/common.rs b/tests/common.rs index 5b95248..0e1bf58 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -37,7 +37,7 @@ pub fn fake_workspace_in_tempdir() -> TestEnvironment { } pub fn build_environment(environment: &TestEnvironment) { - std::process::Command::new("cargo") + let result = std::process::Command::new("cargo") .current_dir( environment .path @@ -47,10 +47,14 @@ pub fn build_environment(environment: &TestEnvironment) { .arg("build") .output() .expect("failed to execute process"); + + println!("{:?}", result.status); + println!("{:?}", std::str::from_utf8(&result.stdout).unwrap()); + println!("{:?}", std::str::from_utf8(&result.stderr).unwrap()); } pub fn build_environment_with_target(environment: &TestEnvironment, target: String) { - std::process::Command::new("cargo") + let result = std::process::Command::new("cargo") .current_dir( environment .path @@ -62,6 +66,10 @@ pub fn build_environment_with_target(environment: &TestEnvironment, target: Stri .arg(target) .output() .expect("failed to execute process"); + + println!("{:?}", result.status); + println!("{:?}", std::str::from_utf8(&result.stdout).unwrap()); + println!("{:?}", std::str::from_utf8(&result.stderr).unwrap()); } pub fn validate(environment: &TestEnvironment, target: Option) { From 48ccfb5a868e3dde5f8161b02a751ec8a6e63778 Mon Sep 17 00:00:00 2001 From: Chris Henk Date: Mon, 18 Dec 2023 19:33:04 -0800 Subject: [PATCH 12/21] test --- tests/fake_workspace/fake_crate/build.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/fake_workspace/fake_crate/build.rs b/tests/fake_workspace/fake_crate/build.rs index c7c2afe..87b469c 100644 --- a/tests/fake_workspace/fake_crate/build.rs +++ b/tests/fake_workspace/fake_crate/build.rs @@ -1,6 +1,8 @@ use omnicopy_to_output::copy_to_output; fn main() -> Result<(), Box> { + println!("{}", env::current_dir()); + // Bin-place resource files copy_to_output("res/nested").unwrap(); copy_to_output("res/empty").unwrap(); From de257f4b0ae228d17886265fabf9e44d9969eeee Mon Sep 17 00:00:00 2001 From: Chris Henk Date: Mon, 18 Dec 2023 19:35:35 -0800 Subject: [PATCH 13/21] fix --- tests/fake_workspace/fake_crate/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fake_workspace/fake_crate/build.rs b/tests/fake_workspace/fake_crate/build.rs index 87b469c..b4a9b59 100644 --- a/tests/fake_workspace/fake_crate/build.rs +++ b/tests/fake_workspace/fake_crate/build.rs @@ -1,7 +1,7 @@ use omnicopy_to_output::copy_to_output; fn main() -> Result<(), Box> { - println!("{}", env::current_dir()); + println!("TEST: {:?}", std::env::current_dir().unwrap()); // Bin-place resource files copy_to_output("res/nested").unwrap(); From c32679bc9891e519aaa72be18363d0add54cfb5e Mon Sep 17 00:00:00 2001 From: Chris Henk Date: Mon, 18 Dec 2023 19:39:43 -0800 Subject: [PATCH 14/21] fix --- src/lib.rs | 2 +- tests/fake_workspace/fake_crate/build.rs | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 62f2b84..1e4056c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,7 +72,7 @@ pub fn copy_to_output_for_build_type(path: &str, build_type: &str) -> Result<()> out_path.push("target"); // TODO: This is a hack, ideally we would plug into https://docs.rs/cargo/latest/cargo/core/compiler/enum.CompileKind.html - let triple = build_target::target_triple().unwrap(); + let triple = build_target::target_triple()?; if env::var_os("OUT_DIR") .unwrap() .to_str() diff --git a/tests/fake_workspace/fake_crate/build.rs b/tests/fake_workspace/fake_crate/build.rs index b4a9b59..a6ca3d3 100644 --- a/tests/fake_workspace/fake_crate/build.rs +++ b/tests/fake_workspace/fake_crate/build.rs @@ -2,6 +2,8 @@ use omnicopy_to_output::copy_to_output; fn main() -> Result<(), Box> { println!("TEST: {:?}", std::env::current_dir().unwrap()); + println!("RES: {:?}", std::path::Path::new("./res").exists()); + println!("RES: {:?}", std::path::Path::new("./res").join("empty").exists()); // Bin-place resource files copy_to_output("res/nested").unwrap(); From 3215bea051172f5c27165dbc713eee95f2f829ac Mon Sep 17 00:00:00 2001 From: Chris Henk Date: Mon, 18 Dec 2023 19:41:22 -0800 Subject: [PATCH 15/21] fix --- tests/fake_workspace/fake_crate/build.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/fake_workspace/fake_crate/build.rs b/tests/fake_workspace/fake_crate/build.rs index a6ca3d3..9ec010e 100644 --- a/tests/fake_workspace/fake_crate/build.rs +++ b/tests/fake_workspace/fake_crate/build.rs @@ -3,7 +3,9 @@ use omnicopy_to_output::copy_to_output; fn main() -> Result<(), Box> { println!("TEST: {:?}", std::env::current_dir().unwrap()); println!("RES: {:?}", std::path::Path::new("./res").exists()); + println!("RES: {:?}", std::path::Path::new("./res").join("nested").exists()); println!("RES: {:?}", std::path::Path::new("./res").join("empty").exists()); + println!("RES: {:?}", std::path::Path::new("./res").join("test.dat").exists()); // Bin-place resource files copy_to_output("res/nested").unwrap(); From 877f18e37e9ffecb3dac8cf91aaf255fc3962671 Mon Sep 17 00:00:00 2001 From: Chris Henk Date: Mon, 18 Dec 2023 19:59:28 -0800 Subject: [PATCH 16/21] fixes --- tests/fake_workspace/fake_crate/build.rs | 17 ++++++++++++----- tests/fake_workspace/fake_crate/src/lib.rs | 3 +-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/tests/fake_workspace/fake_crate/build.rs b/tests/fake_workspace/fake_crate/build.rs index 9ec010e..654efeb 100644 --- a/tests/fake_workspace/fake_crate/build.rs +++ b/tests/fake_workspace/fake_crate/build.rs @@ -1,11 +1,18 @@ use omnicopy_to_output::copy_to_output; +use std::fs; +use std::path::Path; fn main() -> Result<(), Box> { - println!("TEST: {:?}", std::env::current_dir().unwrap()); - println!("RES: {:?}", std::path::Path::new("./res").exists()); - println!("RES: {:?}", std::path::Path::new("./res").join("nested").exists()); - println!("RES: {:?}", std::path::Path::new("./res").join("empty").exists()); - println!("RES: {:?}", std::path::Path::new("./res").join("test.dat").exists()); + // Can't commit empty directories to git, dynamically create to ensure copying them works properly. + fs::create_dir_all(Path::new("res").join("empty")).unwrap(); + fs::create_dir_all(Path::new("res").join("nested").join("emptier")).unwrap(); + fs::create_dir_all( + Path::new("res") + .join("nested") + .join("doublenested") + .join("emptiest"), + ) + .unwrap(); // Bin-place resource files copy_to_output("res/nested").unwrap(); diff --git a/tests/fake_workspace/fake_crate/src/lib.rs b/tests/fake_workspace/fake_crate/src/lib.rs index 25f470f..406e0f3 100644 --- a/tests/fake_workspace/fake_crate/src/lib.rs +++ b/tests/fake_workspace/fake_crate/src/lib.rs @@ -1,4 +1,3 @@ - pub fn hello_world() { println!("Hello world!") -} \ No newline at end of file +} From 89d45c98cc087eca5c355ca0d86b5baaf19ac323 Mon Sep 17 00:00:00 2001 From: Chris Henk Date: Mon, 18 Dec 2023 20:01:31 -0800 Subject: [PATCH 17/21] fix doc --- src/lib.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1e4056c..a5651ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,7 @@ //! //! Provides a generalized implementation for a "post build copy" operation, which isn't //! well-supported in rust at time of writing. This crate is inspired by -//! https://github.com/prenwyn/copy_to_output, but implements more managed helpers + addresses some +//! , but implements more managed helpers + addresses some //! of the missing scenarios (again, at time of writing). //! //! As the name implies, the goal here is to provide coverage for all possible build scenarios as @@ -18,9 +18,6 @@ //! # Examples //! - Use in `build.rs` //! -//! ``` -//! -//! ``` //! //! # Scenario Coverage //! From 5e14627e741c28dc8995d0cb1ddac2a246539338 Mon Sep 17 00:00:00 2001 From: Chris Henk Date: Mon, 18 Dec 2023 20:30:16 -0800 Subject: [PATCH 18/21] remove unwraps --- src/lib.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a5651ee..08d3e4a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,7 +50,7 @@ extern crate core; -use anyhow::*; +use anyhow::{anyhow, Result}; use fs_extra::copy_items; use fs_extra::dir::CopyOptions; use project_root::get_project_root; @@ -68,15 +68,17 @@ pub fn copy_to_output_for_build_type(path: &str, build_type: &str) -> Result<()> let mut out_path = get_project_root()?; out_path.push("target"); - // TODO: This is a hack, ideally we would plug into https://docs.rs/cargo/latest/cargo/core/compiler/enum.CompileKind.html + // This is a hack, ideally we would plug into https://docs.rs/cargo/latest/cargo/core/compiler/enum.CompileKind.html + // However, since the path follows predictable rules https://doc.rust-lang.org/cargo/guide/build-cache.html + // we can just check our parent path for the pattern target/{triple}/{profile}. + // If it is present, we know CompileKind::Target was used, otherwise CompileKind::Host was used. let triple = build_target::target_triple()?; + if env::var_os("OUT_DIR") - .unwrap() - .to_str() - .unwrap() - .contains(&triple) - { - out_path.push(triple) + .ok_or(anyhow!("Failed to read env:OUT_DIR"))? + .to_str().ok_or(anyhow!("Failed to convert env:OUT_DIR to str"))? + .contains(&triple) { + out_path.push(triple); } out_path.push(build_type); From 5c758b1b947e2e530f8f6a98ab400c819c8026f3 Mon Sep 17 00:00:00 2001 From: Chris Henk Date: Mon, 18 Dec 2023 20:35:08 -0800 Subject: [PATCH 19/21] cleanup --- src/lib.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 08d3e4a..8a3c50b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -60,7 +60,7 @@ use std::path::Path; pub fn copy_to_output(path: &str) -> Result<()> { copy_to_output_for_build_type( path, - &env::var("PROFILE").expect("Could not load env:PROFILE"), + &env::var("PROFILE")?, ) } @@ -74,10 +74,7 @@ pub fn copy_to_output_for_build_type(path: &str, build_type: &str) -> Result<()> // If it is present, we know CompileKind::Target was used, otherwise CompileKind::Host was used. let triple = build_target::target_triple()?; - if env::var_os("OUT_DIR") - .ok_or(anyhow!("Failed to read env:OUT_DIR"))? - .to_str().ok_or(anyhow!("Failed to convert env:OUT_DIR to str"))? - .contains(&triple) { + if env::var("OUT_DIR")?.contains(&triple) { out_path.push(triple); } @@ -95,14 +92,13 @@ pub fn copy_to_output_for_build_type(path: &str, build_type: &str) -> Result<()> /// Copies files to output pub fn copy_to_output_by_path(path: &Path) -> Result<()> { - copy_to_output(path_to_str(path)) + copy_to_output(path_to_str(path)?) } -fn path_to_str(path: &Path) -> &str { - path.to_str() - .expect("Could not convert file path to string") +fn path_to_str(path: &Path) -> Result<&str> { + path.to_str().ok_or(anyhow!("Could not convert file path to string")) } pub fn copy_to_output_by_path_for_build_type(path: &Path, build_type: &str) -> Result<()> { - copy_to_output_for_build_type(path_to_str(path), build_type) + copy_to_output_for_build_type(path_to_str(path)?, build_type) } From 8bc1f0c412021548a8384ac193e4471fe555fc07 Mon Sep 17 00:00:00 2001 From: Chris Henk Date: Mon, 18 Dec 2023 20:35:30 -0800 Subject: [PATCH 20/21] fmt --- src/lib.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8a3c50b..d92353a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,10 +58,7 @@ use std::env; use std::path::Path; pub fn copy_to_output(path: &str) -> Result<()> { - copy_to_output_for_build_type( - path, - &env::var("PROFILE")?, - ) + copy_to_output_for_build_type(path, &env::var("PROFILE")?) } pub fn copy_to_output_for_build_type(path: &str, build_type: &str) -> Result<()> { @@ -96,7 +93,8 @@ pub fn copy_to_output_by_path(path: &Path) -> Result<()> { } fn path_to_str(path: &Path) -> Result<&str> { - path.to_str().ok_or(anyhow!("Could not convert file path to string")) + path.to_str() + .ok_or(anyhow!("Could not convert file path to string")) } pub fn copy_to_output_by_path_for_build_type(path: &Path, build_type: &str) -> Result<()> { From 53b1b6e04c4c07e39a655acfbbcbd2df52e3fdbe Mon Sep 17 00:00:00 2001 From: Chris Henk Date: Mon, 18 Dec 2023 21:36:39 -0800 Subject: [PATCH 21/21] better search --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index d92353a..2771cdd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,7 +71,7 @@ pub fn copy_to_output_for_build_type(path: &str, build_type: &str) -> Result<()> // If it is present, we know CompileKind::Target was used, otherwise CompileKind::Host was used. let triple = build_target::target_triple()?; - if env::var("OUT_DIR")?.contains(&triple) { + if env::var("OUT_DIR")?.contains(&format!("target{}{}", std::path::MAIN_SEPARATOR, triple)) { out_path.push(triple); }