File: PhantomJS.hs

package info (click to toggle)
haskell-wai-cors 0.2.7-4
  • links: PTS
  • area: main
  • in suites: forky, trixie
  • size: 136 kB
  • sloc: haskell: 479; javascript: 35; makefile: 6
file content (70 lines) | stat: -rw-r--r-- 1,860 bytes parent folder | download | duplicates (4)
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
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE UnicodeSyntax #-}

-- |
-- Module: Main
-- Description: Test suite that uses PhantomJS to simulate a browser
-- Copyright: © 2015 Lars Kuhtz <lakuhtz@gmail.com>
-- License: MIT
-- Maintainer: Lars Kuhtz <lakuhtz@gmail.com>
-- Stability: experimental
--
module Main
( main
) where

import Control.Concurrent

import Data.Monoid.Unicode
import Data.String

import System.Directory
import System.FilePath
import System.Exit
import System.IO
import System.Process

-- internal modules

import qualified Server as Server

phantomJsBinaryPath ∷ FilePath
phantomJsBinaryPath = "phantomjs"

phantomJsArgs ∷ IsString a ⇒ [a]
phantomJsArgs = ["--ignore-ssl-errors=true"]
-- phantomJsArgs = ["--ignore-ssl-errors=true", "--debug=true"]

phantomJsScriptPath ∷ FilePath
phantomJsScriptPath = "test/phantomjs.js"

indexFilePath ∷ FilePath
indexFilePath = "test/index.html"

runPhantomJs ∷ IO ()
runPhantomJs = do

    -- check that phantomJS binary is available
    -- FIXME check the version
    findExecutable phantomJsBinaryPath >>= \case
        Nothing → do
            hPutStrLn stderr $ "Missing PhantomJS exectuable: in order to run this test-suite PhantomJS must be availabe on the system"
            exitFailure
        Just _ → return ()

    pwd ← getCurrentDirectory
    -- FIXME I consider it an API bug of the directory package that in order
    -- to get the exit code we have also capture stdout and stderr.
    (code, out, err) ← readProcessWithExitCode phantomJsBinaryPath (args pwd) ""
    hPutStrLn stdout out
    hPutStrLn stderr err
    exitWith code
  where
    args pwd = phantomJsArgs ⊕ [phantomJsScriptPath] ⊕ [pwd </> indexFilePath]

main ∷ IO ()
main = do
    _ ← forkIO $ Server.main
    runPhantomJs