11use clap:: Parser ;
22use std:: path:: Path ;
33use tempfile:: { tempdir, NamedTempFile , TempDir } ;
4+ use tmc_langs:: Compression ;
45use tmc_langs_cli:: app:: Cli ;
56use walkdir:: WalkDir ;
67
@@ -22,6 +23,38 @@ fn cp_exercise(path: &Path) -> TempDir {
2223 temp
2324}
2425
26+ fn compress_naive ( path : & impl AsRef < Path > , target : & impl AsRef < Path > , compression : Compression ) {
27+ let cli = Cli :: parse_from ( [
28+ "tmc-langs-cli" ,
29+ "--pretty" ,
30+ "compress-project" ,
31+ "--exercise-path" ,
32+ path_str ( path) ,
33+ "--output-path" ,
34+ path_str ( target) ,
35+ "--compression" ,
36+ & compression. to_string ( ) ,
37+ "--naive" ,
38+ ] ) ;
39+ tmc_langs_cli:: run ( cli) . unwrap ( ) ;
40+ }
41+
42+ fn extract_naive ( path : & impl AsRef < Path > , target : & impl AsRef < Path > , compression : Compression ) {
43+ let cli = Cli :: parse_from ( [
44+ "tmc-langs-cli" ,
45+ "--pretty" ,
46+ "extract-project" ,
47+ "--archive-path" ,
48+ path_str ( path) ,
49+ "--output-path" ,
50+ path_str ( target) ,
51+ "--compression" ,
52+ & compression. to_string ( ) ,
53+ "--naive" ,
54+ ] ) ;
55+ tmc_langs_cli:: run ( cli) . unwrap ( ) ;
56+ }
57+
2558fn path_str ( path : & impl AsRef < Path > ) -> & str {
2659 path. as_ref ( ) . to_str ( ) . unwrap ( )
2760}
@@ -40,6 +73,7 @@ fn sorted_list_of_files(path: &impl AsRef<Path>) -> Vec<String> {
4073
4174// wrapper for all sample exercise tests
4275fn test ( f : impl Fn ( & Path ) ) {
76+ let _ = env_logger:: try_init ( ) ;
4377 insta:: with_settings!( {
4478 filters => vec![
4579 // replace Windows-style path separators
@@ -56,7 +90,8 @@ fn test(f: impl Fn(&Path)) {
5690 ( r"C:/\S*/Temp/\S*" , "[PATH]" ) ,
5791 ] ,
5892 } , {
59- insta:: glob!( "sample_exercises/*/*" , |exercise| {
93+ insta:: glob!( "../../../" , "sample_exercises/*/*" , |exercise| {
94+ println!( "testing {exercise:?}" ) ;
6095 f( exercise)
6196 } )
6297 } )
@@ -100,3 +135,152 @@ fn clean() {
100135 insta:: assert_yaml_snapshot!( files) ;
101136 } )
102137}
138+
139+ #[ test]
140+ fn compress_project_tar ( ) {
141+ test ( |exercise| {
142+ let target = NamedTempFile :: new ( ) . unwrap ( ) ;
143+ let cli = Cli :: parse_from ( [
144+ "tmc-langs-cli" ,
145+ "--pretty" ,
146+ "compress-project" ,
147+ "--exercise-path" ,
148+ path_str ( & exercise) ,
149+ "--output-path" ,
150+ path_str ( & target) ,
151+ "--compression" ,
152+ "tar" ,
153+ ] ) ;
154+ let output = tmc_langs_cli:: run ( cli) . unwrap ( ) ;
155+ insta:: assert_yaml_snapshot!( output) ;
156+
157+ let extracted = tempdir ( ) . unwrap ( ) ;
158+ extract_naive ( & target, & extracted, Compression :: Tar ) ;
159+ let files = sorted_list_of_files ( & extracted) ;
160+ insta:: assert_yaml_snapshot!( files) ;
161+ } )
162+ }
163+
164+ #[ test]
165+ fn compress_project_zip ( ) {
166+ test ( |exercise| {
167+ let target = NamedTempFile :: new ( ) . unwrap ( ) ;
168+ let cli = Cli :: parse_from ( [
169+ "tmc-langs-cli" ,
170+ "--pretty" ,
171+ "compress-project" ,
172+ "--exercise-path" ,
173+ path_str ( & exercise) ,
174+ "--output-path" ,
175+ path_str ( & target) ,
176+ // zip should be the default
177+ // "--compression",
178+ // "zip",
179+ ] ) ;
180+ let output = tmc_langs_cli:: run ( cli) . unwrap ( ) ;
181+ insta:: assert_yaml_snapshot!( output) ;
182+
183+ let extracted = tempdir ( ) . unwrap ( ) ;
184+ extract_naive ( & target, & extracted, Compression :: Zip ) ;
185+ let files = sorted_list_of_files ( & extracted) ;
186+ insta:: assert_yaml_snapshot!( files) ;
187+ } )
188+ }
189+
190+ #[ test]
191+ fn compress_project_zstd ( ) {
192+ test ( |exercise| {
193+ let target = NamedTempFile :: new ( ) . unwrap ( ) ;
194+ let cli = Cli :: parse_from ( [
195+ "tmc-langs-cli" ,
196+ "--pretty" ,
197+ "compress-project" ,
198+ "--exercise-path" ,
199+ path_str ( & exercise) ,
200+ "--output-path" ,
201+ path_str ( & target) ,
202+ "--compression" ,
203+ "zstd" ,
204+ ] ) ;
205+ let output = tmc_langs_cli:: run ( cli) . unwrap ( ) ;
206+ insta:: assert_yaml_snapshot!( output) ;
207+
208+ let extracted = tempdir ( ) . unwrap ( ) ;
209+ extract_naive ( & target, & extracted, Compression :: TarZstd ) ;
210+ let files = sorted_list_of_files ( & extracted) ;
211+ insta:: assert_yaml_snapshot!( files) ;
212+ } )
213+ }
214+
215+ #[ test]
216+ fn extract_project_tar ( ) {
217+ test ( |exercise| {
218+ let compressed = NamedTempFile :: new ( ) . unwrap ( ) ;
219+ compress_naive ( & exercise, & compressed, Compression :: Tar ) ;
220+ let target = tempdir ( ) . unwrap ( ) ;
221+ let cli = Cli :: parse_from ( [
222+ "tmc-langs-cli" ,
223+ "--pretty" ,
224+ "extract-project" ,
225+ "--archive-path" ,
226+ path_str ( & compressed) ,
227+ "--output-path" ,
228+ path_str ( & target) ,
229+ "--compression" ,
230+ "tar" ,
231+ ] ) ;
232+ let output = tmc_langs_cli:: run ( cli) . unwrap ( ) ;
233+ insta:: assert_yaml_snapshot!( output) ;
234+ let files = sorted_list_of_files ( & target) ;
235+ insta:: assert_yaml_snapshot!( files) ;
236+ } )
237+ }
238+
239+ #[ test]
240+ fn extract_project_zip ( ) {
241+ test ( |exercise| {
242+ let compressed = NamedTempFile :: new ( ) . unwrap ( ) ;
243+ compress_naive ( & exercise, & compressed, Compression :: Zip ) ;
244+ let target = tempdir ( ) . unwrap ( ) ;
245+ let cli = Cli :: parse_from ( [
246+ "tmc-langs-cli" ,
247+ "--pretty" ,
248+ "extract-project" ,
249+ "--archive-path" ,
250+ path_str ( & compressed) ,
251+ "--output-path" ,
252+ path_str ( & target) ,
253+ // zip should be the default
254+ // "--compression",
255+ // "zip",
256+ ] ) ;
257+ let output = tmc_langs_cli:: run ( cli) . unwrap ( ) ;
258+ insta:: assert_yaml_snapshot!( output) ;
259+ let files = sorted_list_of_files ( & target) ;
260+ insta:: assert_yaml_snapshot!( files) ;
261+ } )
262+ }
263+
264+ #[ test]
265+ fn extract_project_zstd ( ) {
266+ test ( |exercise| {
267+ let compressed = NamedTempFile :: new ( ) . unwrap ( ) ;
268+ compress_naive ( & exercise, & compressed, Compression :: TarZstd ) ;
269+ let target = tempdir ( ) . unwrap ( ) ;
270+ let cli = Cli :: parse_from ( [
271+ "tmc-langs-cli" ,
272+ "--pretty" ,
273+ "extract-project" ,
274+ "--archive-path" ,
275+ path_str ( & compressed) ,
276+ "--output-path" ,
277+ path_str ( & target) ,
278+ "--compression" ,
279+ "zstd" ,
280+ ] ) ;
281+ let output = tmc_langs_cli:: run ( cli) . unwrap ( ) ;
282+ insta:: assert_yaml_snapshot!( output) ;
283+ let files = sorted_list_of_files ( & target) ;
284+ insta:: assert_yaml_snapshot!( files) ;
285+ } )
286+ }
0 commit comments