| 1 | module builder |
| 2 | |
| 3 | import os |
| 4 | |
| 5 | fn test_is_tcc_compilation_failure_detects_tcc_kind() { |
| 6 | assert is_tcc_compilation_failure('cc', .tcc, '') |
| 7 | } |
| 8 | |
| 9 | fn test_is_tcc_compilation_failure_detects_tcc_compiler_name() { |
| 10 | assert is_tcc_compilation_failure('tcc', .unknown, '') |
| 11 | assert is_tcc_compilation_failure('/opt/v/thirdparty/tcc/tcc.exe', .unknown, '') |
| 12 | assert is_tcc_compilation_failure('/usr/local/bin/tcc-0.9.27', .unknown, '') |
| 13 | assert !is_tcc_compilation_failure('/usr/bin/clang', .unknown, '') |
| 14 | } |
| 15 | |
| 16 | fn test_is_tcc_compilation_failure_detects_tcc_output() { |
| 17 | assert is_tcc_compilation_failure('cc', .unknown, 'tcc: error: bad architecture') |
| 18 | assert is_tcc_compilation_failure('cc', .unknown, 'line 1\nline 2\ntcc: error: lib not found') |
| 19 | assert !is_tcc_compilation_failure('cc', .unknown, 'clang: error: unsupported option') |
| 20 | } |
| 21 | |
| 22 | fn test_c_compiler_failure_output_reports_final_compiler_after_tcc_retry_fails() { |
| 23 | tcc_res := os.Result{ |
| 24 | exit_code: 1 |
| 25 | output: 'tcc: stale retry failure' |
| 26 | } |
| 27 | clang_res := os.Result{ |
| 28 | exit_code: 1 |
| 29 | output: 'clang: final generated C failure' |
| 30 | } |
| 31 | failure_output := c_compiler_failure_output('clang', clang_res, tcc_res) |
| 32 | assert failure_output.display_ccompiler == 'tcc' |
| 33 | assert failure_output.display_res.output == tcc_res.output |
| 34 | assert failure_output.report_ccompiler == 'clang' |
| 35 | assert failure_output.report_res.output == clang_res.output |
| 36 | } |
| 37 | |
| 38 | fn test_c_compiler_failure_output_reports_displayed_compiler_without_tcc_retry() { |
| 39 | clang_res := os.Result{ |
| 40 | exit_code: 1 |
| 41 | output: 'clang: final generated C failure' |
| 42 | } |
| 43 | failure_output := c_compiler_failure_output('clang', clang_res, os.Result{}) |
| 44 | assert failure_output.display_ccompiler == 'clang' |
| 45 | assert failure_output.display_res.output == clang_res.output |
| 46 | assert failure_output.report_ccompiler == 'clang' |
| 47 | assert failure_output.report_res.output == clang_res.output |
| 48 | } |
| 49 | |
| 50 | fn test_is_tcc_compilation_failure_detects_tcc_alias_compiler() { |
| 51 | if os.user_os() == 'windows' { |
| 52 | return |
| 53 | } |
| 54 | test_root := os.join_path(os.vtmp_dir(), 'v_builder_cc_tcc_retry_test_${os.getpid()}') |
| 55 | cc_path := os.join_path(test_root, 'cc') |
| 56 | old_path := os.getenv('PATH') |
| 57 | os.mkdir_all(test_root) or { panic(err) } |
| 58 | os.write_file(cc_path, '#!/bin/sh\necho "Tiny C Compiler"\n') or { panic(err) } |
| 59 | os.chmod(cc_path, 0o700) or { panic(err) } |
| 60 | os.setenv('PATH', '${test_root}${os.path_delimiter}${old_path}', true) |
| 61 | defer { |
| 62 | os.setenv('PATH', old_path, true) |
| 63 | os.rmdir_all(test_root) or {} |
| 64 | } |
| 65 | assert is_tcc_compilation_failure('cc', .unknown, '') |
| 66 | } |
| 67 | |
| 68 | fn fake_windows_short_path(path string) string { |
| 69 | return path.replace(r'C:\Users\Léo', r'C:\Users\LEO~1') |
| 70 | } |
| 71 | |
| 72 | fn test_rewrite_windows_path_arg_rewrites_quoted_object_paths() { |
| 73 | arg := r'"C:\Users\Léo\.vmodules\.cache\bc\artifact.o"' |
| 74 | expected := r'"C:\Users\LEO~1\.vmodules\.cache\bc\artifact.o"' |
| 75 | assert rewrite_windows_path_arg(arg, fake_windows_short_path) == expected |
| 76 | } |
| 77 | |
| 78 | fn test_rewrite_windows_path_arg_rewrites_prefixed_paths() { |
| 79 | assert rewrite_windows_path_arg(r'-I"C:\Users\Léo\include"', fake_windows_short_path) == r'-I"C:\Users\LEO~1\include"' |
| 80 | assert rewrite_windows_path_arg(r'-L"C:\Users\Léo\lib"', fake_windows_short_path) == r'-L"C:\Users\LEO~1\lib"' |
| 81 | assert rewrite_windows_path_arg(r'-o "C:\Users\Léo\bin\tool.exe"', fake_windows_short_path) == r'-o "C:\Users\LEO~1\bin\tool.exe"' |
| 82 | } |
| 83 | |
| 84 | fn test_rewrite_windows_path_arg_leaves_non_paths_alone() { |
| 85 | for arg in ['-bt25', '-std=c99', '-D_DEFAULT_SOURCE'] { |
| 86 | assert rewrite_windows_path_arg(arg, fake_windows_short_path) == arg |
| 87 | } |
| 88 | } |
| 89 | |