packages feed

ForSyDe-3.1: tests/Main.hs

-- Main test module, it must be run from the root directory of the project
module Main (main) where

import Install

import Distribution.System
import Control.Monad (liftM)
import System.IO
import System.Environment (getArgs, getEnv)
import System.Process (runCommand, waitForProcess)
import System.Exit
import System.FilePath (splitDirectories, normalise)


main :: IO ()
main = do
 needed <- testNeeded
 if needed 
   then 
     do hSetBuffering stdout LineBuffering
        testInstall
        --  test the properties using the fresh installation
        -- due to many problems with runghc, I decided to compile it first
        putStrLn ("Compiling the unit testbench: " ++ compilePropertiesCmd)
        h1 <- runCommand  compilePropertiesCmd
        e1 <- waitForProcess h1
        case e1 of
          ExitSuccess -> do h2 <- runCommand properties
                            e2 <- waitForProcess h2
                            exitWith e2
          _ -> exitFailure

--        h <- runCommand runPropertiesCmd
--        e <- waitForProcess h
--        exitWith e

   else putStrLn "There is no need to run the test suite."
 where compilePropertiesCmd = 
         "ghc --make -itests/properties -iexamples -package-conf testInstallation.conf " ++ 
                 "tests/properties/Main.hs -o properties"
       properties = case buildOS of
                     Windows -> "properties.exe"
                     _ -> "./properties"

-- Check if we need to do the tests, This will be true unless an
-- automatic test is done from darcs, in which case we only need
-- to perform the tests if the affected files are ForSyDe.cabal
-- Setup.hs or any file under src/, tests/ or lib/
testNeeded :: IO Bool
testNeeded = do
  mDARCS_FILES <- catch (liftM Just $ getEnv "DARCS_FILES")
                        (\_ -> return Nothing)
  case mDARCS_FILES of
    Nothing -> do putStrLn "DARCS_FILES wasn't set by darcs: forcing test."
                  return True
    Just multiLine -> (return.checkAffected) multiLine
  where checkAffected str = any affected (lines str) 
        affected file = let nfile = normalise file in
                        nfile `elem` ["Setup.hs", "ForSyDe.cabal"] ||
                        firstDir nfile `elem` ["src", "tests", "lib"]         
          
        firstDir filePath = 
               let sDirName = splitDirectories filePath
               in case sDirName of
                  [_]     -> ""
                  (x:xs)  -> x