test.zig 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. const std = @import("std");
  2. const testing = std.testing;
  3. const sep = std.fs.path.sep_str;
  4. const fixdeletetree = @import("fixdeletetree.zig");
  5. pub fn main() !void {
  6. std.log.info("running test!", .{});
  7. try fixdeletetree.deleteTree(std.fs.cwd(), "scratch");
  8. try std.fs.cwd().makeDir("scratch");
  9. const install_dir = "scratch" ++ sep ++ "install";
  10. const bin_dir = "scratch" ++ sep ++ "bin";
  11. try std.fs.cwd().makeDir(install_dir);
  12. try std.fs.cwd().makeDir(bin_dir);
  13. // NOTE: for now we are incorrectly assuming the install dir is CWD/zig-out
  14. const zigup = "." ++ sep ++ "zig-out" ++ sep ++ "bin" ++ sep ++ "zigup" ++ std.builtin.target.exeFileExt();
  15. const path_link = if (std.builtin.os.tag == .windows) "scratch\\zig.bat" else (bin_dir ++ sep ++ "zig");
  16. const zigup_args = &[_][]const u8 { zigup, "--install-dir", install_dir, "--path-link", path_link };
  17. var allocator_store = std.heap.ArenaAllocator.init(std.heap.page_allocator);
  18. defer allocator_store.deinit();
  19. const allocator = &allocator_store.allocator;
  20. {
  21. const result = try runCaptureOuts(allocator, ".", zigup_args ++ &[_][]const u8 {"-h"});
  22. defer { allocator.free(result.stdout); allocator.free(result.stderr); }
  23. try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "Usage"));
  24. }
  25. {
  26. const result = try runCaptureOuts(allocator, ".", zigup_args ++ &[_][]const u8 {"--help"});
  27. defer { allocator.free(result.stdout); allocator.free(result.stderr); }
  28. try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "Usage"));
  29. }
  30. {
  31. const result = try runCaptureOuts(allocator, ".", zigup_args ++ &[_][]const u8 {"default"});
  32. defer { allocator.free(result.stdout); allocator.free(result.stderr); }
  33. try passOrDumpAndThrow(result);
  34. try testing.expect(std.mem.eql(u8, result.stdout, "<no-default>\n"));
  35. }
  36. {
  37. const result = try runCaptureOuts(allocator, ".", zigup_args ++ &[_][]const u8 {"fetch-index"});
  38. defer { allocator.free(result.stdout); allocator.free(result.stderr); }
  39. try passOrDumpAndThrow(result);
  40. try testing.expect(std.mem.containsAtLeast(u8, result.stdout, 1, "master"));
  41. }
  42. try runNoCapture(".", zigup_args ++ &[_][]const u8 {"0.5.0"});
  43. {
  44. const result = try runCaptureOuts(allocator, ".", zigup_args ++ &[_][]const u8 {"default"});
  45. defer { allocator.free(result.stdout); allocator.free(result.stderr); }
  46. try passOrDumpAndThrow(result);
  47. dumpExecResult(result);
  48. try testing.expect(std.mem.eql(u8, result.stdout, "0.5.0\n"));
  49. }
  50. {
  51. const result = try runCaptureOuts(allocator, ".", zigup_args ++ &[_][]const u8 {"fetch", "0.5.0"});
  52. defer { allocator.free(result.stdout); allocator.free(result.stderr); }
  53. try passOrDumpAndThrow(result);
  54. try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "already installed"));
  55. }
  56. try runNoCapture(".", zigup_args ++ &[_][]const u8 {"master"});
  57. try runNoCapture(".", zigup_args ++ &[_][]const u8 {"0.6.0"});
  58. {
  59. const result = try runCaptureOuts(allocator, ".", zigup_args ++ &[_][]const u8 {"default"});
  60. defer { allocator.free(result.stdout); allocator.free(result.stderr); }
  61. try passOrDumpAndThrow(result);
  62. dumpExecResult(result);
  63. try testing.expect(std.mem.eql(u8, result.stdout, "0.6.0\n"));
  64. }
  65. {
  66. const result = try runCaptureOuts(allocator, ".", zigup_args ++ &[_][]const u8 {"list"});
  67. defer { allocator.free(result.stdout); allocator.free(result.stderr); }
  68. try passOrDumpAndThrow(result);
  69. try testing.expect(std.mem.containsAtLeast(u8, result.stdout, 1, "0.5.0"));
  70. try testing.expect(std.mem.containsAtLeast(u8, result.stdout, 1, "0.6.0"));
  71. }
  72. try runNoCapture(".", zigup_args ++ &[_][]const u8 {"default", "0.5.0"});
  73. try testing.expectEqual(@as(u32, 3), try getCompilerCount(install_dir));
  74. try runNoCapture(".", zigup_args ++ &[_][]const u8 {"keep", "0.6.0"});
  75. // doesn't delete anything because we have keepfile and master doens't get deleted
  76. try runNoCapture(".", zigup_args ++ &[_][]const u8 {"clean"});
  77. try testing.expectEqual(@as(u32, 3), try getCompilerCount(install_dir));
  78. {
  79. const result = try runCaptureOuts(allocator, ".", zigup_args ++ &[_][]const u8 {"clean", "0.6.0"});
  80. defer { allocator.free(result.stdout); allocator.free(result.stderr); }
  81. try passOrDumpAndThrow(result);
  82. try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "deleting "));
  83. try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "0.6.0"));
  84. }
  85. try testing.expectEqual(@as(u32, 2), try getCompilerCount(install_dir));
  86. {
  87. const result = try runCaptureOuts(allocator, ".", zigup_args ++ &[_][]const u8 {"clean"});
  88. defer { allocator.free(result.stdout); allocator.free(result.stderr); }
  89. try passOrDumpAndThrow(result);
  90. try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "it is master"));
  91. }
  92. try testing.expectEqual(@as(u32, 2), try getCompilerCount(install_dir));
  93. try runNoCapture(".", zigup_args ++ &[_][]const u8 {"master"});
  94. try testing.expectEqual(@as(u32, 2), try getCompilerCount(install_dir));
  95. {
  96. const result = try runCaptureOuts(allocator, ".", zigup_args ++ &[_][]const u8 {"DOESNOTEXST"});
  97. defer { allocator.free(result.stdout); allocator.free(result.stderr); }
  98. try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "HTTP request failed"));
  99. }
  100. try testing.expectEqual(@as(u32, 2), try getCompilerCount(install_dir));
  101. }
  102. fn getCompilerCount(install_dir: []const u8) !u32 {
  103. var dir = try std.fs.cwd().openDir(install_dir, .{.iterate=true});
  104. defer dir.close();
  105. var it = dir.iterate();
  106. var count: u32 = 0;
  107. while (try it.next()) |entry| {
  108. if (entry.kind == .Directory) {
  109. count += 1;
  110. } else {
  111. if (std.builtin.os.tag == .windows) {
  112. try testing.expect(entry.kind == .File);
  113. } else {
  114. try testing.expect(entry.kind == .SymLink);
  115. }
  116. }
  117. }
  118. return count;
  119. }
  120. fn trailNl(s: []const u8) []const u8 {
  121. return if (s.len == 0 or s[s.len-1] != '\n') "\n" else "";
  122. }
  123. fn dumpExecResult(result: std.ChildProcess.ExecResult) void {
  124. if (result.stdout.len > 0) {
  125. std.debug.print("--- STDOUT ---\n{s}{s}--------------\n", .{result.stdout, trailNl(result.stdout)});
  126. }
  127. if (result.stderr.len > 0) {
  128. std.debug.print("--- STDERR ---\n{s}{s}--------------\n", .{result.stderr, trailNl(result.stderr)});
  129. }
  130. }
  131. fn runNoCapture(cwd: []const u8, argv: []const []const u8) !void {
  132. var arena_store = std.heap.ArenaAllocator.init(std.heap.page_allocator);
  133. defer arena_store.deinit();
  134. const result = try runCaptureOuts(&arena_store.allocator, cwd, argv);
  135. dumpExecResult(result);
  136. try passOrThrow(result.term);
  137. }
  138. fn runCaptureOuts(allocator: *std.mem.Allocator, cwd: []const u8, argv: []const []const u8) !std.ChildProcess.ExecResult {
  139. {
  140. const cmd = try std.mem.join(allocator, " ", argv);
  141. defer allocator.free(cmd);
  142. std.log.info("RUN(cwd={s}): {s}", .{cwd, cmd});
  143. }
  144. return try std.ChildProcess.exec(.{.allocator = allocator, .argv = argv, .cwd = cwd});
  145. }
  146. fn passOrThrow(term: std.ChildProcess.Term) error{ChildProcessFailed}!void {
  147. if (!execResultPassed(term)) {
  148. std.log.err("child process failed with {}", .{term});
  149. return error.ChildProcessFailed;
  150. }
  151. }
  152. fn passOrDumpAndThrow(result: std.ChildProcess.ExecResult) error{ChildProcessFailed}!void {
  153. if (!execResultPassed(result.term)) {
  154. dumpExecResult(result);
  155. std.log.err("child process failed with {}", .{result.term});
  156. return error.ChildProcessFailed;
  157. }
  158. }
  159. fn execResultPassed(term: std.ChildProcess.Term) bool {
  160. switch (term) {
  161. .Exited => |code| return code == 0,
  162. else => return false,
  163. }
  164. }