Skip to content

Commit

Permalink
Fixed relative filepath and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Priya Wadhwa committed Mar 15, 2018
1 parent 140d49d commit 0bb35a9
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 36 deletions.
1 change: 1 addition & 0 deletions integration_tests/context/arr[0].txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
Empty file.
2 changes: 2 additions & 0 deletions integration_tests/dockerfiles/Dockerfile_test_copy
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ COPY . newdir
COPY context/bar /baz/
COPY ["context/foo", "/tmp/foo" ]
COPY context/b* /baz/
COPY context/foo context/bar/ba? /test/
COPY context/arr[[]0].txt /mydir/
4 changes: 2 additions & 2 deletions pkg/commands/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (c *CopyCommand) ExecuteCommand(config *manifest.Schema2Config) error {
logrus.Infof("dest: %s", dest)

// Get a map of [src]:[files rooted at src]
srcMap, err := util.ResolveSources(c.cmd.SourcesAndDest, c.buildcontext, config.WorkingDir)
srcMap, err := util.ResolveSources(c.cmd.SourcesAndDest, c.buildcontext)
if err != nil {
return err
}
Expand Down Expand Up @@ -88,7 +88,7 @@ func (c *CopyCommand) FilesToSnapshot() []string {
return c.snapshotFiles
}

// Author returns some information about the command for the image config
// CreatedBy returns some information about the command for the image config
func (c *CopyCommand) CreatedBy() string {
return strings.Join(c.cmd.SourcesAndDest, " ")
}
49 changes: 28 additions & 21 deletions pkg/util/command_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package util
import (
"github.com/docker/docker/builder/dockerfile/instructions"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"os"
"path/filepath"
"strings"
Expand All @@ -38,21 +39,22 @@ func ContainsWildcards(paths []string) bool {
return false
}

// ResolveSources resolves the given sources if the sources contains wildcard
// ResolveSources resolves the given sources if the sources contains wildcards
// It returns a map of [src]:[files rooted at src]
func ResolveSources(srcsAndDest instructions.SourcesAndDest, root, cwd string) (map[string][]string, error) {
func ResolveSources(srcsAndDest instructions.SourcesAndDest, root string) (map[string][]string, error) {
srcs := srcsAndDest[:len(srcsAndDest)-1]
// If sources contain wildcards, we first need to resolve them to actual paths
wildcard := ContainsWildcards(srcs)
if wildcard {
files, err := Files("", root)
if ContainsWildcards(srcs) {
logrus.Debugf("Resolving srcs %v...", srcs)
files, err := RelativeFiles("", root)
if err != nil {
return nil, err
}
srcs, err = matchSources(srcs, files, cwd)
srcs, err = matchSources(srcs, files)
if err != nil {
return nil, err
}
logrus.Debugf("Resolved sources to %v", srcs)
}
// Now, get a map of [src]:[files rooted at src]
srcMap, err := SourcesToFilesMap(srcs, root)
Expand All @@ -63,8 +65,8 @@ func ResolveSources(srcsAndDest instructions.SourcesAndDest, root, cwd string) (
return srcMap, IsSrcsValid(srcsAndDest, srcMap)
}

// matchSources returns a map of [src]:[matching filepaths], used to resolve wildcards
func matchSources(srcs, files []string, cwd string) ([]string, error) {
// matchSources returns a list of sources that match wildcards
func matchSources(srcs, files []string) ([]string, error) {
var matchedSources []string
for _, src := range srcs {
src = filepath.Clean(src)
Expand All @@ -73,12 +75,7 @@ func matchSources(srcs, files []string, cwd string) ([]string, error) {
if err != nil {
return nil, err
}
// Check cwd
matchedRoot, err := filepath.Match(filepath.Join(cwd, src), file)
if err != nil {
return nil, err
}
if !(matched || matchedRoot) {
if !matched {
continue
}
matchedSources = append(matchedSources, file)
Expand All @@ -91,13 +88,17 @@ func IsDestDir(path string) bool {
return strings.HasSuffix(path, "/")
}

// RelativeFilepath returns the relative filepath
// If source is a file:
// If dest is a dir, copy it to /cwd/dest/relpath
// If dest is a file, copy directly to /cwd/dest
func IsAbsoluteFilepath(path string) bool {
return strings.HasPrefix(path, "/")
}

// RelativeFilepath returns the relative filepath from the build context to the image filesystem
// If source is a file:
// If dest is a dir, copy it to /dest/relpath
// If dest is a file, copy directly to dest
// If source is a dir:
// Assume dest is also a dir, and copy to /cwd/dest/relpath
// Assume dest is also a dir, and copy to dest/relpath
// If dest is not an absolute filepath, add /cwd to the beginning
func RelativeFilepath(filename, srcName, dest, cwd, buildcontext string) (string, error) {
fi, err := os.Stat(filepath.Join(buildcontext, filename))
if err != nil {
Expand All @@ -115,9 +116,15 @@ func RelativeFilepath(filename, srcName, dest, cwd, buildcontext string) (string
if relPath == "." && !fi.IsDir() {
relPath = filepath.Base(filename)
}
destPath := filepath.Join(cwd, dest, relPath)
destPath := filepath.Join(dest, relPath)
if !IsAbsoluteFilepath(dest) {
destPath = filepath.Join(cwd, destPath)
}
return destPath, nil
}
if IsAbsoluteFilepath(dest) {
return dest, nil
}
return filepath.Join(cwd, dest), nil
}

Expand All @@ -126,7 +133,7 @@ func SourcesToFilesMap(srcs []string, root string) (map[string][]string, error)
srcMap := make(map[string][]string)
for _, src := range srcs {
src = filepath.Clean(src)
files, err := Files(src, root)
files, err := RelativeFiles(src, root)
if err != nil {
return nil, err
}
Expand Down
11 changes: 3 additions & 8 deletions pkg/util/command_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var relativeFilepathTests = []struct {
filename: "context/empty",
cwd: "/dir",
dest: "/empty",
expectedFilepath: "/dir/empty",
expectedFilepath: "/empty",
},
{
srcName: "./",
Expand Down Expand Up @@ -121,7 +121,6 @@ func Test_RelativeFilepath(t *testing.T) {
var matchSourcesTests = []struct {
srcs []string
files []string
cwd string
expectedFiles []string
}{
{
Expand All @@ -135,18 +134,16 @@ var matchSourcesTests = []struct {
"pkg/b/d/",
"dir/",
},
cwd: "/",
expectedFiles: []string{
"pkg/a",
"pkg/b",
"/pkg/d",
},
},
}

func Test_MatchSources(t *testing.T) {
for _, test := range matchSourcesTests {
actualFiles, err := matchSources(test.srcs, test.files, test.cwd)
actualFiles, err := matchSources(test.srcs, test.files)
sort.Strings(actualFiles)
sort.Strings(test.expectedFiles)
testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedFiles, actualFiles)
Expand Down Expand Up @@ -253,7 +250,6 @@ func Test_IsSrcsValid(t *testing.T) {

var testResolveSources = []struct {
srcsAndDest []string
cwd string
expectedMap map[string][]string
}{
{
Expand All @@ -262,7 +258,6 @@ var testResolveSources = []struct {
"context/b*",
"dest/",
},
cwd: "/",
expectedMap: map[string][]string{
"context/foo": {
"context/foo",
Expand All @@ -280,7 +275,7 @@ var testResolveSources = []struct {

func Test_ResolveSources(t *testing.T) {
for _, test := range testResolveSources {
actualMap, err := ResolveSources(test.srcsAndDest, buildContextPath, test.cwd)
actualMap, err := ResolveSources(test.srcsAndDest, buildContextPath)
testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedMap, actualMap)
}
}
6 changes: 3 additions & 3 deletions pkg/util/fs_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func fileSystemWhitelist(path string) ([]string, error) {
return whitelist, nil
}

// Files returns a list of all files at the filepath relative to root
func Files(fp string, root string) ([]string, error) {
// RelativeFiles returns a list of all files at the filepath relative to root
func RelativeFiles(fp string, root string) ([]string, error) {
var files []string
fullPath := filepath.Join(root, fp)
logrus.Debugf("Getting files and contents at root %s", fullPath)
Expand All @@ -112,7 +112,7 @@ func Files(fp string, root string) ([]string, error) {
return err
}
files = append(files, relPath)
return err
return nil
})
return files, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/fs_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ var tests = []struct {
},
}

func Test_Files(t *testing.T) {
func Test_RelativeFiles(t *testing.T) {
for _, test := range tests {
testDir, err := ioutil.TempDir("", "")
if err != nil {
Expand All @@ -124,7 +124,7 @@ func Test_Files(t *testing.T) {
if err := testutil.SetupFiles(testDir, test.files); err != nil {
t.Fatalf("err setting up files: %v", err)
}
actualFiles, err := Files(test.directory, testDir)
actualFiles, err := RelativeFiles(test.directory, testDir)
sort.Strings(actualFiles)
sort.Strings(test.expectedFiles)
testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedFiles, actualFiles)
Expand Down

0 comments on commit 0bb35a9

Please sign in to comment.