authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-14 16:38:14-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-15 10:48:15-07:00
log717e2c8718a3161d6b86317b6132fa296325a88c
tree593b0e0fd4a8e0edc6bfadd64e26fd38fa48fad8
parent363d4a107de3af95620f82e851e8777aa9f576c9

std.Build.Cache: make unit tests not depend on cwd

This makes them more resilient to being run multiple times by multiple different processes at the same time.

1 files changed, 35 insertions(+), 43 deletions(-)

lib/std/Build/Cache.zig+35-43
......@@ -976,16 +976,16 @@ fn hashFile(file: fs.File, bin_digest: *[Hasher.mac_length]u8) !void {
976976}
977977
978978// Create/Write a file, close it, then grab its stat.mtime timestamp.
979fn testGetCurrentFileTimestamp() !i128 {
979fn testGetCurrentFileTimestamp(dir: fs.Dir) !i128 {
980980 const test_out_file = "test-filetimestamp.tmp";
981981
982 var file = try fs.cwd().createFile(test_out_file, .{
982 var file = try dir.createFile(test_out_file, .{
983983 .read = true,
984984 .truncate = true,
985985 });
986986 defer {
987987 file.close();
988 fs.cwd().deleteFile(test_out_file) catch {};
988 dir.deleteFile(test_out_file) catch {};
989989 }
990990
991991 return (try file.stat()).mtime;
......@@ -997,16 +997,17 @@ test "cache file and then recall it" {
997997 return error.SkipZigTest;
998998 }
999999
1000 const cwd = fs.cwd();
1000 var tmp = testing.tmpDir(.{});
1001 defer tmp.cleanup();
10011002
10021003 const temp_file = "test.txt";
10031004 const temp_manifest_dir = "temp_manifest_dir";
10041005
1005 try cwd.writeFile(temp_file, "Hello, world!\n");
1006 try tmp.dir.writeFile(temp_file, "Hello, world!\n");
10061007
10071008 // Wait for file timestamps to tick
1008 const initial_time = try testGetCurrentFileTimestamp();
1009 while ((try testGetCurrentFileTimestamp()) == initial_time) {
1009 const initial_time = try testGetCurrentFileTimestamp(tmp.dir);
1010 while ((try testGetCurrentFileTimestamp(tmp.dir)) == initial_time) {
10101011 std.time.sleep(1);
10111012 }
10121013
......@@ -1016,9 +1017,9 @@ test "cache file and then recall it" {
10161017 {
10171018 var cache = Cache{
10181019 .gpa = testing.allocator,
1019 .manifest_dir = try cwd.makeOpenPath(temp_manifest_dir, .{}),
1020 .manifest_dir = try tmp.dir.makeOpenPath(temp_manifest_dir, .{}),
10201021 };
1021 cache.addPrefix(.{ .path = null, .handle = fs.cwd() });
1022 cache.addPrefix(.{ .path = null, .handle = tmp.dir });
10221023 defer cache.manifest_dir.close();
10231024
10241025 {
......@@ -1054,9 +1055,6 @@ test "cache file and then recall it" {
10541055
10551056 try testing.expectEqual(digest1, digest2);
10561057 }
1057
1058 try cwd.deleteTree(temp_manifest_dir);
1059 try cwd.deleteFile(temp_file);
10601058}
10611059
10621060test "check that changing a file makes cache fail" {
......@@ -1064,21 +1062,19 @@ test "check that changing a file makes cache fail" {
10641062 // https://github.com/ziglang/zig/issues/5437
10651063 return error.SkipZigTest;
10661064 }
1067 const cwd = fs.cwd();
1065 var tmp = testing.tmpDir(.{});
1066 defer tmp.cleanup();
10681067
10691068 const temp_file = "cache_hash_change_file_test.txt";
10701069 const temp_manifest_dir = "cache_hash_change_file_manifest_dir";
10711070 const original_temp_file_contents = "Hello, world!\n";
10721071 const updated_temp_file_contents = "Hello, world; but updated!\n";
10731072
1074 try cwd.deleteTree(temp_manifest_dir);
1075 try cwd.deleteTree(temp_file);
1076
1077 try cwd.writeFile(temp_file, original_temp_file_contents);
1073 try tmp.dir.writeFile(temp_file, original_temp_file_contents);
10781074
10791075 // Wait for file timestamps to tick
1080 const initial_time = try testGetCurrentFileTimestamp();
1081 while ((try testGetCurrentFileTimestamp()) == initial_time) {
1076 const initial_time = try testGetCurrentFileTimestamp(tmp.dir);
1077 while ((try testGetCurrentFileTimestamp(tmp.dir)) == initial_time) {
10821078 std.time.sleep(1);
10831079 }
10841080
......@@ -1088,9 +1084,9 @@ test "check that changing a file makes cache fail" {
10881084 {
10891085 var cache = Cache{
10901086 .gpa = testing.allocator,
1091 .manifest_dir = try cwd.makeOpenPath(temp_manifest_dir, .{}),
1087 .manifest_dir = try tmp.dir.makeOpenPath(temp_manifest_dir, .{}),
10921088 };
1093 cache.addPrefix(.{ .path = null, .handle = fs.cwd() });
1089 cache.addPrefix(.{ .path = null, .handle = tmp.dir });
10941090 defer cache.manifest_dir.close();
10951091
10961092 {
......@@ -1110,7 +1106,7 @@ test "check that changing a file makes cache fail" {
11101106 try ch.writeManifest();
11111107 }
11121108
1113 try cwd.writeFile(temp_file, updated_temp_file_contents);
1109 try tmp.dir.writeFile(temp_file, updated_temp_file_contents);
11141110
11151111 {
11161112 var ch = cache.obtain();
......@@ -1132,9 +1128,6 @@ test "check that changing a file makes cache fail" {
11321128
11331129 try testing.expect(!mem.eql(u8, digest1[0..], digest2[0..]));
11341130 }
1135
1136 try cwd.deleteTree(temp_manifest_dir);
1137 try cwd.deleteTree(temp_file);
11381131}
11391132
11401133test "no file inputs" {
......@@ -1142,18 +1135,20 @@ test "no file inputs" {
11421135 // https://github.com/ziglang/zig/issues/5437
11431136 return error.SkipZigTest;
11441137 }
1145 const cwd = fs.cwd();
1138
1139 var tmp = testing.tmpDir(.{});
1140 defer tmp.cleanup();
1141
11461142 const temp_manifest_dir = "no_file_inputs_manifest_dir";
1147 defer cwd.deleteTree(temp_manifest_dir) catch {};
11481143
11491144 var digest1: [hex_digest_len]u8 = undefined;
11501145 var digest2: [hex_digest_len]u8 = undefined;
11511146
11521147 var cache = Cache{
11531148 .gpa = testing.allocator,
1154 .manifest_dir = try cwd.makeOpenPath(temp_manifest_dir, .{}),
1149 .manifest_dir = try tmp.dir.makeOpenPath(temp_manifest_dir, .{}),
11551150 };
1156 cache.addPrefix(.{ .path = null, .handle = fs.cwd() });
1151 cache.addPrefix(.{ .path = null, .handle = tmp.dir });
11571152 defer cache.manifest_dir.close();
11581153
11591154 {
......@@ -1188,18 +1183,19 @@ test "Manifest with files added after initial hash work" {
11881183 // https://github.com/ziglang/zig/issues/5437
11891184 return error.SkipZigTest;
11901185 }
1191 const cwd = fs.cwd();
1186 var tmp = testing.tmpDir(.{});
1187 defer tmp.cleanup();
11921188
11931189 const temp_file1 = "cache_hash_post_file_test1.txt";
11941190 const temp_file2 = "cache_hash_post_file_test2.txt";
11951191 const temp_manifest_dir = "cache_hash_post_file_manifest_dir";
11961192
1197 try cwd.writeFile(temp_file1, "Hello, world!\n");
1198 try cwd.writeFile(temp_file2, "Hello world the second!\n");
1193 try tmp.dir.writeFile(temp_file1, "Hello, world!\n");
1194 try tmp.dir.writeFile(temp_file2, "Hello world the second!\n");
11991195
12001196 // Wait for file timestamps to tick
1201 const initial_time = try testGetCurrentFileTimestamp();
1202 while ((try testGetCurrentFileTimestamp()) == initial_time) {
1197 const initial_time = try testGetCurrentFileTimestamp(tmp.dir);
1198 while ((try testGetCurrentFileTimestamp(tmp.dir)) == initial_time) {
12031199 std.time.sleep(1);
12041200 }
12051201
......@@ -1210,9 +1206,9 @@ test "Manifest with files added after initial hash work" {
12101206 {
12111207 var cache = Cache{
12121208 .gpa = testing.allocator,
1213 .manifest_dir = try cwd.makeOpenPath(temp_manifest_dir, .{}),
1209 .manifest_dir = try tmp.dir.makeOpenPath(temp_manifest_dir, .{}),
12141210 };
1215 cache.addPrefix(.{ .path = null, .handle = fs.cwd() });
1211 cache.addPrefix(.{ .path = null, .handle = tmp.dir });
12161212 defer cache.manifest_dir.close();
12171213
12181214 {
......@@ -1245,11 +1241,11 @@ test "Manifest with files added after initial hash work" {
12451241 try testing.expect(mem.eql(u8, &digest1, &digest2));
12461242
12471243 // Modify the file added after initial hash
1248 try cwd.writeFile(temp_file2, "Hello world the second, updated\n");
1244 try tmp.dir.writeFile(temp_file2, "Hello world the second, updated\n");
12491245
12501246 // Wait for file timestamps to tick
1251 const initial_time2 = try testGetCurrentFileTimestamp();
1252 while ((try testGetCurrentFileTimestamp()) == initial_time2) {
1247 const initial_time2 = try testGetCurrentFileTimestamp(tmp.dir);
1248 while ((try testGetCurrentFileTimestamp(tmp.dir)) == initial_time2) {
12531249 std.time.sleep(1);
12541250 }
12551251
......@@ -1272,8 +1268,4 @@ test "Manifest with files added after initial hash work" {
12721268
12731269 try testing.expect(!mem.eql(u8, &digest1, &digest3));
12741270 }
1275
1276 try cwd.deleteTree(temp_manifest_dir);
1277 try cwd.deleteFile(temp_file1);
1278 try cwd.deleteFile(temp_file2);
12791271}