| 607 | } |
| 608 | |
| 609 | func (HostSuite) TestFileCacheBehavior(ctx context.Context, t *testctx.T) { |
| 610 | dir := t.TempDir() |
| 611 | c := connect(ctx, t) |
| 612 | |
| 613 | tests := []struct { |
| 614 | name string |
| 615 | opts []dagger.HostFileOpts |
| 616 | expected string |
| 617 | }{ |
| 618 | { |
| 619 | name: "default aka cache", |
| 620 | opts: []dagger.HostFileOpts{}, |
| 621 | expected: "1", |
| 622 | }, |
| 623 | { |
| 624 | name: "explicit cache", |
| 625 | opts: []dagger.HostFileOpts{{NoCache: false}}, |
| 626 | expected: "1", |
| 627 | }, |
| 628 | { |
| 629 | name: "explicit no cache", |
| 630 | opts: []dagger.HostFileOpts{{NoCache: true}}, |
| 631 | expected: "12", |
| 632 | }, |
| 633 | } |
| 634 | |
| 635 | for _, test := range tests { |
| 636 | setup := func() (string, *dagger.File) { |
| 637 | bPath := filepath.Join(dir, rand.Text()) |
| 638 | require.NoError(t, os.WriteFile(bPath, []byte("1"), 0o600)) |
| 639 | |
| 640 | file := c.Host().File(bPath, test.opts...) |
| 641 | return bPath, file |
| 642 | } |
| 643 | |
| 644 | t.Run(test.name, func(ctx context.Context, t *testctx.T) { |
| 645 | bPath, file := setup() |
| 646 | content, err := file.Contents(ctx) |
| 647 | require.NoError(t, err) |
| 648 | require.Equal(t, "1", content) |
| 649 | |
| 650 | require.NoError(t, os.WriteFile(bPath, []byte("12"), 0o600)) |
| 651 | |
| 652 | content, err = file.Contents(ctx) |
| 653 | require.NoError(t, err) |
| 654 | require.Equal(t, test.expected, content) |
| 655 | }) |
| 656 | |
| 657 | t.Run(test.name+" fresh query", func(ctx context.Context, t *testctx.T) { |
| 658 | bPath, file := setup() |
| 659 | content, err := file.Contents(ctx) |
| 660 | require.NoError(t, err) |
| 661 | require.Equal(t, "1", content) |
| 662 | |
| 663 | require.NoError(t, os.WriteFile(bPath, []byte("12"), 0o600)) |
| 664 | |
| 665 | file = c.Host().File(bPath, test.opts...) |
| 666 | content, err = file.Contents(ctx) |