@@ -34,7 +34,6 @@ impl StarknetArtifactsFiles {
34
34
}
35
35
}
36
36
37
- // TODO(#2625) add unit tests
38
37
pub ( crate ) fn load_contracts_artifacts (
39
38
self ,
40
39
) -> Result < HashMap < String , ( StarknetContractArtifacts , Utf8PathBuf ) > > {
@@ -61,7 +60,6 @@ impl StarknetArtifactsFiles {
61
60
}
62
61
}
63
62
64
- // TODO(#2625) add unit tests
65
63
fn unique_artifacts (
66
64
artifact_representations : Vec < StarknetArtifactsRepresentation > ,
67
65
current_artifacts : & HashMap < String , ( StarknetContractArtifacts , Utf8PathBuf ) > ,
@@ -92,3 +90,116 @@ fn compile_artifact_at_path(path: &Utf8Path) -> Result<StarknetContractArtifacts
92
90
93
91
Ok ( StarknetContractArtifacts { sierra, casm } )
94
92
}
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
+ }
0 commit comments