test.zig 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. {
  118. const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "fetch", "0.7.0" });
  119. defer {
  120. allocator.free(result.stdout);
  121. allocator.free(result.stderr);
  122. }
  123. try passOrDumpAndThrow(result);
  124. try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "already installed"));
  125. }
  126. try runNoCapture(zigup_args ++ &[_][]const u8{"master"});
  127. try runNoCapture(zigup_args ++ &[_][]const u8{"0.8.0"});
  128. {
  129. const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"default"});
  130. defer {
  131. allocator.free(result.stdout);
  132. allocator.free(result.stderr);
  133. }
  134. try passOrDumpAndThrow(result);
  135. dumpExecResult(result);
  136. try testing.expect(std.mem.eql(u8, result.stdout, "0.8.0\n"));
  137. }
  138. {
  139. const save_path_env = path_env_ptr.*;
  140. defer setPathEnv(save_path_env);
  141. setPathEnv("");
  142. const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "default", "master" });
  143. defer {
  144. allocator.free(result.stdout);
  145. allocator.free(result.stderr);
  146. }
  147. try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, " is not in PATH"));
  148. }
  149. try runNoCapture(zigup_args ++ &[_][]const u8{ "default", "master" });
  150. {
  151. const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"list"});
  152. defer {
  153. allocator.free(result.stdout);
  154. allocator.free(result.stderr);
  155. }
  156. try passOrDumpAndThrow(result);
  157. try testing.expect(std.mem.containsAtLeast(u8, result.stdout, 1, "0.7.0"));
  158. try testing.expect(std.mem.containsAtLeast(u8, result.stdout, 1, "0.8.0"));
  159. }
  160. try runNoCapture(zigup_args ++ &[_][]const u8{ "default", "0.7.0" });
  161. try testing.expectEqual(@as(u32, 3), try getCompilerCount(install_dir));
  162. {
  163. const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "run", "0.8.0", "version" });
  164. defer {
  165. allocator.free(result.stdout);
  166. allocator.free(result.stderr);
  167. }
  168. try testing.expectEqualSlices(u8, "0.8.0\n", result.stdout);
  169. }
  170. {
  171. const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "run", "doesnotexist", "version" });
  172. defer {
  173. allocator.free(result.stdout);
  174. allocator.free(result.stderr);
  175. }
  176. try testing.expectEqualSlices(u8, "error: compiler 'doesnotexist' does not exist, fetch it first with: zigup fetch doesnotexist\n", result.stderr);
  177. }
  178. try runNoCapture(zigup_args ++ &[_][]const u8{ "keep", "0.8.0" });
  179. // doesn't delete anything because we have keepfile and master doens't get deleted
  180. try runNoCapture(zigup_args ++ &[_][]const u8{"clean"});
  181. try testing.expectEqual(@as(u32, 3), try getCompilerCount(install_dir));
  182. // 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
  183. try std.fs.cwd().makeDir(install_dir ++ sep ++ "0.9.0");
  184. try testing.expectEqual(@as(u32, 4), try getCompilerCount(install_dir));
  185. try runNoCapture(zigup_args ++ &[_][]const u8{"clean"});
  186. try testing.expectEqual(@as(u32, 3), try getCompilerCount(install_dir));
  187. {
  188. const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "clean", "0.8.0" });
  189. defer {
  190. allocator.free(result.stdout);
  191. allocator.free(result.stderr);
  192. }
  193. try passOrDumpAndThrow(result);
  194. try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "deleting "));
  195. try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "0.8.0"));
  196. }
  197. try testing.expectEqual(@as(u32, 2), try getCompilerCount(install_dir));
  198. {
  199. const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"clean"});
  200. defer {
  201. allocator.free(result.stdout);
  202. allocator.free(result.stderr);
  203. }
  204. try passOrDumpAndThrow(result);
  205. try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "it is master"));
  206. }
  207. try testing.expectEqual(@as(u32, 2), try getCompilerCount(install_dir));
  208. try runNoCapture(zigup_args ++ &[_][]const u8{"master"});
  209. try testing.expectEqual(@as(u32, 2), try getCompilerCount(install_dir));
  210. {
  211. const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"DOESNOTEXST"});
  212. defer {
  213. allocator.free(result.stdout);
  214. allocator.free(result.stderr);
  215. }
  216. try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "HTTP request failed"));
  217. }
  218. try testing.expectEqual(@as(u32, 2), try getCompilerCount(install_dir));
  219. // verify that we get an error if there is another compiler in the path
  220. {
  221. const bin2_dir = "scratch" ++ sep ++ "bin2";
  222. try std.fs.cwd().makeDir(bin2_dir);
  223. const previous_path = path_env_ptr.*;
  224. const scratch_bin2_path = try std.fs.path.join(allocator, &.{ cwd, bin2_dir });
  225. defer allocator.free(scratch_bin2_path);
  226. {
  227. var file = try std.fs.cwd().createFile(comptime bin2_dir ++ sep ++ "zig" ++ builtin.target.exeFileExt(), .{});
  228. defer file.close();
  229. try file.writer().writeAll("a fake executable");
  230. }
  231. setPathEnv(try std.mem.concat(allocator, u8, &.{ scratch_bin2_path, path_env_sep, previous_path }));
  232. defer setPathEnv(previous_path);
  233. // verify zig isn't currently on 0.7.0 before we set it as the default
  234. try checkZigVersion(allocator, path_link, expected_zig_version_0_7_0, .not_equal);
  235. {
  236. const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "default", "0.7.0" });
  237. defer {
  238. allocator.free(result.stdout);
  239. allocator.free(result.stderr);
  240. }
  241. std.log.info("output: {s}", .{result.stderr});
  242. try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "error: zig compiler '"));
  243. try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "' is higher priority in PATH than the path-link '"));
  244. }
  245. // the path link should still be updated even though it's in a lower path priority.
  246. // Verify zig points to the new defult version we just set.
  247. try checkZigVersion(allocator, path_link, expected_zig_version_0_7_0, .equal);
  248. }
  249. // verify a dev build
  250. // NOTE: this test will eventually break when these builds are cleaned up,
  251. // we should support downloading from bazel and use that instead since
  252. // it should be more permanent
  253. try runNoCapture(zigup_args ++ &[_][]const u8{"0.11.0-dev.4263+f821543e4"});
  254. std.log.info("Success", .{});
  255. return 0;
  256. }
  257. fn checkZigVersion(allocator: std.mem.Allocator, zig: []const u8, compare: []const u8, want_equal: enum { not_equal, equal }) !void {
  258. const result = try runCaptureOuts(allocator, &[_][]const u8{ zig, "version" });
  259. defer {
  260. allocator.free(result.stdout);
  261. allocator.free(result.stderr);
  262. }
  263. try passOrDumpAndThrow(result);
  264. const actual_version = std.mem.trimRight(u8, result.stdout, "\r\n");
  265. const actual_equal = std.mem.eql(u8, compare, actual_version);
  266. const expected_equal = switch (want_equal) {
  267. .not_equal => false,
  268. .equal => true,
  269. };
  270. if (expected_equal != actual_equal) {
  271. const prefix: []const u8 = if (expected_equal) "" else " NOT";
  272. std.log.info("expected zig version to{s} be '{s}', but is '{s}'", .{ prefix, compare, actual_version });
  273. return error.TestUnexpectedResult;
  274. }
  275. }
  276. fn getCompilerCount(install_dir: []const u8) !u32 {
  277. var dir = try std.fs.cwd().openIterableDir(install_dir, .{});
  278. defer dir.close();
  279. var it = dir.iterate();
  280. var count: u32 = 0;
  281. while (try it.next()) |entry| {
  282. if (entry.kind == .directory) {
  283. count += 1;
  284. } else {
  285. if (builtin.os.tag == .windows) {
  286. try testing.expect(entry.kind == .file);
  287. } else {
  288. try testing.expect(entry.kind == .sym_link);
  289. }
  290. }
  291. }
  292. return count;
  293. }
  294. fn trailNl(s: []const u8) []const u8 {
  295. return if (s.len == 0 or s[s.len - 1] != '\n') "\n" else "";
  296. }
  297. fn dumpExecResult(result: std.ChildProcess.ExecResult) void {
  298. if (result.stdout.len > 0) {
  299. std.debug.print("--- STDOUT ---\n{s}{s}--------------\n", .{ result.stdout, trailNl(result.stdout) });
  300. }
  301. if (result.stderr.len > 0) {
  302. std.debug.print("--- STDERR ---\n{s}{s}--------------\n", .{ result.stderr, trailNl(result.stderr) });
  303. }
  304. }
  305. fn runNoCapture(argv: []const []const u8) !void {
  306. var arena_store = std.heap.ArenaAllocator.init(std.heap.page_allocator);
  307. defer arena_store.deinit();
  308. const result = try runCaptureOuts(arena_store.allocator(), argv);
  309. dumpExecResult(result);
  310. try passOrThrow(result.term);
  311. }
  312. fn runCaptureOuts(allocator: std.mem.Allocator, argv: []const []const u8) !std.ChildProcess.ExecResult {
  313. {
  314. const cmd = try std.mem.join(allocator, " ", argv);
  315. defer allocator.free(cmd);
  316. std.log.info("RUN: {s}", .{cmd});
  317. }
  318. return try std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv, .env_map = &child_env_map });
  319. }
  320. fn passOrThrow(term: std.ChildProcess.Term) error{ChildProcessFailed}!void {
  321. if (!execResultPassed(term)) {
  322. std.log.err("child process failed with {}", .{term});
  323. return error.ChildProcessFailed;
  324. }
  325. }
  326. fn passOrDumpAndThrow(result: std.ChildProcess.ExecResult) error{ChildProcessFailed}!void {
  327. if (!execResultPassed(result.term)) {
  328. dumpExecResult(result);
  329. std.log.err("child process failed with {}", .{result.term});
  330. return error.ChildProcessFailed;
  331. }
  332. }
  333. fn execResultPassed(term: std.ChildProcess.Term) bool {
  334. switch (term) {
  335. .Exited => |code| return code == 0,
  336. else => return false,
  337. }
  338. }