1
- const std = @import ("std.zig" );
1
+ const builtin = @import ("builtin" );
2
+ const std = @import ("std" );
2
3
const Blake3 = std .crypto .Blake3 ;
3
4
const fs = std .fs ;
4
5
const base64 = std .base64 ;
5
6
const ArrayList = std .ArrayList ;
6
7
const assert = std .debug .assert ;
7
8
const testing = std .testing ;
9
+ const tmpDir = testing .tmpDir ;
8
10
const mem = std .mem ;
9
11
const fmt = std .fmt ;
10
12
const Allocator = std .mem .Allocator ;
@@ -53,7 +55,6 @@ pub const CacheHash = struct {
53
55
return CacheHash {
54
56
.allocator = allocator ,
55
57
.blake3 = Blake3 .init (),
56
- // TODO should claim ownership of `work_dir`?
57
58
.work_dir = work_dir ,
58
59
.manifest_dir = try work_dir .makeOpenPath (manifest_dir_path , .{}),
59
60
.manifest_file = null ,
@@ -103,7 +104,13 @@ pub const CacheHash = struct {
103
104
assert (self .manifest_file == null );
104
105
105
106
try self .files .ensureCapacity (self .files .items .len + 1 );
106
- const resolved_path = try fs .path .resolveat (self .allocator , self .work_dir , &[_ ][]const u8 {file_path });
107
+
108
+ var resolved_path : []u8 = undefined ;
109
+ if (builtin .os .tag == .wasi ) {
110
+ resolved_path = (try fs .path .resolveRelative (self .allocator , &[_ ][]const u8 {file_path })) orelse unreachable ;
111
+ } else {
112
+ resolved_path = try fs .path .resolveat (self .allocator , self .work_dir , &[_ ][]const u8 {file_path });
113
+ }
107
114
108
115
const idx = self .files .items .len ;
109
116
self .files .addOneAssumeCapacity ().* = .{
@@ -325,7 +332,12 @@ pub const CacheHash = struct {
325
332
pub fn addFilePostFetch (self : * CacheHash , file_path : []const u8 , max_file_size : usize ) ! []u8 {
326
333
assert (self .manifest_file != null );
327
334
328
- const resolved_path = try fs .path .resolveat (self .allocator , self .work_dir , &[_ ][]const u8 {file_path });
335
+ var resolved_path : []u8 = undefined ;
336
+ if (builtin .os .tag == .wasi ) {
337
+ resolved_path = (try fs .path .resolveRelative (self .allocator , &[_ ][]const u8 {file_path })) orelse unreachable ;
338
+ } else {
339
+ resolved_path = try fs .path .resolveat (self .allocator , self .work_dir , &[_ ][]const u8 {file_path });
340
+ }
329
341
errdefer self .allocator .free (resolved_path );
330
342
331
343
const new_ch_file = try self .files .addOne ();
@@ -350,7 +362,12 @@ pub const CacheHash = struct {
350
362
pub fn addFilePost (self : * CacheHash , file_path : []const u8 ) ! void {
351
363
assert (self .manifest_file != null );
352
364
353
- const resolved_path = try fs .path .resolveat (self .allocator , self .work_dir , &[_ ][]const u8 {file_path });
365
+ var resolved_path : []u8 = undefined ;
366
+ if (builtin .os .tag == .wasi ) {
367
+ resolved_path = (try fs .path .resolveRelative (self .allocator , &[_ ][]const u8 {file_path })) orelse unreachable ;
368
+ } else {
369
+ resolved_path = try fs .path .resolveat (self .allocator , self .work_dir , &[_ ][]const u8 {file_path });
370
+ }
354
371
errdefer self .allocator .free (resolved_path );
355
372
356
373
const new_ch_file = try self .files .addOne ();
@@ -473,16 +490,13 @@ fn isProblematicTimestamp(fs_clock: i128) bool {
473
490
}
474
491
475
492
test "cache file and then recall it" {
476
- if (std .Target .current .os .tag == .wasi ) {
477
- // https://github.com/ziglang/zig/issues/5437
478
- return error .SkipZigTest ;
479
- }
480
- const cwd = fs .cwd ();
493
+ var tmp = tmpDir (.{});
494
+ defer tmp .cleanup ();
481
495
482
496
const temp_file = "test.txt" ;
483
497
const temp_manifest_dir = "temp_manifest_dir" ;
484
498
485
- try cwd .writeFile (temp_file , "Hello, world!\n " );
499
+ try tmp . dir .writeFile (temp_file , "Hello, world!\n " );
486
500
487
501
while (isProblematicTimestamp (std .time .nanoTimestamp ())) {
488
502
std .time .sleep (1 );
@@ -492,7 +506,7 @@ test "cache file and then recall it" {
492
506
var digest2 : [BASE64_DIGEST_LEN ]u8 = undefined ;
493
507
494
508
{
495
- var ch = try CacheHash .init (testing .allocator , cwd , temp_manifest_dir );
509
+ var ch = try CacheHash .init (testing .allocator , tmp . dir , temp_manifest_dir );
496
510
defer ch .release ();
497
511
498
512
ch .add (true );
@@ -506,7 +520,7 @@ test "cache file and then recall it" {
506
520
digest1 = ch .final ();
507
521
}
508
522
{
509
- var ch = try CacheHash .init (testing .allocator , cwd , temp_manifest_dir );
523
+ var ch = try CacheHash .init (testing .allocator , tmp . dir , temp_manifest_dir );
510
524
defer ch .release ();
511
525
512
526
ch .add (true );
@@ -520,8 +534,8 @@ test "cache file and then recall it" {
520
534
521
535
testing .expectEqual (digest1 , digest2 );
522
536
523
- try cwd .deleteTree (temp_manifest_dir );
524
- try cwd .deleteFile (temp_file );
537
+ try tmp . dir .deleteTree (temp_manifest_dir );
538
+ try tmp . dir .deleteFile (temp_file );
525
539
}
526
540
527
541
test "give problematic timestamp" {
@@ -537,18 +551,15 @@ test "give nonproblematic timestamp" {
537
551
}
538
552
539
553
test "check that changing a file makes cache fail" {
540
- if (std .Target .current .os .tag == .wasi ) {
541
- // https://github.com/ziglang/zig/issues/5437
542
- return error .SkipZigTest ;
543
- }
544
- const cwd = fs .cwd ();
554
+ var tmp = tmpDir (.{});
555
+ defer tmp .cleanup ();
545
556
546
557
const temp_file = "cache_hash_change_file_test.txt" ;
547
558
const temp_manifest_dir = "cache_hash_change_file_manifest_dir" ;
548
559
const original_temp_file_contents = "Hello, world!\n " ;
549
560
const updated_temp_file_contents = "Hello, world; but updated!\n " ;
550
561
551
- try cwd .writeFile (temp_file , original_temp_file_contents );
562
+ try tmp . dir .writeFile (temp_file , original_temp_file_contents );
552
563
553
564
while (isProblematicTimestamp (std .time .nanoTimestamp ())) {
554
565
std .time .sleep (1 );
@@ -558,7 +569,7 @@ test "check that changing a file makes cache fail" {
558
569
var digest2 : [BASE64_DIGEST_LEN ]u8 = undefined ;
559
570
560
571
{
561
- var ch = try CacheHash .init (testing .allocator , cwd , temp_manifest_dir );
572
+ var ch = try CacheHash .init (testing .allocator , tmp . dir , temp_manifest_dir );
562
573
defer ch .release ();
563
574
564
575
ch .add ("1234" );
@@ -572,14 +583,14 @@ test "check that changing a file makes cache fail" {
572
583
digest1 = ch .final ();
573
584
}
574
585
575
- try cwd .writeFile (temp_file , updated_temp_file_contents );
586
+ try tmp . dir .writeFile (temp_file , updated_temp_file_contents );
576
587
577
588
while (isProblematicTimestamp (std .time .nanoTimestamp ())) {
578
589
std .time .sleep (1 );
579
590
}
580
591
581
592
{
582
- var ch = try CacheHash .init (testing .allocator , cwd , temp_manifest_dir );
593
+ var ch = try CacheHash .init (testing .allocator , tmp . dir , temp_manifest_dir );
583
594
defer ch .release ();
584
595
585
596
ch .add ("1234" );
@@ -596,24 +607,22 @@ test "check that changing a file makes cache fail" {
596
607
597
608
testing .expect (! mem .eql (u8 , digest1 [0.. ], digest2 [0.. ]));
598
609
599
- try cwd .deleteTree (temp_manifest_dir );
600
- try cwd .deleteFile (temp_file );
610
+ try tmp . dir .deleteTree (temp_manifest_dir );
611
+ try tmp . dir .deleteFile (temp_file );
601
612
}
602
613
603
614
test "no file inputs" {
604
- if (std .Target .current .os .tag == .wasi ) {
605
- // https://github.com/ziglang/zig/issues/5437
606
- return error .SkipZigTest ;
607
- }
608
- const cwd = fs .cwd ();
615
+ var tmp = tmpDir (.{});
616
+ defer tmp .cleanup ();
617
+
609
618
const temp_manifest_dir = "no_file_inputs_manifest_dir" ;
610
- defer cwd .deleteTree (temp_manifest_dir ) catch unreachable ;
619
+ defer tmp . dir .deleteTree (temp_manifest_dir ) catch unreachable ;
611
620
612
621
var digest1 : [BASE64_DIGEST_LEN ]u8 = undefined ;
613
622
var digest2 : [BASE64_DIGEST_LEN ]u8 = undefined ;
614
623
615
624
{
616
- var ch = try CacheHash .init (testing .allocator , cwd , temp_manifest_dir );
625
+ var ch = try CacheHash .init (testing .allocator , tmp . dir , temp_manifest_dir );
617
626
defer ch .release ();
618
627
619
628
ch .add ("1234" );
@@ -624,7 +633,7 @@ test "no file inputs" {
624
633
digest1 = ch .final ();
625
634
}
626
635
{
627
- var ch = try CacheHash .init (testing .allocator , cwd , temp_manifest_dir );
636
+ var ch = try CacheHash .init (testing .allocator , tmp . dir , temp_manifest_dir );
628
637
defer ch .release ();
629
638
630
639
ch .add ("1234" );
@@ -636,18 +645,15 @@ test "no file inputs" {
636
645
}
637
646
638
647
test "CacheHashes with files added after initial hash work" {
639
- if (std .Target .current .os .tag == .wasi ) {
640
- // https://github.com/ziglang/zig/issues/5437
641
- return error .SkipZigTest ;
642
- }
643
- const cwd = fs .cwd ();
648
+ var tmp = tmpDir (.{});
649
+ defer tmp .cleanup ();
644
650
645
651
const temp_file1 = "cache_hash_post_file_test1.txt" ;
646
652
const temp_file2 = "cache_hash_post_file_test2.txt" ;
647
653
const temp_manifest_dir = "cache_hash_post_file_manifest_dir" ;
648
654
649
- try cwd .writeFile (temp_file1 , "Hello, world!\n " );
650
- try cwd .writeFile (temp_file2 , "Hello world the second!\n " );
655
+ try tmp . dir .writeFile (temp_file1 , "Hello, world!\n " );
656
+ try tmp . dir .writeFile (temp_file2 , "Hello world the second!\n " );
651
657
652
658
while (isProblematicTimestamp (std .time .nanoTimestamp ())) {
653
659
std .time .sleep (1 );
@@ -658,7 +664,7 @@ test "CacheHashes with files added after initial hash work" {
658
664
var digest3 : [BASE64_DIGEST_LEN ]u8 = undefined ;
659
665
660
666
{
661
- var ch = try CacheHash .init (testing .allocator , cwd , temp_manifest_dir );
667
+ var ch = try CacheHash .init (testing .allocator , tmp . dir , temp_manifest_dir );
662
668
defer ch .release ();
663
669
664
670
ch .add ("1234" );
@@ -672,7 +678,7 @@ test "CacheHashes with files added after initial hash work" {
672
678
digest1 = ch .final ();
673
679
}
674
680
{
675
- var ch = try CacheHash .init (testing .allocator , cwd , temp_manifest_dir );
681
+ var ch = try CacheHash .init (testing .allocator , tmp . dir , temp_manifest_dir );
676
682
defer ch .release ();
677
683
678
684
ch .add ("1234" );
@@ -683,14 +689,14 @@ test "CacheHashes with files added after initial hash work" {
683
689
testing .expect (mem .eql (u8 , & digest1 , & digest2 ));
684
690
685
691
// Modify the file added after initial hash
686
- try cwd .writeFile (temp_file2 , "Hello world the second, updated\n " );
692
+ try tmp . dir .writeFile (temp_file2 , "Hello world the second, updated\n " );
687
693
688
694
while (isProblematicTimestamp (std .time .nanoTimestamp ())) {
689
695
std .time .sleep (1 );
690
696
}
691
697
692
698
{
693
- var ch = try CacheHash .init (testing .allocator , cwd , temp_manifest_dir );
699
+ var ch = try CacheHash .init (testing .allocator , tmp . dir , temp_manifest_dir );
694
700
defer ch .release ();
695
701
696
702
ch .add ("1234" );
@@ -706,7 +712,7 @@ test "CacheHashes with files added after initial hash work" {
706
712
707
713
testing .expect (! mem .eql (u8 , & digest1 , & digest3 ));
708
714
709
- try cwd .deleteTree (temp_manifest_dir );
710
- try cwd .deleteFile (temp_file1 );
711
- try cwd .deleteFile (temp_file2 );
715
+ try tmp . dir .deleteTree (temp_manifest_dir );
716
+ try tmp . dir .deleteFile (temp_file1 );
717
+ try tmp . dir .deleteFile (temp_file2 );
712
718
}
0 commit comments