Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 042906f

Browse files
authored
Merge pull request #334 from vdemeester/lookup-tests
Add tests to lookup package 👼
2 parents c92c70e + 397668b commit 042906f

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

docker/convert_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestParseCommand(t *testing.T) {
2121
func TestParseBindsAndVolumes(t *testing.T) {
2222
ctx := &Context{}
2323
ctx.ComposeFiles = []string{"foo/docker-compose.yml"}
24-
ctx.ResourceLookup = &lookup.FileConfigLookup{}
24+
ctx.ResourceLookup = &lookup.FileResourceLookup{}
2525

2626
abs, err := filepath.Abs(".")
2727
assert.Nil(t, err)
@@ -36,7 +36,7 @@ func TestParseBindsAndVolumes(t *testing.T) {
3636
func TestParseLabels(t *testing.T) {
3737
ctx := &Context{}
3838
ctx.ComposeFiles = []string{"foo/docker-compose.yml"}
39-
ctx.ResourceLookup = &lookup.FileConfigLookup{}
39+
ctx.ResourceLookup = &lookup.FileResourceLookup{}
4040
bashCmd := "bash"
4141
fooLabel := "foo.label"
4242
fooLabelValue := "service.config.value"

docker/project.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const ComposeVersion = "1.5.0"
2323
// NewProject creates a Project with the specified context.
2424
func NewProject(context *Context, parseOptions *config.ParseOptions) (project.APIProject, error) {
2525
if context.ResourceLookup == nil {
26-
context.ResourceLookup = &lookup.FileConfigLookup{}
26+
context.ResourceLookup = &lookup.FileResourceLookup{}
2727
}
2828

2929
if context.EnvironmentLookup == nil {

lookup/file.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func relativePath(file, relativeTo string) string {
2020
// stdin: return the current working directory if possible.
2121
if relativeTo == "-" {
2222
if cwd, err := os.Getwd(); err == nil {
23-
return cwd
23+
return filepath.Join(cwd, file)
2424
}
2525
}
2626

@@ -39,15 +39,15 @@ func relativePath(file, relativeTo string) string {
3939
return abs
4040
}
4141

42-
// FileConfigLookup is a "bare" structure that implements the project.ResourceLookup interface
43-
type FileConfigLookup struct {
42+
// FileResourceLookup is a "bare" structure that implements the project.ResourceLookup interface
43+
type FileResourceLookup struct {
4444
}
4545

4646
// Lookup returns the content and the actual filename of the file that is "built" using the
4747
// specified file and relativeTo string. file and relativeTo are supposed to be file path.
4848
// If file starts with a slash ('/'), it tries to load it, otherwise it will build a
4949
// filename using the folder part of relativeTo joined with file.
50-
func (f *FileConfigLookup) Lookup(file, relativeTo string) ([]byte, string, error) {
50+
func (f *FileResourceLookup) Lookup(file, relativeTo string) ([]byte, string, error) {
5151
file = relativePath(file, relativeTo)
5252
logrus.Debugf("Reading file %s", file)
5353
bytes, err := ioutil.ReadFile(file)
@@ -56,11 +56,11 @@ func (f *FileConfigLookup) Lookup(file, relativeTo string) ([]byte, string, erro
5656

5757
// ResolvePath returns the path to be used for the given path volume. This
5858
// function already takes care of relative paths.
59-
func (f *FileConfigLookup) ResolvePath(path, inFile string) string {
59+
func (f *FileResourceLookup) ResolvePath(path, relativeTo string) string {
6060
vs := strings.SplitN(path, ":", 2)
6161
if len(vs) != 2 || filepath.IsAbs(vs[0]) {
6262
return path
6363
}
64-
vs[0] = relativePath(vs[0], inFile)
64+
vs[0] = relativePath(vs[0], relativeTo)
6565
return strings.Join(vs, ":")
6666
}

lookup/file_test.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package lookup
33
import (
44
"fmt"
55
"io/ioutil"
6+
"os"
67
"path/filepath"
78
"testing"
89
)
@@ -27,7 +28,7 @@ func TestLookupError(t *testing.T) {
2728
input{"does/not/exists/file", "/tmp/"}: "open /tmp/does/not/exists/file: no such file or directory",
2829
}
2930

30-
fileConfigLookup := FileConfigLookup{}
31+
fileConfigLookup := FileResourceLookup{}
3132

3233
for invalid, expectedError := range invalids {
3334
_, _, err := fileConfigLookup.Lookup(invalid.file, invalid.relativeTo)
@@ -51,7 +52,7 @@ func TestLookupOK(t *testing.T) {
5152
t.Fatal(err)
5253
}
5354

54-
fileConfigLookup := FileConfigLookup{}
55+
fileConfigLookup := FileResourceLookup{}
5556

5657
valids := map[input]string{
5758
input{"file1", tmpFolder + "/"}: "content1",
@@ -68,3 +69,34 @@ func TestLookupOK(t *testing.T) {
6869
}
6970
}
7071
}
72+
73+
func TestResolvePath(t *testing.T) {
74+
cwd, err := os.Getwd()
75+
if err != nil {
76+
t.Fatal(err)
77+
}
78+
79+
cases := []struct {
80+
path string
81+
relativeTo string
82+
expected string
83+
}{
84+
{"../path:something", "./docker-compose.yml", filepath.Join(cwd, "../path") + ":something"},
85+
{"../path:something", "docker-compose.yml", filepath.Join(cwd, "../path") + ":something"},
86+
{"../path:something", "/tmp/docker-compose.yml", "/path:something"},
87+
{"path:something", "/tmp/docker-compose.yml", "/tmp/path:something"},
88+
{"/path:something", "/tmp/docker-compose.yml", "/path:something"},
89+
{"path:something", "-", filepath.Join(cwd, "path") + ":something"},
90+
{"path/:something", "-", filepath.Join(cwd, "path") + ":something"},
91+
{"../path:something", "-", filepath.Join(cwd, "../path") + ":something"},
92+
}
93+
94+
fileConfigLookup := FileResourceLookup{}
95+
96+
for index, c := range cases {
97+
actual := fileConfigLookup.ResolvePath(c.path, c.relativeTo)
98+
if actual != c.expected {
99+
t.Errorf("Expected %s, got %s for case %d", c.expected, actual, index)
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)