From 139a0784dc02cfcfe5bcfa28d0b794cc8a6cf7ec Mon Sep 17 00:00:00 2001 From: plastikfan Date: Fri, 16 Feb 2024 18:59:09 +0000 Subject: [PATCH] fix(proxy): non transparent with output path finder unit tests (#172) --- src/app/proxy/filing/filing-defs.go | 2 +- src/app/proxy/filing/path-finder.go | 66 ++++----- src/app/proxy/filing/path-finder_test.go | 178 ++++++++++++++++++----- 3 files changed, 164 insertions(+), 82 deletions(-) diff --git a/src/app/proxy/filing/filing-defs.go b/src/app/proxy/filing/filing-defs.go index 38e469e..a40bea5 100644 --- a/src/app/proxy/filing/filing-defs.go +++ b/src/app/proxy/filing/filing-defs.go @@ -18,6 +18,6 @@ func SupplementFilename(name, supp string, statics *common.StaticInfo) string { return statics.FileSupplement(withoutExt, supp) + path.Ext(name) } -func SupplementDirectory(directory, supp string) string { +func SupplementFolder(directory, supp string) string { return filepath.Join(directory, supp) } diff --git a/src/app/proxy/filing/path-finder.go b/src/app/proxy/filing/path-finder.go index bd6c460..ba74a9f 100644 --- a/src/app/proxy/filing/path-finder.go +++ b/src/app/proxy/filing/path-finder.go @@ -29,7 +29,6 @@ func NewFinder( finder := &PathFinder{ Sch: info.Scheme, ExplicitProfile: info.Profile, - Arity: 1, Stats: &common.StaticInfo{ Adhoc: advanced.AdhocLabel(), Legacy: advanced.LegacyLabel(), @@ -71,9 +70,7 @@ const ( pfPathUndefined pfPath = iota pfPathInputTransferFolder pfPathTxInputDestinationFolder - pfPathInputDestinationFileOriginalExt pfPathResultFolder - pfPathResultFile ) const ( @@ -120,17 +117,11 @@ func init() { pfPathTxInputDestinationFolder: templateSegments{ "${{OUTPUT-ROOT}}", }, - pfPathInputDestinationFileOriginalExt: templateSegments{ - "${{ITEM-FULL-NAME}}", - }, pfPathResultFolder: templateSegments{ "${{OUTPUT-ROOT}}", "${{ITEM-SUB-PATH}}", "${{SUPPLEMENT}}", }, - pfPathResultFile: templateSegments{ - "${{RESULT-NAME}}", - }, } } @@ -194,19 +185,12 @@ type PathFinder struct { // - full: (inline) -- item.parent Output string Trash string - Arity int Ext *ExtensionTransformation transparentInput bool Stats *common.StaticInfo } func (f *PathFinder) init(info *NewFinderInfo) { - schemes := info.Schemes - if f.Sch != "" { - schemeCFG, _ := schemes.Scheme(f.Sch) - f.Arity = len(schemeCFG.Profiles()) - } - f.Output = info.OutputPath f.Trash = info.TrashPath @@ -261,7 +245,15 @@ func (f *PathFinder) Scheme() string { func (f *PathFinder) Transfer(info *common.PathInfo) (folder, file string) { folder = func() string { if info.Cuddle { - return info.Origin + return info.Origin // should we return empty string here? + } + + if info.Output != "" && info.Trash == "" { + // When output folder is specified, then the results will be diverted there. + // This means there is no need to transfer the input, unless trash has + // also been specified. + // + return "" } segments := pfTemplates[pfPathInputTransferFolder] @@ -279,13 +271,21 @@ func (f *PathFinder) Transfer(info *common.PathInfo) (folder, file string) { "${{TRANSFER-DESTINATION}}": to, "${{ITEM-SUB-PATH}}": info.Item.Extension.SubPath, "${{DEJA-VU}}": f.Stats.DejaVu(), - "${{SUPPLEMENT}}": f.folderSupplement(info.Profile), + "${{SUPPLEMENT}}": f.folderProfileSupplement(info.Profile), }, segments...) }() file = func() string { + if info.Output != "" && info.Trash == "" { + // When output folder is specified, then the results will be diverted there. + // This means there is no need to transfer the input, unless trash has + // also been specified. + // + return "" + } + if info.Cuddle { - supp := fmt.Sprintf("%v.%v", f.Stats.DejaVu(), f.fileSupplement(info.Profile)) + supp := fmt.Sprintf("%v.%v", f.Stats.DejaVu(), f.fileProfileSupplement(info.Profile)) return SupplementFilename( info.Item.Extension.Name, supp, f.Stats, @@ -340,7 +340,7 @@ func (f *PathFinder) Result(info *common.PathInfo) (folder, file string) { }, segments...) }, func() string { - segments := pfTemplates[pfPathInputTransferFolder] + segments := pfTemplates[pfPathResultFolder] to := lo.TernaryF(f.Output != "", func() string { return f.Output @@ -350,27 +350,15 @@ func (f *PathFinder) Result(info *common.PathInfo) (folder, file string) { }, ) // If there is no scheme or profile, then the user is - // only relying flags on the command line, ie running adhoc + // only relying on flags on the command line, ie running adhoc // so the result path should include an adhoc label. Otherwise, // the result should reflect the supplementary path. // - // if arity >1 then we need to use a different field list - // the current one below only works for arity=1. - // when arity > 1 we need the following: - - /* - [0]:"${{TRANSFER-DESTINATION}}" - [1]:"${{ITEM-SUB-PATH}}" - [2]:"${{DEJA-VU}}" - [3]:"${{SUPPLEMENT}}" - [4]:"${{TRASH-LABEL}}" - */ - return pfTemplates.evaluate(pfFieldValues{ "${{OUTPUT-ROOT}}": to, - "${{SUPPLEMENT}}": f.folderSupplement(info.Profile), "${{ITEM-SUB-PATH}}": info.Item.Extension.SubPath, + "${{SUPPLEMENT}}": f.folderProfileSupplement(info.Profile), }, segments...) }, ) @@ -380,17 +368,13 @@ func (f *PathFinder) Result(info *common.PathInfo) (folder, file string) { // The file name just matches the input file name. The folder name // provides the context. // - segments := pfTemplates[pfPathResultFile] - - return pfTemplates.evaluate(pfFieldValues{ // this looks like incorrect use of template (file!) - "${{RESULT-NAME}}": info.Item.Extension.Name, - }, segments...) + return info.Item.Extension.Name }() return folder, f.mutateExtension(file) } -func (f *PathFinder) folderSupplement(profile string) string { +func (f *PathFinder) folderProfileSupplement(profile string) string { return lo.TernaryF(f.Sch == "" && profile == "", func() string { adhocLabel := f.Stats.Adhoc @@ -402,7 +386,7 @@ func (f *PathFinder) folderSupplement(profile string) string { ) } -func (f *PathFinder) fileSupplement(profile string) string { +func (f *PathFinder) fileProfileSupplement(profile string) string { var ( result string ) diff --git a/src/app/proxy/filing/path-finder_test.go b/src/app/proxy/filing/path-finder_test.go index ebebb83..0da6581 100644 --- a/src/app/proxy/filing/path-finder_test.go +++ b/src/app/proxy/filing/path-finder_test.go @@ -8,7 +8,6 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/snivilised/cobrass/src/clif" "github.com/snivilised/extendio/xfs/nav" "github.com/snivilised/pixa/src/app/cfg" "github.com/snivilised/pixa/src/app/proxy/common" @@ -69,28 +68,6 @@ var _ = Describe("PathFinder", Ordered, func() { }, } - profiles := cfg.MsProfilesConfig{ - Profiles: cfg.ProfilesConfigMap{ - "blur": clif.ChangedFlagsMap{ - "strip": "true", - "interlace": "plane", - "gaussian-blur": "0.25", - }, - "sf": clif.ChangedFlagsMap{ - "strip": "true", - "gaussian-blur": "0.25", - "adaptive-resize": "60", - }, - "adaptive": clif.ChangedFlagsMap{ - "strip": "true", - "interlace": "plane", - "gaussian-blur": "0.25", - "adaptive-resize": "60", - }, - }, - } - _ = profiles - advanced = &cfg.MsAdvancedConfig{ Abort: false, LabelsCFG: cfg.MsLabelsConfig{ @@ -116,7 +93,7 @@ var _ = Describe("PathFinder", Ordered, func() { } }) - DescribeTable("pixa", + DescribeTable("core", func(entry *pfTE) { finder := filing.NewFinder(&filing.NewFinderInfo{ Advanced: advanced, @@ -165,7 +142,7 @@ var _ = Describe("PathFinder", Ordered, func() { // Entry(nil, &pfTE{ - given: "🌀 TRANSFER: transparent/profile/non-cuddled", + given: "🌀 TRANSFER: transparent/profile/non-cuddled (🎯 @TID-CORE-1_TR-PR-NC_T)", should: "redirect input to supplemented folder // filename not modified", reasons: reasons{ folder: "transparency, result should take place of input in same folder", @@ -183,7 +160,7 @@ var _ = Describe("PathFinder", Ordered, func() { }), Entry(nil, &pfTE{ - given: "🎁 RESULT: transparent/profile", + given: "🎁 RESULT: transparent/profile (🎯 @TID-CORE-2_TR-PR-NC_R)", should: "not modify folder // not modify filename", reasons: reasons{ folder: "transparency, result should take place of input", @@ -197,7 +174,7 @@ var _ = Describe("PathFinder", Ordered, func() { }), Entry(nil, &pfTE{ - given: "🌀 TRANSFER: transparent/profile/cuddled", + given: "🌀 TRANSFER: transparent/profile/cuddled (🎯 @TID-CORE-3_TR-PR-CU_T)", should: "not modify folder // file decorated with supplement", reasons: reasons{ folder: "not modify folder to enable cuddle", @@ -205,8 +182,7 @@ var _ = Describe("PathFinder", Ordered, func() { }, profile: "blur", supplements: supplements{ - folder: "", - file: fmt.Sprintf("%v.%v", "$TRASH$", "blur"), + file: fmt.Sprintf("%v.%v", "$TRASH$", "blur"), }, actionTransfer: true, cuddle: true, @@ -220,7 +196,7 @@ var _ = Describe("PathFinder", Ordered, func() { }), Entry(nil, &pfTE{ - given: "🎁 RESULT: transparent/profile/cuddled", + given: "🎁 RESULT: transparent/profile/cuddled (🎯 @TID-CORE-4_TR-PR-CU_R)", should: "not modify folder // not modify filename", reasons: reasons{ folder: "not modify folder to enable cuddle", @@ -236,7 +212,7 @@ var _ = Describe("PathFinder", Ordered, func() { // === TRANSPARENT / SCHEME (non-cuddle) [BLUE] Entry(nil, &pfTE{ - given: "🌀 TRANSFER: transparent/profile/non-cuddled", + given: "🌀 TRANSFER: transparent/scheme/non-cuddled (🎯 @TID-CORE-5_TR-SC-NC_BLUR_T)", should: "redirect input to supplemented folder // filename not modified", reasons: reasons{ folder: "transparency, result should take place of input in same folder", @@ -257,7 +233,7 @@ var _ = Describe("PathFinder", Ordered, func() { // ... Entry(nil, &pfTE{ - given: "🌀 TRANSFER: transparent/profile/non-cuddled", + given: "🌀 TRANSFER: transparent/scheme/non-cuddled (🎯 @TID-CORE-6_TR-SC-NC_SF_T)", should: "redirect input to supplemented folder // filename not modified", reasons: reasons{ folder: "transparency, result should take place of input in same folder", @@ -278,7 +254,7 @@ var _ = Describe("PathFinder", Ordered, func() { // === TRANSPARENT / SCHEME (cuddle) [GREEN] Entry(nil, &pfTE{ - given: "🌀 TRANSFER: transparent/profile/cuddled", + given: "🌀 TRANSFER: transparent/scheme/cuddled (🎯 @TID-CORE-7_TR-SC-CU_BLUR_T)", should: "not modify folder // file decorated with supplement", reasons: reasons{ folder: "not modify folder to enable cuddle", @@ -304,7 +280,7 @@ var _ = Describe("PathFinder", Ordered, func() { // ... Entry(nil, &pfTE{ - given: "🌀 TRANSFER: transparent/profile/cuddled", + given: "🌀 TRANSFER: transparent/scheme/cuddled (🎯 @TID-CORE-8_TR-SC-CU_SF_T)", should: "not modify folder // file decorated with supplement", reasons: reasons{ folder: "not modify folder to enable cuddle", @@ -331,7 +307,7 @@ var _ = Describe("PathFinder", Ordered, func() { // === TRANSPARENT / ADHOC // Entry(nil, &pfTE{ - given: "🌀 TRANSFER: transparent/adhoc/non-cuddled", + given: "🌀 TRANSFER: transparent/adhoc/non-cuddled (🎯 @TID-CORE-9_TR-AD-NC_SF_T)", should: "redirect input to supplemented folder // filename not modified", reasons: reasons{ folder: "transparency, result should take place of input in same folder", @@ -348,7 +324,7 @@ var _ = Describe("PathFinder", Ordered, func() { }), Entry(nil, &pfTE{ - given: "🎁 RESULT: transparent/adhoc", + given: "🎁 RESULT: transparent/adhoc (🎯 @TID-CORE-10_TR-AD_R)", should: "not modify folder // not modify filename", reasons: reasons{ folder: "transparency, result should take place of input", @@ -364,7 +340,7 @@ var _ = Describe("PathFinder", Ordered, func() { // TRANSPARENT --trash SPECIFIED // Entry(nil, &pfTE{ - given: "🌀 TRANSFER: transparent/profile/trash", + given: "🌀 TRANSFER: transparent/profile/trash (🎯 @TID-CORE-11_TR-PR-TRA_T)", should: "redirect input to supplemented folder // filename not modified", reasons: reasons{ folder: "transparency, result should take place of input in same folder", @@ -383,7 +359,7 @@ var _ = Describe("PathFinder", Ordered, func() { }), Entry(nil, &pfTE{ - given: "🎁 RESULT: transparent/profile/trash", + given: "🎁 RESULT: transparent/profile/trash (🎯 @TID-CORE-12_TR-PR-TRA_R)", should: "not modify folder // not modify filename", reasons: reasons{ folder: "transparency, result should take place of input", @@ -400,7 +376,7 @@ var _ = Describe("PathFinder", Ordered, func() { // === TRANSPARENT / SCHEME (non-cuddle) [BLUE] Entry(nil, &pfTE{ - given: "🌀 TRANSFER: transparent/profile/trash", + given: "🌀 TRANSFER: transparent/scheme/trash (🎯 @TID-CORE-13_TR-SC-TRA_BLUR_T)", should: "redirect input to supplemented folder // filename not modified", reasons: reasons{ folder: "transparency, result should take place of input in same folder", @@ -422,7 +398,7 @@ var _ = Describe("PathFinder", Ordered, func() { // ... Entry(nil, &pfTE{ - given: "🌀 TRANSFER: transparent/profile/trash", + given: "🌀 TRANSFER: transparent/scheme/trash (🎯 @TID-CORE-14_TR-SC-TRA_SF_T)", should: "redirect input to supplemented folder // filename not modified", reasons: reasons{ folder: "transparency, result should take place of input in same folder", @@ -444,5 +420,127 @@ var _ = Describe("PathFinder", Ordered, func() { // // NON-TRANSPARENT --output SPECIFIED // + + Entry(nil, &pfTE{ + given: "🌀 TRANSFER: profile/output (🎯 @TID-CORE-15_NT-PR-OUT_T)", + should: "return empty folder and file", + reasons: reasons{ + folder: "no transfer required", + file: "input file left alone", + }, + profile: "blur", + output: filepath.Join("foo", "sessions", "scan01", "results"), + supplements: supplements{ + folder: filepath.Join("$TRASH$", "blur"), + }, + actionTransfer: true, + assert: func(folder, file string, pi *common.PathInfo, statics *common.StaticInfo, entry *pfTE) { + Expect(folder).To(BeEmpty(), because(entry.reasons.folder, folder)) + Expect(file).To(BeEmpty(), because(entry.reasons.file)) + }, + }), + + Entry(nil, &pfTE{ + given: "🎁 RESULT: profile/output (🎯 @TID-CORE-16_NT-PR-OUT_R)", + should: "redirect result to output // supplement folder // not modify filename", + reasons: reasons{ + folder: "result should be send to supplemented output folder", + file: "filename only needs to match input filename because the folder is supplemented", + }, + profile: "blur", + output: filepath.Join("foo", "sessions", "scan01", "results"), + supplements: supplements{ + folder: "blur", + }, + assert: func(folder, file string, pi *common.PathInfo, statics *common.StaticInfo, entry *pfTE) { + supplemented := filing.SupplementFolder( + entry.output, entry.supplements.folder, + ) + Expect(folder).To(Equal(supplemented), because(entry.reasons.folder, folder)) + Expect(file).To(Equal(pi.Item.Extension.Name), because(entry.reasons.file)) + }, + }), + + // === NON-TRANSPARENT / SCHEME (non-cuddle) [BLUE] + + Entry(nil, &pfTE{ + given: "🌀 TRANSFER: scheme/output (🎯 @TID-CORE-17_NT-SC-OUT_BLUR_T)", + should: "return empty folder and file", + reasons: reasons{ + folder: "no transfer required", + file: "input file left alone", + }, + scheme: "blur-sf", + profile: "blur", + output: filepath.Join("foo", "sessions", "scan01", "results"), + supplements: supplements{ + folder: filepath.Join("rubbish", "$TRASH$", "blur-sf", "blur"), + }, + actionTransfer: true, + assert: func(folder, file string, pi *common.PathInfo, statics *common.StaticInfo, entry *pfTE) { + Expect(folder).To(BeEmpty(), because(entry.reasons.folder)) + Expect(file).To(BeEmpty(), because(entry.reasons.file)) + }, + }), + + // ... + + Entry(nil, &pfTE{ + given: "🌀 TRANSFER: scheme/output (🎯 @TID-CORE-18_NT-SC-OUT_SF_T)", + should: "return empty folder and file", + reasons: reasons{ + folder: "no transfer required", + file: "input file left alone", + }, + scheme: "blur-sf", + profile: "sf", + output: filepath.Join("foo", "sessions", "scan01", "results"), + supplements: supplements{ + folder: filepath.Join("rubbish", "$TRASH$", "blur-sf", "sf"), + }, + actionTransfer: true, + assert: func(folder, file string, pi *common.PathInfo, statics *common.StaticInfo, entry *pfTE) { + Expect(folder).To(BeEmpty(), because(entry.reasons.folder)) + Expect(file).To(BeEmpty(), because(entry.reasons.file)) + }, + }), + + // + // === NON-TRANSPARENT / ADHOC + // + Entry(nil, &pfTE{ + given: "🌀 TRANSFER: adhoc/output (🎯 @TID-CORE-19_NT-AD-OUT_SF_T)", + should: "return empty folder and file", + reasons: reasons{ + folder: "no transfer required", + file: "input file left alone", + }, + output: filepath.Join("foo", "sessions", "scan01", "results"), + actionTransfer: true, + assert: func(folder, file string, pi *common.PathInfo, statics *common.StaticInfo, entry *pfTE) { + Expect(folder).To(BeEmpty(), because(entry.reasons.folder)) + Expect(file).To(BeEmpty(), because(entry.reasons.file)) + }, + }), + + Entry(nil, &pfTE{ + given: "🎁 RESULT: adhoc/output (🎯 @TID-CORE-20_NT-AD-OUT_SF_R)", + should: "redirect result to output // supplement folder // not modify filename", + reasons: reasons{ + folder: "result should be send to supplemented output folder", + file: "filename only needs to match input filename because the folder is supplemented", + }, + output: filepath.Join("foo", "sessions", "scan01", "results"), + supplements: supplements{ + folder: "ADHOC", + }, + assert: func(folder, file string, pi *common.PathInfo, statics *common.StaticInfo, entry *pfTE) { + supplemented := filing.SupplementFolder( + entry.output, entry.supplements.folder, + ) + Expect(folder).To(Equal(supplemented), because(entry.reasons.folder)) + Expect(file).To(Equal(pi.Item.Extension.Name), because(entry.reasons.file)) + }, + }), ) })