From 72af2f322ea12ebc7a28ef2b88c1450e1fe6f588 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Wed, 28 Sep 2022 23:54:38 +0300 Subject: [PATCH] Add env var substitution test for JSON --- source/mirage/json.d | 15 +++++++++++++++ testfiles/server.json | 7 +++++++ 2 files changed, 22 insertions(+) create mode 100644 testfiles/server.json diff --git a/source/mirage/json.d b/source/mirage/json.d index c8505c9..df4b937 100644 --- a/source/mirage/json.d +++ b/source/mirage/json.d @@ -130,6 +130,8 @@ ConfigDictionary loadJsonConfig(const string filePath) { } version (unittest) { + import std.process : environment; + @("Parse JSON") unittest { JSONValue serverJson = ["hostname": "hosty.com", "port": "1234"]; @@ -193,4 +195,17 @@ version (unittest) { assert(config.get("age") == "8728"); assert(config.get("taxNumber") == null); } + + @("Substitute env vars in JSON") + unittest { + environment["MIRAGE_TEST_APP_NAME"] = "Unittest"; + environment["MIRAGE_TEST_HOSTNAME"] = "wonkeyhost"; + environment.remove("MIRAGE_TEST_PORT"); + + auto config = loadJsonConfig("testfiles/server.json"); + + assert(config.get("server.host") == "wonkeyhost"); + assert(config.get("server.port") == "8118"); + assert(config.get("app") == "Unittest server - built with love"); + } } diff --git a/testfiles/server.json b/testfiles/server.json new file mode 100644 index 0000000..c5ae2b8 --- /dev/null +++ b/testfiles/server.json @@ -0,0 +1,7 @@ +{ + "server": { + "host": "$MIRAGE_TEST_HOSTNAME", + "port": "${MIRAGE_TEST_PORT:8118}" + }, + "app": "${MIRAGE_TEST_APP_NAME} server - built with love" +}