Skip to content

Commit ebde2bb

Browse files
authored
test: Add unit tests to artifacts.rs module (#2680)
<!-- 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`
1 parent 1de8ca3 commit ebde2bb

File tree

3 files changed

+117
-4
lines changed

3 files changed

+117
-4
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ jobs:
160160
cargo test --package forge --features scarb_2_8_3 e2e::features
161161
- run: |
162162
cargo test --package scarb-api --features scarb_2_8_3 get_starknet_artifacts_path
163+
- run: |
164+
cargo test --package scarb-api --features scarb_2_8_3 test_load_contracts_artifacts
163165
164166
test-forge-runner:
165167
name: Test Forge Runner

crates/scarb-api/src/artifacts.rs

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ impl StarknetArtifactsFiles {
3434
}
3535
}
3636

37-
// TODO(#2625) add unit tests
3837
pub(crate) fn load_contracts_artifacts(
3938
self,
4039
) -> Result<HashMap<String, (StarknetContractArtifacts, Utf8PathBuf)>> {
@@ -61,7 +60,6 @@ impl StarknetArtifactsFiles {
6160
}
6261
}
6362

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

9391
Ok(StarknetContractArtifacts { sierra, casm })
9492
}
93+
94+
#[cfg(test)]
95+
mod tests {
96+
use super::*;
97+
use crate::ScarbCommand;
98+
use assert_fs::fixture::{FileWriteStr, PathChild};
99+
use camino::Utf8PathBuf;
100+
use deserialized::{StarknetArtifacts, StarknetContract, StarknetContractArtifactPaths};
101+
use indoc::indoc;
102+
103+
#[test]
104+
fn test_unique_artifacts() {
105+
// Mock StarknetArtifactsRepresentation
106+
let mock_base_artifacts = HashMap::from([(
107+
"contract1".to_string(),
108+
(
109+
StarknetContractArtifacts {
110+
sierra: "sierra1".to_string(),
111+
casm: "casm1".to_string(),
112+
},
113+
Utf8PathBuf::from("path1"),
114+
),
115+
)]);
116+
117+
let mock_representation_1 = StarknetArtifactsRepresentation {
118+
base_path: Utf8PathBuf::from("mock/path1"),
119+
artifacts: StarknetArtifacts {
120+
version: 1,
121+
contracts: vec![StarknetContract {
122+
id: "1".to_string(),
123+
package_name: "package1".to_string(),
124+
contract_name: "contract1".to_string(),
125+
artifacts: StarknetContractArtifactPaths {
126+
sierra: Utf8PathBuf::from("mock/path1/contract1.sierra"),
127+
},
128+
}],
129+
},
130+
};
131+
132+
let mock_representation_2 = StarknetArtifactsRepresentation {
133+
base_path: Utf8PathBuf::from("mock/path2"),
134+
artifacts: StarknetArtifacts {
135+
version: 1,
136+
contracts: vec![StarknetContract {
137+
id: "2".to_string(),
138+
package_name: "package2".to_string(),
139+
contract_name: "contract2".to_string(),
140+
artifacts: StarknetContractArtifactPaths {
141+
sierra: Utf8PathBuf::from("mock/path2/contract2.sierra"),
142+
},
143+
}],
144+
},
145+
};
146+
147+
let representations = vec![mock_representation_1, mock_representation_2];
148+
149+
let result = unique_artifacts(representations, &mock_base_artifacts);
150+
151+
assert_eq!(result.len(), 1);
152+
assert_eq!(result[0].0, "contract2");
153+
}
154+
155+
#[test]
156+
#[cfg_attr(not(feature = "scarb_2_8_3"), ignore)]
157+
fn test_load_contracts_artifacts() {
158+
let temp = crate::tests::setup_package("basic_package");
159+
let tests_dir = temp.join("tests");
160+
fs::create_dir(&tests_dir).unwrap();
161+
162+
temp.child(tests_dir.join("test.cairo"))
163+
.write_str(indoc!(
164+
r"
165+
#[test]
166+
fn mock_test() {
167+
assert!(true);
168+
}
169+
"
170+
))
171+
.unwrap();
172+
173+
ScarbCommand::new_with_stdio()
174+
.current_dir(temp.path())
175+
.arg("build")
176+
.arg("--test")
177+
.run()
178+
.unwrap();
179+
180+
// Define path to the generated artifacts
181+
let base_artifacts_path = temp.to_path_buf().join("target").join("dev");
182+
183+
// Get the base artifact
184+
let base_file = Utf8PathBuf::from_path_buf(
185+
base_artifacts_path.join("basic_package_integrationtest.test.starknet_artifacts.json"),
186+
)
187+
.unwrap();
188+
189+
// Load other artifact files and add them to the temporary directory
190+
let other_files = vec![Utf8PathBuf::from_path_buf(
191+
base_artifacts_path.join("basic_package_unittest.test.starknet_artifacts.json"),
192+
)
193+
.unwrap()];
194+
195+
// Create `StarknetArtifactsFiles`
196+
let artifacts_files = StarknetArtifactsFiles::new(base_file, other_files);
197+
198+
// Load the contracts
199+
let result = artifacts_files.load_contracts_artifacts().unwrap();
200+
201+
// Assert the Contract Artifacts are loaded.
202+
assert!(result.contains_key("ERC20"));
203+
assert!(result.contains_key("HelloStarknet"));
204+
}
205+
}

crates/scarb-api/src/artifacts/representation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use anyhow::anyhow;
33
use camino::{Utf8Path, Utf8PathBuf};
44

55
pub struct StarknetArtifactsRepresentation {
6-
base_path: Utf8PathBuf,
7-
artifacts: StarknetArtifacts,
6+
pub(crate) base_path: Utf8PathBuf,
7+
pub(crate) artifacts: StarknetArtifacts,
88
}
99

1010
impl StarknetArtifactsRepresentation {

0 commit comments

Comments
 (0)