Skip to content

Commit

Permalink
Add package script test
Browse files Browse the repository at this point in the history
  • Loading branch information
wawel37 committed Nov 29, 2024
1 parent c28fa31 commit 4a68d19
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
8 changes: 7 additions & 1 deletion scarb/src/ops/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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))
Expand Down
56 changes: 55 additions & 1 deletion scarb/tests/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

0 comments on commit 4a68d19

Please sign in to comment.