Commit a1692da9 authored by Lennart Spitzner's avatar Lennart Spitzner
Browse files

cabal-install: Fix PackageTests using user's config

- previously, tests did not specify config file; default one in ~/.cabal/
  was used. test result depended on non-repository content.
- now, PackageTester has extra parameter for config-file to use.
- the default procedure now is to generate a default config file in
  "PackageTests/cabal-config" and use that.
- specific tests also are able to use non-default config.
parent 7ece59b3
......@@ -20,10 +20,14 @@ import Distribution.Verbosity (normal)
-- Third party modules.
import qualified Control.Exception.Extensible as E
import System.Directory
(canonicalizePath, getCurrentDirectory, setCurrentDirectory)
( canonicalizePath, getCurrentDirectory, setCurrentDirectory
, removeFile, doesFileExist )
import System.FilePath ((</>))
import Test.Framework (Test, defaultMain, testGroup)
import Control.Monad ( when )
-- Modules containing the tests.
import qualified PackageTests.PackageTester (checkBasePath)
import qualified PackageTests.Exec.Check
import qualified PackageTests.Freeze.Check
import qualified PackageTests.MultipleSource.Check
......@@ -55,9 +59,20 @@ main = do
putStrLn $ "Using cabal: " ++ cabalPath
putStrLn $ "Using ghc-pkg: " ++ ghcPkgPath
cwd <- getCurrentDirectory
let confFile = PackageTests.PackageTester.checkBasePath </> "cabal-config"
removeConf = do
b <- doesFileExist confFile
putStrLn confFile
when b $ removeFile confFile
let runTests = do
setCurrentDirectory "tests"
removeConf -- assert that there is no existing config file
-- (we want deterministic testing with the default
-- config values)
defaultMain $ tests cabalPath ghcPkgPath
-- Change back to the old working directory so that the tests can be
-- repeatedly run in `cabal repl` via `:main`.
runTests `E.finally` setCurrentDirectory cwd
runTests `E.finally` do
-- remove the default config file that got created by the tests
removeConf
-- Change back to the old working directory so that the tests can be
-- repeatedly run in `cabal repl` via `:main`.
setCurrentDirectory cwd
......@@ -16,16 +16,19 @@ import System.FilePath ((</>))
import System.Directory (getDirectoryContents)
dir :: FilePath
dir = "PackageTests" </> "Exec"
dir = checkBasePath </> "Exec"
configPath :: FilePath
configPath = checkDefaultConfigRelativePath
tests :: FilePath -> FilePath -> [TF.Test]
tests cabalPath ghcPkgPath =
[ testCase "exits with failure if given no argument" $ do
result <- cabal_exec dir [] cabalPath
result <- cabal_exec dir [] cabalPath configPath
assertExecFailed result
, testCase "prints error message if given no argument" $ do
result <- cabal_exec dir [] cabalPath
result <- cabal_exec dir [] cabalPath configPath
assertExecFailed result
let output = outputText result
expected = "specify an executable to run"
......@@ -35,7 +38,7 @@ tests cabalPath ghcPkgPath =
expected `isInfixOf` (intercalate " " . lines $ output)
, testCase "runs the given command" $ do
result <- cabal_exec dir ["echo", "this", "string"] cabalPath
result <- cabal_exec dir ["echo", "this", "string"] cabalPath configPath
assertExecSucceeded result
let output = outputText result
expected = "this string"
......@@ -53,7 +56,7 @@ tests cabalPath ghcPkgPath =
assertMyExecutableNotFound cabalPath
assertPackageInstall cabalPath
result <- cabal_exec dir ["my-executable"] cabalPath
result <- cabal_exec dir ["my-executable"] cabalPath configPath
assertExecSucceeded result
let output = outputText result
expected = "This is my-executable"
......@@ -66,7 +69,7 @@ tests cabalPath ghcPkgPath =
assertMyExecutableNotFound cabalPath
assertPackageInstall cabalPath
result <- cabal_exec dir ["bash", "--", "-c", "my-executable"] cabalPath
result <- cabal_exec dir ["bash", "--", "-c", "my-executable"] cabalPath configPath
assertExecSucceeded result
let output = outputText result
expected = "This is my-executable"
......@@ -82,7 +85,7 @@ tests cabalPath ghcPkgPath =
assertMyLibIsNotAvailableOutsideofSandbox ghcPkgPath libNameAndVersion
result <- cabal_exec dir ["ghc-pkg", "list"] cabalPath
result <- cabal_exec dir ["ghc-pkg", "list"] cabalPath configPath
assertExecSucceeded result
let output = outputText result
errMsg = "my library should have been found"
......@@ -94,25 +97,24 @@ tests cabalPath ghcPkgPath =
-- , testCase "configures cabal to use the sandbox" $ do
]
cleanPreviousBuilds :: FilePath -> IO ()
cleanPreviousBuilds cabalPath = do
sandboxExists <- not . null . filter (== "cabal.sandbox.config") <$>
getDirectoryContents dir
assertCleanSucceeded =<< cabal_clean dir [] cabalPath
assertCleanSucceeded =<< cabal_clean dir [] cabalPath configPath
when sandboxExists $ do
assertSandboxSucceeded =<< cabal_sandbox dir ["delete"] cabalPath
assertSandboxSucceeded =<< cabal_sandbox dir ["delete"] cabalPath configPath
assertPackageInstall :: FilePath -> IO ()
assertPackageInstall cabalPath = do
assertSandboxSucceeded =<< cabal_sandbox dir ["init"] cabalPath
assertInstallSucceeded =<< cabal_install dir [] cabalPath
assertSandboxSucceeded =<< cabal_sandbox dir ["init"] cabalPath configPath
assertInstallSucceeded =<< cabal_install dir [] cabalPath configPath
assertMyExecutableNotFound :: FilePath -> IO ()
assertMyExecutableNotFound cabalPath = do
result <- cabal_exec dir ["my-executable"] cabalPath
result <- cabal_exec dir ["my-executable"] cabalPath configPath
assertExecFailed result
let output = outputText result
expected = "cabal: The program 'my-executable' is required but it " ++
......
......@@ -17,18 +17,21 @@ import System.FilePath ((</>))
import System.IO.Error (isDoesNotExistError)
dir :: FilePath
dir = "PackageTests" </> "Freeze"
dir = checkBasePath </> "Freeze"
configPath :: FilePath
configPath = checkDefaultConfigRelativePath
tests :: FilePath -> [TF.Test]
tests cabalPath =
[ testCase "runs without error" $ do
removeCabalConfig
result <- cabal_freeze dir [] cabalPath
result <- cabal_freeze dir [] cabalPath configPath
assertFreezeSucceeded result
, testCase "freezes direct dependencies" $ do
removeCabalConfig
result <- cabal_freeze dir [] cabalPath
result <- cabal_freeze dir [] cabalPath configPath
assertFreezeSucceeded result
c <- readCabalConfig
assertBool ("should have frozen base\n" ++ c) $
......@@ -36,7 +39,7 @@ tests cabalPath =
, testCase "freezes transitory dependencies" $ do
removeCabalConfig
result <- cabal_freeze dir [] cabalPath
result <- cabal_freeze dir [] cabalPath configPath
assertFreezeSucceeded result
c <- readCabalConfig
assertBool ("should have frozen ghc-prim\n" ++ c) $
......@@ -46,7 +49,7 @@ tests cabalPath =
-- XXX Test this against a package installed in the sandbox but
-- not depended upon.
removeCabalConfig
result <- cabal_freeze dir [] cabalPath
result <- cabal_freeze dir [] cabalPath configPath
assertFreezeSucceeded result
c <- readCabalConfig
assertBool ("should not have frozen exceptions\n" ++ c) $ not $
......@@ -54,7 +57,7 @@ tests cabalPath =
, testCase "does not include a constraint for the package being frozen" $ do
removeCabalConfig
result <- cabal_freeze dir [] cabalPath
result <- cabal_freeze dir [] cabalPath configPath
assertFreezeSucceeded result
c <- readCabalConfig
assertBool ("should not have frozen self\n" ++ c) $ not $
......@@ -62,14 +65,14 @@ tests cabalPath =
, testCase "--dry-run does not modify the cabal.config file" $ do
removeCabalConfig
result <- cabal_freeze dir ["--dry-run"] cabalPath
result <- cabal_freeze dir ["--dry-run"] cabalPath configPath
assertFreezeSucceeded result
c <- doesFileExist $ dir </> "cabal.config"
assertBool "cabal.config file should not have been created" (not c)
, testCase "--enable-tests freezes test dependencies" $ do
removeCabalConfig
result <- cabal_freeze dir ["--enable-tests"] cabalPath
result <- cabal_freeze dir ["--enable-tests"] cabalPath configPath
assertFreezeSucceeded result
c <- readCabalConfig
assertBool ("should have frozen test-framework\n" ++ c) $
......@@ -77,7 +80,7 @@ tests cabalPath =
, testCase "--disable-tests does not freeze test dependencies" $ do
removeCabalConfig
result <- cabal_freeze dir ["--disable-tests"] cabalPath
result <- cabal_freeze dir ["--disable-tests"] cabalPath configPath
assertFreezeSucceeded result
c <- readCabalConfig
assertBool ("should not have frozen test-framework\n" ++ c) $ not $
......@@ -85,7 +88,7 @@ tests cabalPath =
, testCase "--enable-benchmarks freezes benchmark dependencies" $ do
removeCabalConfig
result <- cabal_freeze dir ["--disable-benchmarks"] cabalPath
result <- cabal_freeze dir ["--disable-benchmarks"] cabalPath configPath
assertFreezeSucceeded result
c <- readCabalConfig
assertBool ("should not have frozen criterion\n" ++ c) $ not $
......@@ -93,14 +96,13 @@ tests cabalPath =
, testCase "--disable-benchmarks does not freeze benchmark dependencies" $ do
removeCabalConfig
result <- cabal_freeze dir ["--disable-benchmarks"] cabalPath
result <- cabal_freeze dir ["--disable-benchmarks"] cabalPath configPath
assertFreezeSucceeded result
c <- readCabalConfig
assertBool ("should not have frozen criterion\n" ++ c) $ not $
" criterion ==" `isInfixOf` (intercalate " " $ lines $ c)
]
removeCabalConfig :: IO ()
removeCabalConfig = do
removeFile (dir </> "cabal.config")
......
......@@ -13,16 +13,20 @@ import System.Directory (doesDirectoryExist)
import System.FilePath ((</>))
dir :: FilePath
dir = "PackageTests" </> "MultipleSource"
dir = checkBasePath </> "MultipleSource"
tests :: FilePath -> [TF.Test]
tests cabalPath =
[ testCase "finds second source of multiple source" $ do
sandboxExists <- doesDirectoryExist $ dir </> ".cabal-sandbox"
let execute cmd params = cmd dir
params
cabalPath
checkDefaultConfigRelativePath
when sandboxExists $
void $ cabal_sandbox dir ["delete"] cabalPath
assertSandboxSucceeded =<< cabal_sandbox dir ["init"] cabalPath
assertSandboxSucceeded =<< cabal_sandbox dir ["add-source", "p"] cabalPath
assertSandboxSucceeded =<< cabal_sandbox dir ["add-source", "q"] cabalPath
assertInstallSucceeded =<< cabal_install dir ["q"] cabalPath
void $ execute cabal_sandbox ["delete"]
assertSandboxSucceeded =<< execute cabal_sandbox ["init"]
assertSandboxSucceeded =<< execute cabal_sandbox ["add-source", "p"]
assertSandboxSucceeded =<< execute cabal_sandbox ["add-source", "q"]
assertInstallSucceeded =<< execute cabal_install ["q"]
]
......@@ -19,6 +19,10 @@
module PackageTests.PackageTester
( Result(..)
, checkBasePath
, checkDefaultConfigFile
, checkDefaultConfigRelativePath
-- * Running cabal commands
, cabal_clean
, cabal_exec
......@@ -42,7 +46,7 @@ import Data.Maybe (fromMaybe)
import System.Directory (canonicalizePath, doesFileExist)
import System.Environment (getEnv)
import System.Exit (ExitCode(ExitSuccess))
import System.FilePath ((<.>))
import System.FilePath ( (<.>), (</>) )
import System.IO (hClose, hGetChar, hIsEOF)
import System.IO.Error (isDoesNotExistError)
import System.Process (runProcess, waitForProcess)
......@@ -75,6 +79,18 @@ data Result = Result
nullResult :: Result
nullResult = Result True Failure ""
------------------------------------------------------------------------
-- * Config
checkBasePath :: FilePath
checkBasePath = "PackageTests"
checkDefaultConfigFile :: FilePath
checkDefaultConfigFile = "cabal-config"
checkDefaultConfigRelativePath :: FilePath
checkDefaultConfigRelativePath = ".." </> checkDefaultConfigFile
------------------------------------------------------------------------
-- * Running cabal commands
......@@ -89,39 +105,46 @@ recordRun (cmd, exitCode, exeOutput) thisSucc res =
}
-- | Run the clean command and return its result.
cabal_clean :: FilePath -> [String] -> FilePath -> IO Result
cabal_clean dir args cabalPath = do
res <- cabal dir (["clean"] ++ args) cabalPath
cabal_clean :: FilePath -> [String] -> FilePath -> FilePath -> IO Result
cabal_clean dir args cabalPath configPath = do
res <- cabal dir (["clean"] ++ args) cabalPath configPath
return $ recordRun res CleanSuccess nullResult
-- | Run the exec command and return its result.
cabal_exec :: FilePath -> [String] -> FilePath -> IO Result
cabal_exec dir args cabalPath = do
res <- cabal dir (["exec"] ++ args) cabalPath
cabal_exec :: FilePath -> [String] -> FilePath -> FilePath -> IO Result
cabal_exec dir args cabalPath configPath = do
res <- cabal dir (["exec"] ++ args) cabalPath configPath
return $ recordRun res ExecSuccess nullResult
-- | Run the freeze command and return its result.
cabal_freeze :: FilePath -> [String] -> FilePath -> IO Result
cabal_freeze dir args cabalPath = do
res <- cabal dir (["freeze"] ++ args) cabalPath
cabal_freeze :: FilePath -> [String] -> FilePath -> FilePath -> IO Result
cabal_freeze dir args cabalPath configPath = do
res <- cabal dir (["freeze"] ++ args) cabalPath configPath
return $ recordRun res FreezeSuccess nullResult
-- | Run the install command and return its result.
cabal_install :: FilePath -> [String] -> FilePath -> IO Result
cabal_install dir args cabalPath = do
res <- cabal dir (["install"] ++ args) cabalPath
cabal_install :: FilePath -> [String] -> FilePath -> FilePath -> IO Result
cabal_install dir args cabalPath configPath = do
res <- cabal dir (["install"] ++ args) cabalPath configPath
return $ recordRun res InstallSuccess nullResult
-- | Run the sandbox command and return its result.
cabal_sandbox :: FilePath -> [String] -> FilePath -> IO Result
cabal_sandbox dir args cabalPath = do
res <- cabal dir (["sandbox"] ++ args) cabalPath
cabal_sandbox :: FilePath -> [String] -> FilePath -> FilePath -> IO Result
cabal_sandbox dir args cabalPath configPath = do
res <- cabal dir (["sandbox"] ++ args) cabalPath configPath
return $ recordRun res SandboxSuccess nullResult
-- | Returns the command that was issued, the return code, and the output text.
cabal :: FilePath -> [String] -> FilePath -> IO (String, ExitCode, String)
cabal dir cabalArgs cabalPath = do
run (Just dir) cabalPath cabalArgs
cabal :: FilePath
-> [String]
-> FilePath
-> FilePath
-> IO (String, ExitCode, String)
cabal dir cabalArgs cabalPath configPath = do
run (Just dir) cabalPath args
where
args = configFileArg : cabalArgs
configFileArg = "--config-file=" ++ configPath
-- | Returns the command that was issued, the return code, and the output text
run :: Maybe FilePath -> String -> [String] -> IO (String, ExitCode, String)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment