core/app/environment_test.go
core/app/environment_test.goBrowse 1970 files
1,575 tokens
5,327 bytes
Token encoding: o200k_base
Snapshot 21a254f
← Back to SKILL.md
1package app2 3import (4 "testing"5 6 "github.com/stretchr/testify/require"7)8 9func TestFromEnvs(t *testing.T) {10 t.Setenv("INHERITED", "from the process environment")11 t.Setenv("EMPTY", "this must not be inherited")12 13 env, err := FromEnvs([]string{14 "PLAIN=value",15 "EMPTY=",16 "INHERITED",17 "LEADING_EQUALS==value",18 "EQUALS=value=with=equals==",19 "WHITESPACE= leading and trailing ",20 `QUOTES='single' and "double"`,21 `ESCAPES=\n\t\\`,22 "MULTILINE=line 1\nline 2\nline 3",23 `JSON={"key":"value","nested":{"enabled":true}}`,24 "SHELL=$HOME ${USER:-default} # literal comment",25 "UNICODE=héllo 世界 🌍",26 "HELLO+WORLD=boop",27 "DUPLICATE=first",28 "DUPLICATE=second",29 })30 31 require.NoError(t, err)32 require.Equal(t, map[string]string{33 "PLAIN": "value",34 "INHERITED": "from the process environment",35 "LEADING_EQUALS": "=value",36 "EQUALS": "value=with=equals==",37 "WHITESPACE": " leading and trailing ",38 "QUOTES": `'single' and "double"`,39 "ESCAPES": `\n\t\\`,40 "MULTILINE": "line 1\nline 2\nline 3",41 "JSON": `{"key":"value","nested":{"enabled":true}}`,42 "SHELL": "$HOME ${USER:-default} # literal comment",43 "UNICODE": "héllo 世界 🌍",44 "HELLO+WORLD": "boop",45 "DUPLICATE": "second",46 }, env.Variables)47}48 49func TestEnvironmentVariables(t *testing.T) {50 env := NewEnvironment(nil)51 52 require.Empty(t, env.GetVariable("MISSING"))53 env.SetVariable("NAME", "value")54 require.Equal(t, "value", env.GetVariable("NAME"))55 require.Equal(t, "RAILPACK_NAME", env.ConfigVariable("NAME"))56}57 58func TestGetConfigVariable(t *testing.T) {59 envVars := map[string]string{60 "RAILPACK_PACKAGES": " \n node@22 jq@latest \t",61 "RAILPACK_EMPTY": " \n\t ",62 }63 env := NewEnvironment(&envVars)64 65 tests := []struct {66 name string67 variable string68 expectedValue string69 expectedConfigName string70 }{71 {72 name: "trims surrounding whitespace",73 variable: "PACKAGES",74 expectedValue: "node@22 jq@latest",75 expectedConfigName: "RAILPACK_PACKAGES",76 },77 {78 name: "preserves the name of an empty variable",79 variable: "EMPTY",80 expectedValue: "",81 expectedConfigName: "RAILPACK_EMPTY",82 },83 {84 name: "returns empty values for a missing variable",85 variable: "MISSING",86 },87 }88 89 for _, tt := range tests {90 t.Run(tt.name, func(t *testing.T) {91 value, configName := env.GetConfigVariable(tt.variable)92 93 require.Equal(t, tt.expectedValue, value)94 require.Equal(t, tt.expectedConfigName, configName)95 })96 }97}98 99func TestGetConfigVariableList(t *testing.T) {100 envVars := map[string]string{101 "RAILPACK_PACKAGES": " node@22 jq@latest ",102 "RAILPACK_SINGLE": "python",103 "RAILPACK_EMPTY": " ",104 }105 env := NewEnvironment(&envVars)106 107 tests := []struct {108 name string109 variable string110 expectedValues []string111 expectedConfigName string112 }{113 {114 name: "splits a space-separated list",115 variable: "PACKAGES",116 expectedValues: []string{"node@22", "jq@latest"},117 expectedConfigName: "RAILPACK_PACKAGES",118 },119 {120 name: "returns a single item",121 variable: "SINGLE",122 expectedValues: []string{"python"},123 expectedConfigName: "RAILPACK_SINGLE",124 },125 {126 name: "returns no list for an empty variable",127 variable: "EMPTY",128 },129 {130 name: "returns no list for a missing variable",131 variable: "MISSING",132 },133 }134 135 for _, tt := range tests {136 t.Run(tt.name, func(t *testing.T) {137 values, configName := env.GetConfigVariableList(tt.variable)138 139 require.Equal(t, tt.expectedValues, values)140 require.Equal(t, tt.expectedConfigName, configName)141 })142 }143}144 145func TestIsConfigVariableTruthy(t *testing.T) {146 envVars := map[string]string{147 "RAILPACK_TRUE": "true",148 "RAILPACK_TRUE_CASE": " True ",149 "RAILPACK_TRUE_INT": "\n1\t",150 "RAILPACK_FALSE": "false",151 "RAILPACK_FALSE_INT": "0",152 "RAILPACK_OTHER_VALUE": "yes",153 "RAILPACK_EMPTY": " ",154 }155 env := NewEnvironment(&envVars)156 157 tests := []struct {158 name string159 variable string160 expected bool161 }{162 {name: "true", variable: "TRUE", expected: true},163 {name: "mixed case and whitespace", variable: "TRUE_CASE", expected: true},164 {name: "integer true and whitespace", variable: "TRUE_INT", expected: true},165 {name: "false", variable: "FALSE", expected: false},166 {name: "integer false", variable: "FALSE_INT", expected: false},167 {name: "unsupported truthy value", variable: "OTHER_VALUE", expected: false},168 {name: "empty", variable: "EMPTY", expected: false},169 {name: "missing", variable: "MISSING", expected: false},170 }171 172 for _, tt := range tests {173 t.Run(tt.name, func(t *testing.T) {174 require.Equal(t, tt.expected, env.IsConfigVariableTruthy(tt.variable))175 })176 }177}178 179func TestGetSecretsWithPrefix(t *testing.T) {180 envVars := map[string]string{181 "SECRET_": "exact prefix",182 "SECRET_ONE": "one",183 "SECRET_TWO": "two",184 "SECRETISH": "not a match",185 "OTHER_SECRET": "not a match",186 "RAILPACK_TOKEN": "not a match",187 }188 env := NewEnvironment(&envVars)189 190 require.ElementsMatch(t, []string{191 "SECRET_",192 "SECRET_ONE",193 "SECRET_TWO",194 }, env.GetSecretsWithPrefix("SECRET_"))195}196