From 5f885c1cefcc2ea733440aa3cf82b15db22d2a39 Mon Sep 17 00:00:00 2001 From: Mateusz Kowalski Date: Fri, 29 Nov 2024 16:53:10 +0100 Subject: [PATCH] Add package script test --- scarb/src/ops/package.rs | 8 ++- scarb/tests/package.rs | 56 ++++++++++++++++++- .../scarb-test-support/src/project_builder.rs | 14 ++++- 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/scarb/src/ops/package.rs b/scarb/src/ops/package.rs index be5f3147b..63588c6a5 100644 --- a/scarb/src/ops/package.rs +++ b/scarb/src/ops/package.rs @@ -2,8 +2,9 @@ use core::str; use std::collections::BTreeMap; use std::fs::File; use std::io::{Seek, SeekFrom, Write}; +use std::path::Path; -use anyhow::{bail, ensure, Context, Result}; +use anyhow::{anyhow, bail, ensure, Context, Result}; use camino::Utf8PathBuf; use indoc::{formatdoc, indoc, writedoc}; @@ -178,6 +179,11 @@ fn package_one_impl( "Running package script with package", &pkg_id.to_string(), )); + let script_path_string = script_definition.to_string(); + let script_path = Path::new(&script_path_string); + if !script_path.exists() || !script_path.is_file() { + return Err(anyhow!("Package script must be a path to an existing file")); + }; let absolute_path_script_definition = ScriptDefinition::new( pkg.root() .join(format!("{}", script_definition)) diff --git a/scarb/tests/package.rs b/scarb/tests/package.rs index 00d3cb279..3fc3d7fdc 100644 --- a/scarb/tests/package.rs +++ b/scarb/tests/package.rs @@ -14,7 +14,7 @@ use scarb::DEFAULT_TARGET_DIR_NAME; use scarb_build_metadata::CAIRO_VERSION; use scarb_test_support::cairo_plugin_project_builder::CairoPluginProjectBuilder; use scarb_test_support::command::Scarb; -use scarb_test_support::fsx::unix_paths_to_os_lossy; +use scarb_test_support::fsx::{make_executable, unix_paths_to_os_lossy}; use scarb_test_support::gitx; use scarb_test_support::project_builder::{Dep, DepBuilder, ProjectBuilder}; use scarb_test_support::registry::local::LocalRegistry; @@ -1525,3 +1525,57 @@ fn package_with_publish_disabled() { [..]Packaged [..] files, [..] ([..] compressed) "#}); } + +#[test] +fn package_with_package_script() { + let t = TempDir::new().unwrap(); + + #[cfg(not(windows))] + let script_name = "script.sh"; + #[cfg(not(windows))] + let script_code = indoc! { r#" + touch text.txt + "#}; + + #[cfg(windows)] + let script_name = "script.bat"; + #[cfg(windows)] + let script_code = indoc! { r#" + @echo off + copy NUL text.txt + "#}; + + ProjectBuilder::start() + .name("foo") + .version("1.0.0") + .manifest_extra(formatdoc! {r#" + [scripts] + package = "{script_name}" + "#}) + .src(script_name, script_code) + .build(&t); + + #[cfg(not(windows))] + make_executable(&t.to_path_buf().join(script_name)); + + Scarb::quick_snapbox() + .arg("package") + .arg("--no-verify") + .arg("--no-metadata") + .current_dir(&t) + .assert() + .success() + .stdout_matches(indoc! {r#" + [..]Packaging[..] + [..]Running package script with package foo v1.0.0[..] + [..]Packaged[..] + "#}); + + assert!(Path::new( + &t.to_path_buf() + .join("target") + .join("package") + .join("text.txt"), + ) + .exists()); +} diff --git a/utils/scarb-test-support/src/project_builder.rs b/utils/scarb-test-support/src/project_builder.rs index d62d7bdbd..51f1fe653 100644 --- a/utils/scarb-test-support/src/project_builder.rs +++ b/utils/scarb-test-support/src/project_builder.rs @@ -1,4 +1,6 @@ use std::collections::HashMap; +use std::fs; +use std::os::unix::fs::PermissionsExt; use std::sync::atomic::{AtomicU64, Ordering}; use assert_fs::fixture::ChildPath; @@ -171,7 +173,17 @@ impl ProjectBuilder { pub fn just_code(&self, t: &impl PathChild) { for (path, source) in &self.src { - t.child(path).write_str(source).unwrap(); + let file = t.child(path); + file.write_str(source).unwrap(); + // Get the current permissions + let mut permissions = fs::metadata(file.path()).unwrap().permissions(); + + // Add execute permissions to the existing permissions + let current_mode = permissions.mode(); + let new_mode = current_mode | 0o111; // Add execute permissions for user, group, others + + permissions.set_mode(new_mode); + fs::set_permissions(file.path(), permissions).unwrap(); } }