summaryrefslogtreecommitdiff
path: root/tests/Main.hs
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-02-24 08:14:42 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-02-24 08:14:42 +0400
commit42eaee8dbcd1cfb922d94e974043d8d564dbd353 (patch)
tree3add5fa532d95f91e99d6681d277679b669b2f1e /tests/Main.hs
parent04d1c3a35696ac79143cdbfd669008631f91512c (diff)
Add test suite configuration
Diffstat (limited to 'tests/Main.hs')
-rw-r--r--tests/Main.hs95
1 files changed, 67 insertions, 28 deletions
diff --git a/tests/Main.hs b/tests/Main.hs
index 63281cf3..6577e6f4 100644
--- a/tests/Main.hs
+++ b/tests/Main.hs
@@ -1,37 +1,76 @@
1{-# LANGUAGE RecordWildCards #-}
1module Main where 2module Main where
2 3import Control.Exception
3import Spec 4import Control.Monad
5import Data.Functor
6import Data.List
7import Data.Maybe
4import System.Exit 8import System.Exit
5import System.Environment 9import System.Environment
6import System.Process 10import System.Process
7import System.Directory 11import System.Directory
8import Control.Exception 12import Text.Printf
9import Data.List 13
10import Data.Maybe 14import Config
11import Data.Functor 15import Spec
16
17
18type Command = String
19type Descr = (ClientName, ClientOpts -> FilePath -> Command)
20
21torrents :: [FilePath]
22torrents =
23 [ "dapper-dvd-amd64-iso.torrent"
24 , "pkg.torrent"
25 , "testfile.torrent"
26 ]
27
28clients :: [Descr]
29clients =
30 [ ("rtorrent"
31 , \ ClientOpts {..} tfile -> printf
32 "rtorrent -p %i-%i -O dht=on -O dht_port=%i -O session=rtorrent-sessiondir %s"
33 (fromEnum peerPort) (fromEnum peerPort) (fromEnum nodePort) tfile
34 )
35 ]
36
37sessionName :: String -- screen session name
38sessionName = "bittorrent-testsuite"
39
40setupEnv :: EnvOpts -> IO (Maybe ())
41setupEnv EnvOpts {..}
42 | Just client <- testClient
43 , Just mkCmd <- lookup client clients = do
44 let runner = printf "screen -dm -S %s %s" sessionName
45 (mkCmd remoteOpts "testfile.torrent")
46 dir <- getCurrentDirectory
47 _ <- createProcess (shell runner) { cwd = Just (dir ++ "/res") }
48 return (Just ())
49
50 | Just client <- testClient = do
51 printf "Bad client `%s`, use one of %s" client (show (fst <$> clients))
52 return Nothing
53
54 | isNothing testClient = do
55 printf "Running without remote client"
56 return (Just ())
57
58terminateEnv :: IO ()
59terminateEnv = do
60 printf "closing screen session: %s" sessionName
61 _ <- system (printf "screen -S %s -X quit" sessionName)
62 return ()
12 63
13clients :: [(String, String)] 64runTestSuite :: [String] -> IO ExitCode
14clients = [ 65runTestSuite args = do
15 ("rtorrent","rtorrent -p 51234-51234 -O dht=on -O dht_port=6881 -O session=rtorrent-sessiondir testfile.torrent") ] 66 printf "running hspec test suite with args: %s\n" (show args)
67 catch (withArgs args hspecMain >> return ExitSuccess) return
16 68
17main :: IO () 69main :: IO ()
18main = do 70main = do
19 args <- getArgs 71 (envOpts, suiteArgs) <- getOpts
20 let cmd' = do 72 running <- setupEnv envOpts
21 cl <- listToMaybe $ reverse 73 code <- runTestSuite suiteArgs
22 $ map (tail . dropWhile (/='=')) 74 when (isJust running) $ do
23 $ filter (isPrefixOf "--bittorrent-client=") args 75 terminateEnv
24 cmd <- (++) "screen -dm -S bittorrent-testsuite " <$> lookup cl clients 76 exitWith code
25 return cmd
26 case cmd' of
27 Just cmd -> do _ <- system "screen -S bittorrent-testsuite -X quit"
28 dir <- getCurrentDirectory
29 _ <- createProcess (shell cmd) { cwd = Just (dir ++ "/res") }
30 return ()
31 Nothing -> return ()
32
33 let args' = (filter (not . isPrefixOf "--bittorrent-client=") args)
34 code <- catch (withArgs args' hspecMain >> return ExitSuccess) return
35
36 _ <- system "screen -S bittorrent-testsuite -X quit"
37 exitWith code >> return ()