Skip to content

Commit

Permalink
Add FileCreators.generateCabalFile unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-renaud committed Apr 9, 2020
1 parent a09afa7 commit abdf2d7
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cabal-install/Distribution/Client/Init/FileCreators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ module Distribution.Client.Init.FileCreators (
, createMainHs
, createTestSuiteIfEligible
, writeCabalFile

-- * For testing
, generateCabalFile
) where

import Prelude ()
Expand Down
1 change: 1 addition & 0 deletions cabal-install/cabal-install.cabal.pp
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@
UnitTests.Distribution.Client.GenericInstances
UnitTests.Distribution.Client.Glob
UnitTests.Distribution.Client.GZipUtils
UnitTests.Distribution.Client.Init.FileCreators
UnitTests.Distribution.Client.Sandbox
UnitTests.Distribution.Client.Sandbox.Timestamp
UnitTests.Distribution.Client.Store
Expand Down
4 changes: 3 additions & 1 deletion cabal-install/tests/UnitTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import qualified UnitTests.Distribution.Client.Described
import qualified UnitTests.Distribution.Client.FileMonitor
import qualified UnitTests.Distribution.Client.Glob
import qualified UnitTests.Distribution.Client.GZipUtils
import qualified UnitTests.Distribution.Client.Init.FileCreators
import qualified UnitTests.Distribution.Client.Sandbox
import qualified UnitTests.Distribution.Client.Sandbox.Timestamp
import qualified UnitTests.Distribution.Client.Store
Expand Down Expand Up @@ -55,6 +56,8 @@ tests mtimeChangeCalibrated =
UnitTests.Distribution.Client.Glob.tests
, testGroup "Distribution.Client.GZipUtils"
UnitTests.Distribution.Client.GZipUtils.tests
, testGroup "Distribution.Client.Init.FileCreators"
UnitTests.Distribution.Client.Init.FileCreators.tests
, testGroup "Distribution.Client.Sandbox"
UnitTests.Distribution.Client.Sandbox.tests
, testGroup "Distribution.Client.Sandbox.Timestamp"
Expand Down Expand Up @@ -95,4 +98,3 @@ main = do
defaultMainWithIngredients
(includingOptions extraOptions : defaultIngredients)
(tests mtimeChange')

171 changes: 171 additions & 0 deletions cabal-install/tests/UnitTests/Distribution/Client/Init/FileCreators.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
module UnitTests.Distribution.Client.Init.FileCreators (
tests
) where

import Distribution.Client.Init.FileCreators
( generateCabalFile )

import qualified Data.Set as Set

import Distribution.Client.Init.Types
( InitFlags(..), PackageType(..) )
import Distribution.Simple.Setup
( Flag(..) )


import Distribution.CabalSpecVersion
( CabalSpecVersion(CabalSpecV2_4) )
import Distribution.Types.Dependency
( Dependency, mkDependency )
import Distribution.Types.PackageName
( mkPackageName )
import Distribution.Types.VersionRange
( majorBoundVersion )
import Distribution.Types.Version
( mkVersion )
import qualified Distribution.ModuleName as ModuleName
( fromString )
import qualified Distribution.SPDX as SPDX
import Language.Haskell.Extension ( Language(..) )

import Test.Tasty
import Test.Tasty.HUnit

tests :: [TestTree]
tests =
[ testCase "generateCabalFile exe"
(generateCabalFileTest "exekg" exeFlags expectedExeOutput)
, testCase "generateCabalFile libExeAndTests"
(generateCabalFileTest "libExeAndTestsPackage" libExeAndTestFlags expectedLibExeAndTestOutput)
]

generateCabalFileTest :: String -> InitFlags -> String -> Assertion
generateCabalFileTest filename flags expected =
assertEqual
("Generated cabal file does not match: '" ++ filename ++ "'") expected actual
where
actual = generateCabalFile filename flags

--------------------------------------------------
-- Simple exe.

exeFlags :: InitFlags
exeFlags = baseTestFlags {
packageType = Flag Executable
, applicationDirs = Just ["app"]
}

expectedExeOutput :: String
expectedExeOutput =
"cabal-version: 2.4\n"
++ "name: foo\n"
++ "version: 3.2.1\n"
++ "synopsis: The foo package\n"
++ "homepage: https://github.com/foo/foo\n"
++ "license: NONE\n"
++ "author: me\n"
++ "maintainer: [email protected]\n"
++ "category: SomeCat\n"
++ "extra-source-files: CHANGELOG.md\n"
++ "\n"
++ "executable foo\n"
++ " main-is: Main.hs\n"
++ " build-depends: base:{} ^>=4.13.0.0, containers:{} ^>=5.7.0.0, unordered-containers:{} ^>=2.7.0.0\n"
++ " hs-source-dirs: app\n"
++ " default-language: Haskell2010\n"

--
--------------------------------------------------



--------------------------------------------------
-- Lib, exe, and test suite

libExeAndTestFlags :: InitFlags
libExeAndTestFlags = baseTestFlags {
packageType = Flag LibraryAndExecutable
, initializeTestSuite = Flag True
, testDirs = Just ["tests"]
, exposedModules = Just (map ModuleName.fromString ["A", "B"])
, sourceDirs = Just ["lib"]
, applicationDirs = Just ["app"]
}

expectedLibExeAndTestOutput :: String
expectedLibExeAndTestOutput =
"cabal-version: 2.4\n"
++ "name: foo\n"
++ "version: 3.2.1\n"
++ "synopsis: The foo package\n"
++ "homepage: https://github.com/foo/foo\n"
++ "license: NONE\n"
++ "author: me\n"
++ "maintainer: [email protected]\n"
++ "category: SomeCat\n"
++ "extra-source-files: CHANGELOG.md\n"
++ "\n"
++ "library\n"
++ " exposed-modules: A, B\n"
++ " build-depends: base:{} ^>=4.13.0.0, containers:{} ^>=5.7.0.0, unordered-containers:{} ^>=2.7.0.0\n"
++ " hs-source-dirs: lib\n"
++ " default-language: Haskell2010\n"
++ "\n"
++ "executable foo\n"
++ " main-is: Main.hs\n"
++ " build-depends: base:{} ^>=4.13.0.0, containers:{} ^>=5.7.0.0, unordered-containers:{} ^>=2.7.0.0\n"
++ " hs-source-dirs: app\n"
++ " default-language: Haskell2010\n"
++ "\n"
++ "test-suite foo-test\n"
++ " default-language: Haskell2010\n"
++ " type: exitcode-stdio-1.0\n"
++ " hs-source-dirs: tests\n"
++ " main-is: MyLibTest.hs\n"
++ " build-depends: base:{} ^>=4.13.0.0, containers:{} ^>=5.7.0.0, unordered-containers:{} ^>=2.7.0.0\n"

--
--------------------------------------------------


testDependencies :: [Dependency]
testDependencies =
[ mkDependency (mkPackageName "base") (majorBoundVersion (mkVersion [4,13,0,0])) Set.empty
, mkDependency (mkPackageName "containers") (majorBoundVersion (mkVersion [5,7,0,0])) Set.empty
, mkDependency (mkPackageName "unordered-containers") (majorBoundVersion (mkVersion [2,7,0,0])) Set.empty
]

baseTestFlags :: InitFlags
baseTestFlags = InitFlags {
packageName = Flag (mkPackageName "foo")
, noComments = Flag False
, minimal = Flag True
, version = Flag (mkVersion [3,2,1])
, synopsis = Flag "The foo package"
, homepage = Flag "https://github.com/foo/foo"
, license = Flag SPDX.NONE
, author = Flag "me"
, email = Flag "[email protected]"
, category = Flag (Left "SomeCat")
, cabalVersion = Flag CabalSpecV2_4
, extraSrc = Just ["CHANGELOG.md"]
, interactive = Flag False
, mainIs = Flag "Main.hs"
, otherModules = Nothing
, otherExts = Nothing
, applicationDirs = Nothing
, language = Flag Haskell2010
, buildTools = Nothing
, packageType = Flag Executable
, initializeTestSuite = Flag False
, exposedModules = Nothing
, sourceDirs = Nothing
, testDirs = Nothing
, dependencies = Just testDependencies
, quiet = Flag True
, packageDir = NoFlag
, simpleProject = Flag False
, initHcPath = NoFlag
, initVerbosity = NoFlag
, overwrite = NoFlag
}

0 comments on commit abdf2d7

Please sign in to comment.