Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add unit tests to artifacts.rs module #2680

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions crates/scarb-api/src/artifacts.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the line // TODO(#2625) add unit tests from this file

Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,107 @@ fn compile_artifact_at_path(path: &Utf8Path) -> Result<StarknetContractArtifacts

Ok(StarknetContractArtifacts { sierra, casm })
}

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

#[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");

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();
Comment on lines +174 to +188
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing the logic that creates the test file in basic_package that was added at some point.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done @cptartur


// 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();

// Ensure no errors and non-empty result
assert!(result.is_ok());

// Assert the Contract Artifacts are loaded.
let artifacts_map = result.unwrap();
println!("{:?}", artifacts_map);
assert!(!artifacts_map.is_empty());
assert!(artifacts_map.contains_key("ERC20"));
assert!(artifacts_map.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
Empty file.