60 lines
1.8 KiB
Go
60 lines
1.8 KiB
Go
package isolation
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestLegacyImportAcceptsOnlyPreparedEmptyRunDirectories(t *testing.T) {
|
|
root := t.TempDir()
|
|
if err := os.MkdirAll(filepath.Join(root, "apps", "public"), 0750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if empty, err := importTargetEmpty(root, "/apps/public"); err != nil || !empty {
|
|
t.Fatalf("prepared directory refused: %v", err)
|
|
}
|
|
file := filepath.Join(root, "apps", "public", "existing.txt")
|
|
if err := os.WriteFile(file, []byte("do not overwrite"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if empty, err := importTargetEmpty(root, "/apps/public"); err != nil || empty {
|
|
t.Fatal("nonempty directory accepted")
|
|
}
|
|
if err := os.Remove(file); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Mkdir(filepath.Join(root, "unrelated"), 0750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if empty, err := importTargetEmpty(root, "/apps/public"); err != nil || empty {
|
|
t.Fatal("unrelated directory accepted")
|
|
}
|
|
}
|
|
|
|
func TestLegacyCopyPreservesSourceAndPreparedDirectories(t *testing.T) {
|
|
source, target := t.TempDir(), t.TempDir()
|
|
for _, root := range []string{source, target} {
|
|
if err := os.Mkdir(filepath.Join(root, "public"), 0750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
original := filepath.Join(source, "public", "index.php")
|
|
if err := os.WriteFile(original, []byte("source marker"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := copyLegacyTree(context.Background(), source, target); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, name := range []string{original, filepath.Join(target, "public", "index.php")} {
|
|
data, err := os.ReadFile(name)
|
|
if err != nil || string(data) != "source marker" {
|
|
t.Fatalf("copy changed data: %v", err)
|
|
}
|
|
}
|
|
if err := copyLegacyTree(context.Background(), source, target); err == nil {
|
|
t.Fatal("existing file overwritten")
|
|
}
|
|
}
|