@@ -30,7 +30,6 @@ pub fn get_artifact(artifact_name: &String, artifacts_file_path: &Path) -> Optio
3030 . map ( |artifact| artifact. 1 . clone ( ) )
3131}
3232
33- #[ cfg( feature = "encoding" ) ]
3433pub fn add_artifact ( artifact_name : String , manifest_hash : String , artifacts_file_path : & Path ) {
3534 let mut artifacts: Vec < ( String , String ) > = read_artifacts_file ( & artifacts_file_path)
3635 . iter ( )
@@ -44,7 +43,6 @@ pub fn add_artifact(artifact_name: String, manifest_hash: String, artifacts_file
4443 . expect ( "Couldn't write artifacts file" ) ;
4544}
4645
47- #[ cfg( feature = "encoding" ) ]
4846fn serialize_artifacts ( artifacts : Vec < ( String , String ) > ) -> String {
4947 let mut string = String :: new ( ) ;
5048
@@ -54,3 +52,95 @@ fn serialize_artifacts(artifacts: Vec<(String, String)>) -> String {
5452
5553 string
5654}
55+
56+ #[ cfg( test) ]
57+ mod tests {
58+ use std:: env:: temp_dir;
59+
60+ use super :: * ;
61+
62+ #[ test]
63+ fn simple_all ( ) {
64+ let artifacts = vec ! [
65+ ( "test1" . to_string( ) , 1 . to_string( ) ) ,
66+ ( "test2" . to_string( ) , 2 . to_string( ) ) ,
67+ ( "test3" . to_string( ) , 3 . to_string( ) ) ,
68+ ] ;
69+
70+ let artifact_file_path = temp_dir ( ) . join ( "LCAS_test_artifact_simple_all.test" ) ;
71+
72+ for ( artifact_name, manifest_hash) in & artifacts {
73+ add_artifact (
74+ artifact_name. to_string ( ) ,
75+ manifest_hash. to_string ( ) ,
76+ & artifact_file_path,
77+ ) ;
78+ }
79+
80+ assert_eq ! ( & read_artifacts_file( & artifact_file_path) , & artifacts) ;
81+
82+ assert_eq ! (
83+ get_artifact( & "test1" . to_string( ) , & artifact_file_path) . unwrap( ) ,
84+ 1 . to_string( )
85+ ) ;
86+ }
87+
88+ #[ test]
89+ fn get_artifact_not_found ( ) {
90+ let artifact_file_path = temp_dir ( ) . join ( "LCAS_test_artifact_not_found.test" ) ;
91+ // Ensure file is empty
92+ fs:: write ( & artifact_file_path, "" ) . unwrap ( ) ;
93+ assert_eq ! (
94+ get_artifact( & "nonexistent" . to_string( ) , & artifact_file_path) ,
95+ None
96+ ) ;
97+ }
98+
99+ #[ test]
100+ fn add_artifact_overwrites_existing ( ) {
101+ let artifact_file_path = temp_dir ( ) . join ( "LCAS_test_artifact_overwrite.test" ) ;
102+ add_artifact (
103+ "artifact" . to_string ( ) ,
104+ "hash1" . to_string ( ) ,
105+ & artifact_file_path,
106+ ) ;
107+ add_artifact (
108+ "artifact" . to_string ( ) ,
109+ "hash2" . to_string ( ) ,
110+ & artifact_file_path,
111+ ) ;
112+ let artifacts = read_artifacts_file ( & artifact_file_path) ;
113+ assert_eq ! ( artifacts. len( ) , 1 ) ;
114+ assert_eq ! ( artifacts[ 0 ] , ( "artifact" . to_string( ) , "hash2" . to_string( ) ) ) ;
115+ }
116+
117+ #[ test]
118+ fn read_artifacts_file_nonexistent ( ) {
119+ let artifact_file_path = temp_dir ( ) . join ( "LCAS_test_artifact_nonexistent.test" ) ;
120+ // Ensure file does not exist
121+ let _ = fs:: remove_file ( & artifact_file_path) ;
122+ let artifacts = read_artifacts_file ( & artifact_file_path) ;
123+ assert ! ( artifacts. is_empty( ) ) ;
124+ }
125+
126+ #[ test]
127+ #[ should_panic( expected = "Malformed artifacts file" ) ]
128+ fn read_artifacts_file_malformed ( ) {
129+ let artifact_file_path = temp_dir ( ) . join ( "LCAS_test_artifact_malformed.test" ) ;
130+ fs:: write ( & artifact_file_path, "bad_line_without_colon\n " ) . unwrap ( ) ;
131+ let _ = read_artifacts_file ( & artifact_file_path) ;
132+ }
133+
134+ #[ test]
135+ fn simple_serialize_artifacts ( ) {
136+ let artifacts = vec ! [
137+ ( "test1" . to_string( ) , 1 . to_string( ) ) ,
138+ ( "test2" . to_string( ) , 2 . to_string( ) ) ,
139+ ( "test3" . to_string( ) , 3 . to_string( ) ) ,
140+ ] ;
141+ assert_eq ! (
142+ serialize_artifacts( artifacts) ,
143+ "test1:1\n test2:2\n test3:3\n "
144+ )
145+ }
146+ }
0 commit comments