From 0d0b3e877edf40e3b874d7bb394b9c7f3615db68 Mon Sep 17 00:00:00 2001 From: plastikfan Date: Wed, 27 Dec 2023 14:02:36 +0000 Subject: [PATCH] ref(proxy): use polt filter and refector tests (#89) --- go.mod | 2 +- go.sum | 2 + src/app/command/root-cmd_test.go | 25 +-- src/app/command/shrink-cmd_test.go | 111 +++++----- src/app/proxy/controller-sampler_test.go | 247 ++++------------------- src/app/proxy/enter-shrink.go | 95 +++++++-- src/app/proxy/path-finder_test.go | 57 ------ src/i18n/messages-command-errors.go | 4 +- src/internal/helpers/mock-config-data.go | 88 ++++++++ src/internal/helpers/test-utils.go | 132 ++++++++++++ 10 files changed, 417 insertions(+), 346 deletions(-) delete mode 100644 src/app/proxy/path-finder_test.go create mode 100644 src/internal/helpers/mock-config-data.go diff --git a/go.mod b/go.mod index d13117f..38a870d 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/onsi/gomega v1.30.0 github.com/pkg/errors v0.9.1 github.com/samber/lo v1.39.0 - github.com/snivilised/extendio v0.5.0 + github.com/snivilised/extendio v0.5.1 github.com/snivilised/lorax v0.4.2 github.com/spf13/cobra v1.8.0 github.com/spf13/viper v1.18.1 diff --git a/go.sum b/go.sum index c482e88..ad12af1 100644 --- a/go.sum +++ b/go.sum @@ -92,6 +92,8 @@ github.com/snivilised/cobrass v0.4.1 h1:0CpC6eIIxj/x7UW3Ba3BCP35LyEamslOaimGxlTw github.com/snivilised/cobrass v0.4.1/go.mod h1:WFbmSiPNREW3cP3cuZ3EJrDgMUKgHQw13RNv6rCgyoo= github.com/snivilised/extendio v0.5.0 h1:XxUW125nGxkluVOekzWWpoE80r5KL0e+MFP2gjiXOhc= github.com/snivilised/extendio v0.5.0/go.mod h1:jBaeTO9432bNbBKR4mbt2d7OGyrDPY/U45siM3VhTks= +github.com/snivilised/extendio v0.5.1 h1:CHvhyTboVOHLj8rm0ZF5dm3jPViHKPAKQxvnI8WzqAo= +github.com/snivilised/extendio v0.5.1/go.mod h1:jBaeTO9432bNbBKR4mbt2d7OGyrDPY/U45siM3VhTks= github.com/snivilised/lorax v0.4.2 h1:jPo+o4cP/BUYBkR6sg3oLybHx0j2R+0wMLnvOf/gh0g= github.com/snivilised/lorax v0.4.2/go.mod h1:vyhM905Fc4fzShAXPvykS8ZRnO6B85hd0emJgZE4iNY= github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= diff --git a/src/app/command/root-cmd_test.go b/src/app/command/root-cmd_test.go index ffae40d..9996539 100644 --- a/src/app/command/root-cmd_test.go +++ b/src/app/command/root-cmd_test.go @@ -10,7 +10,6 @@ import ( "github.com/snivilised/extendio/xfs/storage" "github.com/snivilised/pixa/src/app/command" "github.com/snivilised/pixa/src/internal/helpers" - "github.com/snivilised/pixa/src/internal/matchers" ) type rootTE struct { @@ -23,28 +22,24 @@ var _ = Describe("RootCmd", Ordered, func() { repo string l10nPath string configPath string - nfs storage.VirtualFS - tester helpers.CommandTester + vfs storage.VirtualFS + + tester helpers.CommandTester ) BeforeAll(func() { - nfs = storage.UseNativeFS() repo = helpers.Repo(filepath.Join("..", "..", "..")) - - l10nPath = helpers.Path(repo, filepath.Join("test", "data", "l10n")) - Expect(matchers.AsDirectory(l10nPath)).To(matchers.ExistInFS(nfs)) - - configPath = filepath.Join(repo, "test", "data", "configuration") - Expect(matchers.AsDirectory(configPath)).To(matchers.ExistInFS(nfs)) - - if err := helpers.UseI18n(l10nPath); err != nil { - Fail(err.Error()) - } + l10nPath = helpers.Path(repo, "test/data/l10n") + configPath = helpers.Path(repo, "test/data/configuration") }) BeforeEach(func() { + vfs, _, _ = helpers.SetupTest( + "nasa-scientist-index.xml", configPath, l10nPath, helpers.Silent, + ) + bootstrap := command.Bootstrap{ - Vfs: nfs, + Vfs: vfs, } tester = helpers.CommandTester{ Root: bootstrap.Root(func(co *command.ConfigureOptionsInfo) { diff --git a/src/app/command/shrink-cmd_test.go b/src/app/command/shrink-cmd_test.go index 2c7bc97..615ac61 100644 --- a/src/app/command/shrink-cmd_test.go +++ b/src/app/command/shrink-cmd_test.go @@ -9,15 +9,22 @@ import ( "github.com/snivilised/pixa/src/app/command" "github.com/snivilised/pixa/src/internal/helpers" - "github.com/snivilised/pixa/src/internal/matchers" "github.com/snivilised/extendio/xfs/storage" ) +const ( + BackyardWorldsPlanet9Scan01 = "nasa/exo/Backyard Worlds - Planet 9/sessions/scan-01" +) + type commandTE struct { - message string - args []string - configPath string + message string + args []string + trashFlag string + trashValue string + outputFlag string + outputValue string + configPath string } type shrinkTE struct { @@ -25,19 +32,28 @@ type shrinkTE struct { directory string } -func expectValidShrinkCmdInvocation(vfs storage.VirtualFS, entry *shrinkTE) { +func expectValidShrinkCmdInvocation(vfs storage.VirtualFS, entry *shrinkTE, root string) { bootstrap := command.Bootstrap{ Vfs: vfs, } - // we also prepend the directory name to the command line - // - options := append([]string{helpers.ShrinkCommandName, entry.directory}, []string{ + directory := helpers.Path(root, entry.directory) + args := append([]string{helpers.ShrinkCommandName, directory}, []string{ "--dry-run", "--mode", "tidy", }...) + if entry.outputFlag != "" && entry.outputValue != "" { + output := helpers.Path(root, entry.outputValue) + args = append(args, entry.outputFlag, output) + } + + if entry.trashFlag != "" && entry.trashValue != "" { + trash := helpers.Path(root, entry.trashValue) + args = append(args, entry.trashFlag, trash) + } + tester := helpers.CommandTester{ - Args: append(options, entry.args...), + Args: append(args, entry.args...), Root: bootstrap.Root(func(co *command.ConfigureOptionsInfo) { co.Detector = &DetectorStub{} co.Program = &ExecutorStub{ @@ -59,34 +75,27 @@ var _ = Describe("ShrinkCmd", Ordered, func() { repo string l10nPath string configPath string - nfs storage.VirtualFS + root string + vfs storage.VirtualFS ) BeforeAll(func() { - nfs = storage.UseNativeFS() repo = helpers.Repo(filepath.Join("..", "..", "..")) - - l10nPath = helpers.Path(repo, filepath.Join("test", "data", "l10n")) - Expect(matchers.AsDirectory(l10nPath)).To(matchers.ExistInFS(nfs)) - - configPath = filepath.Join(repo, "test", "data", "configuration") - Expect(matchers.AsDirectory(configPath)).To(matchers.ExistInFS(nfs)) + l10nPath = helpers.Path(repo, "test/data/l10n") + configPath = helpers.Path(repo, "test/data/configuration") }) BeforeEach(func() { - if err := helpers.UseI18n(l10nPath); err != nil { - Fail(err.Error()) - } + vfs, root, _ = helpers.SetupTest( + "nasa-scientist-index.xml", configPath, l10nPath, helpers.Silent, + ) }) DescribeTable("ShrinkCmd", func(entry *shrinkTE) { - // set directory here, because during discovery phase of unit test , - // l10nPath is not set, so we can't set it inside the Entry - // - entry.directory = l10nPath + entry.directory = BackyardWorldsPlanet9Scan01 entry.configPath = configPath - expectValidShrinkCmdInvocation(nfs, entry) + expectValidShrinkCmdInvocation(vfs, entry, root) }, func(entry *shrinkTE) string { return fmt.Sprintf("🧪 ===> given: '%v'", entry.message) @@ -177,64 +186,64 @@ var _ = Describe("ShrinkCmd", Ordered, func() { When("with general long form parameters", func() { It("🧪 should: execute successfully", func() { entry := &shrinkTE{ - directory: l10nPath, + directory: BackyardWorldsPlanet9Scan01, commandTE: commandTE{ - message: "with general long form parameters", - args: []string{ - "--output", l10nPath, - }, - configPath: configPath, + message: "with general long form parameters", + args: []string{}, + outputFlag: "--output", + outputValue: "output", + configPath: configPath, }, } - expectValidShrinkCmdInvocation(nfs, entry) + expectValidShrinkCmdInvocation(vfs, entry, root) }) It("🧪 should: execute successfully", func() { entry := &shrinkTE{ - directory: l10nPath, + directory: BackyardWorldsPlanet9Scan01, commandTE: commandTE{ - message: "with general long form parameters", - args: []string{ - "--trash", l10nPath, - }, + message: "with general long form parameters", + args: []string{}, + trashFlag: "--trash", + trashValue: "discard", configPath: configPath, }, } - expectValidShrinkCmdInvocation(nfs, entry) + expectValidShrinkCmdInvocation(vfs, entry, root) }) }) When("with general short form parameters", func() { It("🧪 should: execute successfully", func() { entry := &shrinkTE{ - directory: l10nPath, + directory: BackyardWorldsPlanet9Scan01, commandTE: commandTE{ - message: "with general short form parameters", - args: []string{ - "-o", l10nPath, - }, - configPath: configPath, + message: "with general short form parameters", + args: []string{}, + outputFlag: "-o", + outputValue: "output", + configPath: configPath, }, } - expectValidShrinkCmdInvocation(nfs, entry) + expectValidShrinkCmdInvocation(vfs, entry, root) }) It("🧪 should: execute successfully", func() { entry := &shrinkTE{ - directory: l10nPath, + directory: BackyardWorldsPlanet9Scan01, commandTE: commandTE{ - message: "with general short form parameters", - args: []string{ - "-t", l10nPath, - }, + message: "with general short form parameters", + args: []string{}, + trashFlag: "-t", + trashValue: "discard", configPath: configPath, }, } - expectValidShrinkCmdInvocation(nfs, entry) + expectValidShrinkCmdInvocation(vfs, entry, root) }) }) }) diff --git a/src/app/proxy/controller-sampler_test.go b/src/app/proxy/controller-sampler_test.go index 318cb91..831713b 100644 --- a/src/app/proxy/controller-sampler_test.go +++ b/src/app/proxy/controller-sampler_test.go @@ -2,14 +2,12 @@ package proxy_test import ( "fmt" - "os" "path/filepath" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "github.com/snivilised/cobrass/src/assistant/configuration" cmocks "github.com/snivilised/cobrass/src/assistant/mocks" - "github.com/snivilised/cobrass/src/clif" "github.com/snivilised/extendio/xfs/storage" "github.com/snivilised/pixa/src/app/command" "github.com/snivilised/pixa/src/app/proxy" @@ -17,16 +15,11 @@ import ( "github.com/snivilised/pixa/src/app/mocks" "github.com/snivilised/pixa/src/internal/helpers" "github.com/snivilised/pixa/src/internal/matchers" - "github.com/spf13/viper" "go.uber.org/mock/gomock" ) const ( - silent = true - verbose = false - faydeaudeau = os.FileMode(0o777) - beezledub = os.FileMode(0o666) - backyardWorldsPlanet9Scan01 = "nasa/exo/Backyard Worlds - Planet 9/sessions/scan-01" + BackyardWorldsPlanet9Scan01 = "nasa/exo/Backyard Worlds - Planet 9/sessions/scan-01" ) var ( @@ -34,143 +27,8 @@ var ( _ proxy.SamplerConfig = &proxy.MsSamplerConfig{} _ proxy.ProfilesConfigReader = &proxy.MsProfilesConfigReader{} _ proxy.SamplerConfigReader = &proxy.MsSamplerConfigReader{} - - backyardWorldsPlanet9Scan01First2 []string - backyardWorldsPlanet9Scan01First4 []string - backyardWorldsPlanet9Scan01First6 []string - - backyardWorldsPlanet9Scan01Last4 []string - - profilesConfigData proxy.ProfilesConfigMap - samplerConfigData *proxy.MsSamplerConfig ) -func init() { - backyardWorldsPlanet9Scan01First2 = []string{ - "01_Backyard-Worlds-Planet-9_s01.jpg", - "02_Backyard-Worlds-Planet-9_s01.jpg", - } - - backyardWorldsPlanet9Scan01First4 = backyardWorldsPlanet9Scan01First2 - backyardWorldsPlanet9Scan01First4 = append( - backyardWorldsPlanet9Scan01First4, - []string{ - "03_Backyard-Worlds-Planet-9_s01.jpg", - "04_Backyard-Worlds-Planet-9_s01.jpg", - }..., - ) - - backyardWorldsPlanet9Scan01First6 = backyardWorldsPlanet9Scan01First4 - backyardWorldsPlanet9Scan01First6 = append( - backyardWorldsPlanet9Scan01First6, - []string{ - "05_Backyard-Worlds-Planet-9_s01.jpg", - "06_Backyard-Worlds-Planet-9_s01.jpg", - }..., - ) - - backyardWorldsPlanet9Scan01Last4 = []string{ - "03_Backyard-Worlds-Planet-9_s01.jpg", - "04_Backyard-Worlds-Planet-9_s01.jpg", - "05_Backyard-Worlds-Planet-9_s01.jpg", - "06_Backyard-Worlds-Planet-9_s01.jpg", - } - - profilesConfigData = proxy.ProfilesConfigMap{ - "blur": clif.ChangedFlagsMap{ - "strip": "true", - "interlace": "plane", - "gaussian-blur": "0.05", - }, - "sf": clif.ChangedFlagsMap{ - "dry-run": "true", - "strip": "true", - "interlace": "plane", - "sampling-factor": "4:2:0", - }, - "adaptive": clif.ChangedFlagsMap{ - "strip": "true", - "interlace": "plane", - "gaussian-blur": "0.25", - "adaptive-resize": "60", - }, - } - - samplerConfigData = &proxy.MsSamplerConfig{ - Files: 2, - Folders: 1, - Schemes: proxy.MsSamplerSchemesConfig{ - "blur-sf": proxy.MsSchemeConfig{ - Profiles: []string{"blur", "sf"}, - }, - "adaptive-sf": proxy.MsSchemeConfig{ - Profiles: []string{"adaptive", "sf"}, - }, - "adaptive-blur": proxy.MsSchemeConfig{ - Profiles: []string{"adaptive", "blur"}, - }, - "singleton": proxy.MsSchemeConfig{ - Profiles: []string{"adaptive"}, - }, - }, - } -} - -func doMockProfilesConfigsWith( - data proxy.ProfilesConfigMap, - config configuration.ViperConfig, - reader *mocks.MockProfilesConfigReader, -) { - reader.EXPECT().Read(config).DoAndReturn( - func(viper configuration.ViperConfig) (proxy.ProfilesConfig, error) { - stub := &proxy.MsProfilesConfig{ - Profiles: data, - } - - return stub, nil - }, - ).AnyTimes() -} - -func doMockSamplerConfigWith( - data *proxy.MsSamplerConfig, - config configuration.ViperConfig, - reader *mocks.MockSamplerConfigReader, -) { - reader.EXPECT().Read(config).DoAndReturn( - func(viper configuration.ViperConfig) (proxy.SamplerConfig, error) { - stub := data - - return stub, nil - }, - ).AnyTimes() -} - -func doMockConfigs( - config configuration.ViperConfig, - profilesReader *mocks.MockProfilesConfigReader, - samplerReader *mocks.MockSamplerConfigReader, -) { - doMockProfilesConfigsWith(profilesConfigData, config, profilesReader) - doMockSamplerConfigWith(samplerConfigData, config, samplerReader) -} - -func doMockViper(config *cmocks.MockViperConfig) { - config.EXPECT().ReadInConfig().DoAndReturn( - func() error { - return nil - }, - ).AnyTimes() -} - -func resetFS(index string, silent bool) (vfs storage.VirtualFS, root string) { - vfs = storage.UseMemFS() - root = helpers.Scientist(vfs, index, silent) - Expect(matchers.AsDirectory(root)).To(matchers.ExistInFS(vfs)) - - return vfs, root -} - type controllerTE struct { given string should string @@ -206,42 +64,20 @@ var _ = Describe("SamplerController", Ordered, func() { BeforeAll(func() { repo = helpers.Repo(filepath.Join("..", "..", "..")) - l10nPath = helpers.Path(repo, filepath.Join("test", "data", "l10n")) - configPath = filepath.Join(repo, "test", "data", "configuration") + l10nPath = helpers.Path(repo, "test/data/l10n") + configPath = helpers.Path(repo, "test/data/configuration") }) BeforeEach(func() { - viper.Reset() - vfs, root = resetFS("nasa-scientist-index.xml", silent) + vfs, root, config = helpers.SetupTest( + "nasa-scientist-index.xml", configPath, l10nPath, helpers.Silent, + ) ctrl = gomock.NewController(GinkgoT()) mockViperConfig = cmocks.NewMockViperConfig(ctrl) mockProfilesReader = mocks.NewMockProfilesConfigReader(ctrl) mockSamplerReader = mocks.NewMockSamplerConfigReader(ctrl) - doMockViper(mockViperConfig) - - // create a dummy config file in vfs - // - _ = vfs.MkdirAll(configPath, beezledub) - if _, err := vfs.Create(filepath.Join(configPath, helpers.PixaConfigTestFilename)); err != nil { - Fail(fmt.Sprintf("🔥 can't create dummy config (err: '%v')", err)) - } - - Expect(matchers.AsDirectory(configPath)).To(matchers.ExistInFS(vfs)) - - config = &configuration.GlobalViperConfig{} - - config.SetConfigType(helpers.PixaConfigType) - config.SetConfigName(helpers.PixaConfigTestFilename) - config.AddConfigPath(configPath) - - if err := config.ReadInConfig(); err != nil { - Fail(fmt.Sprintf("🔥 can't read config (err: '%v')", err)) - } - - if err := helpers.UseI18n(l10nPath); err != nil { - Fail(err.Error()) - } + helpers.DoMockViper(mockViperConfig) }) AfterEach(func() { @@ -250,7 +86,7 @@ var _ = Describe("SamplerController", Ordered, func() { DescribeTable("sampler", func(entry *samplerTE) { - doMockConfigs(config, mockProfilesReader, mockSamplerReader) + helpers.DoMockConfigs(config, mockProfilesReader, mockSamplerReader) directory := helpers.Path(root, entry.relative) options := []string{ @@ -262,13 +98,11 @@ var _ = Describe("SamplerController", Ordered, func() { args = append(args, entry.args...) if entry.outputFlag != "" { output := helpers.Path(root, entry.outputFlag) - args = append(args, "--output") - args = append(args, output) + args = append(args, "--output", output) } if entry.trashFlag != "" { trash := helpers.Path(root, entry.trashFlag) - args = append(args, "--trash") - args = append(args, trash) + args = append(args, "--trash", trash) } bootstrap := command.Bootstrap{ @@ -324,7 +158,7 @@ var _ = Describe("SamplerController", Ordered, func() { controllerTE: controllerTE{ given: "run transparent adhoc", should: "sample(first) with glob filter, result file takes place of input", - relative: backyardWorldsPlanet9Scan01, + relative: BackyardWorldsPlanet9Scan01, args: []string{ "--sample", "--no-files", "4", @@ -332,10 +166,10 @@ var _ = Describe("SamplerController", Ordered, func() { "--gaussian-blur", "0.51", "--interlace", "line", }, - expected: backyardWorldsPlanet9Scan01First4, + expected: helpers.BackyardWorldsPlanet9Scan01First4, intermediate: "nasa/exo/Backyard Worlds - Planet 9/sessions/scan-01", supplement: "ADHOC/TRASH", - inputs: backyardWorldsPlanet9Scan01First4, + inputs: helpers.BackyardWorldsPlanet9Scan01First4, }, }), @@ -343,7 +177,7 @@ var _ = Describe("SamplerController", Ordered, func() { controllerTE: controllerTE{ given: "run transparent with profile", should: "sample(first) with glob filter, result file takes place of input", - relative: backyardWorldsPlanet9Scan01, + relative: BackyardWorldsPlanet9Scan01, args: []string{ "--sample", "--no-files", "4", @@ -352,10 +186,10 @@ var _ = Describe("SamplerController", Ordered, func() { "--gaussian-blur", "0.51", "--interlace", "line", }, - expected: backyardWorldsPlanet9Scan01First4, + expected: helpers.BackyardWorldsPlanet9Scan01First4, intermediate: "nasa/exo/Backyard Worlds - Planet 9/sessions/scan-01", supplement: "adaptive/TRASH", - inputs: backyardWorldsPlanet9Scan01First4, + inputs: helpers.BackyardWorldsPlanet9Scan01First4, }, }), @@ -363,7 +197,7 @@ var _ = Describe("SamplerController", Ordered, func() { controllerTE: controllerTE{ given: "run(last) transparent with profile", should: "sample(last) with glob filter using the defined profile", - relative: backyardWorldsPlanet9Scan01, + relative: BackyardWorldsPlanet9Scan01, args: []string{ "--sample", "--last", @@ -373,10 +207,10 @@ var _ = Describe("SamplerController", Ordered, func() { "--gaussian-blur", "0.51", "--interlace", "line", }, - expected: backyardWorldsPlanet9Scan01Last4, + expected: helpers.BackyardWorldsPlanet9Scan01Last4, intermediate: "nasa/exo/Backyard Worlds - Planet 9/sessions/scan-01", supplement: "adaptive/TRASH", - inputs: backyardWorldsPlanet9Scan01Last4, + inputs: helpers.BackyardWorldsPlanet9Scan01Last4, }, }), @@ -384,16 +218,16 @@ var _ = Describe("SamplerController", Ordered, func() { controllerTE: controllerTE{ given: "profile without no-files in args", should: "sample(first) with glob filter, using no-files from config", - relative: backyardWorldsPlanet9Scan01, + relative: BackyardWorldsPlanet9Scan01, args: []string{ "--sample", "--files-gb", "*Backyard-Worlds*", "--profile", "adaptive", }, - expected: backyardWorldsPlanet9Scan01First2, + expected: helpers.BackyardWorldsPlanet9Scan01First2, intermediate: "nasa/exo/Backyard Worlds - Planet 9/sessions/scan-01", supplement: "adaptive/TRASH", - inputs: backyardWorldsPlanet9Scan01First2, + inputs: helpers.BackyardWorldsPlanet9Scan01First2, }, }), @@ -401,7 +235,7 @@ var _ = Describe("SamplerController", Ordered, func() { controllerTE: controllerTE{ given: "profile", should: "sample with regex filter using the defined profile", - relative: backyardWorldsPlanet9Scan01, + relative: BackyardWorldsPlanet9Scan01, args: []string{ "--sample", "--no-files", "4", @@ -411,10 +245,10 @@ var _ = Describe("SamplerController", Ordered, func() { "--files-rx", "Backyard-Worlds", "--profile", "adaptive", }, - expected: backyardWorldsPlanet9Scan01First4, + expected: helpers.BackyardWorldsPlanet9Scan01First4, intermediate: "nasa/exo/Backyard Worlds - Planet 9/sessions/scan-01", supplement: "adaptive/TRASH", - inputs: backyardWorldsPlanet9Scan01First4, + inputs: helpers.BackyardWorldsPlanet9Scan01First4, }, }), @@ -422,7 +256,7 @@ var _ = Describe("SamplerController", Ordered, func() { controllerTE: controllerTE{ given: "run transparent with scheme with single profile", should: "sample(first) with glob filter, result file takes place of input", - relative: backyardWorldsPlanet9Scan01, + relative: BackyardWorldsPlanet9Scan01, args: []string{ "--sample", "--no-files", "4", @@ -431,10 +265,10 @@ var _ = Describe("SamplerController", Ordered, func() { "--gaussian-blur", "0.51", "--interlace", "line", }, - expected: backyardWorldsPlanet9Scan01First4, + expected: helpers.BackyardWorldsPlanet9Scan01First4, intermediate: "nasa/exo/Backyard Worlds - Planet 9/sessions/scan-01", supplement: "singleton/TRASH", - inputs: backyardWorldsPlanet9Scan01First4, + inputs: helpers.BackyardWorldsPlanet9Scan01First4, }, }), @@ -442,7 +276,7 @@ var _ = Describe("SamplerController", Ordered, func() { controllerTE: controllerTE{ given: "run non transparent adhoc", should: "sample(first) with glob filter, input moved to alternative location", - relative: backyardWorldsPlanet9Scan01, + relative: BackyardWorldsPlanet9Scan01, args: []string{ "--sample", "--no-files", "4", @@ -451,10 +285,10 @@ var _ = Describe("SamplerController", Ordered, func() { "--interlace", "line", }, trashFlag: "discard", - expected: backyardWorldsPlanet9Scan01First4, + expected: helpers.BackyardWorldsPlanet9Scan01First4, intermediate: "discard", supplement: "ADHOC/TRASH", - inputs: backyardWorldsPlanet9Scan01First4, + inputs: helpers.BackyardWorldsPlanet9Scan01First4, }, }), @@ -462,7 +296,7 @@ var _ = Describe("SamplerController", Ordered, func() { controllerTE: controllerTE{ given: "run non transparent with profile", should: "sample(first) with glob filter, input moved to alternative location", - relative: backyardWorldsPlanet9Scan01, + relative: BackyardWorldsPlanet9Scan01, args: []string{ "--sample", "--no-files", "4", @@ -472,10 +306,10 @@ var _ = Describe("SamplerController", Ordered, func() { "--interlace", "line", }, trashFlag: "discard", - expected: backyardWorldsPlanet9Scan01First4, + expected: helpers.BackyardWorldsPlanet9Scan01First4, intermediate: "discard", supplement: "adaptive/TRASH", - inputs: backyardWorldsPlanet9Scan01First4, + inputs: helpers.BackyardWorldsPlanet9Scan01First4, }, }), @@ -483,7 +317,7 @@ var _ = Describe("SamplerController", Ordered, func() { controllerTE: controllerTE{ given: "run non transparent scheme single with profile", should: "sample(first) with glob filter, input moved to alternative location", - relative: backyardWorldsPlanet9Scan01, + relative: BackyardWorldsPlanet9Scan01, args: []string{ "--sample", "--no-files", "4", @@ -493,10 +327,10 @@ var _ = Describe("SamplerController", Ordered, func() { "--interlace", "line", }, trashFlag: "discard", - expected: backyardWorldsPlanet9Scan01First4, + expected: helpers.BackyardWorldsPlanet9Scan01First4, intermediate: "discard", supplement: "singleton/TRASH", - inputs: backyardWorldsPlanet9Scan01First4, + inputs: helpers.BackyardWorldsPlanet9Scan01First4, }, }), @@ -504,19 +338,20 @@ var _ = Describe("SamplerController", Ordered, func() { controllerTE: controllerTE{ given: "scheme", should: "sample all profiles in the scheme", - relative: backyardWorldsPlanet9Scan01, + relative: BackyardWorldsPlanet9Scan01, args: []string{ "--sample", "--no-files", "4", + "--files-gb", "*Backyard-Worlds*", "--strip", "--interlace", "plane", "--quality", "85", "--scheme", "blur-sf", }, - expected: backyardWorldsPlanet9Scan01First6, + expected: helpers.BackyardWorldsPlanet9Scan01First6, intermediate: "nasa/exo/Backyard Worlds - Planet 9/sessions/scan-01", supplement: "blur-sf/TRASH", - inputs: backyardWorldsPlanet9Scan01First4, + inputs: helpers.BackyardWorldsPlanet9Scan01First4, }, }), ) diff --git a/src/app/proxy/enter-shrink.go b/src/app/proxy/enter-shrink.go index 724dccc..d602e01 100644 --- a/src/app/proxy/enter-shrink.go +++ b/src/app/proxy/enter-shrink.go @@ -39,29 +39,96 @@ func (e *ShrinkEntry) LookAheadOptionsFn(o *nav.TraverseOptions) { }, } + defs := e.getFilterDefs() + o.Store.FilterDefs = defs +} + +func (e *ShrinkEntry) getFilterDefs() *nav.FilterDefinitions { + var ( + file, folder *nav.FilterDef + defs *nav.FilterDefinitions + folderDefined = true + pattern string + ) + switch { case e.Inputs.FilesFam.Native.FilesGlob != "": - pattern := e.Inputs.FilesFam.Native.FilesGlob - o.Store.FilterDefs = &nav.FilterDefinitions{ - Node: nav.FilterDef{ - Type: nav.FilterTypeGlobEn, - Description: fmt.Sprintf("--files-gb(G): '%v'", pattern), - Pattern: pattern, - Scope: nav.ScopeFileEn, - }, + pattern = e.Inputs.FilesFam.Native.FilesGlob + file = &nav.FilterDef{ + Type: nav.FilterTypeGlobEn, + Description: fmt.Sprintf("--files-gb(G): '%v'", pattern), + Pattern: pattern, + Scope: nav.ScopeFileEn, } case e.Inputs.FilesFam.Native.FilesRexEx != "": - pattern := e.Inputs.FilesFam.Native.FilesRexEx - o.Store.FilterDefs = &nav.FilterDefinitions{ + pattern = e.Inputs.FilesFam.Native.FilesRexEx + file = &nav.FilterDef{ + Type: nav.FilterTypeRegexEn, + Description: fmt.Sprintf("--files-rx(X): '%v'", pattern), + Pattern: pattern, + Scope: nav.ScopeFileEn, + } + + default: + pattern = "(?i).(jpe?g|png)$" + file = &nav.FilterDef{ + Type: nav.FilterTypeRegexEn, + Description: fmt.Sprintf("--files-rx(X): '%v'", pattern), + Pattern: pattern, + Scope: nav.ScopeFileEn, + } + } + + switch { + case e.Inputs.Root.FoldersFam.Native.FoldersGlob != "": + pattern = e.Inputs.Root.FoldersFam.Native.FoldersRexEx + folder = &nav.FilterDef{ + Type: nav.FilterTypeGlobEn, + Description: fmt.Sprintf("--folders-gb(Z): '%v'", pattern), + Pattern: pattern, + Scope: nav.ScopeFolderEn | nav.ScopeLeafEn, + } + + case e.Inputs.Root.FoldersFam.Native.FoldersRexEx != "": + pattern = e.Inputs.Root.FoldersFam.Native.FoldersRexEx + folder = &nav.FilterDef{ + Type: nav.FilterTypeRegexEn, + Description: fmt.Sprintf("--folders-rx(Y): '%v'", pattern), + Pattern: pattern, + Scope: nav.ScopeFolderEn | nav.ScopeLeafEn, + } + + default: + folderDefined = false + } + + switch { + case folderDefined: + defs = &nav.FilterDefinitions{ Node: nav.FilterDef{ - Type: nav.FilterTypeRegexEn, - Description: fmt.Sprintf("--files-rx(X): '%v'", pattern), - Pattern: pattern, - Scope: nav.ScopeFileEn, + Type: nav.FilterTypePolyEn, + Poly: &nav.PolyFilterDef{ + File: *file, + Folder: *folder, + }, }, } + + default: + defs = &nav.FilterDefinitions{ + Node: *file, + } } + + return lo.TernaryF(pattern != "", + func() *nav.FilterDefinitions { + return defs + }, + func() *nav.FilterDefinitions { + return nil + }, + ) } func (e *ShrinkEntry) PrincipalOptionsFn(o *nav.TraverseOptions) { diff --git a/src/app/proxy/path-finder_test.go b/src/app/proxy/path-finder_test.go deleted file mode 100644 index 1d10c8b..0000000 --- a/src/app/proxy/path-finder_test.go +++ /dev/null @@ -1,57 +0,0 @@ -package proxy_test - -import ( - "path/filepath" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - "github.com/snivilised/extendio/xfs/storage" - "github.com/snivilised/pixa/src/app/proxy" - "github.com/snivilised/pixa/src/internal/helpers" - "github.com/snivilised/pixa/src/internal/matchers" -) - -var _ = Describe("PathFinder", Ordered, func() { - var ( - repo string - l10nPath string - nfs storage.VirtualFS - ) - - BeforeAll(func() { - nfs = storage.UseNativeFS() - repo = helpers.Repo(filepath.Join("..", "..", "..")) - - l10nPath = helpers.Path(repo, filepath.Join("test", "data", "l10n")) - Expect(matchers.AsDirectory(l10nPath)).To(matchers.ExistInFS(nfs)) - }) - - BeforeEach(func() { - if err := helpers.UseI18n(l10nPath); err != nil { - Fail(err.Error()) - } - }) - // the PathFinder should not be aware of profile/sample, it only - // know about paths. So it knows about: - // - output - // - item-path (full-path to current file item) - // - input: parent dir of item - // - current - // - output: is the parent dir of the output, it may be the same as input - // - subpath (this incorporates profile & sample) - // - trash - - // - segments: "profile-name", "sample-scheme" - // - // * output (if output is "" => LOCALISED-MODE else CENTRALISED-MODE): - // - localised (trash-dir=pwd+"__trash") - // - centralised (trash-dir=pwd) - - Context("foo", func() { - It("should:", func() { - _ = proxy.PathFinder{} - Expect(1).To(Equal(1)) - }) - }) -}) diff --git a/src/i18n/messages-command-errors.go b/src/i18n/messages-command-errors.go index 7334381..374696a 100644 --- a/src/i18n/messages-command-errors.go +++ b/src/i18n/messages-command-errors.go @@ -103,8 +103,8 @@ type OutputPathDoesNotExistError struct { xi18n.LocalisableError } -func NewOutputPathDoesNotExistError(path string) InvalidInterlaceError { - return InvalidInterlaceError{ +func NewOutputPathDoesNotExistError(path string) OutputPathDoesNotExistError { + return OutputPathDoesNotExistError{ LocalisableError: xi18n.LocalisableError{ Data: ShrinkCmdOutputPathDoesNotExistTemplData{ Path: path, diff --git a/src/internal/helpers/mock-config-data.go b/src/internal/helpers/mock-config-data.go new file mode 100644 index 0000000..35a9799 --- /dev/null +++ b/src/internal/helpers/mock-config-data.go @@ -0,0 +1,88 @@ +package helpers + +import ( + "github.com/snivilised/cobrass/src/clif" + "github.com/snivilised/pixa/src/app/proxy" +) + +var ( + BackyardWorldsPlanet9Scan01First2 []string + BackyardWorldsPlanet9Scan01First4 []string + BackyardWorldsPlanet9Scan01First6 []string + + BackyardWorldsPlanet9Scan01Last4 []string + + ProfilesConfigData proxy.ProfilesConfigMap + SamplerConfigData *proxy.MsSamplerConfig +) + +func init() { + BackyardWorldsPlanet9Scan01First2 = []string{ + "01_Backyard-Worlds-Planet-9_s01.jpg", + "02_Backyard-Worlds-Planet-9_s01.jpg", + } + + BackyardWorldsPlanet9Scan01First4 = BackyardWorldsPlanet9Scan01First2 + BackyardWorldsPlanet9Scan01First4 = append( + BackyardWorldsPlanet9Scan01First4, + []string{ + "03_Backyard-Worlds-Planet-9_s01.jpg", + "04_Backyard-Worlds-Planet-9_s01.jpg", + }..., + ) + + BackyardWorldsPlanet9Scan01First6 = BackyardWorldsPlanet9Scan01First4 + BackyardWorldsPlanet9Scan01First6 = append( + BackyardWorldsPlanet9Scan01First6, + []string{ + "05_Backyard-Worlds-Planet-9_s01.jpg", + "06_Backyard-Worlds-Planet-9_s01.jpg", + }..., + ) + + BackyardWorldsPlanet9Scan01Last4 = []string{ + "03_Backyard-Worlds-Planet-9_s01.jpg", + "04_Backyard-Worlds-Planet-9_s01.jpg", + "05_Backyard-Worlds-Planet-9_s01.jpg", + "06_Backyard-Worlds-Planet-9_s01.jpg", + } + + ProfilesConfigData = proxy.ProfilesConfigMap{ + "blur": clif.ChangedFlagsMap{ + "strip": "true", + "interlace": "plane", + "gaussian-blur": "0.05", + }, + "sf": clif.ChangedFlagsMap{ + "dry-run": "true", + "strip": "true", + "interlace": "plane", + "sampling-factor": "4:2:0", + }, + "adaptive": clif.ChangedFlagsMap{ + "strip": "true", + "interlace": "plane", + "gaussian-blur": "0.25", + "adaptive-resize": "60", + }, + } + + SamplerConfigData = &proxy.MsSamplerConfig{ + Files: 2, //nolint:gomnd // not magic + Folders: 1, + Schemes: proxy.MsSamplerSchemesConfig{ + "blur-sf": proxy.MsSchemeConfig{ + Profiles: []string{"blur", "sf"}, + }, + "adaptive-sf": proxy.MsSchemeConfig{ + Profiles: []string{"adaptive", "sf"}, + }, + "adaptive-blur": proxy.MsSchemeConfig{ + Profiles: []string{"adaptive", "blur"}, + }, + "singleton": proxy.MsSchemeConfig{ + Profiles: []string{"adaptive"}, + }, + }, + } +} diff --git a/src/internal/helpers/test-utils.go b/src/internal/helpers/test-utils.go index 06d2e39..3293243 100644 --- a/src/internal/helpers/test-utils.go +++ b/src/internal/helpers/test-utils.go @@ -7,10 +7,21 @@ import ( "runtime" "strings" + "github.com/onsi/ginkgo/v2" + "github.com/onsi/gomega" + "github.com/spf13/viper" + + "github.com/pkg/errors" + "github.com/snivilised/cobrass/src/assistant/configuration" ci18n "github.com/snivilised/cobrass/src/assistant/i18n" + cmocks "github.com/snivilised/cobrass/src/assistant/mocks" + "github.com/snivilised/pixa/src/app/mocks" + "github.com/snivilised/pixa/src/app/proxy" "github.com/snivilised/pixa/src/i18n" + "github.com/snivilised/pixa/src/internal/matchers" xi18n "github.com/snivilised/extendio/i18n" + "github.com/snivilised/extendio/xfs/storage" "golang.org/x/text/language" ) @@ -19,6 +30,10 @@ const ( PixaConfigType = "yml" ShrinkCommandName = "shrink" ProgName = "magick" + Faydeaudeau = os.FileMode(0o777) + Beezledub = os.FileMode(0o666) + Silent = true + Verbose = false ) func Path(parent, relative string) string { @@ -72,6 +87,33 @@ func Log() string { panic("could not get root path") } +func SetupTest( + index, configPath, l10nPath string, + silent bool, +) (vfs storage.VirtualFS, root string, config configuration.ViperConfig) { + var ( + err error + ) + + viper.Reset() + + vfs, root = ResetFS(index, silent) + + if err = MockConfigFile(vfs, configPath); err != nil { + ginkgo.Fail(err.Error()) + } + + if config, err = ReadGlobalConfig(configPath); err != nil { + ginkgo.Fail(err.Error()) + } + + if err = UseI18n(l10nPath); err != nil { + ginkgo.Fail(err.Error()) + } + + return vfs, root, config +} + func UseI18n(l10nPath string) error { return xi18n.Use(func(uo *xi18n.UseOptions) { uo.From = xi18n.LoadFrom{ @@ -89,6 +131,96 @@ func UseI18n(l10nPath string) error { }) } +func ReadGlobalConfig(configPath string) (*configuration.GlobalViperConfig, error) { + var ( + err error + ) + + config := &configuration.GlobalViperConfig{} + + config.SetConfigType(PixaConfigType) + config.SetConfigName(PixaConfigTestFilename) + config.AddConfigPath(configPath) + + if e := config.ReadInConfig(); e != nil { + err = errors.Wrap(e, "can't read config") + } + + return config, err +} + +// MockConfigFile create a dummy config file in the file system specified +func MockConfigFile(vfs storage.VirtualFS, configPath string) error { + var ( + err error + ) + + _ = vfs.MkdirAll(configPath, Beezledub) + + if _, err = vfs.Create(filepath.Join(configPath, PixaConfigTestFilename)); err != nil { + ginkgo.Fail(fmt.Sprintf("🔥 can't create dummy config (err: '%v')", err)) + } + + gomega.Expect(matchers.AsDirectory(configPath)).To(matchers.ExistInFS(vfs)) + + return err +} + +func DoMockConfigs( + config configuration.ViperConfig, + profilesReader *mocks.MockProfilesConfigReader, + samplerReader *mocks.MockSamplerConfigReader, +) { + DoMockProfilesConfigsWith(ProfilesConfigData, config, profilesReader) + DoMockSamplerConfigWith(SamplerConfigData, config, samplerReader) +} + +func DoMockViper(config *cmocks.MockViperConfig) { + config.EXPECT().ReadInConfig().DoAndReturn( + func() error { + return nil + }, + ).AnyTimes() +} + +func DoMockProfilesConfigsWith( + data proxy.ProfilesConfigMap, + config configuration.ViperConfig, + reader *mocks.MockProfilesConfigReader, +) { + reader.EXPECT().Read(config).DoAndReturn( + func(viper configuration.ViperConfig) (proxy.ProfilesConfig, error) { + stub := &proxy.MsProfilesConfig{ + Profiles: data, + } + + return stub, nil + }, + ).AnyTimes() +} + +func DoMockSamplerConfigWith( + data *proxy.MsSamplerConfig, + config configuration.ViperConfig, + reader *mocks.MockSamplerConfigReader, +) { + reader.EXPECT().Read(config).DoAndReturn( + func(viper configuration.ViperConfig) (proxy.SamplerConfig, error) { + stub := data + + return stub, nil + }, + ).AnyTimes() +} + +func ResetFS(index string, silent bool) (vfs storage.VirtualFS, root string) { + vfs = storage.UseMemFS() + root = Scientist(vfs, index, silent) + gomega.Expect(matchers.AsDirectory(root)).To(matchers.ExistInFS(vfs)) + + return vfs, root +} + type DetectorStub struct { }