Make 'persistToDisk' take a context argument

This commit is contained in:
Pieter Noordhuis 2024-11-18 21:27:01 +01:00
parent 4fea0219fd
commit 9e7aaa3f26
No known key found for this signature in database
GPG Key ID: 12ACCCC104CF2930
5 changed files with 27 additions and 22 deletions

View File

@ -17,7 +17,7 @@ type file interface {
DstPath() *destinationPath DstPath() *destinationPath
// Write file to disk at the destination path. // Write file to disk at the destination path.
PersistToDisk() error Write(ctx context.Context) error
// contents returns the file contents as a byte slice. // contents returns the file contents as a byte slice.
// This is used for testing purposes. // This is used for testing purposes.
@ -60,7 +60,7 @@ func (f *copyFile) DstPath() *destinationPath {
return f.dstPath return f.dstPath
} }
func (f *copyFile) PersistToDisk() error { func (f *copyFile) Write(ctx context.Context) error {
path := f.DstPath().absPath() path := f.DstPath().absPath()
err := os.MkdirAll(filepath.Dir(path), 0755) err := os.MkdirAll(filepath.Dir(path), 0755)
if err != nil { if err != nil {
@ -97,7 +97,7 @@ func (f *inMemoryFile) DstPath() *destinationPath {
return f.dstPath return f.dstPath
} }
func (f *inMemoryFile) PersistToDisk() error { func (f *inMemoryFile) Write(ctx context.Context) error {
path := f.DstPath().absPath() path := f.DstPath().absPath()
err := os.MkdirAll(filepath.Dir(path), 0755) err := os.MkdirAll(filepath.Dir(path), 0755)

View File

@ -12,7 +12,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func testInMemoryFile(t *testing.T, perm fs.FileMode) { func testInMemoryFile(t *testing.T, ctx context.Context, perm fs.FileMode) {
tmpDir := t.TempDir() tmpDir := t.TempDir()
f := &inMemoryFile{ f := &inMemoryFile{
@ -23,14 +23,14 @@ func testInMemoryFile(t *testing.T, perm fs.FileMode) {
perm: perm, perm: perm,
content: []byte("123"), content: []byte("123"),
} }
err := f.PersistToDisk() err := f.Write(ctx)
assert.NoError(t, err) assert.NoError(t, err)
assertFileContent(t, filepath.Join(tmpDir, "a/b/c"), "123") assertFileContent(t, filepath.Join(tmpDir, "a/b/c"), "123")
assertFilePermissions(t, filepath.Join(tmpDir, "a/b/c"), perm) assertFilePermissions(t, filepath.Join(tmpDir, "a/b/c"), perm)
} }
func testCopyFile(t *testing.T, perm fs.FileMode) { func testCopyFile(t *testing.T, ctx context.Context, perm fs.FileMode) {
tmpDir := t.TempDir() tmpDir := t.TempDir()
err := os.WriteFile(filepath.Join(tmpDir, "source"), []byte("qwerty"), perm) err := os.WriteFile(filepath.Join(tmpDir, "source"), []byte("qwerty"), perm)
require.NoError(t, err) require.NoError(t, err)
@ -45,7 +45,7 @@ func testCopyFile(t *testing.T, perm fs.FileMode) {
srcPath: "source", srcPath: "source",
srcFS: os.DirFS(tmpDir), srcFS: os.DirFS(tmpDir),
} }
err = f.PersistToDisk() err = f.Write(ctx)
assert.NoError(t, err) assert.NoError(t, err)
assertFileContent(t, filepath.Join(tmpDir, "a/b/c"), "qwerty") assertFileContent(t, filepath.Join(tmpDir, "a/b/c"), "qwerty")
@ -78,7 +78,8 @@ func TestTemplateInMemoryFilePersistToDisk(t *testing.T) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
t.SkipNow() t.SkipNow()
} }
testInMemoryFile(t, 0755) ctx := context.Background()
testInMemoryFile(t, ctx, 0755)
} }
func TestTemplateInMemoryFilePersistToDiskForWindows(t *testing.T) { func TestTemplateInMemoryFilePersistToDiskForWindows(t *testing.T) {
@ -87,14 +88,16 @@ func TestTemplateInMemoryFilePersistToDiskForWindows(t *testing.T) {
} }
// we have separate tests for windows because of differences in valid // we have separate tests for windows because of differences in valid
// fs.FileMode values we can use for different operating systems. // fs.FileMode values we can use for different operating systems.
testInMemoryFile(t, 0666) ctx := context.Background()
testInMemoryFile(t, ctx, 0666)
} }
func TestTemplateCopyFilePersistToDisk(t *testing.T) { func TestTemplateCopyFilePersistToDisk(t *testing.T) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
t.SkipNow() t.SkipNow()
} }
testCopyFile(t, 0644) ctx := context.Background()
testCopyFile(t, ctx, 0644)
} }
func TestTemplateCopyFilePersistToDiskForWindows(t *testing.T) { func TestTemplateCopyFilePersistToDiskForWindows(t *testing.T) {
@ -103,5 +106,6 @@ func TestTemplateCopyFilePersistToDiskForWindows(t *testing.T) {
} }
// we have separate tests for windows because of differences in valid // we have separate tests for windows because of differences in valid
// fs.FileMode values we can use for different operating systems. // fs.FileMode values we can use for different operating systems.
testCopyFile(t, 0666) ctx := context.Background()
testCopyFile(t, ctx, 0666)
} }

View File

