|
| 1 | +{-# LANGUAGE DeriveDataTypeable #-} |
| 2 | + |
| 3 | +import Control.Applicative |
| 4 | +import Control.Arrow |
| 5 | +import Control.Concurrent.Async |
| 6 | +import Control.Exception |
| 7 | +import Control.Monad |
| 8 | +import Control.Monad.IO.Class |
| 9 | +import Control.Monad.Trans.Resource |
| 10 | +import qualified Data.ByteString.Lazy as L |
| 11 | +import Data.Char |
| 12 | +import Data.Conduit |
| 13 | +import Data.Conduit.Binary (sinkLbs) |
| 14 | +import Data.Conduit.Filesystem (sourceDirectoryDeep) |
| 15 | +import qualified Data.Conduit.List as CL |
| 16 | +import Data.Conduit.Process |
| 17 | +import Data.List (isSuffixOf, stripPrefix) |
| 18 | +import qualified Data.Map as Map |
| 19 | +import Data.Text.Encoding.Error (lenientDecode) |
| 20 | +import qualified Data.Text.Lazy as TL |
| 21 | +import qualified Data.Text.Lazy.Encoding as TL |
| 22 | +import Data.Typeable |
| 23 | +import System.Directory |
| 24 | +import System.Environment |
| 25 | +import System.Exit |
| 26 | +import System.FilePath |
| 27 | +import System.IO.Temp |
| 28 | +import System.PosixCompat.Files |
| 29 | +import Test.Hspec |
| 30 | + |
| 31 | +main :: IO () |
| 32 | +main = do |
| 33 | + currDir <- getCurrentDirectory |
| 34 | + |
| 35 | + let findExe name = do |
| 36 | + mexe <- findExecutable name |
| 37 | + case mexe of |
| 38 | + Nothing -> error $ name ++ " not found on PATH" |
| 39 | + Just exe -> return exe |
| 40 | + runghc <- findExe "runghc" |
| 41 | + stack <- findExe "stack" |
| 42 | + |
| 43 | + tests <- getDirectoryContents "tests" >>= filterM hasTest |
| 44 | + |
| 45 | + envOrig <- getEnvironment |
| 46 | + |
| 47 | + withSystemTempDirectory ("stack-integration-home") $ \newHome -> do |
| 48 | + let env' = Map.toList |
| 49 | + $ Map.insert "STACK_EXE" stack |
| 50 | + $ Map.insert "HOME" newHome |
| 51 | + $ Map.insert "APPDATA" newHome |
| 52 | + $ Map.delete "GHC_PACKAGE_PATH" |
| 53 | + $ Map.fromList |
| 54 | + $ map (first (map toUpper)) envOrig |
| 55 | + |
| 56 | + origStackRoot <- getAppUserDataDirectory "stack" |
| 57 | + |
| 58 | + hspec $ mapM_ (test runghc env' currDir origStackRoot newHome) tests |
| 59 | + |
| 60 | +hasTest :: FilePath -> IO Bool |
| 61 | +hasTest dir = doesFileExist $ "tests" </> dir </> "Main.hs" |
| 62 | + |
| 63 | +test :: FilePath -- ^ runghc |
| 64 | + -> [(String, String)] -- ^ env |
| 65 | + -> FilePath -- ^ currdir |
| 66 | + -> FilePath -- ^ origStackRoot |
| 67 | + -> FilePath -- ^ newHome |
| 68 | + -> String |
| 69 | + -> Spec |
| 70 | +test runghc env' currDir origStackRoot newHome name = it name $ withDir $ \dir -> do |
| 71 | + copyStackRoot origStackRoot (newHome </> takeFileName origStackRoot) |
| 72 | + let testDir = currDir </> "tests" </> name |
| 73 | + mainFile = testDir </> "Main.hs" |
| 74 | + libDir = currDir </> "lib" |
| 75 | + cp = (proc runghc |
| 76 | + [ "-clear-package-db" |
| 77 | + , "-global-package-db" |
| 78 | + , "-i" ++ libDir |
| 79 | + , mainFile |
| 80 | + ]) |
| 81 | + { cwd = Just dir |
| 82 | + , env = Just env' |
| 83 | + } |
| 84 | + (ClosedStream, outSrc, errSrc, sph) <- streamingProcess cp |
| 85 | + (out, err, ec) <- runConcurrently $ (,,) |
| 86 | + <$> Concurrently (outSrc $$ sinkLbs) |
| 87 | + <*> Concurrently (errSrc $$ sinkLbs) |
| 88 | + <*> Concurrently (waitForStreamingProcess sph) |
| 89 | + when (ec /= ExitSuccess) $ throwIO $ TestFailure out err ec |
| 90 | + where |
| 91 | + withDir = withSystemTempDirectory ("stack-integration-" ++ name) |
| 92 | + |
| 93 | +data TestFailure = TestFailure L.ByteString L.ByteString ExitCode |
| 94 | + deriving Typeable |
| 95 | +instance Show TestFailure where |
| 96 | + show (TestFailure out err ec) = concat |
| 97 | + [ "Exited with " ++ show ec |
| 98 | + , "\n\nstdout:\n" |
| 99 | + , toStr out |
| 100 | + , "\n\nstderr:\n" |
| 101 | + , toStr err |
| 102 | + ] |
| 103 | + where |
| 104 | + toStr = TL.unpack . TL.decodeUtf8With lenientDecode |
| 105 | +instance Exception TestFailure |
| 106 | + |
| 107 | +copyStackRoot :: FilePath -> FilePath -> IO () |
| 108 | +copyStackRoot src dst = |
| 109 | + runResourceT $ sourceDirectoryDeep False src $$ CL.mapM_ go |
| 110 | + where |
| 111 | + go srcfp = when toCopy $ liftIO $ do |
| 112 | + Just suffix <- return $ stripPrefix src srcfp |
| 113 | + let dstfp = dst ++ "/" ++ suffix |
| 114 | + createDirectoryIfMissing True $ takeDirectory dstfp |
| 115 | + createSymbolicLink srcfp dstfp |
| 116 | + where |
| 117 | + toCopy = any (`isSuffixOf` srcfp) |
| 118 | + -- FIXME command line parameters to control how many of these get |
| 119 | + -- copied, trade-off of runtime/bandwidth vs isolation of tests |
| 120 | + [ ".tar" |
| 121 | + , ".xz" |
| 122 | + , ".gz" |
| 123 | + , ".7z.exe" |
| 124 | + , "00-index.cache" |
| 125 | + ] |
0 commit comments