From 6e26e2c38dd28aaa00e72e24b8d7f0c4af71f038 Mon Sep 17 00:00:00 2001 From: Oleg Grenrus Date: Fri, 13 Jan 2017 09:42:53 +0200 Subject: [PATCH 1/9] Try out the setup.lhs and doctests.hsc from bytes https://github.com/ekmett/bytes/pull/32 --- .gitignore | 1 + Setup.lhs | 142 +++++++++++++++++++++++++++++++++++++-------- distributive.cabal | 3 + tests/doctests.hs | 34 ----------- tests/doctests.hsc | 77 ++++++++++++++++++++++++ 5 files changed, 199 insertions(+), 58 deletions(-) delete mode 100644 tests/doctests.hs create mode 100644 tests/doctests.hsc diff --git a/.gitignore b/.gitignore index 922f8d0..23fe399 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ dist +dist-newstyle docs wiki TAGS diff --git a/Setup.lhs b/Setup.lhs index 46dc623..d3d51a6 100644 --- a/Setup.lhs +++ b/Setup.lhs @@ -1,46 +1,140 @@ -#!/usr/bin/runhaskell \begin{code} -{-# OPTIONS_GHC -Wall #-} +{-# LANGUAGE CPP #-} +#ifndef MIN_VERSION_Cabal +#define MIN_VERSION_Cabal(x,y,z) 0 +#endif +#if MIN_VERSION_Cabal(1,24,0) +#define InstalledPackageId UnitId +#endif module Main (main) where +import Control.Monad ( when ) import Data.List ( nub ) -import Data.Version ( showVersion ) -import Distribution.Package ( PackageName(PackageName), PackageId, InstalledPackageId, packageVersion, packageName ) -import Distribution.PackageDescription ( PackageDescription(), TestSuite(..) ) +import Distribution.Package ( InstalledPackageId ) +import Distribution.Package ( PackageId, Package (..), packageVersion ) +import Distribution.PackageDescription ( PackageDescription(), TestSuite(..) , Library (..), BuildInfo (..)) import Distribution.Simple ( defaultMainWithHooks, UserHooks(..), simpleUserHooks ) import Distribution.Simple.Utils ( rewriteFile, createDirectoryIfMissingVerbose ) import Distribution.Simple.BuildPaths ( autogenModulesDir ) -import Distribution.Simple.Setup ( BuildFlags(buildVerbosity), fromFlag ) -import Distribution.Simple.LocalBuildInfo ( withLibLBI, withTestLBI, LocalBuildInfo(), ComponentLocalBuildInfo(componentPackageDeps) ) -import Distribution.Verbosity ( Verbosity ) +import Distribution.Simple.Setup ( BuildFlags(buildDistPref, buildVerbosity), fromFlag) +import Distribution.Simple.LocalBuildInfo ( withPackageDB, withLibLBI, withTestLBI, LocalBuildInfo(), ComponentLocalBuildInfo(componentPackageDeps), compiler ) +import Distribution.Simple.Compiler ( showCompilerId , PackageDB (..)) +import Distribution.Text ( display , simpleParse ) import System.FilePath ( () ) +#if MIN_VERSION_Cabal(1,25,0) +import Distribution.Simple.BuildPaths ( autogenComponentModulesDir ) +#endif + main :: IO () main = defaultMainWithHooks simpleUserHooks { buildHook = \pkg lbi hooks flags -> do - generateBuildModule (fromFlag (buildVerbosity flags)) pkg lbi + generateBuildModule flags pkg lbi buildHook simpleUserHooks pkg lbi hooks flags } -generateBuildModule :: Verbosity -> PackageDescription -> LocalBuildInfo -> IO () -generateBuildModule verbosity pkg lbi = do - let dir = autogenModulesDir lbi - createDirectoryIfMissingVerbose verbosity True dir - withLibLBI pkg lbi $ \_ libcfg -> do - withTestLBI pkg lbi $ \suite suitecfg -> do - rewriteFile (dir "Build_" ++ testName suite ++ ".hs") $ unlines - [ "module Build_" ++ testName suite ++ " where" +generateBuildModule :: BuildFlags -> PackageDescription -> LocalBuildInfo -> IO () +generateBuildModule flags pkg lbi = do + let verbosity = fromFlag (buildVerbosity flags) + let distPref = fromFlag (buildDistPref flags) + + -- Package DBs + let dbStack = withPackageDB lbi ++ [ SpecificPackageDB $ distPref "package.conf.inplace" ] + let dbFlags = "-hide-all-packages" : packageDbArgs dbStack + + withLibLBI pkg lbi $ \lib libcfg -> do + let libBI = libBuildInfo lib + + -- Lib sources and includes + let iArgs = map ("-i"++) $ hsSourceDirs libBI + let includeArgs = map ("-I"++) $ includeDirs libBI + + -- We need the directory with library's cabal_macros.h! +#if MIN_VERSION_Cabal(1,25,0) + let libAutogenDir = autogenComponentModulesDir lbi libcfg +#else + let libAutogenDir = autogenModulesDir lbi +#endif + + -- CPP includes, i.e. include cabal_macros.h + let cppFlags = [ "-optP-include", "-optP" ++ libAutogenDir ++ "/cabal_macros.h" ] + + -- Actually we need to check whether testName suite == "doctests" + -- pending https://github.com/haskell/cabal/pull/4229 getting into GHC HEAD tree + withTestLBI pkg lbi $ \suite suitecfg -> when (testName suite == testName suite) $ do + + -- get and create autogen dir +#if MIN_VERSION_Cabal(1,25,0) + let testAutogenDir = autogenComponentModulesDir lbi suitecfg +#else + let testAutogenDir = autogenModulesDir lbi +#endif + createDirectoryIfMissingVerbose verbosity True testAutogenDir + + -- write autogen'd file + rewriteFile (testAutogenDir "Build_doctests.hs") $ unlines + [ "module Build_doctests where" , "" - , "autogen_dir :: String" - , "autogen_dir = " ++ show dir + -- -package-id etc. flags + , "pkgs :: [String]" + , "pkgs = " ++ (show $ formatDeps $ testDeps libcfg suitecfg) , "" - , "deps :: [String]" - , "deps = " ++ (show $ formatdeps (testDeps libcfg suitecfg)) + , "flags :: [String]" + , "flags = " ++ show (iArgs ++ includeArgs ++ dbFlags ++ cppFlags) + , "" + , "src_dirs :: [String]" + , "src_dirs = " ++ show (hsSourceDirs libBI) ] where - formatdeps = map (formatone . snd) - formatone p = case packageName p of - PackageName n -> n ++ "-" ++ showVersion (packageVersion p) + -- we do this check in Setup, as then doctests don't need to depend on Cabal + isOldCompiler = maybe False id $ do + a <- simpleParse $ showCompilerId $ compiler lbi + b <- simpleParse "7.5" + return $ packageVersion (a :: PackageId) < b + + formatDeps = map formatOne + formatOne (installedPkgId, pkgId) + -- The problem is how different cabal executables handle package databases + -- when doctests depend on the library + | packageId pkg == pkgId = "-package=" ++ display pkgId + | otherwise = "-package-id=" ++ display installedPkgId + + -- From Distribution.Simple.Program.GHC + packageDbArgs :: [PackageDB] -> [String] + packageDbArgs | isOldCompiler = packageDbArgsConf + | otherwise = packageDbArgsDb + + -- GHC <7.6 uses '-package-conf' instead of '-package-db'. + packageDbArgsConf :: [PackageDB] -> [String] + packageDbArgsConf dbstack = case dbstack of + (GlobalPackageDB:UserPackageDB:dbs) -> concatMap specific dbs + (GlobalPackageDB:dbs) -> ("-no-user-package-conf") + : concatMap specific dbs + _ -> ierror + where + specific (SpecificPackageDB db) = [ "-package-conf=" ++ db ] + specific _ = ierror + ierror = error $ "internal error: unexpected package db stack: " + ++ show dbstack + + -- GHC >= 7.6 uses the '-package-db' flag. See + -- https://ghc.haskell.org/trac/ghc/ticket/5977. + packageDbArgsDb :: [PackageDB] -> [String] + -- special cases to make arguments prettier in common scenarios + packageDbArgsDb dbstack = case dbstack of + (GlobalPackageDB:UserPackageDB:dbs) + | all isSpecific dbs -> concatMap single dbs + (GlobalPackageDB:dbs) + | all isSpecific dbs -> "-no-user-package-db" + : concatMap single dbs + dbs -> "-clear-package-db" + : concatMap single dbs + where + single (SpecificPackageDB db) = [ "-package-db=" ++ db ] + single GlobalPackageDB = [ "-global-package-db" ] + single UserPackageDB = [ "-user-package-db" ] + isSpecific (SpecificPackageDB _) = True + isSpecific _ = False testDeps :: ComponentLocalBuildInfo -> ComponentLocalBuildInfo -> [(InstalledPackageId, PackageId)] testDeps xs ys = nub $ componentPackageDeps xs ++ componentPackageDeps ys diff --git a/distributive.cabal b/distributive.cabal index b8c4812..00f7c9c 100644 --- a/distributive.cabal +++ b/distributive.cabal @@ -25,6 +25,9 @@ source-repository head type: git location: git://github.com/ekmett/distributive.git +custom-setup + setup-depends: base >= 4 && <5, Cabal, filepath, directory + flag semigroups manual: True default: True diff --git a/tests/doctests.hs b/tests/doctests.hs deleted file mode 100644 index 47bac6c..0000000 --- a/tests/doctests.hs +++ /dev/null @@ -1,34 +0,0 @@ -{-# LANGUAGE CPP #-} -module Main where - -import Build_doctests (autogen_dir, deps) -#if __GLASGOW_HASKELL__ < 710 -import Control.Applicative -#endif -import Control.Monad -import Data.List -import System.Directory -import System.FilePath -import Test.DocTest - -main :: IO () -main = getSources >>= \sources -> doctest $ - "-isrc" - : "-isrc-compat" - : ("-i" ++ autogen_dir) - : "-optP-include" - : ("-optP" ++ autogen_dir ++ "/cabal_macros.h") - : "-hide-all-packages" - : map ("-package="++) deps ++ sources - -getSources :: IO [FilePath] -getSources = filter (isSuffixOf ".hs") <$> go "src" - where - go dir = do - (dirs, files) <- getFilesAndDirectories dir - (files ++) . concat <$> mapM go dirs - -getFilesAndDirectories :: FilePath -> IO ([FilePath], [FilePath]) -getFilesAndDirectories dir = do - c <- map (dir ) . filter (`notElem` ["..", "."]) <$> getDirectoryContents dir - (,) <$> filterM doesDirectoryExist c <*> filterM doesFileExist c diff --git a/tests/doctests.hsc b/tests/doctests.hsc new file mode 100644 index 0000000..9f0fb44 --- /dev/null +++ b/tests/doctests.hsc @@ -0,0 +1,77 @@ +{-# LANGUAGE CPP #-} +{-# LANGUAGE ForeignFunctionInterface #-} +----------------------------------------------------------------------------- +-- | +-- Module : Main (doctests) +-- Copyright : (C) 2012-14 Edward Kmett +-- License : BSD-style (see the file LICENSE) +-- Maintainer : Edward Kmett +-- Stability : provisional +-- Portability : portable +-- +-- This module provides doctests for a project based on the actual versions +-- of the packages it was built with. It requires a corresponding Setup.lhs +-- to be added to the project +----------------------------------------------------------------------------- +module Main where + +import Build_doctests (flags, pkgs, src_dirs) +#if __GLASGOW_HASKELL__ < 710 +import Control.Applicative +#endif +import Control.Monad +import Data.List +import System.Directory +import System.FilePath +import Test.DocTest + +##if defined(mingw32_HOST_OS) +##if defined(i386_HOST_ARCH) +##define USE_CP +import Control.Applicative +import Control.Exception +import Foreign.C.Types +foreign import stdcall "windows.h SetConsoleCP" c_SetConsoleCP :: CUInt -> IO Bool +foreign import stdcall "windows.h GetConsoleCP" c_GetConsoleCP :: IO CUInt +##elif defined(x86_64_HOST_ARCH) +##define USE_CP +import Control.Applicative +import Control.Exception +import Foreign.C.Types +foreign import ccall "windows.h SetConsoleCP" c_SetConsoleCP :: CUInt -> IO Bool +foreign import ccall "windows.h GetConsoleCP" c_GetConsoleCP :: IO CUInt +##endif +##endif + +-- | Run in a modified codepage where we can print UTF-8 values on Windows. +withUnicode :: IO a -> IO a +##ifdef USE_CP +withUnicode m = do + cp <- c_GetConsoleCP + (c_SetConsoleCP 65001 >> m) `finally` c_SetConsoleCP cp +##else +withUnicode m = m +##endif + +main :: IO () +main = withUnicode $ getSources >>= \sources -> do + mapM_ putStrLn (args sources) + doctest (args sources) + where + args sources = +#ifdef TRUSTWORTHY + "-DTRUSTWORTHY=1" : +#endif + flags ++ pkgs ++ sources + +getSources :: IO [FilePath] +getSources = filter (isSuffixOf ".hs") . concat <$> mapM go src_dirs + where + go dir = do + (dirs, files) <- getFilesAndDirectories dir + (files ++) . concat <$> mapM go dirs + +getFilesAndDirectories :: FilePath -> IO ([FilePath], [FilePath]) +getFilesAndDirectories dir = do + c <- map (dir ) . filter (`notElem` ["..", "."]) <$> getDirectoryContents dir + (,) <$> filterM doesDirectoryExist c <*> filterM doesFileExist c From bf3334bc12edb8348e26748b0daed2ed4d053f07 Mon Sep 17 00:00:00 2001 From: Oleg Grenrus Date: Fri, 13 Jan 2017 10:20:12 +0200 Subject: [PATCH 2/9] Hardcode modules to doctest in Setup.lhs --- Setup.lhs | 9 +++++++-- distributive.cabal | 4 +--- tests/doctests.hsc | 29 +++++++---------------------- 3 files changed, 15 insertions(+), 27 deletions(-) diff --git a/Setup.lhs b/Setup.lhs index d3d51a6..b9026d5 100644 --- a/Setup.lhs +++ b/Setup.lhs @@ -49,6 +49,11 @@ generateBuildModule flags pkg lbi = do let iArgs = map ("-i"++) $ hsSourceDirs libBI let includeArgs = map ("-I"++) $ includeDirs libBI + -- modules + let modules = exposedModules lib ++ otherModules libBI + -- it seems that doctest is happy to take in module names, not actual files! + let module_sources = modules + -- We need the directory with library's cabal_macros.h! #if MIN_VERSION_Cabal(1,25,0) let libAutogenDir = autogenComponentModulesDir lbi libcfg @@ -82,8 +87,8 @@ generateBuildModule flags pkg lbi = do , "flags :: [String]" , "flags = " ++ show (iArgs ++ includeArgs ++ dbFlags ++ cppFlags) , "" - , "src_dirs :: [String]" - , "src_dirs = " ++ show (hsSourceDirs libBI) + , "module_sources :: [String]" + , "module_sources = " ++ show (map display module_sources) ] where -- we do this check in Setup, as then doctests don't need to depend on Cabal diff --git a/distributive.cabal b/distributive.cabal index 00f7c9c..258bff8 100644 --- a/distributive.cabal +++ b/distributive.cabal @@ -80,9 +80,7 @@ test-suite doctests main-is: doctests.hs build-depends: base >= 4, - directory >= 1.0, - doctest >= 0.9.1, - filepath >= 1.2 + doctest >= 0.9.1 ghc-options: -Wall -threaded hs-source-dirs: tests diff --git a/tests/doctests.hsc b/tests/doctests.hsc index 9f0fb44..ccca157 100644 --- a/tests/doctests.hsc +++ b/tests/doctests.hsc @@ -15,14 +15,11 @@ ----------------------------------------------------------------------------- module Main where -import Build_doctests (flags, pkgs, src_dirs) +import Build_doctests (flags, pkgs, module_sources) #if __GLASGOW_HASKELL__ < 710 import Control.Applicative #endif -import Control.Monad -import Data.List -import System.Directory -import System.FilePath +import Data.Foldable (traverse_) import Test.DocTest ##if defined(mingw32_HOST_OS) @@ -54,24 +51,12 @@ withUnicode m = m ##endif main :: IO () -main = withUnicode $ getSources >>= \sources -> do - mapM_ putStrLn (args sources) - doctest (args sources) +main = withUnicode $ do + traverse_ putStrLn args + doctest args where - args sources = + args = #ifdef TRUSTWORTHY "-DTRUSTWORTHY=1" : #endif - flags ++ pkgs ++ sources - -getSources :: IO [FilePath] -getSources = filter (isSuffixOf ".hs") . concat <$> mapM go src_dirs - where - go dir = do - (dirs, files) <- getFilesAndDirectories dir - (files ++) . concat <$> mapM go dirs - -getFilesAndDirectories :: FilePath -> IO ([FilePath], [FilePath]) -getFilesAndDirectories dir = do - c <- map (dir ) . filter (`notElem` ["..", "."]) <$> getDirectoryContents dir - (,) <$> filterM doesDirectoryExist c <*> filterM doesFileExist c + flags ++ pkgs ++ module_sources From 8af9b8b629e54b7c7512e7ad2a9f732419bef483 Mon Sep 17 00:00:00 2001 From: Oleg Grenrus Date: Fri, 13 Jan 2017 10:32:32 +0200 Subject: [PATCH 3/9] if sdist fails, don't try to install from it --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index cec1af6..7af4bf1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -127,7 +127,8 @@ script: cabal test --show-details=always; cabal bench || true; cabal sdist || true; - SRC_TGZ=$(cabal info . | awk '{print $2;exit}').tar.gz && (cd dist && cabal install --force-reinstalls "$SRC_TGZ");; + SRC_TGZ=$(cabal info . | awk '{print $2;exit}').tar.gz; + if [ -f $SRC_TGZ ]; then (cd dist && cabal install --force-reinstalls "$SRC_TGZ"); fi; esac notifications: From c26e5c031f952eebd3cb41013abdb8e164097c44 Mon Sep 17 00:00:00 2001 From: Oleg Grenrus Date: Fri, 13 Jan 2017 10:57:22 +0200 Subject: [PATCH 4/9] Add libAutogenDir to -i args --- Setup.lhs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Setup.lhs b/Setup.lhs index b9026d5..27a3dbb 100644 --- a/Setup.lhs +++ b/Setup.lhs @@ -45,10 +45,6 @@ generateBuildModule flags pkg lbi = do withLibLBI pkg lbi $ \lib libcfg -> do let libBI = libBuildInfo lib - -- Lib sources and includes - let iArgs = map ("-i"++) $ hsSourceDirs libBI - let includeArgs = map ("-I"++) $ includeDirs libBI - -- modules let modules = exposedModules lib ++ otherModules libBI -- it seems that doctest is happy to take in module names, not actual files! @@ -61,6 +57,10 @@ generateBuildModule flags pkg lbi = do let libAutogenDir = autogenModulesDir lbi #endif + -- Lib sources and includes + let iArgs = map ("-i"++) $ libAutogenDir : hsSourceDirs libBI + let includeArgs = map ("-I"++) $ includeDirs libBI + -- CPP includes, i.e. include cabal_macros.h let cppFlags = [ "-optP-include", "-optP" ++ libAutogenDir ++ "/cabal_macros.h" ] From 549dfd83265c47dc84f20e53cbc2cf7ce4283ece Mon Sep 17 00:00:00 2001 From: Oleg Grenrus Date: Fri, 13 Jan 2017 19:14:56 +0200 Subject: [PATCH 5/9] Add cppOptions --- Setup.lhs | 6 ++++-- tests/doctests.hsc | 6 +----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/Setup.lhs b/Setup.lhs index 27a3dbb..54a781a 100644 --- a/Setup.lhs +++ b/Setup.lhs @@ -62,11 +62,13 @@ generateBuildModule flags pkg lbi = do let includeArgs = map ("-I"++) $ includeDirs libBI -- CPP includes, i.e. include cabal_macros.h - let cppFlags = [ "-optP-include", "-optP" ++ libAutogenDir ++ "/cabal_macros.h" ] + let cppFlags = map ("-optP"++) $ + [ "-include", libAutogenDir ++ "/cabal_macros.h" ] + ++ cppOptions libBI -- Actually we need to check whether testName suite == "doctests" -- pending https://github.com/haskell/cabal/pull/4229 getting into GHC HEAD tree - withTestLBI pkg lbi $ \suite suitecfg -> when (testName suite == testName suite) $ do + withTestLBI pkg lbi $ \suite suitecfg -> when (testName suite == "doctests") $ do -- get and create autogen dir #if MIN_VERSION_Cabal(1,25,0) diff --git a/tests/doctests.hsc b/tests/doctests.hsc index ccca157..d47a333 100644 --- a/tests/doctests.hsc +++ b/tests/doctests.hsc @@ -55,8 +55,4 @@ main = withUnicode $ do traverse_ putStrLn args doctest args where - args = -#ifdef TRUSTWORTHY - "-DTRUSTWORTHY=1" : -#endif - flags ++ pkgs ++ module_sources + args = flags ++ pkgs ++ module_sources From a3229f4454452a2e487e0fb043c4ee4c38f05494 Mon Sep 17 00:00:00 2001 From: Oleg Grenrus Date: Mon, 16 Jan 2017 09:08:06 +0200 Subject: [PATCH 6/9] Use absolute paths for includes --- Setup.lhs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/Setup.lhs b/Setup.lhs index 54a781a..0f89abf 100644 --- a/Setup.lhs +++ b/Setup.lhs @@ -3,6 +3,9 @@ #ifndef MIN_VERSION_Cabal #define MIN_VERSION_Cabal(x,y,z) 0 #endif +#ifndef MIN_VERSION_directory +#define MIN_VERSION_directory(x,y,z) 0 +#endif #if MIN_VERSION_Cabal(1,24,0) #define InstalledPackageId UnitId #endif @@ -26,6 +29,19 @@ import System.FilePath ( () ) import Distribution.Simple.BuildPaths ( autogenComponentModulesDir ) #endif +#if MIN_VERSION_directory(1,2,2) +import System.Directory (makeAbsolute) +#else +import System.Directory (getCurrentDirectory) +import System.FilePath (isAbsolute) + +makeAbsolute :: FilePath -> IO FilePath +makeAbsolute p | isAbsolute p = return p + | otherwise = do + cwd <- getCurrentDirectory + return $ cwd p +#endif + main :: IO () main = defaultMainWithHooks simpleUserHooks { buildHook = \pkg lbi hooks flags -> do @@ -58,8 +74,8 @@ generateBuildModule flags pkg lbi = do #endif -- Lib sources and includes - let iArgs = map ("-i"++) $ libAutogenDir : hsSourceDirs libBI - let includeArgs = map ("-I"++) $ includeDirs libBI + iArgs <- mapM (fmap ("-i"++) . makeAbsolute) $ libAutogenDir : hsSourceDirs libBI + includeArgs <- mapM (fmap ("-I"++) . makeAbsolute) $ includeDirs libBI -- CPP includes, i.e. include cabal_macros.h let cppFlags = map ("-optP"++) $ From 1a6deaedcc1fc2f3b84da4a03887f308d1bc3ebc Mon Sep 17 00:00:00 2001 From: Oleg Grenrus Date: Tue, 24 Jan 2017 14:53:32 +0200 Subject: [PATCH 7/9] ghc-head has IsString for component names --- Setup.lhs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Setup.lhs b/Setup.lhs index 0f89abf..e8dd2e1 100644 --- a/Setup.lhs +++ b/Setup.lhs @@ -1,5 +1,6 @@ \begin{code} {-# LANGUAGE CPP #-} +{-# LANGUAGE OverloadedStrings #-} #ifndef MIN_VERSION_Cabal #define MIN_VERSION_Cabal(x,y,z) 0 #endif @@ -82,8 +83,6 @@ generateBuildModule flags pkg lbi = do [ "-include", libAutogenDir ++ "/cabal_macros.h" ] ++ cppOptions libBI - -- Actually we need to check whether testName suite == "doctests" - -- pending https://github.com/haskell/cabal/pull/4229 getting into GHC HEAD tree withTestLBI pkg lbi $ \suite suitecfg -> when (testName suite == "doctests") $ do -- get and create autogen dir From 415f9142ffa278dac9de9d04f32abfb6a42da573 Mon Sep 17 00:00:00 2001 From: Oleg Grenrus Date: Tue, 24 Jan 2017 15:40:41 +0200 Subject: [PATCH 8/9] Add --allow-newer to GHC-head ci job --- .travis.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7af4bf1..2219314 100644 --- a/.travis.yml +++ b/.travis.yml @@ -48,7 +48,7 @@ matrix: compiler: ": #GHC 8.0.2" addons: {apt: {packages: [cabal-install-1.24,ghc-8.0.2,alex-3.1.4,happy-1.19.5], sources: [hvr-ghc]}} - - env: CABALVER=1.24 GHCVER=head BUILD=cabal + - env: CABALVER=1.24 GHCVER=head BUILD=cabal CABALFLAGS=--allow-newer compiler: ": #GHC head" addons: {apt: {packages: [cabal-install-1.24,ghc-head,alex-3.1.4,happy-1.19.5], sources: [hvr-ghc]}} @@ -61,7 +61,7 @@ matrix: allow_failures: - env: CABALVER=1.18 GHCVER=7.0.4 BUILD=cabal - env: CABALVER=1.18 GHCVER=7.2.2 BUILD=cabal - - env: CABALVER=1.24 GHCVER=head BUILD=cabal + - env: CABALVER=1.24 GHCVER=head BUILD=cabal CABALFLAGS=--allow-newer before_install: - unset CC @@ -91,7 +91,7 @@ install: $HOME/.cabal/packages/hackage.haskell.org/00-index.tar; fi; travis_retry cabal update; - cabal install --only-dependencies --enable-tests --enable-benchmarks --dry -v > installplan.txt; + cabal install --only-dependencies --enable-tests --enable-benchmarks --dry -v $CABALFLAGS> installplan.txt; sed -i -e '1,/^Resolving /d' installplan.txt; cat installplan.txt; if diff -u installplan.txt $HOME/.cabsnap/installplan.txt; then echo "cabal build-cache HIT"; @@ -102,7 +102,7 @@ install: echo "cabal build-cache MISS"; rm -rf $HOME/.cabsnap; mkdir -p $HOME/.ghc $HOME/.cabal/lib $HOME/.cabal/share $HOME/.cabal/bin; - cabal install --only-dependencies --enable-tests --enable-benchmarks; + cabal install --only-dependencies --enable-tests --enable-benchmarks $CABALFLAGS; if [ "$GHCVER" = "7.10.3" ]; then cabal install Cabal-1.22.4.0; fi; fi; if [ ! -d $HOME/.cabsnap ]; then @@ -122,7 +122,7 @@ script: stack) scripts/travis_long stack --no-terminal $STACK_OPTIONS build -j2;; cabal) - cabal configure --enable-tests -v2; + cabal configure --enable-tests -v2 $CABALFLAGS; cabal build; cabal test --show-details=always; cabal bench || true; From 40e211f0783841300e53c10c05be6db6c5c1f838 Mon Sep 17 00:00:00 2001 From: Oleg Grenrus Date: Tue, 31 Jan 2017 20:01:47 +0200 Subject: [PATCH 9/9] Use cabal-doctest --- Setup.lhs | 40 +++++++++++++++++++++++--------- distributive.cabal | 6 +++-- tests/doctests.hs | 25 ++++++++++++++++++++ tests/doctests.hsc | 58 ---------------------------------------------- 4 files changed, 58 insertions(+), 71 deletions(-) create mode 100644 tests/doctests.hs delete mode 100644 tests/doctests.hsc diff --git a/Setup.lhs b/Setup.lhs index e8dd2e1..5555bb3 100644 --- a/Setup.lhs +++ b/Setup.lhs @@ -1,6 +1,19 @@ \begin{code} {-# LANGUAGE CPP #-} {-# LANGUAGE OverloadedStrings #-} +module Main (main) where + +#ifndef MIN_VERSION_cabal_doctest +#define MIN_VERSION_cabal_doctest(x,y,z) 0 +#endif + + +#if MIN_VERSION_cabal_doctest(1,0,0) +import Distribution.Extra.Doctest ( defaultMainWithDoctests ) +#else + +-- Otherwise we provide a shim + #ifndef MIN_VERSION_Cabal #define MIN_VERSION_Cabal(x,y,z) 0 #endif @@ -10,10 +23,10 @@ #if MIN_VERSION_Cabal(1,24,0) #define InstalledPackageId UnitId #endif -module Main (main) where import Control.Monad ( when ) import Data.List ( nub ) +import Data.String ( fromString ) import Distribution.Package ( InstalledPackageId ) import Distribution.Package ( PackageId, Package (..), packageVersion ) import Distribution.PackageDescription ( PackageDescription(), TestSuite(..) , Library (..), BuildInfo (..)) @@ -43,15 +56,8 @@ makeAbsolute p | isAbsolute p = return p return $ cwd p #endif -main :: IO () -main = defaultMainWithHooks simpleUserHooks - { buildHook = \pkg lbi hooks flags -> do - generateBuildModule flags pkg lbi - buildHook simpleUserHooks pkg lbi hooks flags - } - -generateBuildModule :: BuildFlags -> PackageDescription -> LocalBuildInfo -> IO () -generateBuildModule flags pkg lbi = do +generateBuildModule :: String -> BuildFlags -> PackageDescription -> LocalBuildInfo -> IO () +generateBuildModule testsuiteName flags pkg lbi = do let verbosity = fromFlag (buildVerbosity flags) let distPref = fromFlag (buildDistPref flags) @@ -83,7 +89,7 @@ generateBuildModule flags pkg lbi = do [ "-include", libAutogenDir ++ "/cabal_macros.h" ] ++ cppOptions libBI - withTestLBI pkg lbi $ \suite suitecfg -> when (testName suite == "doctests") $ do + withTestLBI pkg lbi $ \suite suitecfg -> when (testName suite == fromString testsuiteName) $ do -- get and create autogen dir #if MIN_VERSION_Cabal(1,25,0) @@ -161,4 +167,16 @@ generateBuildModule flags pkg lbi = do testDeps :: ComponentLocalBuildInfo -> ComponentLocalBuildInfo -> [(InstalledPackageId, PackageId)] testDeps xs ys = nub $ componentPackageDeps xs ++ componentPackageDeps ys +defaultMainWithDoctests :: String -> IO () +defaultMainWithDoctests testSuiteName = defaultMainWithHooks simpleUserHooks + { buildHook = \pkg lbi hooks flags -> do + generateBuildModule testSuiteName flags pkg lbi + buildHook simpleUserHooks pkg lbi hooks flags + } + +#endif + +main :: IO () +main = defaultMainWithDoctests "doctests" + \end{code} diff --git a/distributive.cabal b/distributive.cabal index 258bff8..51dc425 100644 --- a/distributive.cabal +++ b/distributive.cabal @@ -26,7 +26,9 @@ source-repository head location: git://github.com/ekmett/distributive.git custom-setup - setup-depends: base >= 4 && <5, Cabal, filepath, directory + setup-depends: + base >= 4 && <5, + cabal-doctest >= 1 && <1.1 flag semigroups manual: True @@ -80,7 +82,7 @@ test-suite doctests main-is: doctests.hs build-depends: base >= 4, - doctest >= 0.9.1 + doctest >= 0.11.1 && <0.12 ghc-options: -Wall -threaded hs-source-dirs: tests diff --git a/tests/doctests.hs b/tests/doctests.hs new file mode 100644 index 0000000..2d080e7 --- /dev/null +++ b/tests/doctests.hs @@ -0,0 +1,25 @@ +----------------------------------------------------------------------------- +-- | +-- Module : Main (doctests) +-- Copyright : (C) 2012-14 Edward Kmett +-- License : BSD-style (see the file LICENSE) +-- Maintainer : Edward Kmett +-- Stability : provisional +-- Portability : portable +-- +-- This module provides doctests for a project based on the actual versions +-- of the packages it was built with. It requires a corresponding Setup.lhs +-- to be added to the project +----------------------------------------------------------------------------- +module Main where + +import Build_doctests (flags, pkgs, module_sources) +import Data.Foldable (traverse_) +import Test.DocTest + +main :: IO () +main = do + traverse_ putStrLn args + doctest args + where + args = flags ++ pkgs ++ module_sources diff --git a/tests/doctests.hsc b/tests/doctests.hsc deleted file mode 100644 index d47a333..0000000 --- a/tests/doctests.hsc +++ /dev/null @@ -1,58 +0,0 @@ -{-# LANGUAGE CPP #-} -{-# LANGUAGE ForeignFunctionInterface #-} ------------------------------------------------------------------------------ --- | --- Module : Main (doctests) --- Copyright : (C) 2012-14 Edward Kmett --- License : BSD-style (see the file LICENSE) --- Maintainer : Edward Kmett --- Stability : provisional --- Portability : portable --- --- This module provides doctests for a project based on the actual versions --- of the packages it was built with. It requires a corresponding Setup.lhs --- to be added to the project ------------------------------------------------------------------------------ -module Main where - -import Build_doctests (flags, pkgs, module_sources) -#if __GLASGOW_HASKELL__ < 710 -import Control.Applicative -#endif -import Data.Foldable (traverse_) -import Test.DocTest - -##if defined(mingw32_HOST_OS) -##if defined(i386_HOST_ARCH) -##define USE_CP -import Control.Applicative -import Control.Exception -import Foreign.C.Types -foreign import stdcall "windows.h SetConsoleCP" c_SetConsoleCP :: CUInt -> IO Bool -foreign import stdcall "windows.h GetConsoleCP" c_GetConsoleCP :: IO CUInt -##elif defined(x86_64_HOST_ARCH) -##define USE_CP -import Control.Applicative -import Control.Exception -import Foreign.C.Types -foreign import ccall "windows.h SetConsoleCP" c_SetConsoleCP :: CUInt -> IO Bool -foreign import ccall "windows.h GetConsoleCP" c_GetConsoleCP :: IO CUInt -##endif -##endif - --- | Run in a modified codepage where we can print UTF-8 values on Windows. -withUnicode :: IO a -> IO a -##ifdef USE_CP -withUnicode m = do - cp <- c_GetConsoleCP - (c_SetConsoleCP 65001 >> m) `finally` c_SetConsoleCP cp -##else -withUnicode m = m -##endif - -main :: IO () -main = withUnicode $ do - traverse_ putStrLn args - doctest args - where - args = flags ++ pkgs ++ module_sources