mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-08-02 00:28:04 +08:00
bake: use default filenames from compose-go
Signed-off-by: Nick Sieger <nick@nicksieger.com>
This commit is contained in:
11
bake/bake.go
11
bake/bake.go
@@ -12,11 +12,13 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
composecli "github.com/compose-spec/compose-go/cli"
|
||||
"github.com/docker/buildx/bake/hclparser"
|
||||
"github.com/docker/buildx/build"
|
||||
controllerapi "github.com/docker/buildx/controller/pb"
|
||||
"github.com/docker/buildx/util/buildflags"
|
||||
"github.com/docker/buildx/util/platformutil"
|
||||
|
||||
"github.com/docker/cli/cli/config"
|
||||
hcl "github.com/hashicorp/hcl/v2"
|
||||
"github.com/moby/buildkit/client/llb"
|
||||
@@ -42,14 +44,15 @@ type Override struct {
|
||||
}
|
||||
|
||||
func defaultFilenames() []string {
|
||||
return []string{
|
||||
"docker-compose.yml", // support app
|
||||
"docker-compose.yaml", // support app
|
||||
names := []string{}
|
||||
names = append(names, composecli.DefaultFileNames...)
|
||||
names = append(names, []string{
|
||||
"docker-bake.json",
|
||||
"docker-bake.override.json",
|
||||
"docker-bake.hcl",
|
||||
"docker-bake.override.hcl",
|
||||
}
|
||||
}...)
|
||||
return names
|
||||
}
|
||||
|
||||
func ReadLocalFiles(names []string) ([]File, error) {
|
||||
|
@@ -2,6 +2,7 @@ package bake
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -1363,3 +1364,56 @@ func TestJSONNullVars(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, map[string]*string{"bar": ptrstr("baz")}, m["default"].Args)
|
||||
}
|
||||
|
||||
func TestReadLocalFilesDefault(t *testing.T) {
|
||||
tests := []struct {
|
||||
filenames []string
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
filenames: []string{"abc.yml", "docker-compose.yml"},
|
||||
expected: []string{"docker-compose.yml"},
|
||||
},
|
||||
{
|
||||
filenames: []string{"test.foo", "compose.yml", "docker-bake.hcl"},
|
||||
expected: []string{"compose.yml", "docker-bake.hcl"},
|
||||
},
|
||||
{
|
||||
filenames: []string{"compose.yaml", "docker-compose.yml", "docker-bake.hcl"},
|
||||
expected: []string{"compose.yaml", "docker-compose.yml", "docker-bake.hcl"},
|
||||
},
|
||||
{
|
||||
filenames: []string{"test.txt", "compsoe.yaml"}, // intentional misspell
|
||||
expected: []string{},
|
||||
},
|
||||
}
|
||||
pwd, err := os.Getwd()
|
||||
require.NoError(t, err)
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(strings.Join(tt.filenames, "-"), func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
t.Cleanup(func() { _ = os.Chdir(pwd) })
|
||||
require.NoError(t, os.Chdir(dir))
|
||||
for _, tf := range tt.filenames {
|
||||
require.NoError(t, os.WriteFile(tf, []byte(tf), 0644))
|
||||
}
|
||||
files, err := ReadLocalFiles(nil)
|
||||
require.NoError(t, err)
|
||||
if len(files) == 0 {
|
||||
require.Equal(t, len(tt.expected), len(files))
|
||||
} else {
|
||||
found := false
|
||||
for _, exp := range tt.expected {
|
||||
for _, f := range files {
|
||||
if f.Name == exp {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
require.True(t, found, exp)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user