buildkit/build_test.go
buildkit/build_test.goBrowse 1970 files
1,475 tokens
4,927 bytes
Token encoding: o200k_base
Snapshot 21a254f
← Back to SKILL.md
1package buildkit2 3import (4 "maps"5 "path/filepath"6 "testing"7 8 "github.com/moby/buildkit/client"9 "github.com/stretchr/testify/require"10)11 12func TestGetImageName(t *testing.T) {13 tests := []struct {14 name string15 appDir string16 expected string17 }{18 {19 name: "lowercase directory name",20 appDir: "/path/to/myapp",21 expected: "myapp",22 },23 {24 name: "uppercase directory name is lowercased",25 appDir: "/path/to/SteelMC",26 expected: "steelmc",27 },28 {29 name: "mixed case directory name is lowercased",30 appDir: "/path/to/MyApp",31 expected: "myapp",32 },33 {34 name: "directory name with hyphens",35 appDir: "/path/to/my-app",36 expected: "my-app",37 },38 {39 name: "empty path returns fallback",40 appDir: "",41 expected: "railpack-app",42 },43 {44 name: "path ending with separator",45 appDir: "/path/to/myapp" + string(filepath.Separator),46 expected: "railpack-app",47 },48 }49 50 for _, tt := range tests {51 t.Run(tt.name, func(t *testing.T) {52 got := getImageName(tt.appDir)53 if got != tt.expected {54 t.Errorf("getImageName(%q) = %q, want %q", tt.appDir, got, tt.expected)55 }56 })57 }58}59 60func TestExtractCacheType(t *testing.T) {61 tests := []struct {62 name string63 input string64 expectedType string65 expectedAttrs map[string]string66 }{67 {68 name: "registry cache type is extracted",69 input: "type=registry,ref=localhost:5555/cache",70 expectedType: "registry",71 expectedAttrs: map[string]string{72 "ref": "localhost:5555/cache",73 },74 },75 {76 name: "mode max kept in attrs",77 input: "type=registry,ref=host.docker.internal:7890/node-bun:cache,mode=max",78 expectedType: "registry",79 expectedAttrs: map[string]string{80 "ref": "host.docker.internal:7890/node-bun:cache",81 "mode": "max",82 },83 },84 {85 name: "gha shape",86 input: "type=gha,scope=my-cache",87 expectedType: "gha",88 expectedAttrs: map[string]string{89 "scope": "my-cache",90 },91 },92 {93 name: "type only",94 input: "type=registry",95 expectedType: "registry",96 expectedAttrs: map[string]string{},97 },98 {99 name: "spaces around keys and values",100 input: " type = registry , ref = myapp:cache ",101 expectedType: "registry",102 expectedAttrs: map[string]string{103 "ref": "myapp:cache",104 },105 },106 {107 name: "missing type is not defaulted",108 input: "scope=my-cache,mode=max",109 expectedType: "",110 expectedAttrs: map[string]string{111 "scope": "my-cache",112 "mode": "max",113 },114 },115 {116 name: "empty attrs",117 input: "",118 expectedType: "",119 expectedAttrs: map[string]string{},120 },121 {122 name: "skips segments without equals",123 input: "type=registry,notavalue,ref=x",124 expectedType: "registry",125 expectedAttrs: map[string]string{126 "ref": "x",127 },128 },129 {130 name: "value may contain equals",131 input: "type=registry,ref=image:tag=latest",132 expectedType: "registry",133 expectedAttrs: map[string]string{134 "ref": "image:tag=latest",135 },136 },137 }138 139 for _, tt := range tests {140 t.Run(tt.name, func(t *testing.T) {141 gotType, gotAttrs := extractCacheType(parseKeyValue(tt.input))142 if gotType != tt.expectedType {143 t.Errorf("extractCacheType(%q) type = %q, want %q", tt.input, gotType, tt.expectedType)144 }145 if !maps.Equal(gotAttrs, tt.expectedAttrs) {146 t.Errorf("extractCacheType(%q) attrs = %v, want %v", tt.input, gotAttrs, tt.expectedAttrs)147 }148 })149 }150}151 152func TestCacheEntriesFromFlags(t *testing.T) {153 t.Run("nil slice", func(t *testing.T) {154 require.Nil(t, cacheEntriesFromFlags(nil))155 })156 157 t.Run("empty slice", func(t *testing.T) {158 require.Nil(t, cacheEntriesFromFlags([]string{}))159 })160 161 t.Run("skips empty strings", func(t *testing.T) {162 got := cacheEntriesFromFlags([]string{"", "type=registry,ref=x", ""})163 require.Equal(t, []client.CacheOptionsEntry{164 {Type: "registry", Attrs: map[string]string{"ref": "x"}},165 }, got)166 })167 168 t.Run("multiple registry entries", func(t *testing.T) {169 got := cacheEntriesFromFlags([]string{170 "type=registry,ref=myapp:cache-main",171 "type=registry,ref=myapp:cache-branch",172 })173 require.Equal(t, []client.CacheOptionsEntry{174 {Type: "registry", Attrs: map[string]string{"ref": "myapp:cache-main"}},175 {Type: "registry", Attrs: map[string]string{"ref": "myapp:cache-branch"}},176 }, got)177 })178 179 t.Run("mixed registry and gha with mode", func(t *testing.T) {180 got := cacheEntriesFromFlags([]string{181 "type=registry,ref=host.docker.internal:7890/node-bun:cache,mode=max",182 "type=gha,scope=ci",183 })184 require.Equal(t, []client.CacheOptionsEntry{185 {186 Type: "registry",187 Attrs: map[string]string{188 "ref": "host.docker.internal:7890/node-bun:cache",189 "mode": "max",190 },191 },192 {Type: "gha", Attrs: map[string]string{"scope": "ci"}},193 }, got)194 })195}196