1 % Copyright (C) 2002-2005 David Roundy
3 % This program is free software; you can redistribute it and/or modify
4 % it under the terms of the GNU General Public License as published by
5 % the Free Software Foundation; either version 2, or (at your option)
8 % This program is distributed in the hope that it will be useful,
9 % but WITHOUT ANY WARRANTY; without even the implied warranty of
10 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 % GNU General Public License for more details.
13 % You should have received a copy of the GNU General Public License
14 % along with this program; see the file COPYING. If not, write to
15 % the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 % Boston, MA 02110-1301, USA.
19 module Darcs.Test ( run_test, get_test,
20 run_posthook, run_prehook )
22 import Darcs.RepoPath ( AbsolutePath )
23 import Darcs.Utils ( withCurrentDirectory )
24 import System.Exit ( ExitCode(..) )
25 import System.Cmd ( system )
26 import Control.Monad ( when )
28 import Darcs.Arguments ( DarcsFlag( Quiet,
29 AskPosthook, AskPrehook ),
30 get_posthook_cmd, get_prehook_cmd )
31 import Darcs.Repository.Prefs ( get_prefval )
32 import Darcs.Utils ( askUser )
33 import System.IO ( hPutStrLn, stderr )
36 If you like, you can configure your repository to be able to run a test
37 suite of some sort. You can do this by using ``setpref'' to set the
38 ``test'' value to be a command to run, e.g.
40 % darcs setpref test "sh configure && make && make test"
42 Or, if you want to define a test specific to one copy of the repository,
43 you could do this by editing the file \verb!_darcs/prefs/prefs!.
46 --leave-test-directory, --remove-test-directory
49 Normally darcs deletes the directory in which the test was run afterwards.
50 Sometimes (especially when the test fails) you'd prefer to be able to be
51 able to examine the test directory after the test is run. You can do this
52 by specifying the \verb!--leave-test-directory! flag. Alas, there is no
53 way to make darcs leave the test directory only if the test fails. The
54 opposite of \verb!--leave-test-directory! is
55 \verb!--remove-test-directory!, which could come in handy if you choose to
56 make \verb!--leave-test-directory! the default (see
57 section~\ref{defaults}).
60 run_test :: [DarcsFlag] -> AbsolutePath -> IO ExitCode
61 run_test opts testdir = do test <- get_test opts
62 withCurrentDirectory testdir test
64 get_test :: [DarcsFlag] -> IO (IO ExitCode)
66 let putInfo s = when (not $ Quiet `elem` opts) $ putStr s
68 testline <- get_prefval "test"
71 Nothing -> return ExitSuccess
73 putInfo "Running test...\n"
76 then putInfo "Test ran successfully.\n"
77 else putInfo "Test failed!\n"
82 run_posthook :: [DarcsFlag] -> AbsolutePath -> IO ExitCode
83 run_posthook opts repodir = do ph <- get_posthook opts
84 withCurrentDirectory repodir $ run_hook opts "Posthook" ph
86 get_posthook :: [DarcsFlag] -> IO (Maybe String)
87 get_posthook opts = case get_posthook_cmd opts of
88 Nothing -> return Nothing
90 if AskPosthook `elem` opts
91 then do yorn <- askUser ("\nThe following command is set to execute.\n"++
92 "Execute the following command now (yes or no)?\n"++
94 case yorn of ('y':_) -> return $ Just command
95 _ -> do putStrLn "Posthook cancelled..."
97 else return $ Just command
99 run_prehook :: [DarcsFlag] -> AbsolutePath -> IO ExitCode
100 run_prehook opts repodir = do ph <- get_prehook opts
101 withCurrentDirectory repodir $ run_hook opts "Prehook" ph
103 get_prehook :: [DarcsFlag] -> IO (Maybe String)
104 get_prehook opts = case get_prehook_cmd opts of
105 Nothing -> return Nothing
107 if AskPrehook `elem` opts
108 then do yorn <- askUser ("\nThe following command is set to execute.\n"++
109 "Execute the following command now (yes or no)?\n"++
111 case yorn of ('y':_) -> return $ Just command
112 _ -> do putStrLn "Prehook cancelled..."
114 else return $ Just command
116 run_hook :: [DarcsFlag] -> String -> Maybe String -> IO ExitCode
117 run_hook _ _ Nothing = return ExitSuccess
118 run_hook opts cname (Just command) =
119 do ec <- system command
120 when (Quiet `notElem` opts) $
122 then putStrLn $ cname++" ran successfully."
123 else hPutStrLn stderr $ cname++" failed!"