mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
04be220f5f
helper/copy CopyDir was used heavily in tests. It differes from internal/copydir in a few ways, the main one being that it creates the dst directory while the internal version expected the dst to exist (there are other differences, which is why I did not just switch tests to using internal's CopyDir). I moved the CopyDir func from helper/copy into command_test.go; I could also have moved it into internal/copy and named it something like CreateDirAndCopy so if that seems like a better option please let me know. helper/copy/CopyFile was used in a couple of spots so I moved it into internal, at which point I thought it made more sense to rename the package copy (instead of copydir). There's also a `go mod tidy` included.
108 lines
2.5 KiB
Go
108 lines
2.5 KiB
Go
package copy
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// TestCopyDir_symlinks sets up a directory with two submodules,
|
|
// one being a symlink to the other
|
|
//
|
|
// The resultant file structure is as follows:
|
|
// ├── modules
|
|
// │ ├── symlink-module -> test-module
|
|
// │ └── test-module
|
|
// │ └── main.tf
|
|
// └── target
|
|
// ├── symlink-module -> test-module
|
|
// └── test-module
|
|
// └── main.tf
|
|
|
|
func TestCopyDir_symlinks(t *testing.T) {
|
|
tmpdir, err := ioutil.TempDir("", "copy-dir-test")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
moduleDir := filepath.Join(tmpdir, "modules")
|
|
err = os.Mkdir(moduleDir, os.ModePerm)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
subModuleDir := filepath.Join(moduleDir, "test-module")
|
|
err = os.Mkdir(subModuleDir, os.ModePerm)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = ioutil.WriteFile(filepath.Join(subModuleDir, "main.tf"), []byte("hello"), 0644)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = os.Symlink("test-module", filepath.Join(moduleDir, "symlink-module"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
targetDir := filepath.Join(tmpdir, "target")
|
|
os.Mkdir(targetDir, os.ModePerm)
|
|
|
|
err = CopyDir(targetDir, moduleDir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if _, err = os.Lstat(filepath.Join(targetDir, "test-module", "main.tf")); os.IsNotExist(err) {
|
|
t.Fatal("target test-module/main.tf was not created")
|
|
}
|
|
|
|
if _, err = os.Lstat(filepath.Join(targetDir, "symlink-module", "main.tf")); os.IsNotExist(err) {
|
|
t.Fatal("target symlink-module/main.tf was not created")
|
|
}
|
|
}
|
|
|
|
func TestCopyDir_symlink_file(t *testing.T) {
|
|
tmpdir, err := ioutil.TempDir("", "copy-file-test")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
moduleDir := filepath.Join(tmpdir, "modules")
|
|
err = os.Mkdir(moduleDir, os.ModePerm)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = ioutil.WriteFile(filepath.Join(moduleDir, "main.tf"), []byte("hello"), 0644)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = os.Symlink("main.tf", filepath.Join(moduleDir, "symlink.tf"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
targetDir := filepath.Join(tmpdir, "target")
|
|
os.Mkdir(targetDir, os.ModePerm)
|
|
|
|
err = CopyDir(targetDir, moduleDir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if _, err = os.Lstat(filepath.Join(targetDir, "main.tf")); os.IsNotExist(err) {
|
|
t.Fatal("target/main.tf was not created")
|
|
}
|
|
|
|
if _, err = os.Lstat(filepath.Join(targetDir, "symlink.tf")); os.IsNotExist(err) {
|
|
t.Fatal("target/symlink.tf was not created")
|
|
}
|
|
}
|