Skip to content

Commit

Permalink
Support .scarbignore files when packaging
Browse files Browse the repository at this point in the history
This PR makes `scarb package` check for `.scarbignore` files when determining package source files set. This is a trivial addition thanks to features of the `ignore` crate.

Supporting this file is effectively a similar feature to the include/exclude fields of Cargo (https://doc.rust-lang.org/cargo/reference/manifest.html#the-exclude-and-include-fields). This PR fixes #644 then, as we do not want to create multiple ways of achieving the same thing.

commit-id:3e87fb71
  • Loading branch information
mkaput committed Oct 5, 2023
1 parent 7fa9827 commit eeb8d74
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
5 changes: 3 additions & 2 deletions scarb/src/core/publishing/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use ignore::{DirEntry, WalkBuilder};

use crate::core::Package;
use crate::internal::fsx::PathBufUtf8Ext;
use crate::{DEFAULT_TARGET_DIR_NAME, LOCK_FILE_NAME, MANIFEST_FILE_NAME};
use crate::{DEFAULT_TARGET_DIR_NAME, LOCK_FILE_NAME, MANIFEST_FILE_NAME, SCARB_IGNORE_FILE_NAME};

/// List all files relevant to building this package inside this source.
///
/// The basic assumption is that all files in the package directory are relevant for building this
/// package, provided that they potentially can be committed to the source directory. The following
/// rules hold:
/// * Look for any `.gitignore` or `.ignore`-like files, using the [`ignore`] crate.
/// * Look for any `.scarbignore`, `.gitignore` or `.ignore`-like files, using the [`ignore`] crate.
/// * Skip `.git` directory.
/// * Skip any subdirectories containing `Scarb.toml`.
/// * Skip `<root>/target` directory.
Expand Down Expand Up @@ -63,6 +63,7 @@ fn push_worktree_files(pkg: &Package, ret: &mut Vec<Utf8PathBuf>) -> Result<()>
.parents(false)
.require_git(true)
.same_file_system(true)
.add_custom_ignore_filename(SCARB_IGNORE_FILE_NAME)
.filter_entry(filter)
.build()
.try_for_each(|entry| {
Expand Down
1 change: 1 addition & 0 deletions scarb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ pub const DEFAULT_SOURCE_PATH: &str = "src/lib.cairo";
pub const DEFAULT_MODULE_MAIN_FILE: &str = "lib.cairo";
pub const DEFAULT_TESTS_PATH: &str = "tests";
pub const DEFAULT_TARGET_DIR_NAME: &str = "target";
pub const SCARB_IGNORE_FILE_NAME: &str = ".scarbignore";
42 changes: 42 additions & 0 deletions scarb/tests/package.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::items_after_test_module)]

use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::io::{BufReader, Read};
Expand Down Expand Up @@ -741,9 +743,11 @@ fn clean_tar_headers() {
#[test_case("../.gitignore", false, false; "gitignore outside")]
#[test_case("../.gitignore", true, false; "gitignore outside with git")]
#[test_case("../.ignore", false, false; "ignore outside")]
#[test_case("../.scarbignore", false, false; "scarbignore outside")]
#[test_case(".gitignore", false, false; "gitignore inside")]
#[test_case(".gitignore", true, true; "gitignore inside with git")]
#[test_case(".ignore", false, true; "ignore inside")]
#[test_case(".scarbignore", false, true; "scarbignore inside")]
fn ignore_file(ignore_path: &str, setup_git: bool, expect_ignore_to_work: bool) {
let g = gitx::new_conditional(setup_git, "package", |t| {
ProjectBuilder::start()
Expand Down Expand Up @@ -780,3 +784,41 @@ fn ignore_file(ignore_path: &str, setup_git: bool, expect_ignore_to_work: bool)
.success()
.stdout_eq(expected.join("\n"));
}

#[test]
fn ignore_whitelist_pattern() {
let t = TempDir::new().unwrap();
ProjectBuilder::start()
.name("foo")
.version("1.0.0")
.src("ignore.txt", "")
.src("noignore.txt", "")
.src("src/ignore.txt", "")
.build(&t);

t.child(".scarbignore")
.write_str(indoc! {r#"
*
!*/
!Scarb.toml
!src/
!src/*
src/ignore.*
!noignore.txt
"#})
.unwrap();

Scarb::quick_snapbox()
.arg("package")
.arg("--list")
.current_dir(&t)
.assert()
.success()
.stdout_eq(indoc! {r#"
VERSION
Scarb.orig.toml
Scarb.toml
noignore.txt
src/lib.cairo
"#});
}

0 comments on commit eeb8d74

Please sign in to comment.