From 7b0c5cd4dbb04b018b36097abf02c3259b2bb830 Mon Sep 17 00:00:00 2001 From: plastikfan Date: Thu, 14 Dec 2023 08:39:53 +0000 Subject: [PATCH] test(proxy): migrate tests to use virtual-fs (#48) --- .gitignore | 1 + src/app/proxy/runner-sampler_test.go | 7 +- .../helpers/directory-tree-builder.go | 43 +- test/data/storage/citizen-scientist-index.xml | 655 ------------------ 4 files changed, 32 insertions(+), 674 deletions(-) delete mode 100644 test/data/storage/citizen-scientist-index.xml diff --git a/.gitignore b/.gitignore index 9e501d8..8ed47e9 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/src/app/proxy/runner-sampler_test.go b/src/app/proxy/runner-sampler_test.go index 26debbf..4911ffb 100644 --- a/src/app/proxy/runner-sampler_test.go +++ b/src/app/proxy/runner-sampler_test.go @@ -17,6 +17,11 @@ import ( "github.com/spf13/viper" ) +const ( + silent = false + verbose = true +) + type runnerTE struct { given string should string @@ -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)) }) diff --git a/src/internal/helpers/directory-tree-builder.go b/src/internal/helpers/directory-tree-builder.go index b91bb5b..0221f5c 100644 --- a/src/internal/helpers/directory-tree-builder.go +++ b/src/internal/helpers/directory-tree-builder.go @@ -10,6 +10,7 @@ import ( "github.com/samber/lo" "github.com/snivilised/extendio/collections" + "github.com/snivilised/extendio/xfs/storage" "github.com/snivilised/extendio/xfs/utils" ) @@ -17,6 +18,7 @@ const offset = 2 const tabSize = 2 type DirectoryTreeBuilder struct { + vfs storage.VirtualFS root string full string stack *collections.Stack[string] @@ -24,10 +26,11 @@ type DirectoryTreeBuilder struct { 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 @@ -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 { @@ -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 @@ -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 } @@ -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() diff --git a/test/data/storage/citizen-scientist-index.xml b/test/data/storage/citizen-scientist-index.xml deleted file mode 100644 index b188202..0000000 --- a/test/data/storage/citizen-scientist-index.xml +++ /dev/null @@ -1,655 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 177 - 656 - - \ No newline at end of file