Test CSV output
[darcs-mirror-arbtt.git] / tests / test.hs
1 {-# LANGUAGE DeriveDataTypeable, OverloadedStrings #-}
2
3 import Test.Tasty hiding (defaultMain)
4 import Test.Tasty.Golden.Manage
5 import Test.Tasty.Golden
6 import Test.Tasty.HUnit
7 import Test.HUnit
8 import System.Process.ByteString.Lazy
9 import qualified Data.ByteString.Lazy as B
10 import Control.Monad
11 import Control.Exception
12 import Data.Typeable
13 import System.Exit
14 import System.Posix.Env
15
16 import Categorize
17 import TimeLog
18 import Data
19
20 main = do
21     putEnv "TZ=UTC" -- to make tests reproducible
22     defaultMain tests
23
24 tests :: TestTree
25 tests = testGroup "Tests" [goldenTests, regressionTests]
26
27 regressionTests :: TestTree
28 regressionTests = testGroup "Regression tests"
29     [ testCase "Issue #4" $ do
30         cat <- readCategorizer "tests/issue4.cfg"
31         let sample = TimeLogEntry undefined 0 (CaptureData [(True, "aa", "program")] 0 "")
32         let [TimeLogEntry _ _ (_,acts)] = cat [sample]
33         [Activity (Just "Cat") "aa"] @=? acts
34         return ()
35     , testCase "Issue #5" $ do
36         cat <- readCategorizer "tests/issue5.cfg"
37         let sample = TimeLogEntry undefined 0 (CaptureData [(True, "aa", "program")] 0 "")
38         let [TimeLogEntry _ _ (_,acts)] = cat [sample]
39         [Activity Nothing "A2"] @=? acts
40         return ()
41     ]
42
43
44 goldenTests :: TestTree
45 goldenTests = testGroup "Golden tests"
46     [ goldenVsString "dump small"
47         "tests/small_dump.out" $
48         run "dist/build/arbtt-dump/arbtt-dump" ["-f","tests/small.log", "-t", "Show"] B.empty
49     , goldenVsFile "import small"
50         "tests/small_import.out" "tests/small_import.out.actual" $ void $
51         B.readFile "tests/small_import.in" >>=
52         run "dist/build/arbtt-import/arbtt-import" ["-f","tests/small_import.out.actual"]
53     , goldenVsFile "recover small"
54         "tests/small_borked_recover.out" "tests/small_borked_recover.out.actual" $ void $
55         run "dist/build/arbtt-recover/arbtt-recover" ["-i","tests/small_borked_recover.out", "-o", "tests/small_borked_recover.out.actual"] B.empty
56     , goldenVsString "stats small"
57         "tests/small_stats.out" $
58         run "dist/build/arbtt-stats/arbtt-stats" ["--logfile", "tests/small.log", "--categorize", "tests/small.cfg"] B.empty
59     , goldenVsString "stats small csv"
60         "tests/small_stats_csv.out" $
61         run "dist/build/arbtt-stats/arbtt-stats" ["--logfile", "tests/small.log", "--categorize", "tests/small.cfg", "--output-format", "csv"] B.empty
62     , goldenVsString "stats small unicode"
63         "tests/unicode_stats.out" $
64         run "dist/build/arbtt-stats/arbtt-stats" ["--logfile", "tests/unicode.log", "--categorize", "tests/unicode.cfg"] B.empty
65     ]
66
67
68 run :: FilePath -> [FilePath] -> B.ByteString -> IO B.ByteString
69 run cmd args stdin = do
70    (ex,stdout,stderr) <- readProcessWithExitCode cmd args stdin
71    unless (B.null stderr) $ throwIO $ StderrException stderr
72    case ex of
73      ExitSuccess   -> return stdout
74      ExitFailure r -> throwIO $ ExitCodeException r
75
76 data StderrException = StderrException B.ByteString
77      deriving (Show, Typeable)
78 data ExitCodeException = ExitCodeException Int
79      deriving (Show, Typeable)
80
81 instance Exception StderrException
82 instance Exception ExitCodeException