|
1 | 1 | (ns eca.config-test |
2 | 2 | (:require |
| 3 | + [babashka.fs :as fs] |
3 | 4 | [clojure.test :refer [deftest is testing]] |
4 | 5 | [eca.config :as config] |
5 | 6 | [eca.logger :as logger] |
|
184 | 185 | :b [:bar]} |
185 | 186 | {:c 3 |
186 | 187 | :b []}))))) |
| 188 | + |
| 189 | +(deftest parse-dynamic-string-test |
| 190 | + (testing "returns nil for nil input" |
| 191 | + (is (nil? (#'config/parse-dynamic-string nil "/tmp")))) |
| 192 | + |
| 193 | + (testing "returns string unchanged when no patterns" |
| 194 | + (is (= "hello world" (#'config/parse-dynamic-string "hello world" "/tmp")))) |
| 195 | + |
| 196 | + (testing "replaces environment variable patterns" |
| 197 | + (with-redefs [config/get-env (fn [env-var] |
| 198 | + (case env-var |
| 199 | + "TEST_VAR" "test-value" |
| 200 | + "ANOTHER_VAR" "another-value" |
| 201 | + nil))] |
| 202 | + (is (= "test-value" (#'config/parse-dynamic-string "${env:TEST_VAR}" "/tmp"))) |
| 203 | + (is (= "prefix test-value suffix" (#'config/parse-dynamic-string "prefix ${env:TEST_VAR} suffix" "/tmp"))) |
| 204 | + (is (= "test-value and another-value" (#'config/parse-dynamic-string "${env:TEST_VAR} and ${env:ANOTHER_VAR}" "/tmp"))))) |
| 205 | + |
| 206 | + (testing "replaces undefined env var with empty string" |
| 207 | + (with-redefs [config/get-env (constantly nil)] |
| 208 | + (is (= "" (#'config/parse-dynamic-string "${env:UNDEFINED_VAR}" "/tmp"))) |
| 209 | + (is (= "prefix suffix" (#'config/parse-dynamic-string "prefix ${env:UNDEFINED_VAR} suffix" "/tmp"))))) |
| 210 | + |
| 211 | + (testing "replaces file pattern with file content - absolute path" |
| 212 | + (with-redefs [fs/absolute? (fn [path] (= path "/absolute/file.txt")) |
| 213 | + slurp (fn [path] |
| 214 | + (if (= (str path) "/absolute/file.txt") |
| 215 | + "test file content" |
| 216 | + (throw (ex-info "File not found" {}))))] |
| 217 | + (is (= "test file content" (#'config/parse-dynamic-string "${file:/absolute/file.txt}" "/tmp"))))) |
| 218 | + |
| 219 | + (testing "replaces file pattern with file content - relative path" |
| 220 | + (with-redefs [fs/absolute? (fn [_] false) |
| 221 | + fs/path (fn [cwd file-path] (str cwd "/" file-path)) |
| 222 | + slurp (fn [path] |
| 223 | + (if (= path "/tmp/test.txt") |
| 224 | + "relative file content" |
| 225 | + (throw (ex-info "File not found" {}))))] |
| 226 | + (is (= "relative file content" (#'config/parse-dynamic-string "${file:test.txt}" "/tmp"))))) |
| 227 | + |
| 228 | + (testing "replaces file pattern with empty string when file not found" |
| 229 | + (with-redefs [logger/warn (fn [& _] nil) |
| 230 | + fs/absolute? (fn [_] true) |
| 231 | + slurp (fn [_] (throw (ex-info "File not found" {})))] |
| 232 | + (is (= "" (#'config/parse-dynamic-string "${file:/nonexistent/file.txt}" "/tmp"))) |
| 233 | + (is (= "prefix suffix" (#'config/parse-dynamic-string "prefix ${file:/nonexistent/file.txt} suffix" "/tmp"))))) |
| 234 | + |
| 235 | + (testing "handles multiple file patterns" |
| 236 | + (with-redefs [fs/absolute? (fn [_] true) |
| 237 | + slurp (fn [path] |
| 238 | + (case (str path) |
| 239 | + "/file1.txt" "content1" |
| 240 | + "/file2.txt" "content2" |
| 241 | + (throw (ex-info "File not found" {}))))] |
| 242 | + (is (= "content1 and content2" |
| 243 | + (#'config/parse-dynamic-string "${file:/file1.txt} and ${file:/file2.txt}" "/tmp"))))) |
| 244 | + |
| 245 | + (testing "handles mixed env and file patterns" |
| 246 | + (with-redefs [config/get-env (fn [env-var] |
| 247 | + (when (= env-var "TEST_VAR") "env-value")) |
| 248 | + fs/absolute? (fn [_] true) |
| 249 | + slurp (fn [path] |
| 250 | + (if (= (str path) "/file.txt") |
| 251 | + "file-value" |
| 252 | + (throw (ex-info "File not found" {}))))] |
| 253 | + (is (= "env-value and file-value" |
| 254 | + (#'config/parse-dynamic-string "${env:TEST_VAR} and ${file:/file.txt}" "/tmp"))))) |
| 255 | + |
| 256 | + (testing "handles patterns within longer strings" |
| 257 | + (with-redefs [config/get-env (fn [env-var] |
| 258 | + (when (= env-var "API_KEY") "secret123"))] |
| 259 | + (is (= "Bearer secret123" (#'config/parse-dynamic-string "Bearer ${env:API_KEY}" "/tmp"))))) |
| 260 | + |
| 261 | + (testing "handles empty string input" |
| 262 | + (is (= "" (#'config/parse-dynamic-string "" "/tmp")))) |
| 263 | + |
| 264 | + (testing "preserves content with escaped-like patterns that don't match" |
| 265 | + (is (= "${notenv:VAR}" (#'config/parse-dynamic-string "${notenv:VAR}" "/tmp"))) |
| 266 | + (is (= "${env:}" (#'config/parse-dynamic-string "${env:}" "/tmp"))))) |
0 commit comments