11{-# LANGUAGE CPP #-}
2+ {-# LANGUAGE NamedFieldPuns #-}
23{-# LANGUAGE OverloadedStrings #-}
34{-# LANGUAGE RankNTypes #-}
45{-# LANGUAGE ViewPatterns #-}
@@ -49,7 +50,6 @@ import Turtle (FilePath, Pattern, Shell, fp)
4950import qualified Control.Exception
5051import qualified Control.Foldl as Foldl
5152import qualified Control.Monad.Trans.State.Strict as State
52- import qualified Data.Foldable
5353import qualified Data.Functor
5454import qualified Data.Text as Text
5555import qualified Data.Text.IO as Text.IO
@@ -64,9 +64,11 @@ import qualified Test.Tasty as Tasty
6464import qualified Test.Tasty.ExpectedFailure as Tasty.ExpectedFailure
6565import qualified Turtle
6666
67- #ifndef WITH_HTTP
67+ #if defined(WITH_HTTP) && defined(NETWORK_TESTS)
68+ import qualified Data.Foldable
69+ #else
6870import Control.Monad.IO.Class (MonadIO (.. ))
69- import Dhall.Core (URL (.. ))
71+ import Dhall.Core (URL (.. ), File ( .. ), Directory ( .. ) )
7072import Lens.Family.State.Strict (zoom )
7173
7274import qualified Data.Foldable
@@ -104,62 +106,85 @@ loadRelativeTo rootDirectory semanticCacheMode expression =
104106 (loadWith expression)
105107 (Dhall.Import. emptyStatus rootDirectory) { _semanticCacheMode = semanticCacheMode }
106108
107- #ifdef WITH_HTTP
109+ #if defined( WITH_HTTP) && defined(NETWORK_TESTS)
108110loadWith :: Expr Src Import -> StateT Status IO (Expr Src Void )
109111loadWith = Dhall.Import. loadWith
110112
111113#else
112114loadWith :: Expr Src Import -> StateT Status IO (Expr Src Void )
113115loadWith expr = do
114- let mockRemote' url = do
115- liftIO . putStrLn $ " \n Testing without real HTTP support --"
116- ++ " using mock HTTP client to resolve remote import."
117- mockRemote url
118- zoom Dhall.Import. remote (State. put mockRemote')
116+ zoom Dhall.Import. remote (State. put mockRemote)
119117 Dhall.Import. loadWith expr
120118
121119mockRemote :: Dhall.Core. URL -> StateT Status IO Data.Text. Text
122- -- Matches anything pointing to
123- -- `https://raw.githubusercontent.com/dhall-lang/dhall-lang/master/`
124- mockRemote (URL { authority = " raw.githubusercontent.com"
125- , path = Dhall.Core. File (Dhall.Core. Directory components) file })
126- | take 3 (reverse components) == [" dhall-lang" , " dhall-lang" , " master" ] = do
127- let dropEnd n ls = take (length ls - n) ls
128- let localDir = dropEnd 3 components ++ [" dhall-lang" ]
129-
130- localPath <- Dhall.Import. localToPath Dhall.Core. Here (Dhall.Core. File (Dhall.Core. Directory localDir) file)
131- liftIO $ Data.Text.IO. readFile localPath
132-
133- -- Matches anything pointing to
134- -- `https://test.dhall-lang.org/Bool/package.dhall`; checks that a `test` header
135- -- is present and redirects to the local copy of the prelude.
136- mockRemote (URL { authority = " test.dhall-lang.org"
137- , path = Dhall.Core. File (Dhall.Core. Directory components) file
138- , headers = Just headersExpr }) =
139- case Data.Foldable. find ((== " test" ) . fst ) hs of
140- Nothing -> fail $ " (mock http) Tried to load an import from "
141- ++ " \" test.dhall-lang.org\" "
142- ++ " without setting the \" test\" header field."
143- Just (_, _) -> do
144- let localDir = components ++ [" Prelude" , " dhall-lang" ]
145- localPath <- Dhall.Import. localToPath Dhall.Core. Here (Dhall.Core. File (Dhall.Core. Directory localDir) file)
146- liftIO $ Data.Text.IO. readFile localPath
147- where
148- hs = Dhall.Import. toHeaders headersExpr
149-
150- -- Emulates `https://httpbin.org/user-agent`
151- mockRemote (URL { authority = " httpbin.org"
152- , path = Dhall.Core. File (Dhall.Core. Directory [] ) " user-agent"
153- , headers = Just headersExpr }) =
154- case Data.Foldable. find ((== " user-agent" ) . fst ) hs of
155- Nothing -> fail $ " (mock http) Tried to read the user agent via "
156- ++ " \" httpbin.com/user-agent\" without supplying one "
157- ++ " in the header!"
158- Just (_, userAgent) -> do
120+ mockRemote
121+ url@ URL
122+ { authority = " raw.githubusercontent.com"
123+ , path = File (Directory components) file
124+ } = do
125+ let localDir = case reverse components of
126+ " dhall-lang" : " dhall-lang" : _ : rest ->
127+ reverse (" dhall-lang" : rest)
128+ " Nadrieril" : " dhall-rust" : _ : " dhall" : rest ->
129+ reverse (" dhall-lang" : rest)
130+ _ -> do
131+ fail (" Unable to mock URL: " <> Text. unpack (Dhall.Core. pretty url))
132+
133+ localPath <- Dhall.Import. localToPath Dhall.Core. Here (File (Directory localDir) file)
134+
135+ liftIO (Data.Text.IO. readFile localPath)
136+
137+ mockRemote
138+ URL { authority = " prelude.dhall-lang.org"
139+ , path = File (Directory components) file
140+ } = do
141+ let localDir = components ++ [ " Prelude" , " dhall-lang" ]
142+
143+ localPath <- Dhall.Import. localToPath Dhall.Core. Here (File (Directory localDir) file)
144+
145+ liftIO (Data.Text.IO. readFile localPath)
146+
147+ mockRemote url@ URL { authority = " test.dhall-lang.org" , path, headers } =
148+ case (path, fmap Dhall.Import. toHeaders headers) of
149+ (File (Directory [] ) " foo" , Just [(" test" , _)]) ->
150+ return " ./bar"
151+ (File (Directory [] ) " bar" , Just [(" test" , _)]) ->
152+ return " True"
153+ (File (Directory [" cors" ]) " AllowedAll.dhall" , _) ->
154+ return " 42"
155+ (File (Directory [" cors" ]) " OnlyGithub.dhall" , _) ->
156+ return " 42"
157+ (File (Directory [" cors" ]) " OnlySelf.dhall" , _) ->
158+ return " 42"
159+ (File (Directory [" cors" ]) " OnlyOther.dhall" , _) ->
160+ return " 42"
161+ (File (Directory [" cors" ]) " Empty.dhall" , _) ->
162+ return " 42"
163+ (File (Directory [" cors" ]) " NoCORS.dhall" , _) ->
164+ return " 42"
165+ (File (Directory [" cors" ]) " Null.dhall" , _) ->
166+ return " 42"
167+ (File (Directory [" cors" ]) " SelfImportAbsolute.dhall" , _) ->
168+ return " https://test.dhall-lang.org/cors/NoCORS.dhall"
169+ (File (Directory [" cors" ]) " SelfImportRelative.dhall" , _) ->
170+ return " ./NoCORS.dhall"
171+ (File (Directory [" cors" ]) " TwoHopsFail.dhall" , _) ->
172+ return " https://raw.githubusercontent.com/dhall-lang/dhall-lang/5ff7ecd2411894dd9ce307dc23020987361d2d43/tests/import/data/cors/OnlySelf.dhall"
173+ (File (Directory [" cors" ]) " TwoHopsSuccess.dhall" , _) ->
174+ return " https://raw.githubusercontent.com/dhall-lang/dhall-lang/5ff7ecd2411894dd9ce307dc23020987361d2d43/tests/import/data/cors/OnlyGithub.dhall"
175+ _ -> do
176+ fail (" Unable to mock URL: " <> Text. unpack (Dhall.Core. pretty url))
177+
178+ mockRemote url@ URL { authority = " httpbin.org" , path, headers } =
179+ case (path, fmap Dhall.Import. toHeaders headers) of
180+ (File (Directory [] ) " user-agent" , Just [(" user-agent" , userAgent)]) -> do
159181 let agentText = Data.Text.Encoding. decodeUtf8 userAgent
182+
160183 return (" {\n \" user-agent\" : \" " <> agentText <> " \"\n }\n " )
161- where
162- hs = Dhall.Import. toHeaders headersExpr
184+ (File (Directory [] ) " user-agent" , Nothing ) -> do
185+ return (" {\n \" user-agent\" : \" Dhall\"\n }\n " )
186+ _ -> do
187+ fail (" Unable to mock URL: " <> Text. unpack (Dhall.Core. pretty url))
163188
164189mockRemote url = do
165190 let urlString = Text. unpack (Dhall.Core. pretty url)
0 commit comments