Skip to content

Commit

Permalink
Add additional tests and implement more of vault selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
aphistic committed Aug 29, 2020
1 parent 03c43a1 commit ea25eaf
Show file tree
Hide file tree
Showing 13 changed files with 268 additions and 29 deletions.
5 changes: 4 additions & 1 deletion cmd/goblin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
flagName string
flagPackage string
flagOut string
flagIncludeRoot string
flagIncludes []string
flagExportLoader bool
flagBinary bool
Expand All @@ -28,6 +29,8 @@ func main() {
StringVar(&flagPackage)
cmdCreate.Flag("out", "Name to use for the output file").Short('o').
StringVar(&flagOut)
cmdCreate.Flag("include-root", "Root path to use when including files in the vault").Short('r').
StringVar(&flagIncludeRoot)
cmdCreate.Flag("include", "Files to include in the vault").Short('i').
StringsVar(&flagIncludes)
cmdCreate.Flag("export-loader", "Export loader in generated code").Short('e').
Expand All @@ -52,7 +55,7 @@ func main() {
goblin.MemoryBuilderLogger(logger),
goblin.MemoryBuilderExportLoader(flagExportLoader),
)
err = b.Include(flagIncludes)
err = b.Include(flagIncludeRoot, flagIncludes)
if err != nil {
fmt.Fprintf(os.Stderr, "Error including files: %s\n", err)
os.Exit(1)
Expand Down
File renamed without changes.
28 changes: 18 additions & 10 deletions fs_vault.go → filesystem_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,27 @@ import (
"strings"
)

type FSVault struct {
type FilesystemVault struct {
rootPath string
}

var _ Vault = &FSVault{}
var _ Vault = &FilesystemVault{}

func NewFSVault(rootPath string) *FSVault {
return &FSVault{
func NewFilesystemVault(rootPath string) *FilesystemVault {
return &FilesystemVault{
rootPath: rootPath,
}
}

func (v *FSVault) makePath(name string) (string, error) {
func (v *FilesystemVault) GoString() string {
return `FilesystemVault{RootPath: "` + v.rootPath + `"}`
}

func (v *FilesystemVault) String() string {
return `Filesystem Vault (` + v.rootPath + `)`
}

func (v *FilesystemVault) makePath(name string) (string, error) {
if name == filesystemRootPath {
return v.rootPath, nil
}
Expand All @@ -34,7 +42,7 @@ func (v *FSVault) makePath(name string) (string, error) {
return fullPath, nil
}

func (v *FSVault) Open(name string) (File, error) {
func (v *FilesystemVault) Open(name string) (File, error) {
fullPath, err := v.makePath(name)
if err != nil {
return nil, err
Expand All @@ -48,7 +56,7 @@ func (v *FSVault) Open(name string) (File, error) {
return newOpenFSFile(fullPath, f), nil
}

func (v *FSVault) Stat(name string) (os.FileInfo, error) {
func (v *FilesystemVault) Stat(name string) (os.FileInfo, error) {
fullPath, err := v.makePath(name)
if err != nil {
return nil, err
Expand All @@ -57,7 +65,7 @@ func (v *FSVault) Stat(name string) (os.FileInfo, error) {
return os.Stat(fullPath)
}

func (v *FSVault) ReadDir(dirName string) ([]os.FileInfo, error) {
func (v *FilesystemVault) ReadDir(dirName string) ([]os.FileInfo, error) {
fullPath, err := v.makePath(dirName)
if err != nil {
return nil, err
Expand All @@ -66,7 +74,7 @@ func (v *FSVault) ReadDir(dirName string) ([]os.FileInfo, error) {
return ioutil.ReadDir(fullPath)
}

func (v *FSVault) Glob(pattern string) ([]string, error) {
func (v *FilesystemVault) Glob(pattern string) ([]string, error) {
fullPattern, err := v.makePath(pattern)
if err != nil {
return nil, err
Expand All @@ -75,7 +83,7 @@ func (v *FSVault) Glob(pattern string) ([]string, error) {
return filepath.Glob(fullPattern)
}

func (v *FSVault) ReadFile(name string) ([]byte, error) {
func (v *FilesystemVault) ReadFile(name string) ([]byte, error) {
fullPath, err := v.makePath(name)
if err != nil {
return nil, err
Expand Down
19 changes: 19 additions & 0 deletions filesystem_vault_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package goblin

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFSVaultStringer(t *testing.T) {
t.Run("string method", func(t *testing.T) {
v := NewFilesystemVault("/")
assert.Equal(t, "Filesystem Vault (/)", v.String())
})

t.Run("gostring method", func(t *testing.T) {
v := NewFilesystemVault("/")
assert.Equal(t, `FilesystemVault{RootPath: "/"}`, v.GoString())
})
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.0
github.com/stretchr/testify v1.6.1
golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect
golang.org/x/tools v0.0.0-20200828161849-5deb26317202 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
)
27 changes: 27 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,33 @@ github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200625001655-4c5254603344 h1:vGXIOMxbNfDTk/aXCmfdLgkrSV+Z2tcbze+pEc3v5W4=
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200822124328-c89045814202 h1:VvcQYSHwXgi7W+TpUR6A9g6Up98WAHf3f/ulnJ62IyA=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200828161849-5deb26317202 h1:DrWbY9UUFi/sl/3HkNVoBjDbGfIPZZfgoGsGxOL1EU8=
golang.org/x/tools v0.0.0-20200828161849-5deb26317202/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Expand Down
5 changes: 5 additions & 0 deletions goblin.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package goblin

import "fmt"

// Refer to https://go.googlesource.com/proposal/+/master/design/draft-iofs.md for FS implementation

const (
Expand All @@ -11,4 +13,7 @@ type Vault interface {
ReadDirFS
GlobFS
ReadFileFS

fmt.GoStringer
fmt.Stringer
}
20 changes: 15 additions & 5 deletions memory_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package goblin

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -51,16 +52,21 @@ func NewMemoryBuilder(opts ...MemoryBuilderOption) *MemoryBuilder {
return b
}

func (b *MemoryBuilder) Include(globs []string) error {
func (b *MemoryBuilder) Include(rootPath string, globs []string) error {
for _, glob := range globs {
globDir := filepath.Dir(glob)
if strings.Contains(glob, "..") {
return fmt.Errorf("include paths cannot contain ..")
}

fullPathGlob := filepath.Join(rootPath, glob)
globDir := filepath.Dir(fullPathGlob)
if !strings.HasSuffix(globDir, string(os.PathSeparator)) {
globDir = globDir + string(os.PathSeparator)
}

matches, err := filepath.Glob(glob)
matches, err := filepath.Glob(fullPathGlob)
if err != nil {
b.logger.Printf("error with path '%s': %s\n", glob, err)
b.logger.Printf("error with path '%s': %s\n", fullPathGlob, err)
return err
}

Expand All @@ -69,7 +75,11 @@ func (b *MemoryBuilder) Include(globs []string) error {
if err != nil {
return err
}
filePath := strings.TrimPrefix(match, globDir)

// TODO Test how this is achieved
filePath := strings.TrimPrefix(match, rootPath)
filePath = strings.TrimPrefix(filePath, globDir)
filePath = strings.TrimPrefix(filePath, pathSeparator)

b.logger.Printf("Adding: %s... ", filePath)
data, err := ioutil.ReadFile(match)
Expand Down
8 changes: 8 additions & 0 deletions memory_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ func NewMemoryVault(opts ...MemoryVaultOption) *MemoryVault {
}
}

func (v *MemoryVault) GoString() string {
return "MemoryVault{}"
}

func (v *MemoryVault) String() string {
return "Memory Vault"
}

func (v *MemoryVault) WriteFile(path string, r io.Reader, opts ...FileOption) error {
pathParts := strings.Split(path, string(os.PathSeparator))
curRoot := v.root
Expand Down
12 changes: 12 additions & 0 deletions memory_vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ func newTestVault() *MemoryVault {
return v
}

func TestMemoryVaultStringers(t *testing.T) {
t.Run("string method", func(t *testing.T) {
mv := NewMemoryVault()
assert.Equal(t, "Memory Vault", mv.String())
})

t.Run("gostring method", func(t *testing.T) {
mv := NewMemoryVault()
assert.Equal(t, "MemoryVault{}", mv.GoString())
})
}

func TestMemoryVaultReadDir(t *testing.T) {
t.Run("read root", func(t *testing.T) {
v := newTestVault()
Expand Down
Loading

0 comments on commit ea25eaf

Please sign in to comment.