chore(*): extract test helpers in pkg/testing

This commit is contained in:
Michelle Noorali 2017-07-28 15:31:11 -04:00
Родитель 235e43835a
Коммит d46fdf44cf
3 изменённых файлов: 46 добавлений и 35 удалений

Просмотреть файл

@ -12,6 +12,7 @@ import (
"testing"
"github.com/Azure/draft/pkg/draft/draftpath"
helpers "github.com/Azure/draft/pkg/testing"
)
const gitkeepfile = ".gitkeep"
@ -45,7 +46,7 @@ func TestCreate(t *testing.T) {
}
pDir = destcompare
}
copyTree(t, tc.src, pDir)
helpers.CopyTree(t, tc.src, pDir)
// Test
create := &createCmd{
@ -86,39 +87,6 @@ func tempDir(t *testing.T) (string, func()) {
}
}
// copyTree copies src directory content tree to dest.
// If dest exists, it's deleted.
// We don't handle symlinks (not needed in this test helper)
func copyTree(t *testing.T, src, dest string) {
if err := os.RemoveAll(dest); err != nil {
t.Fatalf("couldn't remove directory %s: %v", src, err)
}
if err := filepath.Walk(src, func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
dest := filepath.Join(dest, strings.TrimPrefix(p, src))
if info.IsDir() {
if err := os.MkdirAll(dest, info.Mode()); err != nil {
return err
}
} else {
data, err := ioutil.ReadFile(p)
if err != nil {
return err
}
if err = ioutil.WriteFile(dest, data, info.Mode()); err != nil {
return err
}
}
return nil
}); err != nil {
t.Fatalf("couldn't copy %s: %v", src, err)
}
}
// add .gitkeep to generated empty directories
func addGitKeep(t *testing.T, p string) {
if err := filepath.Walk(p, func(p string, info os.FileInfo, err error) error {

Просмотреть файл

@ -6,6 +6,7 @@ import (
"testing"
"github.com/Azure/draft/pkg/draft/draftpath"
helpers "github.com/Azure/draft/pkg/testing"
)
func TestPluginRemoveCmd(t *testing.T) {
@ -26,7 +27,7 @@ func TestPluginRemoveCmd(t *testing.T) {
names: []string{"echo"},
}
copyTree(t, "testdata/plugins/", pluginDirPath(remove.home))
helpers.CopyTree(t, "testdata/plugins/", pluginDirPath(remove.home))
if err := remove.run(); err != nil {
t.Errorf("Error removing plugin: %v", err)

42
pkg/testing/helpers.go Normal file
Просмотреть файл

@ -0,0 +1,42 @@
package testing
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
// copyTree copies src directory content tree to dest.
// If dest exists, it's deleted.
// We don't handle symlinks (not needed in this test helper)
func CopyTree(t *testing.T, src, dest string) {
if err := os.RemoveAll(dest); err != nil {
t.Fatalf("couldn't remove directory %s: %v", src, err)
}
if err := filepath.Walk(src, func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
dest := filepath.Join(dest, strings.TrimPrefix(p, src))
if info.IsDir() {
if err := os.MkdirAll(dest, info.Mode()); err != nil {
return err
}
} else {
data, err := ioutil.ReadFile(p)
if err != nil {
return err
}
if err = ioutil.WriteFile(dest, data, info.Mode()); err != nil {
return err
}
}
return nil
}); err != nil {
t.Fatalf("couldn't copy %s: %v", src, err)
}
}