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