| 1 | const std = @import("std"); |
| 2 | |
| 3 | /// Tests that args are passed to run steps correctly. |
| 4 | /// |
| 5 | /// Note that when `make_absolute` is true we make sure the resulting path argument is absolute, but |
| 6 | /// when it is false we allow either absolute or relative paths. This is because the maker receives |
| 7 | /// absolute paths when build is run from anywhere other than the build root. |
| 8 | pub fn build(b: *std.Build) !void { |
| 9 | const step = b.step("test", "Run artifact args standalone test cases"); |
| 10 | b.default_step = step; |
| 11 | |
| 12 | const exe = b.addExecutable(.{ |
| 13 | .name = "exe", |
| 14 | .root_module = b.createModule(.{ |
| 15 | .root_source_file = b.path("main.zig"), |
| 16 | .target = b.graph.host, |
| 17 | }), |
| 18 | }); |
| 19 | |
| 20 | // Arg |
| 21 | { |
| 22 | const run = b.addRunArtifact(exe); |
| 23 | step.dependOn(&run.step); |
| 24 | run.addArg("arg1"); |
| 25 | run.expectStdErrEqual("arg1\n"); |
| 26 | } |
| 27 | |
| 28 | // Args |
| 29 | { |
| 30 | const run = b.addRunArtifact(exe); |
| 31 | step.dependOn(&run.step); |
| 32 | run.addArgs(&.{ "arg1", "arg2" }); |
| 33 | run.expectStdErrEqual("arg1\narg2\n"); |
| 34 | } |
| 35 | |
| 36 | // Artifact Args |
| 37 | { |
| 38 | // Absolute |
| 39 | { |
| 40 | const run = b.addRunArtifact(exe); |
| 41 | step.dependOn(&run.step); |
| 42 | _ = run.addArtifactArg2(exe, .{ .prefix = "path^", .make_absolute = true, .suffix = "$" }); |
| 43 | run.expectStdErrMatch("abs exe"); |
| 44 | } |
| 45 | // Relative |
| 46 | { |
| 47 | const run = b.addRunArtifact(exe); |
| 48 | step.dependOn(&run.step); |
| 49 | _ = run.addArtifactArg2(exe, .{ .prefix = "path^", .make_absolute = false, .suffix = "$" }); |
| 50 | run.expectStdErrMatch("exe\n"); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // File Args |
| 55 | { |
| 56 | const write_files = b.addWriteFiles(); |
| 57 | const file = write_files.add("file", ""); |
| 58 | |
| 59 | // Absolute |
| 60 | { |
| 61 | const run = b.addRunArtifact(exe); |
| 62 | step.dependOn(&run.step); |
| 63 | _ = run.addFileArg2(file, .{ .prefix = "path^", .make_absolute = true, .suffix = "$" }); |
| 64 | run.expectStdErrEqual("abs file\n"); |
| 65 | } |
| 66 | // Relative |
| 67 | { |
| 68 | const run = b.addRunArtifact(exe); |
| 69 | step.dependOn(&run.step); |
| 70 | _ = run.addFileArg2(file, .{ .prefix = "path^", .make_absolute = false, .suffix = "$" }); |
| 71 | run.expectStdErrMatch("file\n"); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // File Content |
| 76 | { |
| 77 | const write_files = b.addWriteFiles(); |
| 78 | const file = write_files.add("file", "foo bar baz"); |
| 79 | |
| 80 | const run = b.addRunArtifact(exe); |
| 81 | step.dependOn(&run.step); |
| 82 | _ = run.addFileContentArg2(file, .{ .prefix = "content-prefix ", .suffix = " content-suffix" }); |
| 83 | run.expectStdErrEqual("content-prefix foo bar baz content-suffix\n"); |
| 84 | } |
| 85 | |
| 86 | // Output File Args |
| 87 | { |
| 88 | // Absolute |
| 89 | { |
| 90 | const run = b.addRunArtifact(exe); |
| 91 | step.dependOn(&run.step); |
| 92 | _ = run.addOutputFileArg2("output-file", .{ .prefix = "path^", .make_absolute = true, .suffix = "$" }); |
| 93 | run.expectStdErrEqual("abs output-file\n"); |
| 94 | } |
| 95 | // Relative |
| 96 | { |
| 97 | const run = b.addRunArtifact(exe); |
| 98 | step.dependOn(&run.step); |
| 99 | _ = run.addOutputFileArg2("output-file", .{ .prefix = "path^", .make_absolute = false, .suffix = "$" }); |
| 100 | run.expectStdErrMatch("output-file\n"); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Output Directory Args |
| 105 | { |
| 106 | // Absolute |
| 107 | { |
| 108 | const run = b.addRunArtifact(exe); |
| 109 | step.dependOn(&run.step); |
| 110 | _ = run.addOutputDirectoryArg2("output-dir", .{ .prefix = "path^", .make_absolute = true, .suffix = "$" }); |
| 111 | run.expectStdErrEqual("abs output-dir\n"); |
| 112 | } |
| 113 | // Relative |
| 114 | { |
| 115 | const run = b.addRunArtifact(exe); |
| 116 | step.dependOn(&run.step); |
| 117 | _ = run.addOutputDirectoryArg2("output-dir", .{ .prefix = "path^", .make_absolute = false, .suffix = "$" }); |
| 118 | run.expectStdErrMatch("output-dir\n"); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // Directory Args |
| 123 | { |
| 124 | const write_files = b.addWriteFiles(); |
| 125 | const directory = try write_files.getDirectory().join(b.graph.arena, "dir"); |
| 126 | |
| 127 | // Absolute |
| 128 | { |
| 129 | const run = b.addRunArtifact(exe); |
| 130 | step.dependOn(&run.step); |
| 131 | _ = run.addDirectoryArg2(directory, .{ .prefix = "path^", .make_absolute = true, .suffix = "$" }); |
| 132 | run.expectStdErrEqual("abs dir\n"); |
| 133 | } |
| 134 | // Relative |
| 135 | { |
| 136 | const run = b.addRunArtifact(exe); |
| 137 | step.dependOn(&run.step); |
| 138 | _ = run.addDirectoryArg2(directory, .{ .prefix = "path^", .make_absolute = false, .suffix = "$" }); |
| 139 | run.expectStdErrMatch("dir\n"); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // Dep File Args |
| 144 | { |
| 145 | // Absolute |
| 146 | { |
| 147 | const run = b.addRunArtifact(exe); |
| 148 | step.dependOn(&run.step); |
| 149 | _ = run.addDepFileOutputArg2("deps.d", .{ .prefix = "path^", .make_absolute = true, .suffix = "$" }); |
| 150 | run.expectStdErrEqual("abs deps.d\n"); |
| 151 | } |
| 152 | // Relative |
| 153 | { |
| 154 | const run = b.addRunArtifact(exe); |
| 155 | step.dependOn(&run.step); |
| 156 | _ = run.addDepFileOutputArg2("deps.d", .{ .prefix = "path^", .make_absolute = false, .suffix = "$" }); |
| 157 | run.expectStdErrMatch("deps.d\n"); |
| 158 | } |
| 159 | } |
| 160 | } |