gRPC-haskell/tests/GeneratedTests.hs
Gabriel Gonzalez c02546f696 Fix grpc-haskell tests for Nix (#80)
This updates the `release.nix` to now correctly run the test suite.  You can
now build and test `grpc-haskell` by running:

```
$ nix-build -A grpc-haskell release.nix
```

... and `nix-shell` has been updated, too, so that you can do `cabal`
development inside of a `nix-shell` by running:

```
$ nix-shell -A grpc-haskell.env release.nix
```

For people who prefer to use `stack` you can still just build the `grpc`
library by running:

```
$ nix-build -A grpc release.nix
```

... then pass that library as input to `stack`
2016-12-12 10:40:23 -08:00

87 lines
2.3 KiB
Haskell

{-# LANGUAGE OverloadedStrings #-}
module GeneratedTests where
import Test.Tasty
import Test.Tasty.HUnit (testCase, (@?=))
import Data.String
import Data.Protobuf.Wire.DotProto.Generate
import qualified Data.Text as T
import Turtle
generatedTests :: TestTree
generatedTests = testGroup "Code generator tests"
[ testServerGeneration
, testClientGeneration ]
testServerGeneration :: TestTree
testServerGeneration = testCase "server generation" $ do
mktree hsTmpDir
mktree pyTmpDir
compileSimpleDotProto
exitCode <- proc "tests/simple-server.sh" [hsTmpDir] empty
exitCode @?= ExitSuccess
exitCode <- proc "tests/protoc.sh" [pyTmpDir] empty
exitCode @?= ExitSuccess
runManaged $ do
serverExitCodeA <- fork (shell (hsTmpDir <> "/simple-server") empty)
clientExitCodeA <- fork
(export "PYTHONPATH" pyTmpDir >> shell "tests/test-client.sh" empty)
liftIO $ do
serverExitCode <- liftIO (wait serverExitCodeA)
clientExitCode <- liftIO (wait clientExitCodeA)
serverExitCode @?= ExitSuccess
clientExitCode @?= ExitSuccess
rmtree hsTmpDir
rmtree pyTmpDir
testClientGeneration :: TestTree
testClientGeneration = testCase "client generation" $ do
mktree hsTmpDir
mktree pyTmpDir
compileSimpleDotProto
exitCode <- proc "tests/simple-client.sh" [hsTmpDir] empty
exitCode @?= ExitSuccess
exitCode <- proc "tests/protoc.sh" [pyTmpDir] empty
exitCode @?= ExitSuccess
runManaged $ do
serverExitCodeA <- fork
(export "PYTHONPATH" pyTmpDir >> shell "tests/test-server.sh" empty)
clientExitCodeA <- fork (shell (hsTmpDir <> "/simple-client") empty)
liftIO $ do
serverExitCode <- liftIO (wait serverExitCodeA)
clientExitCode <- liftIO (wait clientExitCodeA)
serverExitCode @?= ExitSuccess
clientExitCode @?= ExitSuccess
rmtree hsTmpDir
rmtree pyTmpDir
hsTmpDir, pyTmpDir :: IsString a => a
hsTmpDir = "tests/tmp"
pyTmpDir = "tests/py-tmp"
compileSimpleDotProto :: IO ()
compileSimpleDotProto =
do dpRes <- readDotProtoWithContext "tests/simple.proto"
case dpRes of
Left err -> fail (show err)
Right (dp, ctxt) ->
case renderHsModuleForDotProto dp ctxt of
Left err -> fail ("compileSimpleDotProto: Error compiling test.proto: " <> show err)
Right hsSrc -> writeFile (hsTmpDir ++ "/Simple.hs") hsSrc