@@ -10,6 +10,8 @@ use tmc_langs_framework::{
1010 domain:: {
1111 ExerciseDesc , RunResult , RunStatus , Strategy , TestDesc , TestResult , ValidationResult ,
1212 } ,
13+ error:: FileIo ,
14+ io:: file_util,
1315 plugin:: Language ,
1416 zip:: ZipArchive ,
1517 LanguagePlugin , TmcError ,
@@ -18,8 +20,8 @@ use tmc_langs_framework::{
1820use std:: collections:: HashMap ;
1921use std:: env;
2022use std:: ffi:: { OsStr , OsString } ;
21- use std:: fs:: { self , File } ;
22- use std:: io:: { BufReader , Cursor , Read , Seek , Write } ;
23+ use std:: fs:: File ;
24+ use std:: io:: { BufReader , Cursor , Read , Seek } ;
2325use std:: path:: { Path , PathBuf } ;
2426use std:: time:: Duration ;
2527use walkdir:: WalkDir ;
@@ -44,18 +46,13 @@ impl CSharpPlugin {
4446 if file. is_file ( ) {
4547 let target_file_path = target. join ( file. sanitized_name ( ) ) ;
4648 if let Some ( parent) = target_file_path. parent ( ) {
47- fs:: create_dir_all ( & parent)
48- . map_err ( |e| CSharpError :: CreateDir ( target_file_path. clone ( ) , e) ) ?;
49+ file_util:: create_dir_all ( & parent) ?;
4950 }
50- let mut target_file = File :: create ( & target_file_path)
51- . map_err ( |e| CSharpError :: CreateFile ( target_file_path. clone ( ) , e) ) ?;
5251 let bytes: Vec < u8 > = file
5352 . bytes ( )
5453 . collect :: < Result < Vec < _ > , _ > > ( )
55- . map_err ( |e| CSharpError :: ReadFile ( target_file_path. clone ( ) , e) ) ?;
56- target_file
57- . write_all ( & bytes)
58- . map_err ( |e| CSharpError :: WriteFile ( target_file_path, e) ) ?;
54+ . map_err ( |e| FileIo :: FileRead ( target_file_path. clone ( ) , e) ) ?;
55+ file_util:: write_to_file ( & mut bytes. as_slice ( ) , target_file_path) ?;
5956 }
6057 }
6158 Ok ( ( ) )
@@ -94,8 +91,7 @@ impl CSharpPlugin {
9491
9592 /// Parses the test results JSON file at the path argument.
9693 fn parse_test_results ( test_results_path : & Path ) -> Result < RunResult , CSharpError > {
97- let test_results = File :: open ( test_results_path)
98- . map_err ( |e| CSharpError :: ReadFile ( test_results_path. to_path_buf ( ) , e) ) ?;
94+ let test_results = file_util:: open_file ( test_results_path) ?;
9995 let test_results: Vec < CSTestResult > = serde_json:: from_reader ( test_results)
10096 . map_err ( |e| CSharpError :: ParseTestResults ( test_results_path. to_path_buf ( ) , e) ) ?;
10197
@@ -122,14 +118,13 @@ impl LanguagePlugin for CSharpPlugin {
122118 // checks the directories in src for csproj files
123119 fn is_exercise_type_correct ( path : & Path ) -> bool {
124120 WalkDir :: new ( path. join ( "src" ) )
125- . min_depth ( 2 )
126121 . max_depth ( 2 )
127122 . into_iter ( )
128123 . filter_map ( |e| e. ok ( ) )
129124 . any ( |e| e. path ( ) . extension ( ) == Some ( & OsString :: from ( "csproj" ) ) )
130125 }
131126
132- /// Finds .csproj files and checks whether they are in a X/src/ directory, returning X if so .
127+ /// Finds any directory X which contains a X/src/*.csproj file .
133128 fn find_project_dir_in_zip < R : Read + Seek > (
134129 zip_archive : & mut ZipArchive < R > ,
135130 ) -> Result < PathBuf , TmcError > {
@@ -155,21 +150,16 @@ impl LanguagePlugin for CSharpPlugin {
155150
156151 // runs --generate-points-file and parses the generated .tmc_available_points.json
157152 fn scan_exercise ( & self , path : & Path , exercise_name : String ) -> Result < ExerciseDesc , TmcError > {
158- let mut command = TmcCommand :: new ( "dotnet" ) ;
153+ let mut command = TmcCommand :: new ( "dotnet" . to_string ( ) ) ;
159154 command
160155 . current_dir ( path)
161156 . arg ( Self :: get_bootstrap_path ( ) ?)
162157 . arg ( "--generate-points-file" ) ;
163- let output = command. output ( ) ?;
164- log:: trace!( "stdout: {}" , String :: from_utf8_lossy( & output. stdout) ) ;
165- log:: debug!( "stderr: {}" , String :: from_utf8_lossy( & output. stderr) ) ;
166- if !output. status . success ( ) {
167- return Err ( CSharpError :: CommandFailed ( "dotnet" , output. status ) . into ( ) ) ;
168- }
158+ command. output_checked ( ) ?;
169159
170160 let exercise_desc_json_path = path. join ( ".tmc_available_points.json" ) ;
171161 let exercise_desc_json = File :: open ( & exercise_desc_json_path)
172- . map_err ( |e| CSharpError :: ReadFile ( exercise_desc_json_path. clone ( ) , e) ) ?;
162+ . map_err ( |e| FileIo :: FileRead ( exercise_desc_json_path. clone ( ) , e) ) ?;
173163 let json: HashMap < String , Vec < String > > =
174164 serde_json:: from_reader ( BufReader :: new ( exercise_desc_json) )
175165 . map_err ( |e| CSharpError :: ParseExerciseDesc ( exercise_desc_json_path, e) ) ?;
@@ -192,10 +182,9 @@ impl LanguagePlugin for CSharpPlugin {
192182 ) -> Result < RunResult , TmcError > {
193183 let test_results_path = path. join ( ".tmc_test_results.json" ) ;
194184 if test_results_path. exists ( ) {
195- fs:: remove_file ( & test_results_path)
196- . map_err ( |e| CSharpError :: RemoveFile ( test_results_path. clone ( ) , e) ) ?;
185+ file_util:: remove_file ( & test_results_path) ?;
197186 }
198- let mut command = TmcCommand :: new ( "dotnet" ) ;
187+ let mut command = TmcCommand :: new ( "dotnet" . to_string ( ) ) ;
199188 command
200189 . current_dir ( path)
201190 . arg ( Self :: get_bootstrap_path ( ) ?)
@@ -257,16 +246,14 @@ impl LanguagePlugin for CSharpPlugin {
257246 fn clean ( & self , path : & Path ) -> Result < ( ) , TmcError > {
258247 let test_results_path = path. join ( ".tmc_test_results.json" ) ;
259248 if test_results_path. exists ( ) {
260- fs:: remove_file ( & test_results_path)
261- . map_err ( |e| CSharpError :: RemoveFile ( test_results_path. clone ( ) , e) ) ?;
249+ file_util:: remove_file ( & test_results_path) ?;
262250 }
263251 for entry in WalkDir :: new ( path) . into_iter ( ) . filter_map ( |e| e. ok ( ) ) {
264252 let file_name = entry. path ( ) . file_name ( ) ;
265253 if file_name == Some ( & OsString :: from ( "bin" ) )
266254 || file_name == Some ( & OsString :: from ( "obj" ) )
267255 {
268- fs:: remove_dir_all ( entry. path ( ) )
269- . map_err ( |e| CSharpError :: RemoveDir ( entry. path ( ) . to_path_buf ( ) , e) ) ?;
256+ file_util:: remove_dir_all ( entry. path ( ) ) ?;
270257 }
271258 }
272259 Ok ( ( ) )
@@ -276,6 +263,7 @@ impl LanguagePlugin for CSharpPlugin {
276263#[ cfg( test) ]
277264mod test {
278265 use super :: * ;
266+ use std:: fs;
279267 use std:: sync:: Once ;
280268 use tempfile:: TempDir ;
281269
0 commit comments