2023-08-01 13:43:27 +00:00
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
2024-11-20 10:11:31 +00:00
|
|
|
"github.com/databricks/cli/libs/filer"
|
2023-08-01 13:43:27 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2024-11-20 10:11:31 +00:00
|
|
|
func testInMemoryFile(t *testing.T, ctx context.Context, perm fs.FileMode) {
|
2023-08-01 13:43:27 +00:00
|
|
|
tmpDir := t.TempDir()
|
|
|
|
|
|
|
|
f := &inMemoryFile{
|
|
|
|
perm: perm,
|
2024-11-20 10:11:31 +00:00
|
|
|
relPath: "a/b/c",
|
2023-08-01 13:43:27 +00:00
|
|
|
content: []byte("123"),
|
|
|
|
}
|
2024-11-20 10:11:31 +00:00
|
|
|
|
|
|
|
out, err := filer.NewLocalClient(tmpDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = f.Write(ctx, out)
|
2023-08-01 13:43:27 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assertFileContent(t, filepath.Join(tmpDir, "a/b/c"), "123")
|
|
|
|
assertFilePermissions(t, filepath.Join(tmpDir, "a/b/c"), perm)
|
|
|
|
}
|
|
|
|
|
2024-11-20 10:11:31 +00:00
|
|
|
func testCopyFile(t *testing.T, ctx context.Context, perm fs.FileMode) {
|
2023-08-01 13:43:27 +00:00
|
|
|
tmpDir := t.TempDir()
|
2024-11-20 09:28:35 +00:00
|
|
|
err := os.WriteFile(filepath.Join(tmpDir, "source"), []byte("qwerty"), perm)
|
2023-08-01 13:43:27 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
f := ©File{
|
2024-11-20 09:28:35 +00:00
|
|
|
perm: perm,
|
2024-11-20 10:11:31 +00:00
|
|
|
relPath: "a/b/c",
|
2024-11-20 09:28:35 +00:00
|
|
|
srcFS: os.DirFS(tmpDir),
|
2024-11-20 10:11:31 +00:00
|
|
|
srcPath: "source",
|
2023-08-01 13:43:27 +00:00
|
|
|
}
|
2024-11-20 10:11:31 +00:00
|
|
|
|
|
|
|
out, err := filer.NewLocalClient(tmpDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = f.Write(ctx, out)
|
2023-08-01 13:43:27 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assertFileContent(t, filepath.Join(tmpDir, "a/b/c"), "qwerty")
|
|
|
|
assertFilePermissions(t, filepath.Join(tmpDir, "a/b/c"), perm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateInMemoryFilePersistToDisk(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
2024-11-20 10:11:31 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
testInMemoryFile(t, ctx, 0755)
|
2023-08-01 13:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateInMemoryFilePersistToDiskForWindows(t *testing.T) {
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
// we have separate tests for windows because of differences in valid
|
|
|
|
// fs.FileMode values we can use for different operating systems.
|
2024-11-20 10:11:31 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
testInMemoryFile(t, ctx, 0666)
|
2023-08-01 13:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateCopyFilePersistToDisk(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
2024-11-20 10:11:31 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
testCopyFile(t, ctx, 0644)
|
2023-08-01 13:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateCopyFilePersistToDiskForWindows(t *testing.T) {
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
// we have separate tests for windows because of differences in valid
|
|
|
|
// fs.FileMode values we can use for different operating systems.
|
2024-11-20 10:11:31 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
testCopyFile(t, ctx, 0666)
|
2023-08-01 13:43:27 +00:00
|
|
|
}
|