Skip to content

Commit

Permalink
test(proxy): migrate tests to use virtual-fs (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Dec 14, 2023
1 parent 5e85c75 commit 7b0c5cd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 674 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dist/

src/i18n/out/en-US/active.en-GB.json
test/data/storage/scientist/
test/data/research/

.DS_Store
thumbs.db
7 changes: 6 additions & 1 deletion src/app/proxy/runner-sampler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import (
"github.com/spf13/viper"
)

const (
silent = false
verbose = true
)

type runnerTE struct {
given string
should string
Expand Down Expand Up @@ -50,7 +55,7 @@ var _ = Describe("SamplerRunner", Ordered, func() {
configPath = filepath.Join(repo, "test", "data", "configuration")
Expect(matchers.AsDirectory(configPath)).To(matchers.ExistInFS(nfs))

root = helpers.Scientist()
root = helpers.Scientist(nfs, "nasa-scientist-index.xml", verbose)
Expect(matchers.AsDirectory(root)).To(matchers.ExistInFS(nfs))
})

Expand Down
43 changes: 25 additions & 18 deletions src/internal/helpers/directory-tree-builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,27 @@ import (
"github.com/samber/lo"
"github.com/snivilised/extendio/collections"

"github.com/snivilised/extendio/xfs/storage"
"github.com/snivilised/extendio/xfs/utils"
)

const offset = 2
const tabSize = 2

type DirectoryTreeBuilder struct {
vfs storage.VirtualFS
root string
full string
stack *collections.Stack[string]
index string
write bool
depth int
padding string
silent bool
}

func (r *DirectoryTreeBuilder) read() (*Directory, error) {
data, err := os.ReadFile(r.index)
data, err := os.ReadFile(r.index) // always read from real fs

if err != nil {
return nil, err
Expand Down Expand Up @@ -73,10 +76,12 @@ func (r *DirectoryTreeBuilder) dec() {
}

func (r *DirectoryTreeBuilder) show(path, indicator, name string) {
status := r.status(path)
fmt.Printf("%v(depth: '%v') (%v) %v: -> '%v' (🌟 %v)\n",
r.padding, r.depth, status, indicator, name, r.full,
)
if !r.silent {
status := r.status(path)
fmt.Printf("%v(depth: '%v') (%v) %v: -> '%v' (🌟 %v)\n",
r.padding, r.depth, status, indicator, name, r.full,
)
}
}

func (r *DirectoryTreeBuilder) walk() error {
Expand All @@ -99,7 +104,7 @@ func (r *DirectoryTreeBuilder) dir(dir Directory) error { //nolint:gocritic // p
_, dn := utils.SplitParent(dir.Name)

if r.write {
err := os.MkdirAll(r.full, os.ModePerm)
err := r.vfs.MkdirAll(r.full, os.ModePerm)

if err != nil {
return err
Expand All @@ -119,7 +124,7 @@ func (r *DirectoryTreeBuilder) dir(dir Directory) error { //nolint:gocritic // p
fp := Path(r.full, file.Name)

if r.write {
err := os.WriteFile(fp, []byte(file.Text), os.ModePerm)
err := r.vfs.WriteFile(fp, []byte(file.Text), os.ModePerm)
if err != nil {
return err
}
Expand Down Expand Up @@ -153,27 +158,29 @@ type File struct {

const doWrite = true

func Scientist() string {
func Scientist(vfs storage.VirtualFS, index string, silent bool) string {
repo := Repo(filepath.Join("..", "..", ".."))
storage := filepath.Join(repo, "Test", "data", "storage")
scientist := filepath.Join(storage, "scientist")
index := filepath.Join(storage, "citizen-scientist-index.xml")
utils.Must(Ensure(scientist, index))
research := filepath.Join(repo, "Test", "data", "research")
scientist := filepath.Join(research, "scientist")
indexPath := filepath.Join(research, index)
utils.Must(ensure(scientist, indexPath, vfs, silent))

return scientist
}

func Ensure(root, index string) error {
if utils.FolderExists(root) {
func ensure(root, index string, vfs storage.VirtualFS, silent bool) error {
if vfs.DirectoryExists(root) {
return nil
}

parent, _ := utils.SplitParent(root)
builder := DirectoryTreeBuilder{
root: root,
stack: collections.NewStackWith([]string{parent}),
index: index,
write: doWrite,
vfs: vfs,
root: root,
stack: collections.NewStackWith([]string{parent}),
index: index,
write: doWrite,
silent: silent,
}

return builder.walk()
Expand Down
Loading

0 comments on commit 7b0c5cd

Please sign in to comment.