authorgravatar for leroycepearson@geemili.xyzLeRoyce Pearson <leroycepearson@geemili.xyz> 2020-04-30 19:47:04-06:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-25 13:48:43-04:00
loge6a37ed94193faf6450a0888c23168a83e914955
treed185e3c818da7e1aed100c38b92285b60f103f6b
parent42007307bed84ba691b86e17b6e414eef352e94c

Change null pointer test to `addFilePost` test


1 files changed, 32 insertions(+), 13 deletions(-)

lib/std/cache_hash.zig+32-13
...@@ -232,7 +232,13 @@ pub const CacheHash = struct {...@@ -232,7 +232,13 @@ pub const CacheHash = struct {
232 // reset the hash232 // reset the hash
233 self.blake3 = Blake3.init();233 self.blake3 = Blake3.init();
234 self.blake3.update(&bin_digest);234 self.blake3.update(&bin_digest);
235
236 // Remove files not in the initial hash
237 for (self.files.items[input_file_count..]) |*file| {
238 file.deinit(self.alloc);
239 }
235 try self.files.resize(input_file_count);240 try self.files.resize(input_file_count);
241
236 for (self.files.items) |file| {242 for (self.files.items) |file| {
237 self.blake3.update(&file.bin_digest);243 self.blake3.update(&file.bin_digest);
238 }244 }
...@@ -512,18 +518,19 @@ test "no file inputs" {...@@ -512,18 +518,19 @@ test "no file inputs" {
512 testing.expectEqual(digest1, digest2);518 testing.expectEqual(digest1, digest2);
513}519}
514520
515test "manifest file with extra line does not cause null pointer exeception" {521test "CacheHashes with files added after initial hash work" {
516 const cwd = fs.cwd();522 const cwd = fs.cwd();
517523
518 const temp_file1 = "cache_hash_remove_file_test1.txt";524 const temp_file1 = "cache_hash_post_file_test1.txt";
519 const temp_file2 = "cache_hash_remove_file_test2.txt";525 const temp_file2 = "cache_hash_post_file_test2.txt";
520 const temp_manifest_dir = "cache_hash_remove_file_manifest_dir";526 const temp_manifest_dir = "cache_hash_post_file_manifest_dir";
521527
522 try cwd.writeFile(temp_file1, "Hello, world!\n");528 try cwd.writeFile(temp_file1, "Hello, world!\n");
523 try cwd.writeFile(temp_file2, "Hello world the second!\n");529 try cwd.writeFile(temp_file2, "Hello world the second!\n");
524530
525 var digest1: [BASE64_DIGEST_LEN]u8 = undefined;531 var digest1: [BASE64_DIGEST_LEN]u8 = undefined;
526 var digest2: [BASE64_DIGEST_LEN]u8 = undefined;532 var digest2: [BASE64_DIGEST_LEN]u8 = undefined;
533 var digest3: [BASE64_DIGEST_LEN]u8 = undefined;
527534
528 {535 {
529 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);536 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);
...@@ -531,11 +538,12 @@ test "manifest file with extra line does not cause null pointer exeception" {...@@ -531,11 +538,12 @@ test "manifest file with extra line does not cause null pointer exeception" {
531538
532 ch.add("1234");539 ch.add("1234");
533 _ = try ch.addFile(temp_file1);540 _ = try ch.addFile(temp_file1);
534 _ = try ch.addFile(temp_file2);
535541
536 // There should be nothing in the cache542 // There should be nothing in the cache
537 testing.expectEqual(@as(?[64]u8, null), try ch.hit());543 testing.expectEqual(@as(?[64]u8, null), try ch.hit());
538544
545 _ = try ch.addFilePost(temp_file2);
546
539 digest1 = ch.final();547 digest1 = ch.final();
540 }548 }
541 {549 {
...@@ -544,20 +552,31 @@ test "manifest file with extra line does not cause null pointer exeception" {...@@ -544,20 +552,31 @@ test "manifest file with extra line does not cause null pointer exeception" {
544552
545 ch.add("1234");553 ch.add("1234");
546 _ = try ch.addFile(temp_file1);554 _ = try ch.addFile(temp_file1);
547 _ = try ch.addFile(temp_file2);
548 {
549 // Remove an input file from the cache hash.
550 // We still have to add the input file, or else the initial cache
551 // hash will be different, and a different manifest file checked
552 const chf = ch.files.orderedRemove(1);
553 testing.allocator.free(chf.path.?);
554 }
555555
556 // A file that we depend on has been updated, so the cache should not contain an entry for it556 // A file that we depend on has been updated, so the cache should not contain an entry for it
557 digest2 = (try ch.hit()).?;557 digest2 = (try ch.hit()).?;
558 }558 }
559559
560 // Modify the file added after initial hash
561 try cwd.writeFile(temp_file2, "Hello world the second, updated\n");
562
563 {
564 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);
565 defer ch.release() catch unreachable;
566
567 ch.add("1234");
568 _ = try ch.addFile(temp_file1);
569
570 // A file that we depend on has been updated, so the cache should not contain an entry for it
571 testing.expectEqual(@as(?[64]u8, null), try ch.hit());
572
573 _ = try ch.addFilePost(temp_file2);
574
575 digest3 = ch.final();
576 }
577
560 testing.expect(mem.eql(u8, digest1[0..], digest2[0..]));578 testing.expect(mem.eql(u8, digest1[0..], digest2[0..]));
579 testing.expect(!mem.eql(u8, digest1[0..], digest3[0..]));
561580
562 try cwd.deleteTree(temp_manifest_dir);581 try cwd.deleteTree(temp_manifest_dir);
563 try cwd.deleteFile(temp_file1);582 try cwd.deleteFile(temp_file1);