Skip to content

Commit

Permalink
Merge pull request #42 from fasmat/feature/upgrade-genny-for-embedFS
Browse files Browse the repository at this point in the history
Upgrade genny for embed fs
  • Loading branch information
paganotoni authored Nov 17, 2021
2 parents 15c6f16 + 94c7662 commit 865b265
Show file tree
Hide file tree
Showing 25 changed files with 385 additions and 107 deletions.
30 changes: 21 additions & 9 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
name: Tests
on: [push]
jobs:
on:
push:
branches:
- master
pull_request:

tests-on:
name: ${{matrix.go-version}} ${{matrix.os}}
jobs:
tests-off:
name: ${{ matrix.os }} - ${{ matrix.go-version }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
go-version: [1.13.x, 1.14.x]
os: [macos-latest, windows-latest, ubuntu-latest]
go-version:
- "1.16.x"
- "1.17.x"
os:
- "macos-latest"
- "windows-latest"
- "ubuntu-latest"

steps:
- name: Checkout Code
uses: actions/checkout@v1
- uses: actions/checkout@v2
- name: Setup Go ${{ matrix.go }}
uses: actions/setup-go@v2
with:
fetch-depth: 1
go-version: ${{ matrix.go-version }}

- name: Test
run: |
go mod tidy -v
Expand Down
26 changes: 24 additions & 2 deletions disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package genny
import (
"bytes"
"io"
"io/fs"
"os"
"runtime"
"sort"
Expand All @@ -28,6 +29,24 @@ func (d *Disk) AddBox(box packd.Walker) error {
})
}

func (d *Disk) AddFS(fsys fs.FS) error {
return fs.WalkDir(fsys, ".", func(path string, dir fs.DirEntry, err error) error {
if err != nil {
return err
}
if dir.IsDir() {
return nil
}

file, err := fsys.Open(path)
if err != nil {
return err
}
d.Add(NewFile(path, file))
return nil
})
}

