test.zig 8.0 KB

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