@@ -43,11 +43,8 @@ pub fn main() !void {
43
43
.{ .func = testGodboltApi , .name = "godbolt API" },
44
44
.{ .func = testMissingOutputPath , .name = "missing output path" },
45
45
.{ .func = testZigFmt , .name = "zig fmt" },
46
- }
47
- // Test writer was not able to figure out how to start a child process and
48
- // give an fd to it's stdin. fork/exec works, but we are limiting ourselves
49
- // to POSIX.
50
- ++ if (builtin .os .tag == .windows ) [0 ]Test {} else [1 ]Test {.{ .func = testZigCC , .name = "zig cc" }};
46
+ .{ .func = testZigCC , .name = "zig cc" },
47
+ };
51
48
inline for (tests ) | t | {
52
49
try fs .cwd ().deleteTree (dir_path );
53
50
try fs .cwd ().makeDir (dir_path );
@@ -68,7 +65,7 @@ fn printCmd(cwd: []const u8, argv: []const []const u8) void {
68
65
std .debug .print ("\n " , .{});
69
66
}
70
67
71
- fn exec (cwd : []const u8 , expect_0 : bool , argv : []const []const u8 ) ! ChildProcess.ExecResult {
68
+ fn exec (cwd : []const u8 , expect_code : u8 , argv : []const []const u8 ) ! ChildProcess.ExecResult {
72
69
const max_output_size = 100 * 1024 ;
73
70
const result = ChildProcess .exec (.{
74
71
.allocator = a ,
@@ -82,8 +79,11 @@ fn exec(cwd: []const u8, expect_0: bool, argv: []const []const u8) !ChildProcess
82
79
};
83
80
switch (result .term ) {
84
81
.Exited = > | code | {
85
- if ((code != 0 ) == expect_0 ) {
86
- std .debug .print ("The following command exited with error code {}:\n " , .{code });
82
+ if (code != expect_code ) {
83
+ std .debug .print (
84
+ "The following command exited with error code {}, expected {}:\n " ,
85
+ .{ code , expect_code },
86
+ );
87
87
printCmd (cwd , argv );
88
88
std .debug .print ("stderr:\n {s}\n " , .{result .stderr });
89
89
return error .CommandFailed ;
@@ -100,34 +100,45 @@ fn exec(cwd: []const u8, expect_0: bool, argv: []const []const u8) !ChildProcess
100
100
}
101
101
102
102
fn testZigInitLib (zig_exe : []const u8 , dir_path : []const u8 ) ! void {
103
- _ = try exec (dir_path , true , &[_ ][]const u8 { zig_exe , "init-lib" });
104
- const test_result = try exec (dir_path , true , &[_ ][]const u8 { zig_exe , "build" , "test" });
103
+ _ = try exec (dir_path , 0 , &[_ ][]const u8 { zig_exe , "init-lib" });
104
+ const test_result = try exec (dir_path , 0 , &[_ ][]const u8 { zig_exe , "build" , "test" });
105
105
try testing .expectStringEndsWith (test_result .stderr , "All 1 tests passed.\n " );
106
106
}
107
107
108
108
fn testZigInitExe (zig_exe : []const u8 , dir_path : []const u8 ) ! void {
109
- _ = try exec (dir_path , true , &[_ ][]const u8 { zig_exe , "init-exe" });
110
- const run_result = try exec (dir_path , true , &[_ ][]const u8 { zig_exe , "build" , "run" });
109
+ _ = try exec (dir_path , 0 , &[_ ][]const u8 { zig_exe , "init-exe" });
110
+ const run_result = try exec (dir_path , 0 , &[_ ][]const u8 { zig_exe , "build" , "run" });
111
111
try testing .expectEqualStrings ("All your codebase are belong to us.\n " , run_result .stderr );
112
112
try testing .expectEqualStrings ("Run `zig build test` to run the tests.\n " , run_result .stdout );
113
113
}
114
114
115
115
fn testZigCC (zig_exe : []const u8 , dir_path : []const u8 ) ! void {
116
+ // Test writer was not able to figure out how to start a child process and
117
+ // give an fd to it's stdin. fork/exec works, but we are limiting ourselves
118
+ // to POSIX.
119
+ if (builtin .os .tag == .windows )
120
+ // no error.SkipZigTest, because this is not a "test {...}" block.
121
+ return ;
122
+
123
+ return testZigCCPosix (zig_exe , dir_path );
124
+ }
125
+
126
+ fn testZigCCPosix (zig_exe : []const u8 , dir_path : []const u8 ) ! void {
116
127
var tmp = testing .tmpDir (.{});
117
128
defer tmp .cleanup ();
118
- try tmp .dir .writeFile ("truth.c" , "int truth = 42;" );
129
+ try tmp .dir .writeFile ("truth.c" , "int main() { return 42; } " );
119
130
var infile = try tmp .dir .openFile ("truth.c" , .{});
120
131
121
132
const pid_result = try std .os .fork ();
122
133
const zig_exe0 = try a .dupeZ (u8 , zig_exe );
123
- const outfile = try fs .path .join (a , &[_ ][]const u8 { dir_path , "truth.o " });
134
+ const outfile = try fs .path .joinZ (a , &[_ ][]const u8 { dir_path , "truth" });
124
135
if (pid_result == 0 ) {
125
136
try std .os .dup2 (infile .handle , std .os .STDIN_FILENO );
126
137
const argv = &[_ :null ]? [* :0 ]const u8 {
127
138
zig_exe0 , "cc" ,
128
- "-o" , try a . dupeZ ( u8 , outfile ) ,
139
+ "-o" , outfile ,
129
140
"-x" , "c" ,
130
- "-c" , "- " ,
141
+ "-" ,
131
142
};
132
143
const envp = &[_ :null ]? [* :0 ]const u8 {
133
144
try std .fmt .allocPrintZ (a , "ZIG_GLOBAL_CACHE_DIR={s}" , .{dir_path }),
@@ -144,9 +155,8 @@ fn testZigCC(zig_exe: []const u8, dir_path: []const u8) !void {
144
155
145
156
try testing .expectEqual (@as (u32 , 0 ), res .status );
146
157
147
- // absence of failure to open the file means success
148
- const outf = try fs .cwd ().openFile (outfile , .{});
149
- outf .close ();
158
+ // run the compiled executable and check if it's telling the truth.
159
+ _ = try exec (dir_path , 42 , &[_ ][]const u8 {outfile });
150
160
}
151
161
152
162
fn testGodboltApi (zig_exe : []const u8 , dir_path : []const u8 ) anyerror ! void {
@@ -181,7 +191,7 @@ fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void {
181
191
const emit_asm_arg = try std .fmt .allocPrint (a , "-femit-asm={s}" , .{example_s_path });
182
192
try args .append (emit_asm_arg );
183
193
184
- _ = try exec (dir_path , true , args .items );
194
+ _ = try exec (dir_path , 0 , args .items );
185
195
186
196
const out_asm = try std .fs .cwd ().readFileAlloc (a , example_s_path , std .math .maxInt (usize ));
187
197
try testing .expect (std .mem .indexOf (u8 , out_asm , "square:" ) != null );
@@ -190,38 +200,38 @@ fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void {
190
200
}
191
201
192
202
fn testMissingOutputPath (zig_exe : []const u8 , dir_path : []const u8 ) ! void {
193
- _ = try exec (dir_path , true , &[_ ][]const u8 { zig_exe , "init-exe" });
203
+ _ = try exec (dir_path , 0 , &[_ ][]const u8 { zig_exe , "init-exe" });
194
204
const output_path = try fs .path .join (a , &[_ ][]const u8 { "does" , "not" , "exist" , "foo.exe" });
195
205
const output_arg = try std .fmt .allocPrint (a , "-femit-bin={s}" , .{output_path });
196
206
const source_path = try fs .path .join (a , &[_ ][]const u8 { "src" , "main.zig" });
197
- const result = try exec (dir_path , false , &[_ ][]const u8 { zig_exe , "build-exe" , source_path , output_arg });
207
+ const result = try exec (dir_path , 1 , &[_ ][]const u8 { zig_exe , "build-exe" , source_path , output_arg });
198
208
const s = std .fs .path .sep_str ;
199
209
const expected : []const u8 = "error: unable to open output directory 'does" ++ s ++ "not" ++ s ++ "exist': FileNotFound\n " ;
200
210
try testing .expectEqualStrings (expected , result .stderr );
201
211
}
202
212
203
213
fn testZigFmt (zig_exe : []const u8 , dir_path : []const u8 ) ! void {
204
- _ = try exec (dir_path , true , &[_ ][]const u8 { zig_exe , "init-exe" });
214
+ _ = try exec (dir_path , 0 , &[_ ][]const u8 { zig_exe , "init-exe" });
205
215
206
216
const unformatted_code = " // no reason for indent" ;
207
217
208
218
const fmt1_zig_path = try fs .path .join (a , &[_ ][]const u8 { dir_path , "fmt1.zig" });
209
219
try fs .cwd ().writeFile (fmt1_zig_path , unformatted_code );
210
220
211
- const run_result1 = try exec (dir_path , true , &[_ ][]const u8 { zig_exe , "fmt" , fmt1_zig_path });
221
+ const run_result1 = try exec (dir_path , 0 , &[_ ][]const u8 { zig_exe , "fmt" , fmt1_zig_path });
212
222
// stderr should be file path + \n
213
223
try testing .expect (std .mem .startsWith (u8 , run_result1 .stdout , fmt1_zig_path ));
214
224
try testing .expect (run_result1 .stdout .len == fmt1_zig_path .len + 1 and run_result1 .stdout [run_result1 .stdout .len - 1 ] == '\n ' );
215
225
216
226
const fmt2_zig_path = try fs .path .join (a , &[_ ][]const u8 { dir_path , "fmt2.zig" });
217
227
try fs .cwd ().writeFile (fmt2_zig_path , unformatted_code );
218
228
219
- const run_result2 = try exec (dir_path , true , &[_ ][]const u8 { zig_exe , "fmt" , dir_path });
229
+ const run_result2 = try exec (dir_path , 0 , &[_ ][]const u8 { zig_exe , "fmt" , dir_path });
220
230
// running it on the dir, only the new file should be changed
221
231
try testing .expect (std .mem .startsWith (u8 , run_result2 .stdout , fmt2_zig_path ));
222
232
try testing .expect (run_result2 .stdout .len == fmt2_zig_path .len + 1 and run_result2 .stdout [run_result2 .stdout .len - 1 ] == '\n ' );
223
233
224
- const run_result3 = try exec (dir_path , true , &[_ ][]const u8 { zig_exe , "fmt" , dir_path });
234
+ const run_result3 = try exec (dir_path , 0 , &[_ ][]const u8 { zig_exe , "fmt" , dir_path });
225
235
// both files have been formatted, nothing should change now
226
236
try testing .expect (run_result3 .stdout .len == 0 );
227
237
@@ -230,7 +240,7 @@ fn testZigFmt(zig_exe: []const u8, dir_path: []const u8) !void {
230
240
var unformatted_code_utf16 = "\xff\xfe \x00 \x00 \x00 \x00 /\x00 /\x00 \x00 n\x00 o\x00 \x00 r\x00 e\x00 a\x00 s\x00 o\x00 n\x00 " ;
231
241
try fs .cwd ().writeFile (fmt4_zig_path , unformatted_code_utf16 );
232
242
233
- const run_result4 = try exec (dir_path , true , &[_ ][]const u8 { zig_exe , "fmt" , dir_path });
243
+ const run_result4 = try exec (dir_path , 0 , &[_ ][]const u8 { zig_exe , "fmt" , dir_path });
234
244
try testing .expect (std .mem .startsWith (u8 , run_result4 .stdout , fmt4_zig_path ));
235
245
try testing .expect (run_result4 .stdout .len == fmt4_zig_path .len + 1 and run_result4 .stdout [run_result4 .stdout .len - 1 ] == '\n ' );
236
246
}
0 commit comments