Skip to content

Commit

Permalink
set Compose working dir explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
aksiksi committed Sep 23, 2024
1 parent 0a0ba2c commit e523bb1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
6 changes: 6 additions & 0 deletions compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,15 @@ func (g *Generator) Run(ctx context.Context) (*NixContainerConfig, error) {
})
}

rootPath, err := g.GetRootPath()
if err != nil {
return nil, err
}

composeProject, err := loader.LoadWithContext(ctx, types.ConfigDetails{
ConfigFiles: configFiles,
Environment: types.NewMapping(env),
WorkingDir: rootPath,
}, opts...)
if err != nil {
return nil, fmt.Errorf("failed to parse Compose project: %w", err)
Expand Down
20 changes: 18 additions & 2 deletions nix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ func (t *testGetWd) GetWd() (string, error) {
func runSubtestsWithGenerator(t *testing.T, g *Generator) {
t.Helper()
ctx := context.Background()

if g.RootPath == "" && g.GetWorkingDir == nil {
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
g.RootPath = cwd
}

for _, runtime := range []ContainerRuntime{ContainerRuntimeDocker, ContainerRuntimePodman} {
t.Run(runtime.String(), func(t *testing.T) {
testName := strings.ReplaceAll(t.Name(), "/", ".")
Expand Down Expand Up @@ -157,19 +166,26 @@ func TestEnvFilesOnly(t *testing.T) {
runSubtestsWithGenerator(t, g)
}

// TODO(aksiksi): Clean this test up.
func TestIgnoreMissingEnvFiles(t *testing.T) {
ctx := context.Background()
composePath, envFilePath := getPaths(t, true)
g := Generator{
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
g := &Generator{
Runtime: ContainerRuntimeDocker,
RootPath: cwd,
Inputs: []string{composePath},
EnvFiles: []string{path.Join(t.TempDir(), "bad-path"), envFilePath},
IncludeEnvFiles: true,
EnvFilesOnly: true,
IgnoreMissingEnvFiles: true,
}

if _, err := g.Run(ctx); err != nil {
t.Fatal(err)
t.Error(err)
}
}

Expand Down

0 comments on commit e523bb1

Please sign in to comment.