git.iko.soy/typeable/cabal2nix
on master help sign in
test: run the same tests with both the cabal2nix library and the executable
commit95c25b73813ae9f1c170e714e50ad809b56bf5ba authorPeter Simons <simons@cryp.to> dateThu 28 Jun 2018, 16:14 (8 years ago) parente19c746
2 files changed+66−21
cabal2nix.cabal+5−2 test/Main.hs+61−19
cabal2nix.cabal+5−2
@@ -189,15 +189,18 @@ test-suite regression-test
189189 default-extensions: MonadFailDesugaring
190190 ghc-options: -Wall -Wcompat -Wincomplete-uni-patterns -Wincomplete-record-updates
191191 -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
192+ -with-rtsopts=-K1K
192193 build-depends: base
193194 , Cabal >2.2
194195 , cabal2nix
195− , filepath
196+ , containers
196197 , directory
198+ , filepath
197199 , language-nix
198200 , lens
199201 , pretty
202+ , process >1.2
200203 , tasty
201204 , tasty-golden
202− ghc-options: -Wall -with-rtsopts=-K1K
205+ build-tools: cabal2nix
203206 default-language: Haskell2010
test/Main.hs+61−19
@@ -1,12 +1,17 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+
13module Main ( main ) where
24
3−import Distribution.Nixpkgs.Haskell.FromCabal
4−import Distribution.Nixpkgs.Haskell.Derivation
55import Distribution.Nixpkgs.Fetch
6+import Distribution.Nixpkgs.Haskell.Derivation
7+import Distribution.Nixpkgs.Haskell.FromCabal
8+import Distribution.Nixpkgs.Haskell.FromCabal.Flags
69
710import Control.Lens
811import Control.Monad
12+import qualified Data.Set as Set
913import Distribution.Compiler
14+import Distribution.Package
1015import Distribution.PackageDescription
1116import Distribution.PackageDescription.Parsec
1217import Distribution.System
@@ -15,6 +20,7 @@ import Distribution.Version
1520import Language.Nix
1621import System.Directory
1722import System.FilePath
23+import System.Process
1824import Test.Tasty
1925import Test.Tasty.Golden
2026import Text.PrettyPrint.HughesPJClass
@@ -23,36 +29,65 @@ main :: IO ()
2329main = do
2430 -- TODO: run this test with all kinds of setLocaleEncoding values to ensure we don't
2531 -- depend on the system environment: https://github.com/NixOS/cabal2nix/issues/333
32+ cabal2nix <- findExecutable "cabal2nix" >>= maybe (fail "cannot find 'cabal2nix' in $PATH") return
2633 testCases <- listDirectoryFilesBySuffix ".cabal" "test/golden-test-cases"
27− defaultMain $ testGroup "regression-tests" (map regressionTest testCases)
34+ defaultMain $ testGroup "regression-tests"
35+ [ testGroup "cabal2nix library" (map testLibrary testCases)
36+ , testGroup "cabal2nix executable" (map (testExecutable cabal2nix) testCases)
37+ ]
2838
29−regressionTest :: String -> TestTree
30−regressionTest cabalFile = do
39+testLibrary :: String -> TestTree
40+testLibrary cabalFile = do
3141 let nixFile = cabalFile `replaceExtension` "nix"
3242 goldenFile = nixFile `addExtension` "golden"
3343
3444 cabal2nix :: GenericPackageDescription -> Derivation
3545 cabal2nix gpd = fromGenericPackageDescription
36− (const True)
37− (\i -> Just (binding # (i, path # [i])))
38− (Platform X86_64 Linux)
39− (unknownCompilerInfo (CompilerId GHC (mkVersion [8,2])) NoAbiTag)
40− mempty
41− []
42− gpd
43− & src .~ DerivationSource
44− { derivKind = "url"
45− , derivUrl = "http://example.org/"
46− , derivRevision = ""
47− , derivHash = "abc"
48− }
46+ (const True)
47+ (\i -> Just (binding # (i, path # [i])))
48+ (Platform X86_64 Linux)
49+ (unknownCompilerInfo (CompilerId GHC (mkVersion [8,2])) NoAbiTag)
50+ (configureCabalFlags (packageId gpd))
51+ []
52+ gpd
53+ & src .~ DerivationSource
54+ { derivKind = "url"
55+ , derivUrl = "mirror://hackage/foo.tar.gz"
56+ , derivRevision = ""
57+ , derivHash = "deadbeef"
58+ }
59+ & extraFunctionArgs %~ Set.union (Set.singleton "inherit stdenv")
4960 goldenVsFileDiff
5061 nixFile
5162 (\ref new -> ["diff", "-u", ref, new])
5263 goldenFile
5364 nixFile
54− (readGenericPackageDescription silent cabalFile >>= writeFile nixFile . prettyShow . cabal2nix)
65+ (readGenericPackageDescription silent cabalFile >>= writeFileLn nixFile . prettyShow . cabal2nix)
66+
67+-- | TODO:
68+--
69+-- * Run cabal2nix in an empty environment. Currently, this fails because the
70+-- tool uses $HOME to write cache files for SHA256 hashes. To fix this, use
71+-- of the cache directory ought to be optional. Arguably, the whole notion
72+-- of caching those hashes is a bit silly in the presense of format 01
73+-- cabal-install tarballs which contain them already.
74+--
75+-- * If we cannot get along without $HOME, then we should at least set it to
76+-- some temporary location that is empty.
77+--
78+-- * Run this test with different kinds of encodings.
79+
5580
81+testExecutable :: FilePath -> FilePath -> TestTree
82+testExecutable exe cabalFile = do
83+ let nixFile = cabalFile `replaceExtension` "nix"
84+ goldenFile = nixFile `addExtension` "golden"
85+ goldenVsFileDiff
86+ nixFile
87+ (\ref new -> ["diff", "-u", ref, new])
88+ goldenFile
89+ nixFile
90+ (callCommand (unwords ["env LANG=en_US.UTF-8", exe, "--sha256=deadbeef", "--compiler=ghc-8.2", "--", cabalFile, ">"++nixFile]))
5691
5792-------------------------------------------------------------------------------
5893-- * Helper functions
@@ -80,3 +115,10 @@ listDirectoryFiles dirPath = do
80115listDirectoryFilesBySuffix :: String -> FilePath -> IO [FilePath]
81116listDirectoryFilesBySuffix suff =
82117 fmap (filter ((suff ==) . takeExtension)) . listDirectoryFiles
118+
119+
120+-- | A variant of 'writeFile' that appends a newline to the end of the buffer
121+-- before writing.
122+
123+writeFileLn :: FilePath -> String -> IO ()
124+writeFileLn p buf = writeFile p (buf ++ "\n")