Skip to content

Commit 6d981c2

Browse files
committed
Set the write bit for files written during template initialization
This used to work because the permission bits for builtin templates were hardcoded to 0644 for files and 0755 for directories. As of #1912 (and the PRs it depends on), builtin templates are no longer pre-materialized to a temporary directory and read directly from the embedded filesystem. It turns out that this builtin filesystem returns 0444 as the permission bits for the files it contains. These bits are carried over to the destination filesystem. This change updates template materialization to always set the owner's write bit. It doesn't really make sense to write read-only files and expect users to work with these files in a VCS (note: Git only stores the executable bit). The regression shipped as part of v0.235.0 and will be fixed as of v0.238.0.
1 parent 60782b5 commit 6d981c2

File tree

4 files changed

+48
-22
lines changed

4 files changed

+48
-22
lines changed

internal/testutil/file.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"os"
55
"path/filepath"
66

7+
"github.com/stretchr/testify/assert"
78
"github.com/stretchr/testify/require"
89
)
910

@@ -52,3 +53,31 @@ func ReadFile(t TestingT, path string) string {
5253

5354
return string(b)
5455
}
56+
57+
// StatFile returns the file info for a file.
58+
func StatFile(t TestingT, path string) os.FileInfo {
59+
fi, err := os.Stat(path)
60+
require.NoError(t, err)
61+
62+
return fi
63+
}
64+
65+
// AssertFileContents asserts that the file at path has the expected content.
66+
func AssertFileContents(t TestingT, path, expected string) bool {
67+
actual := ReadFile(t, path)
68+
return assert.Equal(t, expected, actual)
69+
}
70+
71+
// AssertFilePermissions asserts that the file at path has the expected permissions.
72+
func AssertFilePermissions(t TestingT, path string, expected os.FileMode) bool {
73+
fi := StatFile(t, path)
74+
assert.False(t, fi.Mode().IsDir(), "expected a file, got a directory")
75+
return assert.Equal(t, expected, fi.Mode().Perm(), "expected 0%o, got 0%o", expected, fi.Mode().Perm())
76+
}
77+
78+
// AssertDirPermissions asserts that the file at path has the expected permissions.
79+
func AssertDirPermissions(t TestingT, path string, expected os.FileMode) bool {
80+
fi := StatFile(t, path)
81+
assert.True(t, fi.Mode().IsDir(), "expected a directory, got a file")
82+
return assert.Equal(t, expected, fi.Mode().Perm(), "expected 0%o, got 0%o", expected, fi.Mode().Perm())
83+
}

libs/template/file_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"runtime"
99
"testing"
1010

11+
"github.com/databricks/cli/internal/testutil"
1112
"github.com/databricks/cli/libs/filer"
1213
"github.com/stretchr/testify/assert"
1314
"github.com/stretchr/testify/require"
@@ -27,8 +28,8 @@ func testInMemoryFile(t *testing.T, ctx context.Context, perm fs.FileMode) {
2728
err = f.Write(ctx, out)
2829
assert.NoError(t, err)
2930

30-
assertFileContent(t, filepath.Join(tmpDir, "a/b/c"), "123")
31-
assertFilePermissions(t, filepath.Join(tmpDir, "a/b/c"), perm)
31+
testutil.AssertFileContents(t, filepath.Join(tmpDir, "a/b/c"), "123")
32+
testutil.AssertFilePermissions(t, filepath.Join(tmpDir, "a/b/c"), perm)
3233
}
3334

