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 {...@@ -976,16 +976,16 @@ fn hashFile(file: fs.File, bin_digest: *[Hasher.mac_length]u8) !void {
976}976}
977977
978// Create/Write a file, close it, then grab its stat.mtime timestamp.978// Create/Write a file, close it, then grab its stat.mtime timestamp.
979fn testGetCurrentFileTimestamp() !i128 {979fn testGetCurrentFileTimestamp(dir: fs.Dir) !i128 {
980 const test_out_file = "test-filetimestamp.tmp";980 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, .{
983 .read = true,983 .read = true,
984 .truncate = true,984 .truncate = true,
985 });985 });
986 defer {986 defer {
987 file.close();987 file.close();
988 fs.cwd().deleteFile(test_out_file) catch {};988 dir.deleteFile(test_out_file) catch {};
989 }989 }
990990
991 return (try file.stat()).mtime;991 return (try file.stat()).mtime;
...@@ -997,16 +997,17 @@ test "cache file and then recall it" {...@@ -997,16 +997,17 @@ test "cache file and then recall it" {
997 return error.SkipZigTest;997 return error.SkipZigTest;
998 }998 }
999999
1000 const cwd = fs.cwd();1000 var tmp = testing.tmpDir(.{});
1001 defer tmp.cleanup();
10011002
1002 const temp_file = "test.txt";1003 const temp_file = "test.txt";
1003 const temp_manifest_dir = "temp_manifest_dir";1004 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
1007 // Wait for file timestamps to tick1008 // Wait for file timestamps to tick
1008 const initial_time = try testGetCurrentFileTimestamp();1009 const initial_time = try testGetCurrentFileTimestamp(tmp.dir);
1009 while ((try testGetCurrentFileTimestamp()) == initial_time) {1010 while ((try testGetCurrentFileTimestamp(tmp.dir)) == initial_time) {
1010 std.time.sleep(1);1011 std.time.sleep(1);
1011 }1012 }
10121013
...@@ -1016,9 +1017,9 @@ test "cache file and then recall it" {...@@ -1016,9 +1017,9 @@ test "cache file and then recall it" {
1016 {1017 {
1017 var cache = Cache{1018 var cache = Cache{
1018 .gpa = testing.allocator,1019 .gpa = testing.allocator,
1019 .manifest_dir = try cwd.makeOpenPath(temp_manifest_dir, .{}),1020 .manifest_dir = try tmp.dir.makeOpenPath(temp_manifest_dir, .{}),
1020 };1021 };
1021 cache.addPrefix(.{ .path = null, .handle = fs.cwd() });1022 cache.addPrefix(.{ .path = null, .handle = tmp.dir });
1022 defer cache.manifest_dir.close();1023 defer cache.manifest_dir.close();
10231024
1024 {1025 {
...@@ -1054,9 +1055,6 @@ test "cache file and then recall it" {...@@ -1054,9 +1055,6 @@ test "cache file and then recall it" {
10541055
1055 try testing.expectEqual(digest1, digest2);1056 try testing.expectEqual(digest1, digest2);
1056 }1057 }
1057
1058 try cwd.deleteTree(temp_manifest_dir);
1059 try cwd.deleteFile(temp_file);
1060}1058}
10611059
1062test "check that changing a file makes cache fail" {1060test "check that changing a file makes cache fail" {
...@@ -1064,21 +1062,19 @@ test "check that changing a file makes cache fail" {...@@ -1064,21 +1062,19 @@ test "check that changing a file makes cache fail" {
1064 // https://github.com/ziglang/zig/issues/54371062 // https://github.com/ziglang/zig/issues/5437
1065 return error.SkipZigTest;1063 return error.SkipZigTest;
1066 }1064 }
1067 const cwd = fs.cwd();1065 var tmp = testing.tmpDir(.{});
1066 defer tmp.cleanup();
10681067
1069 const temp_file = "cache_hash_change_file_test.txt";1068 const temp_file = "cache_hash_change_file_test.txt";
1070 const temp_manifest_dir = "cache_hash_change_file_manifest_dir";1069 const temp_manifest_dir = "cache_hash_change_file_manifest_dir";
1071 const original_temp_file_contents = "Hello, world!\n";1070 const original_temp_file_contents = "Hello, world!\n";
1072 const updated_temp_file_contents = "Hello, world; but updated!\n";1071 const updated_temp_file_contents = "Hello, world; but updated!\n";
10731072
1074 try cwd.deleteTree(temp_manifest_dir);1073 try tmp.dir.writeFile(temp_file, original_temp_file_contents);
1075 try cwd.deleteTree(temp_file);
1076
1077 try cwd.writeFile(temp_file, original_temp_file_contents);
10781074
1079 // Wait for file timestamps to tick1075 // Wait for file timestamps to tick
1080 const initial_time = try testGetCurrentFileTimestamp();1076 const initial_time = try testGetCurrentFileTimestamp(tmp.dir);
1081 while ((try testGetCurrentFileTimestamp()) == initial_time) {1077 while ((try testGetCurrentFileTimestamp(tmp.dir)) == initial_time) {
1082 std.time.sleep(1);1078 std.time.sleep(1);
1083 }1079 }
10841080
...@@ -1088,9 +1084,9 @@ test "check that changing a file makes cache fail" {...@@ -1088,9 +1084,9 @@ test "check that changing a file makes cache fail" {
1088 {1084 {
1089 var cache = Cache{1085 var cache = Cache{
1090 .gpa = testing.allocator,1086 .gpa = testing.allocator,
1091 .manifest_dir = try cwd.makeOpenPath(temp_manifest_dir, .{}),1087 .manifest_dir = try tmp.dir.makeOpenPath(temp_manifest_dir, .{}),
1092 };1088 };
1093 cache.addPrefix(.{ .path = null, .handle = fs.cwd() });1089 cache.addPrefix(.{ .path = null, .handle = tmp.dir });
1094 defer cache.manifest_dir.close();1090 defer cache.manifest_dir.close();
10951091
1096 {1092 {
...@@ -1110,7 +1106,7 @@ test "check that changing a file makes cache fail" {...@@ -1110,7 +1106,7 @@ test "check that changing a file makes cache fail" {
1110 try ch.writeManifest();1106 try ch.writeManifest();
1111 }1107 }
11121108
1113 try cwd.writeFile(temp_file, updated_temp_file_contents);1109 try tmp.dir.writeFile(temp_file, updated_temp_file_contents);
11141110
1115 {1111 {
1116 var ch = cache.obtain();1112 var ch = cache.obtain();
...@@ -1132,9 +1128,6 @@ test "check that changing a file makes cache fail" {...@@ -1132,9 +1128,6 @@ test "check that changing a file makes cache fail" {
11321128
1133 try testing.expect(!mem.eql(u8, digest1[0..], digest2[0..]));1129 try testing.expect(!mem.eql(u8, digest1[0..], digest2[0..]));
1134 }1130 }
1135
1136 try cwd.deleteTree(temp_manifest_dir);
1137 try cwd.deleteTree(temp_file);
1138}1131}
11391132
1140test "no file inputs" {1133test "no file inputs" {
...@@ -1142,18 +1135,20 @@ test "no file inputs" {...@@ -1142,18 +1135,20 @@ test "no file inputs" {
1142 // https://github.com/ziglang/zig/issues/54371135 // https://github.com/ziglang/zig/issues/5437
1143 return error.SkipZigTest;1136 return error.SkipZigTest;
1144 }1137 }
1145 const cwd = fs.cwd();1138
1139 var tmp = testing.tmpDir(.{});
1140 defer tmp.cleanup();
1141
1146 const temp_manifest_dir = "no_file_inputs_manifest_dir";1142 const temp_manifest_dir = "no_file_inputs_manifest_dir";
1147 defer cwd.deleteTree(temp_manifest_dir) catch {};
11481143
1149 var digest1: [hex_digest_len]u8 = undefined;1144 var digest1: [hex_digest_len]u8 = undefined;
1150 var digest2: [hex_digest_len]u8 = undefined;1145 var digest2: [hex_digest_len]u8 = undefined;
11511146
1152 var cache = Cache{1147 var cache = Cache{
1153 .gpa = testing.allocator,1148 .gpa = testing.allocator,
1154 .manifest_dir = try cwd.makeOpenPath(temp_manifest_dir, .{}),1149 .manifest_dir = try tmp.dir.makeOpenPath(temp_manifest_dir, .{}),
1155 };1150 };
1156 cache.addPrefix(.{ .path = null, .handle = fs.cwd() });1151 cache.addPrefix(.{ .path = null, .handle = tmp.dir });
1157 defer cache.manifest_dir.close();1152 defer cache.manifest_dir.close();
11581153
1159 {1154 {
...@@ -1188,18 +1183,19 @@ test "Manifest with files added after initial hash work" {...@@ -1188,18 +1183,19 @@ test "Manifest with files added after initial hash work" {
1188 // https://github.com/ziglang/zig/issues/54371183 // https://github.com/ziglang/zig/issues/5437
1189 return error.SkipZigTest;1184 return error.SkipZigTest;
1190 }1185 }
1191 const cwd = fs.cwd();1186 var tmp = testing.tmpDir(.{});
1187 defer tmp.cleanup();
11921188
1193 const temp_file1 = "cache_hash_post_file_test1.txt";1189 const temp_file1 = "cache_hash_post_file_test1.txt";
1194 const temp_file2 = "cache_hash_post_file_test2.txt";1190 const temp_file2 = "cache_hash_post_file_test2.txt";
1195 const temp_manifest_dir = "cache_hash_post_file_manifest_dir";1191 const temp_manifest_dir = "cache_hash_post_file_manifest_dir";
11961192
1197 try cwd.writeFile(temp_file1, "Hello, world!\n");1193 try tmp.dir.writeFile(temp_file1, "Hello, world!\n");
1198 try cwd.writeFile(temp_file2, "Hello world the second!\n");1194 try tmp.dir.writeFile(temp_file2, "Hello world the second!\n");
11991195
1200 // Wait for file timestamps to tick1196 // Wait for file timestamps to tick
1201 const initial_time = try testGetCurrentFileTimestamp();1197 const initial_time = try testGetCurrentFileTimestamp(tmp.dir);
1202 while ((try testGetCurrentFileTimestamp()) == initial_time) {1198 while ((try testGetCurrentFileTimestamp(tmp.dir)) == initial_time) {
1203 std.time.sleep(1);1199 std.time.sleep(1);
1204 }1200 }
12051201
...@@ -1210,9 +1206,9 @@ test "Manifest with files added after initial hash work" {...@@ -1210,9 +1206,9 @@ test "Manifest with files added after initial hash work" {
1210 {1206 {
1211 var cache = Cache{1207 var cache = Cache{
1212 .gpa = testing.allocator,1208 .gpa = testing.allocator,
1213 .manifest_dir = try cwd.makeOpenPath(temp_manifest_dir, .{}),1209 .manifest_dir = try tmp.dir.makeOpenPath(temp_manifest_dir, .{}),
1214 };1210 };
1215 cache.addPrefix(.{ .path = null, .handle = fs.cwd() });1211 cache.addPrefix(.{ .path = null, .handle = tmp.dir });
1216 defer cache.manifest_dir.close();1212 defer cache.manifest_dir.close();
12171213
1218 {1214 {
...@@ -1245,11 +1241,11 @@ test "Manifest with files added after initial hash work" {...@@ -1245,11 +1241,11 @@ test "Manifest with files added after initial hash work" {
1245 try testing.expect(mem.eql(u8, &digest1, &digest2));1241 try testing.expect(mem.eql(u8, &digest1, &digest2));
12461242
1247 // Modify the file added after initial hash1243 // 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
1250 // Wait for file timestamps to tick1246 // Wait for file timestamps to tick
1251 const initial_time2 = try testGetCurrentFileTimestamp();1247 const initial_time2 = try testGetCurrentFileTimestamp(tmp.dir);
1252 while ((try testGetCurrentFileTimestamp()) == initial_time2) {1248 while ((try testGetCurrentFileTimestamp(tmp.dir)) == initial_time2) {
1253 std.time.sleep(1);1249 std.time.sleep(1);
1254 }1250 }
12551251
...@@ -1272,8 +1268,4 @@ test "Manifest with files added after initial hash work" {...@@ -1272,8 +1268,4 @@ test "Manifest with files added after initial hash work" {
12721268
1273 try testing.expect(!mem.eql(u8, &digest1, &digest3));1269 try testing.expect(!mem.eql(u8, &digest1, &digest3));
1274 }1270 }
1275
1276 try cwd.deleteTree(temp_manifest_dir);
1277 try cwd.deleteFile(temp_file1);
1278 try cwd.deleteFile(temp_file2);
1279}1271}