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

Support copying a file for build dependencies #555

Merged
merged 1 commit into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions internal/archive/fsops.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,18 @@ func CopyByPatterns(source, target string, patterns []string) error {
func copyByPattern(source, target, pattern string) error {
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))
// Check if the source is a file or a folder. If it's a file, the pattern "*" should copy the file itself.
info, err := os.Stat(source)
if err != nil {
return err
}
var fullPattern string
if !info.IsDir() && pattern == "*" {
fullPattern = source
} else {
// 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 {
Expand Down
95 changes: 74 additions & 21 deletions internal/archive/fsops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,49 +377,69 @@ var _ = Describe("FSOPS", func() {
var _ = Describe("Copy By Patterns", func() {

AfterEach(func() {
os.RemoveAll(getFullPath("testdata", "result"))
Ω(os.RemoveAll(getFullPath("testdata", "result"))).Should(Succeed())
})

var _ = DescribeTable("Valid Cases", func(modulePath string, patterns, expectedFiles []string) {
sourcePath := getFullPath("testdata", "testbuildparams", modulePath)
var _ = DescribeTable("Valid Cases", func(sourceFolder string, patterns, expectedFiles []string) {
sourcePath := getFullPath("testdata", "testbuildparams", sourceFolder)
targetPath := getFullPath("testdata", "result")
Ω(CopyByPatterns(sourcePath, targetPath, patterns)).Should(Succeed())
for _, file := range expectedFiles {
Ω(file).Should(BeAnExistingFile())
}
validateFilesInDir(targetPath, expectedFiles)
},
Entry("Single file", "ui2",
[]string{"deep/folder/inui2/anotherfile.txt"},
[]string{getFullPath("testdata", "result", "anotherfile.txt")}),
[]string{"anotherfile.txt"}),
Entry("Wildcard for 2 files", "ui2",
[]string{"deep/*/inui2/another*"},
[]string{getFullPath("testdata", "result", "anotherfile.txt"),
getFullPath("testdata", "result", "anotherfile2.txt")}),
[]string{"anotherfile.txt", "anotherfile2.txt"}),
Entry("Wildcard for 2 files - dot start", "ui2",
[]string{"./deep/*/inui2/another*"},
[]string{getFullPath("testdata", "result", "anotherfile.txt"),
getFullPath("testdata", "result", "anotherfile2.txt")}),
[]string{"anotherfile.txt", "anotherfile2.txt"}),
Entry("Specific folder of second level", "ui2",
[]string{"*/folder/*"},
[]string{
getFullPath("testdata", "result", "inui2", "anotherfile.txt"),
getFullPath("testdata", "result", "inui2", "anotherfile2.txt")}),
[]string{"inui2/", "inui2/anotherfile.txt", "inui2/anotherfile2.txt"}),
Entry("All", "ui1",
[]string{"*"},
[]string{getFullPath("testdata", "result", "webapp", "Component.js")}),
[]string{
"webapp/",
"webapp/controller/", "webapp/controller/View1.controller.js",
"webapp/i18n/", "webapp/i18n/i18n.properties",
"webapp/model/", "webapp/model/models.js",
"webapp/view/", "webapp/view/View1.view.xml",
"webapp/Component.js",
}),
Entry("Dot", "ui1",
[]string{"."},
[]string{getFullPath("testdata", "result", "ui1", "webapp", "Component.js")}),
[]string{
"ui1/",
"ui1/webapp/",
"ui1/webapp/controller/", "ui1/webapp/controller/View1.controller.js",
"ui1/webapp/i18n/", "ui1/webapp/i18n/i18n.properties",
"ui1/webapp/model/", "ui1/webapp/model/models.js",
"ui1/webapp/view/", "ui1/webapp/view/View1.view.xml",
"ui1/webapp/Component.js",
}),
Entry("Multiple patterns", "ui2", //
[]string{"deep/folder/inui2/anotherfile.txt", "*/folder/"},
[]string{
getFullPath("testdata", "result", "folder", "inui2", "anotherfile.txt"),
getFullPath("testdata", "result", "anotherfile.txt")}),
Entry("Empty patterns", "ui2",
[]string{},
[]string{"folder/", "folder/inui2/", "folder/inui2/anotherfile.txt", "folder/inui2/anotherfile2.txt", "anotherfile.txt"}),
Entry("File with * pattern copies the file", "mta.yaml",
[]string{"*"},
[]string{"mta.yaml"}),
Entry("File with non-* pattern doesn't copy the file", "mta.yaml",
[]string{"*a*"},
[]string{}),
Entry("File with several patterns", "mta.yaml",
[]string{"*", "a/", "*.*"},
[]string{"mta.yaml"}),
)

It("doesn't create the target folder if there are no patterns", func() {
sourcePath := getFullPath("testdata", "testbuildparams", "ui2")
targetPath := getFullPath("testdata", "result")
Ω(CopyByPatterns(sourcePath, targetPath, []string{})).Should(Succeed())
Ω(targetPath).ShouldNot(BeADirectory())
})

var _ = DescribeTable("Invalid Cases", func(targetPath, modulePath string, patterns []string) {
sourcePath := getFullPath("testdata", "testbuildparams", modulePath)
err := CopyByPatterns(sourcePath, targetPath, patterns)
Expand Down Expand Up @@ -590,6 +610,39 @@ func validateArchiveContents(expectedFilesInArchive []string, archiveLocation st
}
}

// Check the folder exists and includes exactly the expected files
func validateFilesInDir(src string, expectedFilesInDir []string) {
// List all files in the folder recursively
filesInDir := make([]string, 0)
err := filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Don't include the folder itself
if filepath.Clean(path) == filepath.Clean(src) {
return nil
}
relPath, err := filepath.Rel(src, path)
if err != nil {
return err
}
relPath = filepath.ToSlash(relPath)
if info.IsDir() {
relPath += "/"
}
filesInDir = append(filesInDir, relPath)
return nil
})
Ω(err).Should(Succeed())

for _, expectedFile := range expectedFilesInDir {
Ω(contains(expectedFile, filesInDir)).Should(BeTrue(), fmt.Sprintf("expected %s to be in the directory; directory contains %v", expectedFile, filesInDir))
}
for _, existingFile := range filesInDir {
Ω(contains(existingFile, expectedFilesInDir)).Should(BeTrue(), fmt.Sprintf("did not expect %s to be in the directory; directory contains %v", existingFile, filesInDir))
}
}

func contains(element string, elements []string) bool {
for _, el := range elements {
if el == element {
Expand Down