Skip to content

Commit

Permalink
Fix package op issues on Windows
Browse files Browse the repository at this point in the history
commit-id:75542114
  • Loading branch information
mkaput committed Oct 9, 2023
1 parent 8fd1c46 commit c2e1b1d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
18 changes: 13 additions & 5 deletions scarb/src/ops/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ fn list_one_impl(
fn prepare_archive_recipe(pkg: &Package) -> Result<ArchiveRecipe> {
let mut recipe = source_files(pkg)?;

// Sort the recipe before any checks, to ensure generated errors are reproducible.
sort_recipe(&mut recipe);

check_filenames(&recipe)?;
check_no_reserved_files(&recipe)?;

Expand All @@ -171,11 +174,8 @@ fn prepare_archive_recipe(pkg: &Package) -> Result<ArchiveRecipe> {
contents: ArchiveFileContents::Generated(Box::new(|| Ok(VERSION.to_string().into_bytes()))),
});

// Sort archive files alphabetically, putting the version file first.
recipe.sort_unstable_by_key(|f| {
let priority = if f.path == VERSION_FILE_NAME { 0 } else { 1 };
(priority, f.path.clone())
});
// Put generated files in right order within the recipe.
sort_recipe(&mut recipe);

// Assert there are no duplicates. We make use of the fact, that recipe is now sorted.
assert!(
Expand Down Expand Up @@ -237,6 +237,14 @@ fn check_filenames(recipe: &ArchiveRecipe) -> Result<()> {
Ok(())
}

/// Sort archive files alphabetically, putting the version file first.
fn sort_recipe(recipe: &mut ArchiveRecipe) {
recipe.sort_unstable_by_key(|f| {
let priority = if f.path == VERSION_FILE_NAME { 0 } else { 1 };
(priority, f.path.clone())
});
}

fn normalize_manifest(pkg: Package) -> Result<Vec<u8>> {
let mut buf = Vec::new();

Expand Down
25 changes: 14 additions & 11 deletions scarb/tests/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use itertools::Itertools;
use test_case::test_case;

use scarb_test_support::command::Scarb;
use scarb_test_support::fsx::unix_paths_to_os_lossy;
use scarb_test_support::gitx;
use scarb_test_support::project_builder::{Dep, DepBuilder, ProjectBuilder};
use scarb_test_support::workspace_builder::WorkspaceBuilder;
Expand Down Expand Up @@ -224,13 +225,13 @@ fn list_simple() {
.current_dir(&t)
.assert()
.success()
.stdout_eq(indoc! {r#"
.stdout_eq(unix_paths_to_os_lossy(indoc! {r#"
VERSION
Scarb.orig.toml
Scarb.toml
src/foo.cairo
src/lib.cairo
"#});
"#}));
}

#[test]
Expand All @@ -254,7 +255,7 @@ fn list_workspace() {
.current_dir(&t)
.assert()
.success()
.stdout_eq(indoc! {r#"
.stdout_eq(unix_paths_to_os_lossy(indoc! {r#"
first:
VERSION
Scarb.orig.toml
Expand All @@ -266,7 +267,7 @@ fn list_workspace() {
Scarb.orig.toml
Scarb.toml
src/lib.cairo
"#});
"#}));
}

#[test]
Expand Down Expand Up @@ -536,12 +537,12 @@ fn list_ignore_nested() {
.current_dir(&t)
.assert()
.success()
.stdout_eq(indoc! {r#"
.stdout_eq(unix_paths_to_os_lossy(indoc! {r#"
VERSION
Scarb.orig.toml
Scarb.toml
src/lib.cairo
"#});
"#}));
}

// TODO(mkaput): Invalid readme/license path
Expand Down Expand Up @@ -707,12 +708,12 @@ fn exclude_dot_files_and_directories_by_default() {
.current_dir(&t)
.assert()
.success()
.stdout_eq(indoc! {r#"
.stdout_eq(unix_paths_to_os_lossy(indoc! {r#"
VERSION
Scarb.orig.toml
Scarb.toml
src/lib.cairo
"#});
"#}));
}

#[test]
Expand Down Expand Up @@ -776,13 +777,15 @@ fn ignore_file(ignore_path: &str, setup_git: bool, expect_ignore_to_work: bool)
expected.push("src/lib.cairo");
expected.push(""); // Ensure there's trailing \n

let expected = unix_paths_to_os_lossy(&expected.join("\n"));

Scarb::quick_snapbox()
.arg("package")
.arg("--list")
.current_dir(g.p)
.assert()
.success()
.stdout_eq(expected.join("\n"));
.stdout_eq(expected);
}

#[test]
Expand Down Expand Up @@ -814,11 +817,11 @@ fn ignore_whitelist_pattern() {
.current_dir(&t)
.assert()
.success()
.stdout_eq(indoc! {r#"
.stdout_eq(unix_paths_to_os_lossy(indoc! {r#"
VERSION
Scarb.orig.toml
Scarb.toml
noignore.txt
src/lib.cairo
"#});
"#}));
}
13 changes: 13 additions & 0 deletions utils/scarb-test-support/src/fsx.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fs::File;
use std::io::BufReader;
use std::path::MAIN_SEPARATOR_STR;

use assert_fs::fixture::ChildPath;
use assert_fs::TempDir;
Expand Down Expand Up @@ -61,3 +62,15 @@ impl ChildPathEx for ChildPath {
serde_json::from_reader(reader).unwrap()
}
}

/// Convert all UNIX-style paths in a string to platform native.
///
/// This method is doing dump pattern search & replace, it might replace unexpected parts of the
/// input string. Use with caution.
pub fn unix_paths_to_os_lossy(text: &str) -> String {
if cfg!(unix) {
text.to_string()
} else {
text.replace('/', MAIN_SEPARATOR_STR)
}
}

0 comments on commit c2e1b1d

Please sign in to comment.