Skip to content

Commit 683bf33

Browse files
authored
Enable parsing of JSON env vars (#2849)
1 parent 1c36d2c commit 683bf33

File tree

11 files changed

+84
-12
lines changed

11 files changed

+84
-12
lines changed

waspc/e2e-test/test-outputs/waspCompile-golden/waspCompile/.wasp/out/.waspchecksums

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

waspc/e2e-test/test-outputs/waspCompile-golden/waspCompile/.wasp/out/server/.env

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

waspc/e2e-test/test-outputs/waspComplexTest-golden/waspComplexTest/.wasp/out/.waspchecksums

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

waspc/e2e-test/test-outputs/waspComplexTest-golden/waspComplexTest/.wasp/out/server/.env

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

waspc/e2e-test/test-outputs/waspJob-golden/waspJob/.wasp/out/.waspchecksums

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

waspc/e2e-test/test-outputs/waspJob-golden/waspJob/.wasp/out/server/.env

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

waspc/e2e-test/test-outputs/waspMigrate-golden/waspMigrate/.wasp/out/.waspchecksums

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

waspc/e2e-test/test-outputs/waspMigrate-golden/waspMigrate/.wasp/out/server/.env

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

waspc/src/Wasp/Env.hs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Wasp.Env
66
EnvVarValue,
77
parseDotEnvFile,
88
envVarsToDotEnvContent,
9+
formatEnvVarValue,
910
)
1011
where
1112

@@ -31,6 +32,17 @@ parseDotEnvFile envFile =
3132
-- report as a bug in compiler, so we instead convert these to IOExceptions.
3233
`catch` \(ErrorCall msg) -> throwIO $ userError $ "Failed to parse dot env file: " <> msg
3334

35+
-- | Formats environment variables for .env file content.
3436
envVarsToDotEnvContent :: [EnvVar] -> T.Text
3537
envVarsToDotEnvContent vars =
36-
T.pack $ intercalate "\n" $ map (\(name, value) -> name <> "=" <> show value) vars
38+
T.pack $ intercalate "\n" $ map formatEnvVar vars
39+
where
40+
formatEnvVar (name, value) = name <> "=" <> formatEnvVarValue value
41+
42+
formatEnvVarValue :: EnvVarValue -> EnvVarValue
43+
formatEnvVarValue rawValue
44+
| needsQuoting rawValue = concat ["\"", rawValue, "\""]
45+
| otherwise = rawValue
46+
where
47+
needsQuoting :: String -> Bool
48+
needsQuoting val = ' ' `elem` val

waspc/test/EnvTest.hs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
module EnvTest where
2+
3+
import qualified Data.Text as T
4+
import NeatInterpolation (trimming)
5+
import Test.Tasty.Hspec
6+
import Wasp.Env
7+
( envVarsToDotEnvContent,
8+
formatEnvVarValue,
9+
)
10+
11+
spec_envVarHelpers :: Spec
12+
spec_envVarHelpers = do
13+
describe "formatEnvVarValue" $ do
14+
it "should handle simple string values correctly" $ do
15+
envVarValueShouldBeFormattedCorrectly "simple_value" "simple_value"
16+
17+
it "should handle JSON values correctly" $ do
18+
envVarValueShouldBeFormattedCorrectly "{\"teamConcurrency\":3}" "{\"teamConcurrency\":3}"
19+
20+
it "should handle nested JSON objects correctly" $ do
21+
envVarValueShouldBeFormattedCorrectly
22+
"{\"database\":{\"host\":\"localhost\",\"port\":5432},\"options\":[\"option1\",\"option2\"]}"
23+
"{\"database\":{\"host\":\"localhost\",\"port\":5432},\"options\":[\"option1\",\"option2\"]}"
24+
25+
it "should handle boolean and number values in JSON correctly" $ do
26+
envVarValueShouldBeFormattedCorrectly
27+
"{\"enabled\":true,\"maxRetries\":5,\"timeout\":30.5}"
28+
"{\"enabled\":true,\"maxRetries\":5,\"timeout\":30.5}"
29+
30+
it "should handle values that need quoting in shell environments" $ do
31+
envVarValueShouldBeFormattedCorrectly "hello world with spaces" "\"hello world with spaces\""
32+
envVarValueShouldBeFormattedCorrectly "{\"teamConcurrency\":3, \"retryLimit\":2}" "\"{\"teamConcurrency\":3, \"retryLimit\":2}\""
33+
34+
it "should handle special characters correctly" $ do
35+
envVarValueShouldBeFormattedCorrectly "value$with&special=chars" "value$with&special=chars"
36+
37+
describe "envVarsToDotEnvContent" $ do
38+
it "should handle multiple environment variables correctly" $ do
39+
let envVars =
40+
[ ("DATABASE_URL", "postgresql://localhost:5432/mydb"),
41+
("PG_BOSS_NEW_OPTIONS", "{\"teamConcurrency\":3,\"retryLimit\":2}"),
42+
("SIMPLE_VAR", "value")
43+
]
44+
let expected =
45+
[trimming|
46+
DATABASE_URL=postgresql://localhost:5432/mydb
47+
PG_BOSS_NEW_OPTIONS={"teamConcurrency":3,"retryLimit":2}
48+
SIMPLE_VAR=value
49+
|]
50+
51+
envVarsToDotEnvContent envVars `shouldBe` expected
52+
53+
it "should handle empty values correctly" $ do
54+
envVarsToDotEnvContent [("EMPTY_VAR", "")] `shouldBe` T.pack "EMPTY_VAR="
55+
where
56+
envVarValueShouldBeFormattedCorrectly :: String -> String -> Expectation
57+
envVarValueShouldBeFormattedCorrectly rawValue expectedFormattedValue = do
58+
let formattedValue = formatEnvVarValue rawValue
59+
formattedValue `shouldBe` expectedFormattedValue

0 commit comments

Comments
 (0)