3435
func testCopyFile(t *testing.T, ctx context.Context, perm fs.FileMode) {
@@ -48,8 +49,8 @@ func testCopyFile(t *testing.T, ctx context.Context, perm fs.FileMode) {
4849
err = f.Write(ctx, out)
4950
assert.NoError(t, err)
5051

51-
assertFileContent(t, filepath.Join(tmpDir, "a/b/c"), "qwerty")
52-
assertFilePermissions(t, filepath.Join(tmpDir, "a/b/c"), perm)
52+
testutil.AssertFileContents(t, filepath.Join(tmpDir, "source"), "qwerty")
53+
testutil.AssertFilePermissions(t, filepath.Join(tmpDir, "source"), perm)
5354
}
5455

5556
func TestTemplateInMemoryFilePersistToDisk(t *testing.T) {

libs/template/renderer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ func (r *renderer) computeFile(relPathTemplate string) (file, error) {
150150
}
151151
perm := info.Mode().Perm()
152152

153+
// Always include the write bit for the owner of the file.
154+
// It does not make sense to have a file that is not writable by the owner.
155+
perm |= 0o200
156+
153157
// Execute relative path template to get destination path for the file
154158
relPath, err := r.executeTemplate(relPathTemplate)
155159
if err != nil {

libs/template/renderer_test.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,6 @@ import (
2727
"github.com/stretchr/testify/require"
2828
)
2929

30-
func assertFileContent(t *testing.T, path, content string) {
31-
b, err := os.ReadFile(path)
32-
require.NoError(t, err)
33-
assert.Equal(t, content, string(b))
34-
}
35-
36-
func assertFilePermissions(t *testing.T, path string, perm fs.FileMode) {
37-
info, err := os.Stat(path)
38-
require.NoError(t, err)
39-
assert.Equal(t, perm, info.Mode().Perm())
40-
}
41-
4230
func assertBuiltinTemplateValid(t *testing.T, template string, settings map[string]any, target string, isServicePrincipal, build bool, tempDir string) {
4331
ctx := context.Background()
4432

@@ -69,6 +57,10 @@ func assertBuiltinTemplateValid(t *testing.T, template string, settings map[stri
6957
err = renderer.persistToDisk(ctx, out)
7058
require.NoError(t, err)
7159

60+
// Verify permissions on file and directory
61+
testutil.AssertFilePermissions(t, filepath.Join(tempDir, "my_project/README.md"), fs.FileMode(0o644))
62+
testutil.AssertDirPermissions(t, filepath.Join(tempDir, "my_project/resources"), fs.FileMode(0o755))
63+
7264
b, err := bundle.Load(ctx, filepath.Join(tempDir, "my_project"))
7365
require.NoError(t, err)
7466
diags := bundle.Apply(ctx, b, phases.LoadNamedTarget(target))
@@ -347,10 +339,10 @@ func TestRendererPersistToDisk(t *testing.T) {
347339
assert.NoFileExists(t, filepath.Join(tmpDir, "a", "b", "c"))
348340
assert.NoFileExists(t, filepath.Join(tmpDir, "mno"))
349341

350-
assertFileContent(t, filepath.Join(tmpDir, "a", "b", "d"), "123")
351-
assertFilePermissions(t, filepath.Join(tmpDir, "a", "b", "d"), 0o444)
352-
assertFileContent(t, filepath.Join(tmpDir, "mmnn"), "456")
353-
assertFilePermissions(t, filepath.Join(tmpDir, "mmnn"), 0o444)
342+
testutil.AssertFileContents(t, filepath.Join(tmpDir, "a/b/d"), "123")
343+
testutil.AssertFilePermissions(t, filepath.Join(tmpDir, "a/b/d"), fs.FileMode(0o444))
344+
testutil.AssertFileContents(t, filepath.Join(tmpDir, "mmnn"), "456")
345+
testutil.AssertFilePermissions(t, filepath.Join(tmpDir, "mmnn"), fs.FileMode(0o444))
354346
}
355347

356348
func TestRendererWalk(t *testing.T) {
@@ -617,8 +609,8 @@ func TestRendererFileTreeRendering(t *testing.T) {
617609
require.NoError(t, err)
618610

619611
// Assert files and directories are correctly materialized.
620-
assert.DirExists(t, filepath.Join(tmpDir, "my_directory"))
621-
assert.FileExists(t, filepath.Join(tmpDir, "my_directory", "my_file"))
612+
testutil.AssertDirPermissions(t, filepath.Join(tmpDir, "my_directory"), fs.FileMode(0o755))
613+
testutil.AssertFilePermissions(t, filepath.Join(tmpDir, "my_directory", "my_file"), fs.FileMode(0o644))
622614
}
623615

624616
func TestRendererSubTemplateInPath(t *testing.T) {

0 commit comments

Comments
 (0)