From 397668bb9790b718654faeb3551a5dd1da4afa7c Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Wed, 13 Jul 2016 22:08:58 +0200 Subject: [PATCH] Add tests to lookup package - Refactored a little bit (naming) - Add some tests and fixing tiny bug Signed-off-by: Vincent Demeester --- docker/convert_test.go | 4 ++-- docker/project.go | 2 +- lookup/file.go | 12 ++++++------ lookup/file_test.go | 36 ++++++++++++++++++++++++++++++++++-- 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/docker/convert_test.go b/docker/convert_test.go index 0a782d112..e3442e06b 100644 --- a/docker/convert_test.go +++ b/docker/convert_test.go @@ -21,7 +21,7 @@ func TestParseCommand(t *testing.T) { func TestParseBindsAndVolumes(t *testing.T) { ctx := &Context{} ctx.ComposeFiles = []string{"foo/docker-compose.yml"} - ctx.ResourceLookup = &lookup.FileConfigLookup{} + ctx.ResourceLookup = &lookup.FileResourceLookup{} abs, err := filepath.Abs(".") assert.Nil(t, err) @@ -36,7 +36,7 @@ func TestParseBindsAndVolumes(t *testing.T) { func TestParseLabels(t *testing.T) { ctx := &Context{} ctx.ComposeFiles = []string{"foo/docker-compose.yml"} - ctx.ResourceLookup = &lookup.FileConfigLookup{} + ctx.ResourceLookup = &lookup.FileResourceLookup{} bashCmd := "bash" fooLabel := "foo.label" fooLabelValue := "service.config.value" diff --git a/docker/project.go b/docker/project.go index 5b5b95383..b356c436d 100644 --- a/docker/project.go +++ b/docker/project.go @@ -23,7 +23,7 @@ const ComposeVersion = "1.5.0" // NewProject creates a Project with the specified context. func NewProject(context *Context, parseOptions *config.ParseOptions) (project.APIProject, error) { if context.ResourceLookup == nil { - context.ResourceLookup = &lookup.FileConfigLookup{} + context.ResourceLookup = &lookup.FileResourceLookup{} } if context.EnvironmentLookup == nil { diff --git a/lookup/file.go b/lookup/file.go index 70b3d8d60..643592d79 100644 --- a/lookup/file.go +++ b/lookup/file.go @@ -20,7 +20,7 @@ func relativePath(file, relativeTo string) string { // stdin: return the current working directory if possible. if relativeTo == "-" { if cwd, err := os.Getwd(); err == nil { - return cwd + return filepath.Join(cwd, file) } } @@ -39,15 +39,15 @@ func relativePath(file, relativeTo string) string { return abs } -// FileConfigLookup is a "bare" structure that implements the project.ResourceLookup interface -type FileConfigLookup struct { +// FileResourceLookup is a "bare" structure that implements the project.ResourceLookup interface +type FileResourceLookup struct { } // Lookup returns the content and the actual filename of the file that is "built" using the // specified file and relativeTo string. file and relativeTo are supposed to be file path. // If file starts with a slash ('/'), it tries to load it, otherwise it will build a // filename using the folder part of relativeTo joined with file. -func (f *FileConfigLookup) Lookup(file, relativeTo string) ([]byte, string, error) { +func (f *FileResourceLookup) Lookup(file, relativeTo string) ([]byte, string, error) { file = relativePath(file, relativeTo) logrus.Debugf("Reading file %s", file) bytes, err := ioutil.ReadFile(file) @@ -56,11 +56,11 @@ func (f *FileConfigLookup) Lookup(file, relativeTo string) ([]byte, string, erro // ResolvePath returns the path to be used for the given path volume. This // function already takes care of relative paths. -func (f *FileConfigLookup) ResolvePath(path, inFile string) string { +func (f *FileResourceLookup) ResolvePath(path, relativeTo string) string { vs := strings.SplitN(path, ":", 2) if len(vs) != 2 || filepath.IsAbs(vs[0]) { return path } - vs[0] = relativePath(vs[0], inFile) + vs[0] = relativePath(vs[0], relativeTo) return strings.Join(vs, ":") } diff --git a/lookup/file_test.go b/lookup/file_test.go index 7078dea80..75f221d6f 100644 --- a/lookup/file_test.go +++ b/lookup/file_test.go @@ -3,6 +3,7 @@ package lookup import ( "fmt" "io/ioutil" + "os" "path/filepath" "testing" ) @@ -27,7 +28,7 @@ func TestLookupError(t *testing.T) { input{"does/not/exists/file", "/tmp/"}: "open /tmp/does/not/exists/file: no such file or directory", } - fileConfigLookup := FileConfigLookup{} + fileConfigLookup := FileResourceLookup{} for invalid, expectedError := range invalids { _, _, err := fileConfigLookup.Lookup(invalid.file, invalid.relativeTo) @@ -51,7 +52,7 @@ func TestLookupOK(t *testing.T) { t.Fatal(err) } - fileConfigLookup := FileConfigLookup{} + fileConfigLookup := FileResourceLookup{} valids := map[input]string{ input{"file1", tmpFolder + "/"}: "content1", @@ -68,3 +69,34 @@ func TestLookupOK(t *testing.T) { } } } + +func TestResolvePath(t *testing.T) { + cwd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + + cases := []struct { + path string + relativeTo string + expected string + }{ + {"../path:something", "./docker-compose.yml", filepath.Join(cwd, "../path") + ":something"}, + {"../path:something", "docker-compose.yml", filepath.Join(cwd, "../path") + ":something"}, + {"../path:something", "/tmp/docker-compose.yml", "/path:something"}, + {"path:something", "/tmp/docker-compose.yml", "/tmp/path:something"}, + {"/path:something", "/tmp/docker-compose.yml", "/path:something"}, + {"path:something", "-", filepath.Join(cwd, "path") + ":something"}, + {"path/:something", "-", filepath.Join(cwd, "path") + ":something"}, + {"../path:something", "-", filepath.Join(cwd, "../path") + ":something"}, + } + + fileConfigLookup := FileResourceLookup{} + + for index, c := range cases { + actual := fileConfigLookup.ResolvePath(c.path, c.relativeTo) + if actual != c.expected { + t.Errorf("Expected %s, got %s for case %d", c.expected, actual, index) + } + } +}