git.iko.soy/typeable/cabal2nix
on master help sign in
test: run the same tests with both the cabal2nix library and the executable
95c25b7Peter Simons
cabal2nix.cabal+5−2
@@ -189,15 +189,18 @@ test-suite regression-test
189 default-extensions: MonadFailDesugaring
189 default-extensions: MonadFailDesugaring
190 ghc-options: -Wall -Wcompat -Wincomplete-uni-patterns -Wincomplete-record-updates
190 ghc-options: -Wall -Wcompat -Wincomplete-uni-patterns -Wincomplete-record-updates
191 -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
191 -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
192+ -with-rtsopts=-K1K
192 build-depends: base
193 build-depends: base
193 , Cabal >2.2
194 , Cabal >2.2
194 , cabal2nix
195 , cabal2nix
195− , filepath
196+ , containers
196 , directory
197 , directory
198+ , filepath
197 , language-nix
199 , language-nix
198 , lens
200 , lens
199 , pretty
201 , pretty
202+ , process >1.2
200 , tasty
203 , tasty
201 , tasty-golden
204 , tasty-golden
202− ghc-options: -Wall -with-rtsopts=-K1K
205+ build-tools: cabal2nix
203 default-language: Haskell2010
206 default-language: Haskell2010
test/Main.hs+61−19
@@ -1,12 +1,17 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+
1module Main ( main ) where
3module Main ( main ) where
2
4
3−import Distribution.Nixpkgs.Haskell.FromCabal
4−import Distribution.Nixpkgs.Haskell.Derivation
5import Distribution.Nixpkgs.Fetch
5import Distribution.Nixpkgs.Fetch
6+import Distribution.Nixpkgs.Haskell.Derivation
7+import Distribution.Nixpkgs.Haskell.FromCabal
8+import Distribution.Nixpkgs.Haskell.FromCabal.Flags
6
9
7import Control.Lens
10import Control.Lens
8import Control.Monad
11import Control.Monad
12+import qualified Data.Set as Set
9import Distribution.Compiler
13import Distribution.Compiler
14+import Distribution.Package
10import Distribution.PackageDescription
15import Distribution.PackageDescription
11import Distribution.PackageDescription.Parsec
16import Distribution.PackageDescription.Parsec
12import Distribution.System
17import Distribution.System
@@ -15,6 +20,7 @@ import Distribution.Version
15import Language.Nix
20import Language.Nix
16import System.Directory
21import System.Directory
17import System.FilePath
22import System.FilePath
23+import System.Process
18import Test.Tasty
24import Test.Tasty
19import Test.Tasty.Golden
25import Test.Tasty.Golden
20import Text.PrettyPrint.HughesPJClass
26import Text.PrettyPrint.HughesPJClass
@@ -23,36 +29,65 @@ main :: IO ()
23main = do
29main = do
24 -- TODO: run this test with all kinds of setLocaleEncoding values to ensure we don't
30 -- TODO: run this test with all kinds of setLocaleEncoding values to ensure we don't
25 -- depend on the system environment: https://github.com/NixOS/cabal2nix/issues/333
31 -- depend on the system environment: https://github.com/NixOS/cabal2nix/issues/333
32+ cabal2nix <- findExecutable "cabal2nix" >>= maybe (fail "cannot find 'cabal2nix' in $PATH") return
26 testCases <- listDirectoryFilesBySuffix ".cabal" "test/golden-test-cases"
33 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+ ]
28
38
29−regressionTest :: String -> TestTree
39+testLibrary :: String -> TestTree
30−regressionTest cabalFile = do
40+testLibrary cabalFile = do
31 let nixFile = cabalFile `replaceExtension` "nix"
41 let nixFile = cabalFile `replaceExtension` "nix"
32 goldenFile = nixFile `addExtension` "golden"
42 goldenFile = nixFile `addExtension` "golden"
33
43
34 cabal2nix :: GenericPackageDescription -> Derivation
44 cabal2nix :: GenericPackageDescription -> Derivation
35 cabal2nix gpd = fromGenericPackageDescription
45 cabal2nix gpd = fromGenericPackageDescription
36− (const True)
46+ (const True)
37− (\i -> Just (binding # (i, path # [i])))
47+ (\i -> Just (binding # (i, path # [i])))
38− (Platform X86_64 Linux)
48+ (Platform X86_64 Linux)
39− (unknownCompilerInfo (CompilerId GHC (mkVersion [8,2])) NoAbiTag)
49+ (unknownCompilerInfo (CompilerId GHC (mkVersion [8,2])) NoAbiTag)
40− mempty
50+ (configureCabalFlags (packageId gpd))
41− []
51+ []
42− gpd
52+ gpd
43− & src .~ DerivationSource
53+ & src .~ DerivationSource
44− { derivKind = "url"
54+ { derivKind = "url"
45− , derivUrl = "http://example.org/"
55+ , derivUrl = "mirror://hackage/foo.tar.gz"
46− , derivRevision = ""
56+ , derivRevision = ""
47− , derivHash = "abc"
57+ , derivHash = "deadbeef"
48− }
58+ }
59+ & extraFunctionArgs %~ Set.union (Set.singleton "inherit stdenv")
49 goldenVsFileDiff
60 goldenVsFileDiff
50 nixFile
61 nixFile
51 (\ref new -> ["diff", "-u", ref, new])
62 (\ref new -> ["diff", "-u", ref, new])
52 goldenFile
63 goldenFile
53 nixFile
64 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+
55
80
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]))
56
91
57-------------------------------------------------------------------------------
92-------------------------------------------------------------------------------
58-- * Helper functions
93-- * Helper functions
@@ -80,3 +115,10 @@ listDirectoryFiles dirPath = do
80listDirectoryFilesBySuffix :: String -> FilePath -> IO [FilePath]
115listDirectoryFilesBySuffix :: String -> FilePath -> IO [FilePath]
81listDirectoryFilesBySuffix suff =
116listDirectoryFilesBySuffix suff =
82 fmap (filter ((suff ==) . takeExtension)) . listDirectoryFiles
117 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")