Skip to content

Commit

Permalink
test: Add unit tests to artifacts.rs module (#2680)
Browse files Browse the repository at this point in the history
<!-- Reference any GitHub issues resolved by this PR -->

Closes #2625 

## Introduced changes
Tests directory in `crates/scarb-api/tests/data/basic_package` for
integration tests starknet artifacts.


<!-- A brief description of the changes -->
Integration tests artifacts only get built when there's a tests
directory in a cairo package
-

## Checklist

<!-- Make sure all of these are complete -->

- [x] Linked relevant issue
- [x] Updated relevant documentation
- [x] Added relevant tests
- [x] Performed self-review of the code
- [x] Added changes to `CHANGELOG.md`
  • Loading branch information
Abeeujah authored Jan 22, 2025
1 parent 1de8ca3 commit ebde2bb
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ jobs:
cargo test --package forge --features scarb_2_8_3 e2e::features
- run: |
cargo test --package scarb-api --features scarb_2_8_3 get_starknet_artifacts_path
- run: |
cargo test --package scarb-api --features scarb_2_8_3 test_load_contracts_artifacts
test-forge-runner:
name: Test Forge Runner
Expand Down
115 changes: 113 additions & 2 deletions crates/scarb-api/src/artifacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ impl StarknetArtifactsFiles {
}
}

// TODO(#2625) add unit tests
pub(crate) fn load_contracts_artifacts(
self,
) -> Result<HashMap<String, (StarknetContractArtifacts, Utf8PathBuf)>> {
Expand All @@ -61,7 +60,6 @@ impl StarknetArtifactsFiles {
}
}

// TODO(#2625) add unit tests
fn unique_artifacts(
artifact_representations: Vec<StarknetArtifactsRepresentation>,
current_artifacts: &HashMap<String, (StarknetContractArtifacts, Utf8PathBuf)>,
Expand Down Expand Up @@ -92,3 +90,116 @@ fn compile_artifact_at_path(path: &Utf8Path) -> Result<StarknetContractArtifacts

Ok(StarknetContractArtifacts { sierra, casm })
}

#[cfg(test)]
mod tests {
use super::*;
use crate::ScarbCommand;
use assert_fs::fixture::{FileWriteStr, PathChild};
use camino::Utf8PathBuf;
use deserialized::{StarknetArtifacts, StarknetContract, StarknetContractArtifactPaths};
use indoc::indoc;

#[test]
fn test_unique_artifacts() {
// Mock StarknetArtifactsRepresentation
let mock_base_artifacts = HashMap::from([(
"contract1".to_string(),
(
StarknetContractArtifacts {
sierra: "sierra1".to_string(),
casm: "casm1".to_string(),
},
Utf8PathBuf::from("path1"),
),
)]);

let mock_representation_1 = StarknetArtifactsRepresentation {
base_path: Utf8PathBuf::from("mock/path1"),
artifacts: StarknetArtifacts {
version: 1,
contracts: vec![StarknetContract {
id: "1".to_string(),
package_name: "package1".to_string(),
contract_name: "contract1".to_string(),
artifacts: StarknetContractArtifactPaths {
sierra: Utf8PathBuf::from("mock/path1/contract1.sierra"),
},
}],
},
};

let mock_representation_2 = StarknetArtifactsRepresentation {
base_path: Utf8PathBuf::from("mock/path2"),
artifacts: StarknetArtifacts {
version: 1,
contracts: vec![StarknetContract {
id: "2".to_string(),
package_name: "package2".to_string(),
contract_name: "contract2".to_string(),
artifacts: StarknetContractArtifactPaths {
sierra: Utf8PathBuf::from("mock/path2/contract2.sierra"),
},
}],
},
};

let representations = vec![mock_representation_1, mock_representation_2];

let result = unique_artifacts(representations, &mock_base_artifacts);

assert_eq!(result.len(), 1);
assert_eq!(result[0].0, "contract2");
}

#[test]
#[cfg_attr(not(feature = "scarb_2_8_3"), ignore)]
fn test_load_contracts_artifacts() {
let temp = crate::tests::setup_package("basic_package");
let tests_dir = temp.join("tests");
fs::create_dir(&tests_dir).unwrap();

temp.child(tests_dir.join("test.cairo"))
.write_str(indoc!(
r"
#[test]
fn mock_test() {
assert!(true);
}
"
))
.unwrap();

ScarbCommand::new_with_stdio()
.current_dir(temp.path())
.arg("build")
.arg("--test")
.run()
.unwrap();

// Define path to the generated artifacts
let base_artifacts_path = temp.to_path_buf().join("target").join("dev");

// Get the base artifact
let base_file = Utf8PathBuf::from_path_buf(
base_artifacts_path.join("basic_package_integrationtest.test.starknet_artifacts.json"),
)
.unwrap();

// Load other artifact files and add them to the temporary directory
let other_files = vec![Utf8PathBuf::from_path_buf(
base_artifacts_path.join("basic_package_unittest.test.starknet_artifacts.json"),
)
.unwrap()];

// Create `StarknetArtifactsFiles`
let artifacts_files = StarknetArtifactsFiles::new(base_file, other_files);

// Load the contracts
let result = artifacts_files.load_contracts_artifacts().unwrap();

// Assert the Contract Artifacts are loaded.
assert!(result.contains_key("ERC20"));
assert!(result.contains_key("HelloStarknet"));
}
}
4 changes: 2 additions & 2 deletions crates/scarb-api/src/artifacts/representation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use anyhow::anyhow;
use camino::{Utf8Path, Utf8PathBuf};

pub struct StarknetArtifactsRepresentation {
base_path: Utf8PathBuf,
artifacts: StarknetArtifacts,
pub(crate) base_path: Utf8PathBuf,
pub(crate) artifacts: StarknetArtifacts,
}

impl StarknetArtifactsRepresentation {
Expand Down

0 comments on commit ebde2bb

Please sign in to comment.