// Files returns a sorted list of all the files in the disk
func (d *Disk) Files() []File {
var files []File
Expand Down Expand Up @@ -55,7 +74,7 @@ func newDisk(r *Runner) *Disk {
func (d *Disk) Remove(name string) {
d.moot.Lock()
defer d.moot.Unlock()
for f, _ := range d.files {
for f := range d.files {
if strings.HasPrefix(f, name) {
delete(d.files, f)
}
Expand All @@ -81,7 +100,10 @@ func (d *Disk) Find(name string) (File, error) {
d.moot.RLock()
if f, ok := d.files[name]; ok {
if seek, ok := f.(io.Seeker); ok {
seek.Seek(0, 0)
_, err := seek.Seek(0, 0)
if err != nil {
return nil, err
}
}
d.moot.RUnlock()
return f, nil
Expand Down
25 changes: 21 additions & 4 deletions disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/gobuffalo/genny/v2"
"github.com/gobuffalo/genny/v2/internal/testdata"
"github.com/gobuffalo/packd"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -45,7 +46,7 @@ func Test_Disk_Delete(t *testing.T) {
d := run.Disk
d.Add(genny.NewFile("foo.txt", nil))
d.Add(genny.NewFile("bar.txt", nil))
d.Delete("foo.txt")
r.NoError(d.Delete("foo.txt"))

files := d.Files()
r.Len(files, 1)
Expand Down Expand Up @@ -111,10 +112,9 @@ func Test_Disk_AddBox(t *testing.T) {
return err
}
p := strings.TrimPrefix(path, td+string(filepath.Separator))
box.AddBytes(p, b)

return nil
return box.AddBytes(p, b)
})
r.NoError(err)

run := genny.DryRunner(context.Background())
d := run.Disk
Expand All @@ -129,3 +129,20 @@ func Test_Disk_AddBox(t *testing.T) {
r.NoError(err)
r.Equal("bar/baz.txt", f.Name())
}

func Test_Disk_AddFS(t *testing.T) {
r := require.New(t)

run := genny.DryRunner(context.Background())
d := run.Disk
err := d.AddFS(testdata.Data())
r.NoError(err)

f, err := d.Find("foo.txt")
r.NoError(err)
r.Equal("foo.txt", f.Name())

f, err = d.Find("bar/baz.txt")
r.NoError(err)
r.Equal("bar/baz.txt", f.Name())
}
25 changes: 25 additions & 0 deletions force.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package genny

import (
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -23,6 +24,30 @@ func ForceBox(g *Generator, box packd.Walker, force bool) error {
})
}

// ForceFS will mount each file in the fs.FS and wrap it with ForceFile
func ForceFS(g *Generator, fsys fs.FS, force bool) error {
return fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
r, err := fsys.Open(path)
if err != nil {
return err
}
f := NewFile(path, r)
ff := ForceFile(f, force)
f, err = ff(f)
if err != nil {
return err
}
g.File(f)
return nil
})
}

// ForceFile is a TransformerFn that will return an error if the path exists if `force` is false. If `force` is true it will delete the path.
func ForceFile(f File, force bool) TransformerFn {
return func(f File) (File, error) {
Expand Down
19 changes: 19 additions & 0 deletions generator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package genny

import (
"io/fs"
"math/rand"
"os/exec"
"sync"
Expand Down Expand Up @@ -79,6 +80,24 @@ func (g *Generator) Box(box packd.Walker) error {
})
}

// FS walks through a fs.FS and adds Files for each entry.
func (g *Generator) FS(fsys fs.FS) error {
return fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
f, err := fsys.Open(path)
if err != nil {
return err
}
g.File(NewFile(path, f))
return nil
})
}

// RunFn adds a generic "runner" function to the generator.
func (g *Generator) RunFn(fn RunFn) {
g.moot.Lock()
Expand Down
32 changes: 28 additions & 4 deletions generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"testing"

"github.com/gobuffalo/genny/v2/internal/testdata"
"github.com/gobuffalo/packd"
"github.com/stretchr/testify/require"
)
Expand All @@ -24,7 +25,7 @@ func Test_Generator_File(t *testing.T) {
g.File(NewFile("foo.txt", strings.NewReader("hello")))

run := DryRunner(context.Background())
run.With(g)
r.NoError(run.With(g))
r.NoError(run.Run())

res := run.Results()
Expand All @@ -43,7 +44,30 @@ func Test_Generator_Box(t *testing.T) {
r.NoError(g.Box(fixtures))

run := DryRunner(context.Background())
run.With(g)
r.NoError(run.With(g))
r.NoError(run.Run())

res := run.Results()
r.Len(res.Commands, 0)
r.Len(res.Files, 2)

f := res.Files[0]
r.Equal("bar/baz.txt", f.Name())
r.Equal("baz!", f.String())

f = res.Files[1]
r.Equal("foo.txt", f.Name())
r.Equal("foo!", f.String())
}

func Test_Generator_FS(t *testing.T) {
r := require.New(t)

g := New()
r.NoError(g.FS(testdata.Data()))

run := DryRunner(context.Background())
r.NoError(run.With(g))
r.NoError(run.Run())

res := run.Results()
Expand All @@ -66,7 +90,7 @@ func Test_Command(t *testing.T) {
g.Command(exec.Command("echo", "hello"))

run := DryRunner(context.Background())
run.With(g)
r.NoError(run.With(g))
r.NoError(run.Run())

res := run.Results()
Expand Down Expand Up @@ -111,7 +135,7 @@ func Test_Merge(t *testing.T) {
g1.Merge(g2)

run := DryRunner(context.Background())
run.With(g1)
r.NoError(run.With(g1))
r.NoError(run.Run())

res := run.Results()
Expand Down
77 changes: 76 additions & 1 deletion gentest/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package gentest

import (
"fmt"
"io/fs"
"io/ioutil"
"path/filepath"
"sort"
"strings"
Expand Down Expand Up @@ -35,7 +37,7 @@ func CompareFiles(exp []string, act []genny.File) error {
}

// CompareBox compares a packd.Walkable box of files (usually fixtures)
// the results of a genny.Runner
// to the results of a genny.Runner
func CompareBox(exp packd.Walkable, res genny.Results) error {
return exp.Walk(func(path string, file packd.File) error {
if filepath.Base(path) == ".DS_Store" {
Expand All @@ -52,6 +54,43 @@ func CompareBox(exp packd.Walkable, res genny.Results) error {
})
}

// CompareFS compares a fs.FS of files (usually fixtures) to the results
// of a genny.Runner
func CompareFS(exp fs.FS, res genny.Results) error {
return fs.WalkDir(exp, ".", func(path string, d fs.DirEntry, err error) error {
if filepath.Base(path) == ".DS_Store" {
return nil
}

if err != nil {
return err
}
if d.IsDir() {
return nil
}

f, err := res.Find(path)
if err != nil {
return err
}

file, err := exp.Open(path)
if err != nil {
return err
}
b, err := ioutil.ReadAll(file)
if err != nil {
return err
}

if string(b) != f.String() {
return fmt.Errorf("[%s] expected %s to match %s", path, string(b), f)
}

return nil
})
}

// CompareBoxStripped compares a packd.Walkable box of files (usually fixtures)
// the results of a genny.Runner by removing any whitespaces, tabs, or newlines.
func CompareBoxStripped(exp packd.Walkable, res genny.Results) error {
Expand All @@ -70,6 +109,42 @@ func CompareBoxStripped(exp packd.Walkable, res genny.Results) error {
})
}

// CompareFSStripped compares a fs.FS (usually fixtures) to the results of a
// genny.Runner by removing any whitespaces, tabs, or newlines.
func CompareFSStripped(exp fs.FS, res genny.Results) error {
return fs.WalkDir(exp, ".", func(path string, d fs.DirEntry, err error) error {
if filepath.Base(path) == ".DS_Store" {
return nil
}

if err != nil {
return err
}
if d.IsDir() {
return nil
}

f, err := res.Find(path)
if err != nil {
return err
}

file, err := exp.Open(path)
if err != nil {
return err
}
b, err := ioutil.ReadAll(file)
if err != nil {
return err
}

if clean(string(b)) != clean(f.String()) {
return fmt.Errorf("[%s] expected %s to match %s", path, file, f)
}
return nil
})
}

func clean(s string) string {
s = strings.TrimSpace(s)
s = strings.Replace(s, "\n", "", -1)
Expand Down
Loading

0 comments on commit 865b265

Please sign in to comment.