Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug4793 in assembly #433

Merged
merged 19 commits into from
Jun 30, 2019
3 changes: 2 additions & 1 deletion cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"fmt"
dir "github.com/SAP/cloud-mta-build-tool/internal/archive"
"os"
"path/filepath"

Expand Down Expand Up @@ -34,7 +35,7 @@ var _ = Describe("Commands", func() {
var _ = Describe("cleanup command", func() {

BeforeEach(func() {
os.MkdirAll(getTestPath("resultClean", "mtahtml5", "mtahtml5"), os.ModePerm)
dir.CreateDirIfNotExist(getTestPath("resultClean", "mtahtml5", "mtahtml5"))
})

AfterEach(func() {
Expand Down
19 changes: 13 additions & 6 deletions cmd/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,25 @@ var _ = Describe("Commands", func() {
})

It("Generate Meta", func() {
os.MkdirAll(getTestPath("result", ".mtahtml5_mta_build_tmp", "testapp"), os.ModePerm)
dir.CreateDirIfNotExist(getTestPath("result", ".mtahtml5_mta_build_tmp", "testapp"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this folder have to be created? Shouldn't it exist only in the project's sources?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, when creating manifest we check target artifacts in temp folder

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The module's temp folder is under the module's name, not its path, isn't it?

file, _ := os.Create(getTestPath("result", ".mtahtml5_mta_build_tmp", "testapp", "data.zip"))
file.Close()
os.MkdirAll(getTestPath("result", ".mtahtml5_mta_build_tmp", "ui5app2"), os.ModePerm)
dir.CreateDirIfNotExist(getTestPath("result", ".mtahtml5_mta_build_tmp", "ui5app2"))
dir.CreateDirIfNotExist(getTestPath("result", ".mtahtml5_mta_build_tmp", "ui5app"))
file, _ = os.Create(getTestPath("result", ".mtahtml5_mta_build_tmp", "ui5app2", "data.zip"))
file.Close()
file, _ = os.Create(getTestPath("result", ".mtahtml5_mta_build_tmp", "xs-security.json"))
file.Close()
file, _ = os.Create(getTestPath("result", ".mtahtml5_mta_build_tmp", "ui5app", "data.zip"))
file.Close()
metaCmdSrc = getTestPath("mtahtml5")
ep = dir.Loc{SourcePath: metaCmdSrc, TargetPath: metaCmdTrg}
Ω(metaCmd.RunE(nil, []string{})).Should(Succeed())
Ω(ep.GetMtadPath()).Should(BeAnExistingFile())
})
It("Generate Mtad - Sanity", func() {
os.MkdirAll(getTestPath("result", ".mtahtml5_mta_build_tmp", "testapp"), os.ModePerm)
os.MkdirAll(getTestPath("result", ".mtahtml5_mta_build_tmp", "ui5app2"), os.ModePerm)
dir.CreateDirIfNotExist(getTestPath("result", ".mtahtml5_mta_build_tmp", "testapp"))
dir.CreateDirIfNotExist(getTestPath("result", ".mtahtml5_mta_build_tmp", "ui5app2"))
mtadCmdSrc = getTestPath("mtahtml5")
mtadCmdPlatform = "cf"
Ω(mtadCmd.RunE(nil, []string{})).Should(Succeed())
Expand All @@ -71,10 +74,14 @@ var _ = Describe("Commands", func() {
platform.PlatformConfig = config
})
It("Generate Mtar", func() {
os.MkdirAll(getTestPath("result", ".mtahtml5_mta_build_tmp", "testapp"), os.ModePerm)
os.MkdirAll(getTestPath("result", ".mtahtml5_mta_build_tmp", "ui5app2"), os.ModePerm)
dir.CreateDirIfNotExist(getTestPath("result", ".mtahtml5_mta_build_tmp", "ui5app2"))
file, _ := os.Create(getTestPath("result", ".mtahtml5_mta_build_tmp", "xs-security.json"))
file.Close()
dir.CreateDirIfNotExist(getTestPath("result", ".mtahtml5_mta_build_tmp", "ui5app"))
file, _ = os.Create(getTestPath("result", ".mtahtml5_mta_build_tmp", "ui5app", "data.zip"))
file.Close()
file, _ = os.Create(getTestPath("result", ".mtahtml5_mta_build_tmp", "ui5app2", "data.zip"))
file.Close()
mtarCmdSrc = getTestPath("mtahtml5")
mtarCmdMtarName = ""
Ω(metaCmd.RunE(nil, []string{})).Should(Succeed())
Expand Down
10 changes: 2 additions & 8 deletions cmd/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ import (
// flags of pack command
var packCmdSrc string
var packCmdTrg string
var packCmdDesc string
var packCmdModule string
var packCmdPlatform string

// flags of build command
var buildCmdSrc string
var buildCmdTrg string
var buildCmdDesc string
var buildCmdModule string
var buildCmdPlatform string

Expand All @@ -29,8 +27,6 @@ func init() {
"the path to the MTA project; the current path is is set as the default")
packModuleCmd.Flags().StringVarP(&packCmdTrg, "target", "t", "",
"the path to the MBT results folder; the current path is set as the default")
packModuleCmd.Flags().StringVarP(&packCmdDesc, "desc", "d", "",
`the MTA descriptor; supported values: "dev" (development descriptor, default value) and "dep" (deployment descriptor)`)
packModuleCmd.Flags().StringVarP(&packCmdModule, "module", "m", "",
"the name of the module")
packModuleCmd.Flags().StringVarP(&packCmdPlatform, "platform", "p", "",
Expand All @@ -41,8 +37,6 @@ func init() {
"the path to the MTA project; the current path is set as the default")
buildModuleCmd.Flags().StringVarP(&buildCmdTrg, "target", "t", "",
"the path to the MBT results folder; the current path is set as the default")
buildModuleCmd.Flags().StringVarP(&buildCmdDesc, "desc", "d", "",
`the MTA descriptor; supported values: "dev" (development descriptor, default value) and "dep" (deployment descriptor)`)
buildModuleCmd.Flags().StringVarP(&buildCmdModule, "module", "m", "",
"the name of the module")
buildModuleCmd.Flags().StringVarP(&buildCmdPlatform, "platform", "p", "",
Expand All @@ -56,7 +50,7 @@ var buildModuleCmd = &cobra.Command{
Long: "builds module and archives its artifacts",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
err := artifacts.ExecuteBuild(buildCmdSrc, buildCmdTrg, buildCmdDesc, buildCmdModule, buildCmdPlatform, os.Getwd)
err := artifacts.ExecuteBuild(buildCmdSrc, buildCmdTrg, buildCmdModule, buildCmdPlatform, os.Getwd)
logError(err)
return err
},
Expand All @@ -75,7 +69,7 @@ var packModuleCmd = &cobra.Command{
Long: "packs the module artifacts after the build process",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
err := artifacts.ExecutePack(packCmdSrc, packCmdTrg, packCmdDesc, packCmdModule, packCmdPlatform, os.Getwd)
err := artifacts.ExecutePack(packCmdSrc, packCmdTrg, packCmdModule, packCmdPlatform, os.Getwd)
logError(err)
return err
},
Expand Down
4 changes: 2 additions & 2 deletions cmd/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ var _ = Describe("Commands", func() {
packCmdPlatform = "cf"
ep := dir.Loc{SourcePath: packCmdSrc, TargetPath: packCmdTrg}
targetTmpDir := ep.GetTargetTmpDir()
err := os.MkdirAll(targetTmpDir, os.ModePerm)
err := dir.CreateDirIfNotExist(targetTmpDir)
if err != nil {
logs.Logger.Error(err)
}
f, _ := os.Create(filepath.Join(targetTmpDir, "ui5app"))
Ω(packModuleCmd.RunE(nil, []string{})).Should(HaveOccurred())
fmt.Println(str.String())
Ω(str.String()).Should(ContainSubstring(`packing of the "ui5app" module failed when creating`))
Ω(str.String()).Should(ContainSubstring(`packing of the "ui5app" module failed when archiving: archiving failed when creating`))

f.Close()
// cleanup command used for test temp file removal
Expand Down
Empty file.
4 changes: 2 additions & 2 deletions integration/cloud_mta_build_tool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ modules:
It("Generate mtad", func() {
dir, _ := os.Getwd()
path := filepath.Join(dir, "testdata", "mta_demo")
os.MkdirAll(filepath.Join(path, ".mta_demo_mta_build_tmp", "node"), os.ModePerm)
os.MkdirAll(filepath.Join(path, ".mta_demo_mta_build_tmp", "node-js"), os.ModePerm)
dir.CreateDirIfNotExist(filepath.Join(path, ".mta_demo_mta_build_tmp", "node"))
dir.CreateDirIfNotExist(filepath.Join(path, ".mta_demo_mta_build_tmp", "node-js"))
bin := filepath.FromSlash(binPath)
_, err, _ := execute(bin, "gen mtad", path)
Ω(err).Should(Equal(""))
Expand Down
36 changes: 21 additions & 15 deletions internal/archive/fsops.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func CreateDirIfNotExist(dir string) error {
info, err := os.Stat(dir)
if os.IsNotExist(err) {
err = os.MkdirAll(dir, os.ModePerm)
} else if !info.IsDir() {
err = fmt.Errorf("creation of the %v folder failed because a file exists with the same name", dir)
} else if err == nil && !info.IsDir() {
err = fmt.Errorf("creation of the %s folder failed because a file exists with the same name", dir)
}
return err
}
Expand All @@ -38,6 +38,12 @@ func Archive(sourcePath, targetArchivePath string, ignore []string) (e error) {
return err
}

// create folder of archive file if not exists
err = CreateDirIfNotExist(filepath.Dir(targetArchivePath))
if err != nil {
return errors.Wrapf(err, `archiving failed when creating the "%s" folder`, targetArchivePath)
}

// create archive file
zipfile, err := os.Create(targetArchivePath)
if err != nil {
Expand Down Expand Up @@ -176,7 +182,7 @@ func CopyDir(src string, dst string, withParents bool, copyDirEntries func(entri
if !withParents && err != nil {
err = os.Mkdir(dst, os.ModePerm)
} else if err != nil {
err = os.MkdirAll(dst, os.ModePerm)
err = CreateDirIfNotExist(dst)
}
if err != nil {
return err
Expand All @@ -198,22 +204,22 @@ func CopyByPatterns(source, target string, patterns []string) error {
return nil
}

logs.Logger.Infof("copying the patterns [%v,...] from the %v folder to the %v folder",
logs.Logger.Infof("copying the patterns [%s,...] from the %s folder to the %s folder",
patterns[0], source, target)

infoTargetDir, err := os.Stat(target)
if err != nil {
err = os.MkdirAll(target, os.ModePerm)
err = CreateDirIfNotExist(target)
if err != nil {
return errors.Wrapf(err,
"copying the patterns [%v,...] from the %v folder to the %v folder failed when creating the target folder",
"copying the patterns [%s,...] from the %s folder to the %s folder failed when creating the target folder",
patterns[0], source, target)
}
logs.Logger.Infof(`the "%v" folder has been created`, target)
logs.Logger.Infof(`the "%s" folder has been created`, target)

} else if !infoTargetDir.IsDir() {
return errors.Errorf(
`copying the patterns [%v,...] from the "%v" folder to the "%v" folder failed because the target is not a folder`,
`copying the patterns [%s,...] from the "%s" folder to the "%s" folder failed because the target is not a folder`,
patterns[0], source, target)
}

Expand All @@ -229,15 +235,15 @@ func CopyByPatterns(source, target string, patterns []string) error {

// copyByPattern - copy files/directories according to pattern
func copyByPattern(source, target, pattern string) error {
logs.Logger.Infof(`copying the "%v" pattern from the "%v" folder to the "%v" folder`,
logs.Logger.Infof(`copying the "%s" pattern from the "%s" folder to the "%s" folder`,
pattern, source, target)
// build full pattern concatenating source path and pattern
fullPattern := filepath.Join(source, strings.Replace(pattern, "./", "", -1))
// get all entries matching the pattern
sourceEntries, err := filepath.Glob(fullPattern)
if err != nil {
return errors.Wrapf(err,
`copying the "%v" pattern from the "%v" folder to the "%v" folder failed when getting matching entries`,
`copying the "%s" pattern from the "%s" folder to the "%s" folder failed when getting matching entries`,
pattern, source, target)
}

Expand All @@ -254,7 +260,7 @@ func copyEntries(entries []string, source, target, pattern string) error {
info, err := os.Stat(entry)
if err != nil {
return errors.Wrapf(err,
`copying the "%v" pattern from the "%v" folder to the "%v" folder failed when getting the status of the source entry: %v`,
`copying the "%s" pattern from the "%s" folder to the "%s" folder failed when getting the status of the source entry: %s`,
pattern, source, target, entry)
}
targetEntry := filepath.Join(target, filepath.Base(entry))
Expand All @@ -265,7 +271,7 @@ func copyEntries(entries []string, source, target, pattern string) error {
}
if err != nil {
return errors.Wrapf(err,
`copying the "%v" pattern from the "%v" folder to the "%v" folder failed when copying the "%v" entry to the "%v" entry`,
`copying the "%s" pattern from the "%s" folder to the "%s" folder failed when copying the "%s" entry to the "%s" entry`,
pattern, source, target, entry, targetEntry)
}
}
Expand All @@ -290,7 +296,7 @@ func CopyEntries(entries []os.FileInfo, src, dst string) error {
// Todo check posix compatibility
if entry.Mode()&os.ModeSymlink != 0 {
logs.Logger.Infof(
`copying of the entries from the "%v" folder to the "%v" folder skipped the "%v" entry because its mode is a symbolic link`,
`copying of the entries from the "%s" folder to the "%s" folder skipped the "%s" entry because its mode is a symbolic link`,
src, dst, entry.Name())
} else {
err = CopyFileWithMode(srcPath, dstPath, entry.Mode())
Expand Down Expand Up @@ -336,7 +342,7 @@ func CopyEntriesInParallel(entries []os.FileInfo, src, dst string) (rerr error)
// Todo check posix compatibility
if e.Mode()&os.ModeSymlink != 0 {
logs.Logger.Infof(
`copying of the entries from the "%v" folder to the "%v" folder skipped the "%v" entry because its mode is a symbolic link`,
`copying of the entries from the "%s" folder to the "%s" folder skipped the "%s" entry because its mode is a symbolic link`,
src, dst, e.Name())
} else {
err = CopyFileWithMode(srcPath, dstPath, e.Mode())
Expand Down Expand Up @@ -431,7 +437,7 @@ func Read(ep IMtaYaml) ([]byte, error) {
func readFile(file string) ([]byte, error) {
content, err := ioutil.ReadFile(file)
if err != nil {
return nil, errors.Wrapf(err, `failed to read the "%v" file`, file)
return nil, errors.Wrapf(err, `failed to read the "%s" file`, file)
}
s := string(content)
s = strings.Replace(s, "\r\n", "\r", -1)
Expand Down
30 changes: 16 additions & 14 deletions internal/archive/fsops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,29 @@ var _ = Describe("FSOPS", func() {
os.RemoveAll(targetFilePath)
})

var _ = DescribeTable("Archive", func(source, target string, ignore []string, matcher GomegaMatcher, created bool) {
var _ = DescribeTable("Archive", func(source, target string, ignore []string, fails bool) {

Ω(Archive(source, target, ignore)).Should(matcher)
if created {
Ω(target).Should(BeAnExistingFile())
err := Archive(source, target, ignore)
if fails {
Ω(err).Should(HaveOccurred())
} else {
Ω(target).ShouldNot(BeAnExistingFile())
Ω(err).Should(Succeed())
Ω(target).Should(BeAnExistingFile())
}
},
Entry("Sanity",
getFullPath("testdata", "mtahtml5"), targetFilePath, nil, Succeed(), true),
getFullPath("testdata", "mtahtml5"), targetFilePath, nil, false),
Entry("Target is file",
tal-sapan marked this conversation as resolved.
Show resolved Hide resolved
getFullPath("testdata", "mtahtml5"), getFullPath("testdata"), nil, true),
Entry("Sanity - ignore folder",
getFullPath("testdata", "testproject"), targetFilePath, []string{"ui5app/"}, Succeed(), true),
getFullPath("testdata", "testproject"), targetFilePath, []string{"ui5app/"}, false),
Entry("Sanity - ignore file",
getFullPath("testdata", "testproject"), targetFilePath, []string{"ui5app/Gr*.js"}, Succeed(), true),
getFullPath("testdata", "testproject"), targetFilePath, []string{"ui5app/Gr*.js"}, false),
Entry("SourceIsNotFolder",
getFullPath("testdata", "level2", "level2_one.txt"), targetFilePath, nil, Succeed(), true),
getFullPath("testdata", "level2", "level2_one.txt"), targetFilePath, nil, false),
Entry("Target is empty string",
getFullPath("testdata", "mtahtml5"), "", nil, HaveOccurred(), false),
Entry("Source is empty string",
"", "", nil, HaveOccurred(), false),
getFullPath("testdata", "mtahtml5"), "", nil, true),
Entry("Source is empty string", "", "", nil, true),
)
})

Expand Down Expand Up @@ -181,7 +183,7 @@ var _ = Describe("FSOPS", func() {
It("Sanity", func() {
sourcePath := getFullPath("testdata", "level2", "level3")
targetPath := getFullPath("testdata", "result")
os.MkdirAll(targetPath, os.ModePerm)
CreateDirIfNotExist(targetPath)
files, _ := ioutil.ReadDir(sourcePath)
// Files wrapped to overwrite their methods
var filesWrapped []os.FileInfo
Expand All @@ -196,7 +198,7 @@ var _ = Describe("FSOPS", func() {
It("Sanity - copy in parallel", func() {
sourcePath := getFullPath("testdata", "level2", "level3")
targetPath := getFullPath("testdata", "result")
os.MkdirAll(targetPath, os.ModePerm)
CreateDirIfNotExist(targetPath)
files, _ := ioutil.ReadDir(sourcePath)
// Files wrapped to overwrite their methods
var filesWrapped []os.FileInfo
Expand Down
2 changes: 1 addition & 1 deletion internal/archive/mta_location.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (ep *Loc) GetManifestPath() string {
// ValidateDeploymentDescriptor validates the deployment descriptor.
func ValidateDeploymentDescriptor(descriptor string) error {
if descriptor != "" && descriptor != Dev && descriptor != Dep {
return fmt.Errorf(`the "%v" descriptor is invalid; expected one of the following values: Dev, Dep`, descriptor)
return fmt.Errorf(`the "%s" descriptor is invalid; expected one of the following values: Dev, Dep`, descriptor)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/artifacts/assembly.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func Assembly(source, target, platform, mtarName, copyInParallel string, getWd f
parallelCopy = false
}
// copy from source to target
err = CopyMtaContent(source, target, dir.Dep, parallelCopy, getWd)
err = CopyMtaContent(source, target, parallelCopy, getWd)
if err != nil {
return errors.Wrap(err, "assembly of the MTA project failed when copying the MTA content")
}
Expand Down
Loading