| ... | @@ -232,7 +232,13 @@ pub const CacheHash = struct { | ... | @@ -232,7 +232,13 @@ pub const CacheHash = struct { |
| 232 | // reset the hash | 232 | // 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 | } |
| 514 | | 520 | |
| 515 | test "manifest file with extra line does not cause null pointer exeception" { | 521 | test "CacheHashes with files added after initial hash work" { |
| 516 | const cwd = fs.cwd(); | 522 | const cwd = fs.cwd(); |
| 517 | | 523 | |
| 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"; |
| 521 | | 527 | |
| 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"); |
| 524 | | 530 | |
| 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; |
| 527 | | 534 | |
| 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" { |
| 531 | | 538 | |
| 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); | | |
| 535 | | 541 | |
| 536 | // There should be nothing in the cache | 542 | // 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()); |
| 538 | | 544 | |
| | 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" { |
| 544 | | 552 | |
| 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 | } | | |
| 555 | | 555 | |
| 556 | // A file that we depend on has been updated, so the cache should not contain an entry for it | 556 | // 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 | } |
| 559 | | 559 | |
| | 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..])); |
| 561 | | 580 | |
| 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); |