core/config/config_test.go
core/config/config_test.goBrowse 1970 files
2,700 tokens
8,043 bytes
Token encoding: o200k_base
Snapshot 21a254f
← Back to SKILL.md
1package config2 3import (4 "encoding/json"5 "testing"6 7 "github.com/google/go-cmp/cmp"8 "github.com/stretchr/testify/require"9)10 11func TestEmptyConfig(t *testing.T) {12 config := EmptyConfig()13 require.NotNil(t, config)14 require.Empty(t, config.Caches)15 require.Empty(t, config.Packages)16 require.Empty(t, config.BuildAptPackages)17 require.Empty(t, config.Steps)18 require.Nil(t, config.Provider)19 20 require.NotNil(t, config.Deploy)21 require.Nil(t, config.Deploy.Inputs)22 require.Nil(t, config.Deploy.Paths)23}24 25func TestMergeConfigSmall(t *testing.T) {26 config1JSON := `{27 "baseImage": "ubuntu:20.04",28 "packages": {29 "python": "latest",30 "node": "22"31 },32 "aptPackages": ["git"],33 "steps": {34 "install": {35 "dependsOn": ["packages"],36 "commands": [37 "echo first"38 ],39 "variables": {40 "HELLO": "world"41 },42 "caches": ["pip"]43 }44 },45 "deploy": {46 "variables": {47 "SHARED": "one",48 "HELLO": "world"49 }50 }51 }`52 53 config2JSON := `{54 "baseImage": "secondd",55 "packages": {56 "node": "23",57 "bun": "latest"58 },59 "steps": {60 "install": {61 "variables": {62 "another": "boop"63 }64 }65 },66 "deploy": {67 "variables": {68 "SHARED": "two",69 "FOO": "bar"70 }71 }72 }`73 74 expectedJSON := `{75 "baseImage": "secondd",76 "packages": {77 "python": "latest",78 "node": "23",79 "bun": "latest"80 },81 "aptPackages": ["git"],82 "steps": {83 "install": {84 "dependsOn": ["packages"],85 "commands": [86 "echo first"87 ],88 "variables": {89 "HELLO": "world",90 "another": "boop"91 },92 "caches": ["pip"]93 }94 },95 "deploy": {96 "variables": {97 "HELLO": "world",98 "SHARED": "two",99 "FOO": "bar"100 }101 },102 "caches": {}103 }`104 105 var config1, config2, expected Config106 require.NoError(t, json.Unmarshal([]byte(config1JSON), &config1))107 require.NoError(t, json.Unmarshal([]byte(config2JSON), &config2))108 require.NoError(t, json.Unmarshal([]byte(expectedJSON), &expected))109 110 result := Merge(&config1, &config2)111 112 if diff := cmp.Diff(expected, *result); diff != "" {113 t.Errorf("configs mismatch (-want +got):\n%s", diff)114 }115 116}117 118func TestMergeConfig(t *testing.T) {119 config1JSON := `{120 "baseImage": "ubuntu:20.04",121 "packages": {122 "python": "latest",123 "node": "22"124 },125 "aptPackages": ["git"],126 "caches": {127 "npm": {128 "directory": "/root/.npm",129 "type": "locked"130 },131 "pip": {132 "directory": "/root/.cache/pip"133 }134 },135 "secrets": ["SECRET_1", "API_KEY"],136 "steps": {137 "install": {138 "name": "install",139 "secrets": ["*"],140 "assets": {141 "package.json": "content1",142 "requirements.txt": "content2"143 },144 "commands": [145 {"type": "exec", "cmd": "npm install", "caches": ["npm"], "customName": "Install NPM deps"},146 {"type": "path", "path": "/usr/local/bin"},147 {"type": "variable", "name": "NODE_ENV", "value": "production"},148 {"type": "copy", "src": "/src", "dest": "/app", "image": "alpine:latest"},149 {"type": "file", "path": "/app", "name": "config.json", "mode": 384, "customName": "Write config"}150 ]151 },152 "build": {153 "name": "build",154 "commands": [155 {"type": "exec", "cmd": "config 1 a"},156 {"type": "exec", "cmd": "config 1 b"}157 ]158 }159 },160 "deploy": {161 "startCommand": "python app.py"162 }163 }`164 165 config2JSON := `{166 "providers": ["node"],167 "baseImage": "ubuntu:22.04",168 "packages": {169 "node": "23"170 },171 "aptPackages": ["curl"],172 "caches": {173 "npm": {174 "directory": "/root/.npm-new",175 "type": "shared"176 },177 "go": {178 "directory": "/root/.cache/go-build"179 }180 },181 "secrets": ["SECRET_2"],182 "steps": {183 "install": {184 "name": "install",185 "secrets": ["*"],186 "assets": {187 "package.json": "content3"188 }189 },190 "build": {191 "name": "build",192 "secrets": [],193 "commands": [194 {"type": "exec", "cmd": "config 2 a"},195 {"type": "exec", "cmd": "config 2 b"}196 ]197 }198 },199 "deploy": {200 "aptPackages": ["curl"],201 "startCommand": "node server.js",202 "paths": ["/usr/local/bin", "/app/bin"]203 }204 }`205 206 expectedJSON := `{207 "providers": ["node"],208 "baseImage": "ubuntu:22.04",209 "packages": {210 "python": "latest",211 "node": "23"212 },213 "aptPackages": ["curl"],214 "caches": {215 "npm": {216 "directory": "/root/.npm-new",217 "type": "shared"218 },219 "go": {220 "directory": "/root/.cache/go-build"221 },222 "pip": {223 "directory": "/root/.cache/pip"224 }225 },226 "secrets": ["SECRET_2"],227 "steps": {228 "install": {229 "name": "install",230 "secrets": ["*"],231 "assets": {232 "package.json": "content3",233 "requirements.txt": "content2"234 },235 "commands": [236 {"type": "exec", "cmd": "npm install", "caches": ["npm"], "customName": "Install NPM deps"},237 {"type": "path", "path": "/usr/local/bin"},238 {"type": "variable", "name": "NODE_ENV", "value": "production"},239 {"type": "copy", "src": "/src", "dest": "/app", "image": "alpine:latest"},240 {"type": "file", "path": "/app", "name": "config.json", "mode": 384, "customName": "Write config"}241 ]242 },243 "build": {244 "name": "build",245 "secrets": [],246 "commands": [247 {"type": "exec", "cmd": "config 2 a"},248 {"type": "exec", "cmd": "config 2 b"}249 ]250 }251 },252 "deploy": {253 "aptPackages": ["curl"],254 "startCommand": "node server.js",255 "paths": ["/usr/local/bin", "/app/bin"]256 }257 }`258 259 var config1, config2, expected Config260 require.NoError(t, json.Unmarshal([]byte(config1JSON), &config1))261 require.NoError(t, json.Unmarshal([]byte(config2JSON), &config2))262 require.NoError(t, json.Unmarshal([]byte(expectedJSON), &expected))263 264 result := Merge(&config1, &config2)265 266 if diff := cmp.Diff(expected, *result); diff != "" {267 t.Errorf("configs mismatch (-want +got):\n%s", diff)268 }269}270 271func TestMergeConfigStart(t *testing.T) {272 config1JSON := `{273 "deploy": {274 "startCommand": "python app.py"275 }276 }`277 278 config2JSON := `{279 "packages": {280 "node": "23"281 }282 }`283 284 expectedJSON := `{285 "packages": {286 "node": "23"287 },288 "deploy": {289 "startCommand": "python app.py"290 },291 "steps": {},292 "caches": {}293 }`294 295 var config1, config2, expected Config296 require.NoError(t, json.Unmarshal([]byte(config1JSON), &config1))297 require.NoError(t, json.Unmarshal([]byte(config2JSON), &config2))298 require.NoError(t, json.Unmarshal([]byte(expectedJSON), &expected))299 300 result := Merge(&config1, &config2)301 302 if diff := cmp.Diff(expected, *result); diff != "" {303 t.Errorf("configs mismatch (-want +got):\n%s", diff)304 }305}306 307func TestGetJsonSchema(t *testing.T) {308 schema := GetJsonSchema()309 require.NotEmpty(t, schema)310 311 require.NotContains(t, schema.Required, "provider")312 providerSchema, ok := schema.Properties.Get("provider")313 require.True(t, ok)314 require.ElementsMatch(t, []any{315 "php",316 "golang",317 "java",318 "rust",319 "ruby",320 "elixir",321 "python",322 "deno",323 "dotnet",324 "node",325 "gleam",326 "cpp",327 "staticfile",328 "shell",329 }, providerSchema.Enum)330 331 schemaJson, err := json.MarshalIndent(schema, "", " ")332 require.NoError(t, err)333 require.NotEmpty(t, schemaJson)334}335 336func TestConfigExcludeInclude(t *testing.T) {337 configJSON := `{338 "exclude": [339 "node_modules",340 "*.log",341 ".env",342 "!important.log"343 ]344 }`345 346 var config Config347 require.NoError(t, json.Unmarshal([]byte(configJSON), &config))348 349 require.Equal(t, []string{"node_modules", "*.log", ".env", "!important.log"}, config.Exclude)350}351 352func TestMergeConfigExcludeInclude(t *testing.T) {353 config1JSON := `{354 "exclude": ["node_modules", ".env", "!important.log"]355 }`356 357 config2JSON := `{358 "exclude": ["*.tmp", "!keep.tmp"]359 }`360 361 expectedJSON := `{362 "exclude": ["*.tmp", "!keep.tmp"],363 "steps": {},364 "packages": {},365 "caches": {},366 "deploy": {}367 }`368 369 var config1, config2, expected Config370 require.NoError(t, json.Unmarshal([]byte(config1JSON), &config1))371 require.NoError(t, json.Unmarshal([]byte(config2JSON), &config2))372 require.NoError(t, json.Unmarshal([]byte(expectedJSON), &expected))373 374 result := Merge(&config1, &config2)375 376 if diff := cmp.Diff(expected, *result); diff != "" {377 t.Errorf("configs mismatch (-want +got):\n%s", diff)378 }379}380