From 0de0c0a46a1f5637a471acbc99076256527fedf8 Mon Sep 17 00:00:00 2001 From: Volkov Date: Wed, 20 Feb 2019 19:28:23 +0200 Subject: [PATCH 1/2] use mta_archive subfolder only if target equals source --- cmd/assembly_test.go | 2 +- cmd/gen_test.go | 2 +- internal/artifacts/meta_test.go | 7 ++++++- internal/artifacts/module_arch_test.go | 9 +++++++++ internal/artifacts/mtad.go | 4 ++++ internal/artifacts/mtar.go | 11 +++++------ internal/artifacts/mtar_test.go | 19 +++++++++++++------ internal/fs/mta_location.go | 14 ++++++++++++++ internal/fs/mta_location_test.go | 8 ++++++++ 9 files changed, 61 insertions(+), 15 deletions(-) diff --git a/cmd/assembly_test.go b/cmd/assembly_test.go index 9e4c9d28b..9940c557d 100644 --- a/cmd/assembly_test.go +++ b/cmd/assembly_test.go @@ -18,7 +18,7 @@ var _ = Describe("Assembly", func() { err := assembly(getTestPath("assembly-sample"), getTestPath("result"), "cf", os.Getwd) Ω(err).Should(Succeed()) - Ω(getTestPath("result", "mta_archives", "com.sap.xs2.samples.javahelloworld_0.1.0.mtar")).Should(BeAnExistingFile()) + Ω(getTestPath("result", "com.sap.xs2.samples.javahelloworld_0.1.0.mtar")).Should(BeAnExistingFile()) }) var _ = DescribeTable("Fails on location initialization", func(maxCalls int) { calls := 0 diff --git a/cmd/gen_test.go b/cmd/gen_test.go index 8ffa26b8c..4feb2c1fb 100644 --- a/cmd/gen_test.go +++ b/cmd/gen_test.go @@ -68,7 +68,7 @@ var _ = Describe("Commands", func() { mtarCmdSrc = getTestPath("mtahtml5") Ω(metaCmd.RunE(nil, []string{})).Should(Succeed()) Ω(mtarCmd.RunE(nil, []string{})).Should(Succeed()) - Ω(getTestPath("result", "mta_archives", "mtahtml5_0.0.1.mtar")).Should(BeAnExistingFile()) + Ω(getTestPath("result", "mtahtml5_0.0.1.mtar")).Should(BeAnExistingFile()) }) }) diff --git a/internal/artifacts/meta_test.go b/internal/artifacts/meta_test.go index c12c8a529..b4a6709d5 100644 --- a/internal/artifacts/meta_test.go +++ b/internal/artifacts/meta_test.go @@ -144,7 +144,7 @@ cli_version:["x"] if err != nil { fmt.Println(err) } - mtarPath, err := generateMtar(&ep, &ep) + mtarPath, err := generateMtar(&ep, &ep, &ep) if err != nil { fmt.Println(err) } @@ -168,3 +168,8 @@ func (loc *testLoc) GetMtadPath() string { func (loc *testLoc) GetManifestPath() string { return filepath.Join(loc.loc.GetManifestPath(), "folderNotExists", "MANIFEST.MF") } + +func (loc *testLoc) GetMtarDir() string { + return loc.GetMtarDir() +} + diff --git a/internal/artifacts/module_arch_test.go b/internal/artifacts/module_arch_test.go index fe949c404..2afb561b8 100644 --- a/internal/artifacts/module_arch_test.go +++ b/internal/artifacts/module_arch_test.go @@ -397,6 +397,15 @@ module-types: }) }) + var _ = Describe("copyMtaContentFromPath", func(){ + It("content is file; fails because target folder exists and it's not a folder, but a file", func() { + file, _ := os.Create(getTestPath("result")) + defer file.Close() + Ω(copyMtaContentFromPath(getTestPath("mta", "mta.yaml"), getTestPath("result", "mta.yaml" ), + getTestPath("result", "mta.yaml" ), getTestPath("result"))).Should(HaveOccurred()) + }) + }) + var _ = Describe("cleanUpCopiedContent", func() { It("Sanity", func() { err := cleanUpCopiedContent(getTestPath(), []string{"result"}) diff --git a/internal/artifacts/mtad.go b/internal/artifacts/mtad.go index d8a565fd2..151a26261 100644 --- a/internal/artifacts/mtad.go +++ b/internal/artifacts/mtad.go @@ -31,6 +31,10 @@ func (loc *mtadLoc) GetManifestPath() string { return "" } +func (loc *mtadLoc) GetMtarDir() string { + return "" +} + // ExecuteGenMtad - generates MTAD from MTA func ExecuteGenMtad(source, target, platform string, wdGetter func() (string, error)) error { logs.Logger.Info("generating the MTAD file...") diff --git a/internal/artifacts/mtar.go b/internal/artifacts/mtar.go index 985d52172..96375c108 100644 --- a/internal/artifacts/mtar.go +++ b/internal/artifacts/mtar.go @@ -11,7 +11,6 @@ import ( const ( mtarExtension = ".mtar" - mtarFolder = "mta_archives" ) // ExecuteGenMtar - generates MTAR @@ -21,7 +20,7 @@ func ExecuteGenMtar(source, target, desc string, wdGetter func() (string, error) if err != nil { return errors.Wrap(err, "generation of the MTA archive failed when initializing the location") } - path, err := generateMtar(loc, loc) + path, err := generateMtar(loc, loc, loc) if err != nil { return err } @@ -30,7 +29,7 @@ func ExecuteGenMtar(source, target, desc string, wdGetter func() (string, error) } // generateMtar - generate mtar archive from the build artifacts -func generateMtar(targetLoc dir.ITargetPath, parser dir.IMtaParser) (string, error) { +func generateMtar(targetLoc dir.ITargetPath, targetArtifacts dir.ITargetArtifacts, parser dir.IMtaParser) (string, error) { // get MTA object m, err := parser.ParseFile() if err != nil { @@ -41,11 +40,11 @@ func generateMtar(targetLoc dir.ITargetPath, parser dir.IMtaParser) (string, err // create the mta_archives folder // get directory - where mtar will be saved - mtarFolderPath := filepath.Join(targetLoc.GetTarget(), mtarFolder) + mtarFolderPath := targetArtifacts.GetMtarDir() err = dir.CreateDirIfNotExist(mtarFolderPath) if err != nil { - return "", errors.Wrap(err, - `generation of the MTA archive failed when creating the "mta_archives" folder`) + return "", errors.Wrapf(err, + `generation of the MTA archive failed when creating the "%s" folder`, mtarFolderPath) } // archive building artifacts to mtar mtarPath := filepath.Join(mtarFolderPath, m.ID+"_"+m.Version+mtarExtension) diff --git a/internal/artifacts/mtar_test.go b/internal/artifacts/mtar_test.go index 5f53005b5..72b2fad2c 100644 --- a/internal/artifacts/mtar_test.go +++ b/internal/artifacts/mtar_test.go @@ -24,7 +24,7 @@ var _ = Describe("Mtar", func() { os.MkdirAll(getTestPath("result", ".mtahtml5_mta_build_tmp", "ui5app2"), os.ModePerm) Ω(ExecuteGenMeta(getTestPath("mtahtml5"), getResultPath(), "dev", "cf", true, os.Getwd)).Should(Succeed()) Ω(ExecuteGenMtar(getTestPath("mtahtml5"), getResultPath(), "dev", os.Getwd)).Should(Succeed()) - Ω(getTestPath("result", "mta_archives", "mtahtml5_0.0.1.mtar")).Should(BeAnExistingFile()) + Ω(getTestPath("result", "mtahtml5_0.0.1.mtar")).Should(BeAnExistingFile()) }) It("Fails on location initialization", func() { @@ -43,7 +43,7 @@ var _ = Describe("Mtar", func() { os.MkdirAll(getTestPath("result", ".mtahtml5_mta_build_tmp", "testapp"), os.ModePerm) os.MkdirAll(getTestPath("result", ".mtahtml5_mta_build_tmp", "ui5app2"), os.ModePerm) Ω(generateMeta(&ep, &ep, &ep, false, "cf", true)).Should(Succeed()) - mtarPath, err := generateMtar(&ep, &ep) + mtarPath, err := generateMtar(&ep, &ep, &ep) Ω(err).Should(Succeed()) Ω(mtarPath).Should(BeAnExistingFile()) }) @@ -51,25 +51,32 @@ var _ = Describe("Mtar", func() { It("Generate Mtar - Fails on wrong source", func() { ep := dir.Loc{SourcePath: getTestPath("not_existing"), TargetPath: getResultPath()} ep1 := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath()} - _, err := generateMtar(&ep, &ep1) + _, err := generateMtar(&ep, &ep1, &ep1) Ω(err).Should(HaveOccurred()) }) It("Generate Mtar - Invalid mta", func() { ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath(), MtaFilename: "mtaBroken.yaml"} - _, err := generateMtar(&ep, &ep) + _, err := generateMtar(&ep, &ep, &ep) Ω(err).Should(HaveOccurred()) }) It("Generate Mtar - Mta not exists", func() { ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath(), MtaFilename: "mtaNotExists.yaml"} - _, err := generateMtar(&ep, &ep) + _, err := generateMtar(&ep, &ep, &ep) + Ω(err).Should(HaveOccurred()) + }) + It("Generate Mtar - results file exists, folder results can't be created ", func() { + file, _ := os.Create(getTestPath("result")) + defer file.Close() + ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath()} + _, err := generateMtar(&ep, &ep, &ep) Ω(err).Should(HaveOccurred()) }) var _ = Describe("Target Failures", func() { var _ = DescribeTable("Invalid location", func(loc *testMtarLoc) { ep := dir.Loc{} - _, err := generateMtar(loc, &ep) + _, err := generateMtar(loc, &ep, &ep) Ω(err).Should(HaveOccurred()) }, Entry("Fails on GetTargetTmpDir", &testMtarLoc{ diff --git a/internal/fs/mta_location.go b/internal/fs/mta_location.go index 4306875c7..de6155e39 100644 --- a/internal/fs/mta_location.go +++ b/internal/fs/mta_location.go @@ -18,6 +18,8 @@ const ( TempFolderSuffix = "_mta_build_tmp" // Mtad - deployment descriptor file name Mtad = "mtad.yaml" + // MtarFolder - default archives folder + MtarFolder = "mta_archives" ) // IMtaParser - MTA Parser interface @@ -71,6 +73,7 @@ type ITargetArtifacts interface { GetMetaPath() string GetMtadPath() string GetManifestPath() string + GetMtarDir() string } // Loc - MTA tool file properties @@ -100,6 +103,17 @@ func (ep *Loc) GetDescriptor() string { return ep.Descriptor } +// GetMtarDir - gets archive folder +// if the target folder differs from the source folder archive folder is target folder +// otherwise archives folder - "mta_archives" subfolder in the project folder +func (ep *Loc) GetMtarDir() string { + if ep.SourcePath == ep.TargetPath { + return filepath.Join(ep.SourcePath, MtarFolder) + } + + return ep.TargetPath +} + // GetTarget gets the target path; // if it is not provided, use the path of the processed project. func (ep *Loc) GetTarget() string { diff --git a/internal/fs/mta_location_test.go b/internal/fs/mta_location_test.go index 55b4b41f7..18335c17b 100644 --- a/internal/fs/mta_location_test.go +++ b/internal/fs/mta_location_test.go @@ -68,6 +68,14 @@ var _ = Describe("Path", func() { location := Loc{SourcePath: getPath("xyz"), TargetPath: getPath("abc")} Ω(location.GetMtadPath()).Should(Equal(getPath("abc", ".xyz_mta_build_tmp", "META-INF", "mtad.yaml"))) }) + It("GetMtarDir - mta_archives subfolder", func() { + location := Loc{SourcePath: getPath("xyz"), TargetPath: getPath("xyz")} + Ω(location.GetMtarDir()).Should(Equal(getPath("xyz", "mta_archives"))) + }) + It("GetMtarDir - target folder", func() { + location := Loc{SourcePath: getPath("xyz"), TargetPath: getPath("abc")} + Ω(location.GetMtarDir()).Should(Equal(getPath("abc"))) + }) It("GetManifestPath", func() { location := Loc{SourcePath: getPath("xyz"), TargetPath: getPath("abc")} Ω(location.GetManifestPath()).Should(Equal(getPath("abc", ".xyz_mta_build_tmp", "META-INF", "MANIFEST.MF"))) From 52addef32c4dcae022117b36649d18daf8c7f334 Mon Sep 17 00:00:00 2001 From: Volkov Date: Wed, 20 Feb 2019 19:34:58 +0200 Subject: [PATCH 2/2] fix lint --- internal/artifacts/meta_test.go | 1 - internal/artifacts/module_arch_test.go | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/internal/artifacts/meta_test.go b/internal/artifacts/meta_test.go index b4a6709d5..b426fca6c 100644 --- a/internal/artifacts/meta_test.go +++ b/internal/artifacts/meta_test.go @@ -172,4 +172,3 @@ func (loc *testLoc) GetManifestPath() string { func (loc *testLoc) GetMtarDir() string { return loc.GetMtarDir() } - diff --git a/internal/artifacts/module_arch_test.go b/internal/artifacts/module_arch_test.go index 2afb561b8..36341685b 100644 --- a/internal/artifacts/module_arch_test.go +++ b/internal/artifacts/module_arch_test.go @@ -397,12 +397,12 @@ module-types: }) }) - var _ = Describe("copyMtaContentFromPath", func(){ + var _ = Describe("copyMtaContentFromPath", func() { It("content is file; fails because target folder exists and it's not a folder, but a file", func() { file, _ := os.Create(getTestPath("result")) defer file.Close() - Ω(copyMtaContentFromPath(getTestPath("mta", "mta.yaml"), getTestPath("result", "mta.yaml" ), - getTestPath("result", "mta.yaml" ), getTestPath("result"))).Should(HaveOccurred()) + Ω(copyMtaContentFromPath(getTestPath("mta", "mta.yaml"), getTestPath("result", "mta.yaml"), + getTestPath("result", "mta.yaml"), getTestPath("result"))).Should(HaveOccurred()) }) })