@@ -20,6 +20,7 @@ import (
2020 "context"
2121 "os"
2222 "path/filepath"
23+ "runtime"
2324 "testing"
2425
2526 "github.com/compose-spec/compose-go/v2/types"
@@ -162,6 +163,67 @@ services:
162163
163164}
164165
166+ func TestIncludeWithProjectDirectory (t * testing.T ) {
167+ var envs map [string ]string
168+ if runtime .GOOS == "windows" {
169+ envs = map [string ]string {"COMPOSE_CONVERT_WINDOWS_PATHS" : "1" }
170+ }
171+ p , err := LoadWithContext (context .Background (), types.ConfigDetails {
172+ WorkingDir : "testdata/include" ,
173+ Environment : envs ,
174+ ConfigFiles : []types.ConfigFile {
175+ {
176+ Filename : "testdata/include/project-directory.yaml" ,
177+ },
178+ },
179+ }, withProjectName ("test-load-project-directory" , true ))
180+ assert .NilError (t , err )
181+ assert .Equal (t , filepath .ToSlash (p .Services ["service" ].Build .Context ), "testdata/subdir" )
182+ assert .Equal (t , filepath .ToSlash (p .Services ["service" ].Volumes [0 ].Source ), "testdata/subdir/compose-test-extends-imported.yaml" )
183+ assert .Equal (t , filepath .ToSlash (p .Services ["service" ].EnvFiles [0 ].Path ), "testdata/subdir/extra.env" )
184+
185+ }
186+
187+ func TestNestedIncludeAndExtends (t * testing.T ) {
188+ fileName := "compose.yml"
189+ yaml := `
190+ include:
191+ - project_directory: .
192+ path: dir/included.yaml
193+ `
194+ tmpdir := t .TempDir ()
195+ path := createFile (t , tmpdir , yaml , fileName )
196+
197+ yaml = `
198+ services:
199+ included:
200+ extends:
201+ file: dir/extended.yaml
202+ service: extended
203+ `
204+ createFileSubDir (t , tmpdir , "dir" , yaml , "included.yaml" )
205+
206+ yaml = `
207+ services:
208+ extended:
209+ image: alpine
210+ `
211+ createFile (t , filepath .Join (tmpdir , "dir" ), yaml , "extended.yaml" )
212+ p , err := Load (types.ConfigDetails {
213+ WorkingDir : tmpdir ,
214+ ConfigFiles : []types.ConfigFile {{
215+ Filename : path ,
216+ }},
217+ Environment : nil ,
218+ }, func (options * Options ) {
219+ options .SkipNormalization = true
220+ options .ResolvePaths = true
221+ options .SetProjectName ("project" , true )
222+ })
223+ assert .NilError (t , err )
224+ assert .Equal (t , p .Services ["included" ].Image , "alpine" )
225+ }
226+
165227func createFile (t * testing.T , rootDir , content , fileName string ) string {
166228 path := filepath .Join (rootDir , fileName )
167229 assert .NilError (t , os .WriteFile (path , []byte (content ), 0o600 ))
0 commit comments