-
Notifications
You must be signed in to change notification settings - Fork 63
/
test.zig
415 lines (378 loc) · 16.8 KB
/
test.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
const std = @import("std");
const builtin = @import("builtin");
const testing = std.testing;
const sep = std.fs.path.sep_str;
const path_env_sep = if (builtin.os.tag == .windows) ";" else ":";
const fixdeletetree = @import("fixdeletetree.zig");
var child_env_map: std.process.EnvMap = undefined;
var path_env_ptr: *[]const u8 = undefined;
fn setPathEnv(new_path: []const u8) void {
path_env_ptr.* = new_path;
std.log.info("PATH={s}", .{new_path});
}
// For some odd reason, the "zig version" output is different on macos
const expected_zig_version_0_7_0 = if (builtin.os.tag == .macos) "0.7.0+9af53f8e0" else "0.7.0";
pub fn main() !u8 {
std.log.info("running test!", .{});
try fixdeletetree.deleteTree(std.fs.cwd(), "scratch");
try std.fs.cwd().makeDir("scratch");
const bin_dir = "scratch" ++ sep ++ "bin";
try std.fs.cwd().makeDir(bin_dir);
const install_dir = if (builtin.os.tag == .windows) (bin_dir ++ "\\zig") else ("scratch/install");
try std.fs.cwd().makeDir(install_dir);
// NOTE: for now we are incorrectly assuming the install dir is CWD/zig-out
const zigup = comptime "." ++ sep ++ bin_dir ++ sep ++ "zigup" ++ builtin.target.exeFileExt();
try std.fs.cwd().copyFile(
comptime "zig-out" ++ sep ++ "bin" ++ sep ++ "zigup" ++ builtin.target.exeFileExt(),
std.fs.cwd(),
zigup,
.{},
);
if (builtin.os.tag == .windows) {
const zigup_pdb = comptime "." ++ sep ++ bin_dir ++ sep ++ "zigup.pdb";
try std.fs.cwd().copyFile(
comptime "zig-out" ++ sep ++ "bin" ++ sep ++ "zigup.pdb",
std.fs.cwd(),
zigup_pdb,
.{},
);
}
const install_args = if (builtin.os.tag == .windows) [_][]const u8{} else [_][]const u8{ "--install-dir", install_dir };
const zigup_args = &[_][]const u8{zigup} ++ install_args;
var allocator_store = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer allocator_store.deinit();
const allocator = allocator_store.allocator();
const path_link = try std.fs.path.join(allocator, &.{ bin_dir, comptime "zig" ++ builtin.target.exeFileExt() });
defer allocator.free(path_link);
// add our scratch/bin directory to PATH
child_env_map = try std.process.getEnvMap(allocator);
path_env_ptr = child_env_map.getPtr("PATH") orelse {
std.log.err("the PATH environment variable does not exist?", .{});
return 1;
};
const cwd = try std.process.getCwdAlloc(allocator);
const original_path_env = path_env_ptr.*;
{
const scratch_bin_path = try std.fs.path.join(allocator, &.{ cwd, bin_dir });
defer allocator.free(scratch_bin_path);
setPathEnv(try std.mem.concat(allocator, u8, &.{ scratch_bin_path, path_env_sep, original_path_env }));
}
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "default", "master" });
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "master has not been fetched"));
}
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"-h"});
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "Usage"));
}
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"--help"});
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "Usage"));
}
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"default"});
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try passOrDumpAndThrow(result);
try testing.expect(std.mem.eql(u8, result.stdout, "<no-default>\n"));
}
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"fetch-index"});
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try passOrDumpAndThrow(result);
try testing.expect(std.mem.containsAtLeast(u8, result.stdout, 1, "master"));
}
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "default", "0.7.0" });
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
dumpExecResult(result);
switch (result.term) {
.Exited => |code| try testing.expectEqual(@as(u8, 1), code),
else => |term| std.debug.panic("unexpected exit {}", .{term}),
}
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "error: compiler '0.7.0' is not installed\n"));
}
try runNoCapture(zigup_args ++ &[_][]const u8{"0.7.0"});
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"default"});
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try passOrDumpAndThrow(result);
dumpExecResult(result);
try testing.expect(std.mem.eql(u8, result.stdout, "0.7.0\n"));
}
// verify we print a nice error message if we can't update the symlink
// because it's a directory
{
const zig_exe_link = comptime "scratch" ++ sep ++ "bin" ++ sep ++ "zig" ++ builtin.target.exeFileExt();
if (std.fs.cwd().access(zig_exe_link, .{})) {
try std.fs.cwd().deleteFile(zig_exe_link);
} else |err| switch (err) {
error.FileNotFound => {},
else => |e| return e,
}
try std.fs.cwd().makeDir(zig_exe_link);
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "default", "0.7.0" });
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
dumpExecResult(result);
switch (result.term) {
.Exited => |code| try testing.expectEqual(@as(u8, 1), code),
else => |term| std.debug.panic("unexpected exit {}", .{term}),
}
if (builtin.os.tag == .windows) {
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "unable to create the exe link, the path '"));
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "' is a directory"));
} else {
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "unable to update/overwrite the 'zig' PATH symlink, the file '"));
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "' already exists and is not a symlink"));
}
try std.fs.cwd().deleteDir(zig_exe_link);
}
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "fetch", "0.7.0" });
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try passOrDumpAndThrow(result);
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "already installed"));
}
try runNoCapture(zigup_args ++ &[_][]const u8{"master"});
try runNoCapture(zigup_args ++ &[_][]const u8{"0.8.0"});
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"default"});
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try passOrDumpAndThrow(result);
dumpExecResult(result);
try testing.expect(std.mem.eql(u8, result.stdout, "0.8.0\n"));
}
{
const save_path_env = path_env_ptr.*;
defer setPathEnv(save_path_env);
setPathEnv("");
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "default", "master" });
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, " is not in PATH"));
}
try runNoCapture(zigup_args ++ &[_][]const u8{ "default", "master" });
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"list"});
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try passOrDumpAndThrow(result);
try testing.expect(std.mem.containsAtLeast(u8, result.stdout, 1, "0.7.0"));
try testing.expect(std.mem.containsAtLeast(u8, result.stdout, 1, "0.8.0"));
}
try runNoCapture(zigup_args ++ &[_][]const u8{ "default", "0.7.0" });
try testing.expectEqual(@as(u32, 3), try getCompilerCount(install_dir));
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "run", "0.8.0", "version" });
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try testing.expectEqualSlices(u8, "0.8.0\n", result.stdout);
}
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "run", "doesnotexist", "version" });
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try testing.expectEqualSlices(u8, "error: compiler 'doesnotexist' does not exist, fetch it first with: zigup fetch doesnotexist\n", result.stderr);
}
try runNoCapture(zigup_args ++ &[_][]const u8{ "keep", "0.8.0" });
// doesn't delete anything because we have keepfile and master doens't get deleted
try runNoCapture(zigup_args ++ &[_][]const u8{"clean"});
try testing.expectEqual(@as(u32, 3), try getCompilerCount(install_dir));
// Just make a directory to trick zigup into thinking there is another compiler so we don't have to wait for it to download/install
try std.fs.cwd().makeDir(install_dir ++ sep ++ "0.9.0");
try testing.expectEqual(@as(u32, 4), try getCompilerCount(install_dir));
try runNoCapture(zigup_args ++ &[_][]const u8{"clean"});
try testing.expectEqual(@as(u32, 3), try getCompilerCount(install_dir));
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "clean", "0.8.0" });
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try passOrDumpAndThrow(result);
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "deleting "));
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "0.8.0"));
}
try testing.expectEqual(@as(u32, 2), try getCompilerCount(install_dir));
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"clean"});
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try passOrDumpAndThrow(result);
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "it is master"));
}
try testing.expectEqual(@as(u32, 2), try getCompilerCount(install_dir));
try runNoCapture(zigup_args ++ &[_][]const u8{"master"});
try testing.expectEqual(@as(u32, 2), try getCompilerCount(install_dir));
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"DOESNOTEXST"});
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "download"));
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "failed"));
}
try testing.expectEqual(@as(u32, 2), try getCompilerCount(install_dir));
// verify that we get an error if there is another compiler in the path
{
const bin2_dir = "scratch" ++ sep ++ "bin2";
try std.fs.cwd().makeDir(bin2_dir);
const previous_path = path_env_ptr.*;
const scratch_bin2_path = try std.fs.path.join(allocator, &.{ cwd, bin2_dir });
defer allocator.free(scratch_bin2_path);
{
var file = try std.fs.cwd().createFile(comptime bin2_dir ++ sep ++ "zig" ++ builtin.target.exeFileExt(), .{});
defer file.close();
try file.writer().writeAll("a fake executable");
}
setPathEnv(try std.mem.concat(allocator, u8, &.{ scratch_bin2_path, path_env_sep, previous_path }));
defer setPathEnv(previous_path);
// verify zig isn't currently on 0.7.0 before we set it as the default
try checkZigVersion(allocator, path_link, expected_zig_version_0_7_0, .not_equal);
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "default", "0.7.0" });
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
std.log.info("output: {s}", .{result.stderr});
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "error: zig compiler '"));
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "' is higher priority in PATH than the path-link '"));
}
// the path link should still be updated even though it's in a lower path priority.
// Verify zig points to the new defult version we just set.
try checkZigVersion(allocator, path_link, expected_zig_version_0_7_0, .equal);
}
// verify a dev build
// NOTE: this test will eventually break when these builds are cleaned up,
// we should support downloading from bazel and use that instead since
// it should be more permanent
try runNoCapture(zigup_args ++ &[_][]const u8{ "0.12.0-dev.3639+9cfac4718" });
std.log.info("Success", .{});
return 0;
}
fn checkZigVersion(allocator: std.mem.Allocator, zig: []const u8, compare: []const u8, want_equal: enum { not_equal, equal }) !void {
const result = try runCaptureOuts(allocator, &[_][]const u8{ zig, "version" });
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try passOrDumpAndThrow(result);
const actual_version = std.mem.trimRight(u8, result.stdout, "\r\n");
const actual_equal = std.mem.eql(u8, compare, actual_version);
const expected_equal = switch (want_equal) {
.not_equal => false,
.equal => true,
};
if (expected_equal != actual_equal) {
const prefix: []const u8 = if (expected_equal) "" else " NOT";
std.log.info("expected zig version to{s} be '{s}', but is '{s}'", .{ prefix, compare, actual_version });
return error.TestUnexpectedResult;
}
}
fn getCompilerCount(install_dir: []const u8) !u32 {
var dir = try std.fs.cwd().openDir(install_dir, .{ .iterate = true });
defer dir.close();
var it = dir.iterate();
var count: u32 = 0;
while (try it.next()) |entry| {
if (entry.kind == .directory) {
count += 1;
} else {
if (builtin.os.tag == .windows) {
try testing.expect(entry.kind == .file);
} else {
try testing.expect(entry.kind == .sym_link);
}
}
}
return count;
}
fn trailNl(s: []const u8) []const u8 {
return if (s.len == 0 or s[s.len - 1] != '\n') "\n" else "";
}
fn dumpExecResult(result: std.ChildProcess.RunResult) void {
if (result.stdout.len > 0) {
std.debug.print("--- STDOUT ---\n{s}{s}--------------\n", .{ result.stdout, trailNl(result.stdout) });
}
if (result.stderr.len > 0) {
std.debug.print("--- STDERR ---\n{s}{s}--------------\n", .{ result.stderr, trailNl(result.stderr) });
}
}
fn runNoCapture(argv: []const []const u8) !void {
var arena_store = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena_store.deinit();
const result = try runCaptureOuts(arena_store.allocator(), argv);
dumpExecResult(result);
try passOrThrow(result.term);
}
fn runCaptureOuts(allocator: std.mem.Allocator, argv: []const []const u8) !std.ChildProcess.RunResult {
{
const cmd = try std.mem.join(allocator, " ", argv);
defer allocator.free(cmd);
std.log.info("RUN: {s}", .{cmd});
}
return try std.ChildProcess.run(.{ .allocator = allocator, .argv = argv, .env_map = &child_env_map });
}
fn passOrThrow(term: std.ChildProcess.Term) error{ChildProcessFailed}!void {
if (!execResultPassed(term)) {
std.log.err("child process failed with {}", .{term});
return error.ChildProcessFailed;
}
}
fn passOrDumpAndThrow(result: std.ChildProcess.RunResult) error{ChildProcessFailed}!void {
if (!execResultPassed(result.term)) {
dumpExecResult(result);
std.log.err("child process failed with {}", .{result.term});
return error.ChildProcessFailed;
}
}
fn execResultPassed(term: std.ChildProcess.Term) bool {
switch (term) {
.Exited => |code| return code == 0,
else => return false,
}
}