11
2+ #include " anyfin/threads.hpp"
3+
24#include " test_suite.hpp"
35
46extern File_Path working_directory; // Path to the root directory where the 'verify' program has been called
@@ -65,13 +67,13 @@ static void validate_binary (Memory_Arena &arena, String binary_name, String exp
6567 require (has_substring (output, expected_result));
6668}
6769
68- static u32 count_lines_starting_with (String output, String start_with, u32 expected_count) {
70+ static void require_lines_count (String output, String start_with, u32 expected_count) {
6971 u32 count = 0 ;
7072
7173 split_string (output, ' \n ' )
7274 .for_each ([&] (auto it) { if (starts_with (it, start_with)) count += 1 ; });
7375
74- return count;
76+ require ( count == expected_count) ;
7577}
7678
7779static void build_init_project_st_test (Memory_Arena &arena) {
@@ -110,7 +112,7 @@ static void build_testsite_tests (Memory_Arena &arena) {
110112 require (has_substring (output, concat_string (local, " Selected toolchain - " , toolchain)));
111113 require (has_substring (output, concat_string (local, " Selected configuration - " , config)));
112114
113- count_lines_starting_with (output, " Building file" , 9 );
115+ require_lines_count (output, " Building file" , 10 );
114116
115117 require_path_exists (cbuild_output_folder);
116118
@@ -126,28 +128,28 @@ static void build_registry_tests (Memory_Arena &arena) {
126128 auto executable_path = make_file_path (arena, " .cbuild" , " build" , " out" , " main.exe" );
127129
128130 auto output = build_testsite (arena);
129- count_lines_starting_with (output, " Building file" , 9 );
131+ require_lines_count (output, " Building file" , 10 );
130132 validate_binary (arena, " binary1" , " lib1,lib2,dyn1,dyn2,bin1" );
131133 validate_binary (arena, " binary2" , " lib3,dyn3,bin2" );
132134
133135 for (int idx = 0 ; idx < 5 ; idx++) {
134136 auto output2 = build_testsite (arena);
135- count_lines_starting_with (output2, " Building file" , 0 );
137+ require_lines_count (output2, " Building file" , 0 );
136138 validate_binary (arena, " binary1" , " lib1,lib2,dyn1,dyn2,bin1" );
137139 validate_binary (arena, " binary2" , " lib3,dyn3,bin2" );
138140 }
139141
140142 for (int idx = 0 ; idx < 5 ; idx++) {
141143 auto output2 = build_testsite (arena, " cache=off" );
142- count_lines_starting_with (output2, " Building file" , 9 );
144+ require_lines_count (output2, " Building file" , 10 );
143145
144146 validate_binary (arena, " binary1" , " lib1,lib2,dyn1,dyn2,bin1" );
145147 validate_binary (arena, " binary2" , " lib3,dyn3,bin2" );
146148 }
147149
148150 for (int idx = 0 ; idx < 5 ; idx++) {
149151 auto output2 = build_testsite (arena);
150- count_lines_starting_with (output2, " Building file" , 0 );
152+ require_lines_count (output2, " Building file" , 0 );
151153 validate_binary (arena, " binary1" , " lib1,lib2,dyn1,dyn2,bin1" );
152154 validate_binary (arena, " binary2" , " lib3,dyn3,bin2" );
153155 }
@@ -157,7 +159,7 @@ static void build_changes_tests (Memory_Arena &arena) {
157159 using enum File_System_Flags;
158160
159161 auto output = build_testsite (arena);
160- count_lines_starting_with (output, " Building file" , 9 );
162+ require_lines_count (output, " Building file" , 10 );
161163
162164 validate_binary (arena, " binary1" , " lib1,lib2,dyn1,dyn2,bin1" );
163165 validate_binary (arena, " binary2" , " lib3,dyn3,bin2" );
@@ -183,8 +185,8 @@ void library2 () {
183185 {
184186 auto output = build_testsite (arena);
185187
186- count_lines_starting_with (output, " Building file" , 1 ); // library2.cpp
187- count_lines_starting_with (output, " Linking target" , 3 ); // library2, dynamic2, binary1
188+ require_lines_count (output, " Building file" , 1 ); // library2.cpp
189+ require_lines_count (output, " Linking target" , 3 ); // library2, dynamic2, binary1
188190
189191 validate_binary (arena, " binary1" , " lib2_updated,dyn1,dyn2,bin1" );
190192 }
@@ -214,8 +216,8 @@ void library2 () {
214216 {
215217 auto output = build_testsite (arena);
216218
217- count_lines_starting_with (output, " Building file" , 3 ); // dynamic1, dynamic2, dynamic3
218- count_lines_starting_with (output, " Linking target" , 5 ); // dynamic1, dynamic2, dynamic3, binary1, binary2
219+ require_lines_count (output, " Building file" , 3 ); // dynamic1, dynamic2, dynamic3
220+ require_lines_count (output, " Linking target" , 5 ); // dynamic1, dynamic2, dynamic3, binary1, binary2
219221
220222 validate_binary (arena, " binary1" , " lib2_updated,dyn1,dyn2,bin1" );
221223 validate_binary (arena, " binary2" , " lib3,dyn3,bin2" );
@@ -226,7 +228,7 @@ static void build_errors_tests (Memory_Arena &arena) {
226228 using enum File_System_Flags;
227229
228230 auto output = build_testsite (arena);
229- count_lines_starting_with (output, " Building file" , 9 );
231+ require_lines_count (output, " Building file" , 10 );
230232
231233 validate_binary (arena, " binary1" , " lib1,lib2,dyn1,dyn2,bin1" );
232234 validate_binary (arena, " binary2" , " lib3,dyn3,bin2" );
@@ -268,9 +270,9 @@ void library2 () {
268270 require (build_result.is_ok ());
269271 require (build_result.value .status_code != 0 );
270272
271- count_lines_starting_with (build_result.value .output , " Building file" , 2 ); // dynamic1, library2
272- count_lines_starting_with (build_result.value .output , " Linking target" , 1 ); // library2
273- count_lines_starting_with (build_result.value .output , " Program terminated with an error status " , 1 );
273+ require_lines_count (build_result.value .output , " Building file" , 2 ); // dynamic1, library2
274+ require_lines_count (build_result.value .output , " Linking target" , 1 ); // library2
275+ require_lines_count (build_result.value .output , " WARNING: File compilation failed " , 1 );
274276 }
275277
276278 for (int idx = 0 ; idx < 5 ; idx++) {
@@ -279,9 +281,9 @@ void library2 () {
279281 require (build_result.is_ok ());
280282 require (build_result.value .status_code != 0 );
281283
282- count_lines_starting_with (build_result.value .output , " Building file" , 1 ); // dynamic1
283- count_lines_starting_with (build_result.value .output , " Linking target" , 0 );
284- count_lines_starting_with (build_result.value .output , " Program terminated with an error status " , 1 );
284+ require_lines_count (build_result.value .output , " Building file" , 1 ); // dynamic1
285+ require_lines_count (build_result.value .output , " Linking target" , 0 );
286+ require_lines_count (build_result.value .output , " WARNING: File compilation failed " , 1 );
285287 }
286288
287289 String fixed_code_impl = R"lib(
@@ -303,8 +305,8 @@ EXPORT_SYMBOL void dynamic1 () {
303305
304306 auto output3 = build_testsite (arena);
305307
306- count_lines_starting_with (output3, " Building file" , 1 ); // dynamic1
307- count_lines_starting_with (output3, " Linking target" , 3 ); // dynamic1, dynamic2, binary1
308+ require_lines_count (output3, " Building file" , 1 ); // dynamic1
309+ require_lines_count (output3, " Linking target" , 3 ); // dynamic1, dynamic2, binary1
308310
309311 validate_binary (arena, " binary1" , " lib2_updated,dyn1_updated,dyn2,bin1" );
310312 validate_binary (arena, " binary2" , " lib3,dyn3,bin2" );
@@ -315,42 +317,43 @@ static void test_modify_file (Memory_Arena &arena, File_Path file_path) {
315317
316318 auto file = open_file (move (file_path), Write_Access).value ;
317319 auto mapping = map_file_into_memory (file).value ;
318- defer {
319- unmap_file (mapping);
320- close_file (file);
321- };
322320
323321 auto file_content = reserve<char >(arena, mapping.size + 2 );
324322 copy_memory (file_content, mapping.memory , mapping.size );
325323 file_content[mapping.size ] = ' ' ;
326324 file_content[mapping.size + 1 ] = ' \0 ' ;
325+ unmap_file (mapping);
326+
327+ // Short sleep in case things go too fast and the update won't be noticed?
328+ thread_sleep (1000 );
327329
328330 reset_file_cursor (file);
329-
330331 require (write_bytes_to_file (file, file_content, mapping.size + 2 ));
332+
333+ close_file (file);
331334}
332335
333336static void build_project_tests (Memory_Arena &arena) {
334337 auto output = build_testsite (arena);
335- count_lines_starting_with (output, " Building file" , 9 );
336- count_lines_starting_with (output, " Linking target" , 9 );
338+ require_lines_count (output, " Building file" , 10 );
339+ require_lines_count (output, " Linking target" , 10 );
337340
338341 validate_binary (arena, " binary1" , " lib1,lib2,dyn1,dyn2,bin1" );
339342 validate_binary (arena, " binary2" , " lib3,dyn3,bin2" );
340343
341344 test_modify_file (arena, make_file_path (arena, " project" , " build.cpp" ));
342345
343346 auto output2 = build_testsite (arena);
344- count_lines_starting_with (output2, " Building file" , 9 );
345- count_lines_starting_with (output2, " Linking target" , 9 );
347+ require_lines_count (output2, " Building file" , 10 );
348+ require_lines_count (output2, " Linking target" , 10 );
346349
347350 validate_binary (arena, " binary1" , " lib1,lib2,dyn1,dyn2,bin1" );
348351 validate_binary (arena, " binary2" , " lib3,dyn3,bin2" );
349352
350353 for (int idx = 0 ; idx < 5 ; idx++) {
351354 auto output3 = build_testsite (arena);
352- count_lines_starting_with (output3, " Building file" , 0 );
353- count_lines_starting_with (output3, " Linking target" , 0 );
355+ require_lines_count (output3, " Building file" , 0 );
356+ require_lines_count (output3, " Linking target" , 0 );
354357
355358 validate_binary (arena, " binary1" , " lib1,lib2,dyn1,dyn2,bin1" );
356359 validate_binary (arena, " binary2" , " lib3,dyn3,bin2" );
@@ -366,51 +369,52 @@ static void build_cache_tests (Memory_Arena &arena) {
366369 require_path_exists (registry_file);
367370
368371 auto output = build_testsite (arena);
369- count_lines_starting_with (output, " Building file" , 9 );
370- count_lines_starting_with (output, " Linking target" , 9 );
372+ require_lines_count (output, " Building file" , 10 );
373+ require_lines_count (output, " Linking target" , 10 );
371374
372375 require_path_exists (registry_file);
373376
374377 auto output2 = build_testsite (arena, " cache=flush" );
375- count_lines_starting_with (output2, " Building file" , 9 );
376- count_lines_starting_with (output2, " Linking target" , 9 );
378+ require_lines_count (output2, " Building file" , 10 );
379+ require_lines_count (output2, " Linking target" , 10 );
377380
378381 require_path_exists (registry_file);
379382
380383 auto output3 = build_testsite (arena);
381- count_lines_starting_with (output3, " Building file" , 0 );
382- count_lines_starting_with (output3, " Linking target" , 0 );
384+ require_lines_count (output3, " Building file" , 0 );
385+ require_lines_count (output3, " Linking target" , 0 );
383386
384387 auto output4 = build_testsite (arena, " cache=off" );
385- count_lines_starting_with (output4, " Building file" , 9 );
386- count_lines_starting_with (output4, " Linking target" , 9 );
388+ require_lines_count (output4, " Building file" , 10 );
389+ require_lines_count (output4, " Linking target" , 10 );
387390
388391 validate_binary (arena, " binary1" , " lib1,lib2,dyn1,dyn2,bin1" );
389392 validate_binary (arena, " binary2" , " lib3,dyn3,bin2" );
390393}
391394
392395static void build_targets_tests (Memory_Arena &arena) {
393396 auto output = build_testsite (arena, " targets=library1" );
394- count_lines_starting_with (output, " Building file" , 1 ); // library1
395- count_lines_starting_with (output, " Linking target" , 1 ); // library1
397+ require_lines_count (output, " Building file" , 1 ); // library1
398+ require_lines_count (output, " Linking target" , 1 ); // library1
396399
397400 auto output3 = build_testsite (arena, " targets=binary2,library3" );
398- count_lines_starting_with (output3, " Building file" , 3 ); // binary2, dynamic3, library3
399- count_lines_starting_with (output3, " Linking target" , 3 );
401+ require_lines_count (output3, " Building file" , 3 ); // binary2, dynamic3, library3
402+ require_lines_count (output3, " Linking target" , 3 );
400403
401404 {
402- auto output = build_testsite (arena, " targets=,library1" );
403- count_lines_starting_with (output, " Building file" , 1 ); // library1
404- count_lines_starting_with (output, " Linking target" , 1 ); // library1
405+ // Thi was already build, we should not build it second time.
406+ auto output = build_testsite (arena, " targets=library1" );
407+ require_lines_count (output, " Building file" , 0 );
408+ require_lines_count (output, " Linking target" , 0 );
405409 }
406410
407411 auto output2 = build_testsite (arena, " targets=dynamic2," );
408- count_lines_starting_with (output2, " Building file" , 3 ); // library2, dynamic1, dynamic2
409- count_lines_starting_with (output2, " Linking target" , 3 );
412+ require_lines_count (output2, " Building file" , 3 ); // library2, dynamic1, dynamic2
413+ require_lines_count (output2, " Linking target" , 3 );
410414
411415 auto output4 = build_testsite (arena);
412- count_lines_starting_with (output4, " Building file" , 2 ); // binary1, library4
413- count_lines_starting_with (output4, " Linking target" , 2 );
416+ require_lines_count (output4, " Building file" , 3 ); // binary1, library4, binary3
417+ require_lines_count (output4, " Linking target" , 3 );
414418
415419 {
416420 auto build_command = format_string (arena, " % build targets=nonexisting" , binary_path);
@@ -420,6 +424,13 @@ static void build_targets_tests (Memory_Arena &arena) {
420424 require (has_substring (build_result.value .output , " Target 'nonexisting' not found in the project" ));
421425 }
422426
427+ {
428+ auto build_command = format_string (arena, " % build targets=library1,nonexisting" , binary_path);
429+ auto build_result = run_system_command (arena, build_command);
430+ require (build_result.is_ok ());
431+ require (build_result.value .status_code != 0 );
432+ require (has_substring (build_result.value .output , " Target 'nonexisting' not found in the project" ));
433+ }
423434}
424435
425436static Test_Case build_command_tests [] {
0 commit comments