@ -72,7 +72,7 @@ func Materialize(ctx context.Context, configFilePath string, templateFS fs.FS, o
return err return err
} }
err = r.persistToDisk() err = r.persistToDisk(ctx)
if err != nil { if err != nil {
return err return err
} }

View File

@ -299,7 +299,7 @@ func (r *renderer) walk() error {
return nil return nil
} }
func (r *renderer) persistToDisk() error { func (r *renderer) persistToDisk(ctx context.Context) error {
// Accumulate files which we will persist, skipping files whose path matches // Accumulate files which we will persist, skipping files whose path matches
// any of the skip patterns // any of the skip patterns
filesToPersist := make([]file, 0) filesToPersist := make([]file, 0)
@ -329,7 +329,7 @@ func (r *renderer) persistToDisk() error {
// Persist files to disk // Persist files to disk
for _, file := range filesToPersist { for _, file := range filesToPersist {
err := file.PersistToDisk() err := file.Write(ctx)
if err != nil { if err != nil {
return err return err
} }

View File

@ -63,7 +63,7 @@ func assertBuiltinTemplateValid(t *testing.T, template string, settings map[stri
// Evaluate template // Evaluate template
err = renderer.walk() err = renderer.walk()
require.NoError(t, err) require.NoError(t, err)
err = renderer.persistToDisk() err = renderer.persistToDisk(ctx)
require.NoError(t, err) require.NoError(t, err)
b, err := bundle.Load(ctx, filepath.Join(tempDir, "my_project")) b, err := bundle.Load(ctx, filepath.Join(tempDir, "my_project"))
@ -187,7 +187,7 @@ func TestRendererWithAssociatedTemplateInLibrary(t *testing.T) {
err = r.walk() err = r.walk()
require.NoError(t, err) require.NoError(t, err)
err = r.persistToDisk() err = r.persistToDisk(ctx)
require.NoError(t, err) require.NoError(t, err)
b, err := os.ReadFile(filepath.Join(tmpDir, "my_email")) b, err := os.ReadFile(filepath.Join(tmpDir, "my_email"))
@ -350,7 +350,7 @@ func TestRendererPersistToDisk(t *testing.T) {
}, },
} }
err := r.persistToDisk() err := r.persistToDisk(ctx)
require.NoError(t, err) require.NoError(t, err)
assert.NoFileExists(t, filepath.Join(tmpDir, "a", "b", "c")) assert.NoFileExists(t, filepath.Join(tmpDir, "a", "b", "c"))
@ -438,7 +438,7 @@ func TestRendererSkipAllFilesInCurrentDirectory(t *testing.T) {
// All 3 files are executed and have in memory representations // All 3 files are executed and have in memory representations
require.Len(t, r.files, 3) require.Len(t, r.files, 3)
err = r.persistToDisk() err = r.persistToDisk(ctx)
require.NoError(t, err) require.NoError(t, err)
entries, err := os.ReadDir(tmpDir) entries, err := os.ReadDir(tmpDir)
@ -480,7 +480,7 @@ func TestRendererSkip(t *testing.T) {
// This is because "dir2/*" matches the files in dir2, but not dir2 itself // This is because "dir2/*" matches the files in dir2, but not dir2 itself
assert.Len(t, r.files, 6) assert.Len(t, r.files, 6)
err = r.persistToDisk() err = r.persistToDisk(ctx)
require.NoError(t, err) require.NoError(t, err)
assert.FileExists(t, filepath.Join(tmpDir, "file1")) assert.FileExists(t, filepath.Join(tmpDir, "file1"))
@ -534,6 +534,7 @@ func TestRendererReadsPermissionsBits(t *testing.T) {
func TestRendererErrorOnConflictingFile(t *testing.T) { func TestRendererErrorOnConflictingFile(t *testing.T) {
tmpDir := t.TempDir() tmpDir := t.TempDir()
ctx := context.Background()
f, err := os.Create(filepath.Join(tmpDir, "a")) f, err := os.Create(filepath.Join(tmpDir, "a"))
require.NoError(t, err) require.NoError(t, err)
@ -553,7 +554,7 @@ func TestRendererErrorOnConflictingFile(t *testing.T) {
}, },
}, },
} }
err = r.persistToDisk() err = r.persistToDisk(ctx)
assert.EqualError(t, err, fmt.Sprintf("failed to initialize template, one or more files already exist: %s", filepath.Join(tmpDir, "a"))) assert.EqualError(t, err, fmt.Sprintf("failed to initialize template, one or more files already exist: %s", filepath.Join(tmpDir, "a")))
} }
@ -580,7 +581,7 @@ func TestRendererNoErrorOnConflictingFileIfSkipped(t *testing.T) {
}, },
}, },
} }
err = r.persistToDisk() err = r.persistToDisk(ctx)
// No error is returned even though a conflicting file exists. This is because // No error is returned even though a conflicting file exists. This is because
// the generated file is being skipped // the generated file is being skipped
assert.NoError(t, err) assert.NoError(t, err)
@ -623,7 +624,7 @@ func TestRendererFileTreeRendering(t *testing.T) {
assert.Len(t, r.files, 1) assert.Len(t, r.files, 1)
assert.Equal(t, r.files[0].DstPath().absPath(), filepath.Join(tmpDir, "my_directory", "my_file")) assert.Equal(t, r.files[0].DstPath().absPath(), filepath.Join(tmpDir, "my_directory", "my_file"))
err = r.persistToDisk() err = r.persistToDisk(ctx)
require.NoError(t, err) require.NoError(t, err)
// Assert files and directories are correctly materialized. // Assert files and directories are correctly materialized.