File: LoadEnvSpec.hs

package info (click to toggle)
haskell-load-env 0.2.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 92 kB
  • sloc: haskell: 249; makefile: 5
file content (88 lines) | stat: -rw-r--r-- 2,549 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
module LoadEnvSpec
    ( spec
    ) where

import Control.Monad (when)
import LoadEnv
import System.Directory
import System.Environment
import System.IO.Temp
import Test.Hspec

spec :: Spec
spec = after_ cleanup $ do
    describe "loadEnv" $ do
        it "loads environment variables from ./.env if present" $ do
            writeFile envFile $ unlines
                [ "FOO=\"bar\""
                , "BAZ=\"bat\""
                ]

            loadEnvFrom envFile

            mbar <- lookupEnv "FOO"
            mbat <- lookupEnv "BAZ"
            mbar `shouldBe` Just "bar"
            mbat `shouldBe` Just "bat"

        it "does not override pre-existing variables" $ do
            writeFile envFile $ unlines ["FOO=bar"]
            setEnv "FOO" "baz"

            loadEnvFrom envFile

            mbar <- lookupEnv "FOO"
            mbar `shouldBe` Just "baz"

        it "does not fail if the file is not present" $ do
            loadEnvFrom "i-do-not-exist"

            return ()

    describe "loadEnvFrom" $ do
        it "traverses up the directory tree" $ do
            inTempDirectory $ do
                writeFile ".env.test" "FOO=\"bar\"\n"
                inNewDirectory "foo/bar/baz" $ do
                    loadEnvFrom ".env.test"

            lookupEnv "FOO" `shouldReturn` Just "bar"

        it "loads only the nearest file" $ do
            inTempDirectory $ do
                writeFile ".env.test" "FOO=\"bar\"\n"
                inNewDirectory "foo/bar" $ do
                    writeFile ".env.test" "BAR=\"baz\"\n"
                    inNewDirectory "baz/bat" $ do
                        loadEnvFrom ".env.test"

            lookupEnv "BAR" `shouldReturn` Just "baz"
            lookupEnv "FOO" `shouldReturn` Nothing

    describe "loadEnvFromAbsolute" $ do
        it "does not traverse up the directory tree" $ do
            inTempDirectory $ do
                writeFile ".env.test" "FOO=\"bar\"\n"
                inNewDirectory "foo/bar/baz" $ do
                    loadEnvFromAbsolute ".env.test"

            lookupEnv "FOO" `shouldReturn` Nothing

inTempDirectory :: IO a -> IO a
inTempDirectory f =
    withSystemTempDirectory "" $ \tmp -> withCurrentDirectory tmp f

inNewDirectory :: FilePath -> IO a -> IO a
inNewDirectory path f = do
    createDirectoryIfMissing True path
    withCurrentDirectory path f

cleanup :: IO ()
cleanup = do
    unsetEnv "FOO"
    unsetEnv "BAR"
    e <- doesFileExist envFile
    when e $ removeFile envFile

envFile :: FilePath
envFile = "/tmp/load-env-test-file"