Skip to content

Commit

Permalink
let resourceloader compute relative working dir
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Feb 29, 2024
1 parent e2eab1f commit 7aaf043
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 31 deletions.
46 changes: 22 additions & 24 deletions loader/include.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,43 +60,41 @@ func ApplyInclude(ctx context.Context, configDetails types.ConfigDetails, model
})
}

var relworkingdir string
for i, p := range r.Path {
for _, loader := range options.ResourceLoaders {
if loader.Accept(p) {
path, err := loader.Load(ctx, p)
if err != nil {
return err
if !loader.Accept(p) {
continue
}
path, err := loader.Load(ctx, p)
if err != nil {
return err
}
p = path

if i == 0 { // This is the "main" file, used to define project-directory. Others are overrides
relworkingdir = loader.Dir(path)
if r.ProjectDirectory == "" {
r.ProjectDirectory = filepath.Dir(path)
}

for _, f := range included {
if f == path {
included = append(included, path)
return fmt.Errorf("include cycle detected:\n%s\n include %s", included[0], strings.Join(included[1:], "\n include "))
}
}
p = path
break
}
}
r.Path[i] = p
}

mainFile := r.Path[0]
for _, f := range included {
if f == mainFile {
included = append(included, mainFile)
return fmt.Errorf("include cycle detected:\n%s\n include %s", included[0], strings.Join(included[1:], "\n include "))
}
}

if r.ProjectDirectory == "" {
r.ProjectDirectory = filepath.Dir(mainFile)
}
relworkingdir, err := filepath.Rel(configDetails.WorkingDir, r.ProjectDirectory)
if err != nil {
// included file path is not inside project working directory => use absolute path
relworkingdir = r.ProjectDirectory
}

loadOptions := options.clone()
loadOptions.ResolvePaths = true
loadOptions.SkipNormalization = true
loadOptions.SkipConsistencyCheck = true
loadOptions.ResourceLoaders = append(loadOptions.RemoteResourceLoaders(), localResourceLoader{
WorkingDir: relworkingdir,
WorkingDir: r.ProjectDirectory,
})

if len(r.EnvFile) == 0 {
Expand Down
25 changes: 21 additions & 4 deletions loader/include_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,21 @@ import (
"gotest.tools/v3/assert"
)

func TestLoadIncludeExtendsCombined(t *testing.T) {
_, err := LoadWithContext(context.Background(), types.ConfigDetails{
WorkingDir: "testdata/combined",
ConfigFiles: []types.ConfigFile{
{
Filename: "testdata/combined/compose.yaml",
},
},
}, withProjectName("test-load-combined", true))
assert.NilError(t, err)
}

func TestLoadWithMultipleInclude(t *testing.T) {
// include same service twice should not trigger an error
p, err := Load(buildConfigDetails(`
details := buildConfigDetails(`
name: 'test-multi-include'
include:
Expand All @@ -41,17 +53,21 @@ services:
image: busybox
depends_on:
- imported
`, map[string]string{"SOURCE": "override"}), func(options *Options) {
`, map[string]string{"SOURCE": "override"})

p, err := Load(details, func(options *Options) {
options.SkipNormalization = true
options.ResolvePaths = true
})
assert.NilError(t, err)
imported, err := p.GetService("imported")
assert.NilError(t, err)
assert.Equal(t, imported.ContainerName, "override")
}

func TestLoadWithMultipleIncludeConflict(t *testing.T) {
// include 2 different services with same name should trigger an error
_, err = Load(buildConfigDetails(`
details := buildConfigDetails(`
name: 'test-multi-include'
include:
Expand All @@ -64,7 +80,8 @@ include:
services:
bar:
image: busybox
`, map[string]string{"SOURCE": "override"}), func(options *Options) {
`, map[string]string{"SOURCE": "override"})
_, err := Load(details, func(options *Options) {
options.SkipNormalization = true
options.ResolvePaths = true
})
Expand Down
5 changes: 2 additions & 3 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,8 @@ func (l localResourceLoader) abs(p string) string {
return filepath.Join(l.WorkingDir, p)
}

func (l localResourceLoader) Accept(p string) bool {
_, err := os.Stat(l.abs(p))
return err == nil
func (l localResourceLoader) Accept(_ string) bool {
return true
}

func (l localResourceLoader) Load(_ context.Context, p string) (string, error) {
Expand Down
4 changes: 4 additions & 0 deletions loader/testdata/combined/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include:
- path:
- dir/included.yaml

4 changes: 4 additions & 0 deletions loader/testdata/combined/dir/extended.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
service:
build: .

6 changes: 6 additions & 0 deletions loader/testdata/combined/dir/included.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
service:
extends:
file: extended.yaml
service: service

0 comments on commit 7aaf043

Please sign in to comment.