test.zig 16 KB

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