where is the harmony?

This commit is contained in:
Mark Bates 2019-10-30 17:32:47 -04:00
parent 296799efbc
commit 6638215c4d
9 changed files with 195 additions and 12 deletions

View File

@ -2,7 +2,9 @@ package costello
import ( import (
"fmt" "fmt"
"os"
"testing" "testing"
"time"
"github.com/markbates/pkger/pkging" "github.com/markbates/pkger/pkging"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -16,12 +18,15 @@ func All(t *testing.T, ref *Ref, fn AllFn) {
type tf func(*testing.T, *Ref, pkging.Pkger) type tf func(*testing.T, *Ref, pkging.Pkger)
tests := map[string]tf{ tests := map[string]tf{
"OpenTest": OpenTest, "Open": OpenTest,
"StatTest": StatTest, "Stat": StatTest,
"CreateTest": CreateTest, "Create": CreateTest,
"CurrentTest": CurrentTest, "Current": CurrentTest,
"InfoTest": InfoTest, "Info": InfoTest,
"MkdirAll": MkdirAllTest, "MkdirAll": MkdirAllTest,
"Remove": RemoveTest,
"RemoveAll": RemoveAllTest,
"Walk": WalkTest,
} }
pkg, err := fn(ref) pkg, err := fn(ref)
@ -38,3 +43,13 @@ func All(t *testing.T, ref *Ref, fn AllFn) {
} }
} }
func cmpFileInfo(t *testing.T, a os.FileInfo, b os.FileInfo) {
t.Helper()
r := require.New(t)
r.Equal(a.IsDir(), b.IsDir())
r.Equal(a.ModTime().Format(time.RFC3339), b.ModTime().Format(time.RFC3339))
r.Equal(a.Mode(), b.Mode())
r.Equal(a.Name(), b.Name())
r.Equal(a.Size(), b.Size())
}

View File

@ -6,7 +6,6 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
"time"
"github.com/markbates/pkger/pkging" "github.com/markbates/pkger/pkging"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -33,11 +32,7 @@ func openTest(name string, t *testing.T, ref *Ref, pkg pkging.Pkger) {
psi, err := pf.Stat() psi, err := pf.Stat()
r.NoError(err) r.NoError(err)
r.Equal(osi.IsDir(), psi.IsDir()) cmpFileInfo(t, osi, psi)
r.Equal(osi.Name(), psi.Name())
r.Equal(osi.Mode(), psi.Mode())
r.Equal(osi.Size(), psi.Size())
r.Equal(osi.ModTime().Format(time.RFC3339), psi.ModTime().Format(time.RFC3339))
if osi.IsDir() { if osi.IsDir() {
return return

View File

@ -1 +1,24 @@
package costello package costello
import (
"testing"
"github.com/markbates/pkger/pkging"
"github.com/stretchr/testify/require"
)
func RemoveTest(t *testing.T, ref *Ref, pkg pkging.Pkger) {
r := require.New(t)
r.NoError(LoadRef(ref, pkg))
name := "/go.mod"
_, err := pkg.Stat(name)
r.NoError(err)
r.NoError(pkg.Remove(name))
_, err = pkg.Stat(name)
r.Error(err)
}

View File

@ -1 +1,24 @@
package costello package costello
import (
"testing"
"github.com/markbates/pkger/pkging"
"github.com/stretchr/testify/require"
)
func RemoveAllTest(t *testing.T, ref *Ref, pkg pkging.Pkger) {
r := require.New(t)
r.NoError(LoadRef(ref, pkg))
name := "/public/assets"
_, err := pkg.Stat(name)
r.NoError(err)
r.NoError(pkg.RemoveAll(name))
_, err = pkg.Stat(name)
r.Error(err)
}

View File

@ -1 +1,48 @@
package costello package costello
import (
"os"
"path/filepath"
"testing"
"github.com/markbates/pkger/pkging"
"github.com/stretchr/testify/require"
)
func WalkTest(t *testing.T, ref *Ref, pkg pkging.Pkger) {
r := require.New(t)
r.NoError(LoadRef(ref, pkg))
name := "public"
fp := filepath.Join(ref.Dir, name)
var exp []os.FileInfo
err := filepath.Walk(fp, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
exp = append(exp, info)
return nil
})
r.NoError(err)
var act []os.FileInfo
err = pkg.Walk("/"+name, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
act = append(act, info)
return nil
})
r.NoError(err)
r.Len(act, len(exp))
for i, info := range exp {
cmpFileInfo(t, info, act[i])
}
}

View File

@ -0,0 +1,20 @@
package mem
import (
"testing"
"github.com/markbates/pkger/pkging/costello"
"github.com/stretchr/testify/require"
)
func Test_Pkger_MkdirAll(t *testing.T) {
r := require.New(t)
ref, err := costello.NewRef()
r.NoError(err)
pkg, err := New(ref.Info)
r.NoError(err)
costello.MkdirAllTest(t, ref, pkg)
}

20
pkging/mem/remove_test.go Normal file
View File

@ -0,0 +1,20 @@
package mem
import (
"testing"
"github.com/markbates/pkger/pkging/costello"
"github.com/stretchr/testify/require"
)
func Test_Pkger_Remove(t *testing.T) {
r := require.New(t)
ref, err := costello.NewRef()
r.NoError(err)
pkg, err := New(ref.Info)
r.NoError(err)
costello.RemoveTest(t, ref, pkg)
}

View File

@ -0,0 +1,20 @@
package mem
import (
"testing"
"github.com/markbates/pkger/pkging/costello"
"github.com/stretchr/testify/require"
)
func Test_Pkger_RemoveAll(t *testing.T) {
r := require.New(t)
ref, err := costello.NewRef()
r.NoError(err)
pkg, err := New(ref.Info)
r.NoError(err)
costello.RemoveAllTest(t, ref, pkg)
}

20
pkging/mem/walk_test.go Normal file
View File

@ -0,0 +1,20 @@
package mem
import (
"testing"
"github.com/markbates/pkger/pkging/costello"
"github.com/stretchr/testify/require"
)
func Test_Pkger_Walk(t *testing.T) {
r := require.New(t)
ref, err := costello.NewRef()
r.NoError(err)
pkg, err := New(ref.Info)
r.NoError(err)
costello.WalkTest(t, ref, pkg)
}