Skip to content

Commit 315ed11

Browse files
committed
Add FileCreators.generateCabalFile unit tests.
1 parent a09afa7 commit 315ed11

File tree

8 files changed

+249
-1
lines changed

8 files changed

+249
-1
lines changed

cabal-install/Distribution/Client/Init/FileCreators.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ module Distribution.Client.Init.FileCreators (
2222
, createMainHs
2323
, createTestSuiteIfEligible
2424
, writeCabalFile
25+
26+
-- * For testing
27+
, generateCabalFile
2528
) where
2629

2730
import Prelude ()

cabal-install/cabal-install.cabal.pp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@
475475
UnitTests.Distribution.Client.GenericInstances
476476
UnitTests.Distribution.Client.Glob
477477
UnitTests.Distribution.Client.GZipUtils
478+
UnitTests.Distribution.Client.Init.FileCreators
478479
UnitTests.Distribution.Client.Sandbox
479480
UnitTests.Distribution.Client.Sandbox.Timestamp
480481
UnitTests.Distribution.Client.Store
@@ -515,6 +516,7 @@
515516
network-uri < 2.6.2.0,
516517
network,
517518
tasty >= 1.2.3 && <1.3,
519+
tasty-golden >=2.3.1.1 && <2.4,
518520
tasty-hunit >= 0.10,
519521
tasty-quickcheck,
520522
tagged,

cabal-install/tests/UnitTests.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import qualified UnitTests.Distribution.Client.Described
1717
import qualified UnitTests.Distribution.Client.FileMonitor
1818
import qualified UnitTests.Distribution.Client.Glob
1919
import qualified UnitTests.Distribution.Client.GZipUtils
20+
import qualified UnitTests.Distribution.Client.Init.FileCreators
2021
import qualified UnitTests.Distribution.Client.Sandbox
2122
import qualified UnitTests.Distribution.Client.Sandbox.Timestamp
2223
import qualified UnitTests.Distribution.Client.Store
@@ -55,6 +56,8 @@ tests mtimeChangeCalibrated =
5556
UnitTests.Distribution.Client.Glob.tests
5657
, testGroup "Distribution.Client.GZipUtils"
5758
UnitTests.Distribution.Client.GZipUtils.tests
59+
, testGroup "Distribution.Client.Init.FileCreators"
60+
UnitTests.Distribution.Client.Init.FileCreators.tests
5861
, testGroup "Distribution.Client.Sandbox"
5962
UnitTests.Distribution.Client.Sandbox.tests
6063
, testGroup "Distribution.Client.Sandbox.Timestamp"
@@ -95,4 +98,3 @@ main = do
9598
defaultMainWithIngredients
9699
(includingOptions extraOptions : defaultIngredients)
97100
(tests mtimeChange')
98-
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
module UnitTests.Distribution.Client.Init.FileCreators (
2+
tests
3+
) where
4+
5+
import Distribution.Client.Init.FileCreators
6+
( generateCabalFile )
7+
8+
import Test.Tasty
9+
import Test.Tasty.Golden (goldenVsString)
10+
11+
import System.FilePath
12+
( (</>) )
13+
import qualified Data.ByteString.Lazy as BS
14+
import qualified Data.ByteString.Lazy.Char8 as BS8
15+
import qualified Data.Set as Set
16+
17+
import Distribution.Client.Init.Types
18+
( InitFlags(..), PackageType(..), defaultInitFlags )
19+
import Distribution.Simple.Setup
20+
( Flag(..) )
21+
22+
import Distribution.CabalSpecVersion
23+
( CabalSpecVersion(CabalSpecV2_4) )
24+
import Distribution.Types.Dependency
25+
( Dependency, mkDependency )
26+
import Distribution.Types.LibraryName
27+
( LibraryName(LMainLibName) )
28+
import Distribution.Types.PackageName
29+
( mkPackageName )
30+
import Distribution.Types.VersionRange
31+
( majorBoundVersion )
32+
import Distribution.Types.Version
33+
( mkVersion )
34+
import qualified Distribution.ModuleName as ModuleName
35+
( fromString )
36+
import qualified Distribution.SPDX as SPDX
37+
import Language.Haskell.Extension ( Language(..) )
38+
39+
tests :: [TestTree]
40+
tests = [ testGroup "cabal init goldens"
41+
[ checkCabalFileGolden exeFlags "exe-only-golden.cabal"
42+
, checkCabalFileGolden libExeAndTestFlags "lib-exe-and-test-golden.cabal"
43+
]
44+
]
45+
46+
checkCabalFileGolden :: InitFlags -> FilePath -> TestTree
47+
checkCabalFileGolden flags goldenFileName =
48+
goldenVsString goldenFileName goldenFilePath generatedCabalFile
49+
where
50+
goldenFilePath :: FilePath
51+
goldenFilePath = "tests" </> "fixtures" </> "init" </> goldenFileName
52+
53+
generatedCabalFile :: IO BS.ByteString
54+
generatedCabalFile = pure . BS8.pack $ generateCabalFile goldenFileName flags
55+
56+
-- ==================================================
57+
-- Base flags to set common InitFlags values.
58+
59+
baseFlags :: InitFlags
60+
baseFlags = defaultInitFlags {
61+
-- Values common to all (or most) test flags.
62+
packageName = Flag (mkPackageName "foo")
63+
, noComments = Flag False
64+
, minimal = Flag True
65+
, version = Flag (mkVersion [3,2,1])
66+
, synopsis = Flag "The foo package"
67+
, homepage = Flag "https://github.com/foo/foo"
68+
, license = Flag SPDX.NONE
69+
, author = Flag "me"
70+
, email = Flag "[email protected]"
71+
, category = Flag (Left "SomeCat")
72+
, cabalVersion = Flag CabalSpecV2_4
73+
, extraSrc = Just ["CHANGELOG.md"]
74+
, interactive = Flag False
75+
, otherModules = Nothing
76+
, otherExts = Nothing
77+
, language = Flag Haskell2010
78+
, buildTools = Nothing
79+
, dependencies = Just testDependencies
80+
, quiet = Flag True
81+
, packageDir = NoFlag
82+
, simpleProject = Flag False
83+
, initHcPath = NoFlag
84+
, overwrite = NoFlag
85+
86+
-- Commonly overridden values in test InitFlags.
87+
-- It is fine to provide the same value in an overridden InitFlags
88+
-- to make it clear what that particular test case is differentiating
89+
-- from others.
90+
, packageType = Flag Executable
91+
, mainIs = Flag "Main.hs"
92+
, applicationDirs = Just ["app"]
93+
, sourceDirs = Nothing
94+
, exposedModules = Nothing
95+
, initializeTestSuite = Flag False
96+
, testDirs = Nothing
97+
}
98+
99+
100+
-- ==================================================
101+
-- Simple exe.
102+
103+
exeFlags :: InitFlags
104+
exeFlags = baseFlags {
105+
-- Create an executable only, with main living in app/Main.hs.
106+
packageType = Flag Executable
107+
, mainIs = Flag "Main.hs"
108+
, applicationDirs = Just ["app"]
109+
}
110+
111+
112+
-- ==================================================
113+
-- Lib, exe, and test suite
114+
115+
libExeAndTestFlags :: InitFlags
116+
libExeAndTestFlags = baseFlags {
117+
-- Create a library and executable
118+
packageType = Flag LibraryAndExecutable
119+
120+
-- Main living in app/Main.hs.
121+
, mainIs = Flag "Main.hs"
122+
, applicationDirs = Just ["app"]
123+
124+
-- Library sources live in src/ and expose the modules A and B.
125+
, sourceDirs = Just ["src"]
126+
, exposedModules = Just (map ModuleName.fromString ["A", "B"])
127+
128+
-- Create a test suite living in tests/
129+
, initializeTestSuite = Flag True
130+
, testDirs = Just ["tests"]
131+
}
132+
133+
134+
-- ==================================================
135+
-- Test dependency.
136+
137+
testDependencies :: [Dependency]
138+
testDependencies =
139+
[ mkDependency
140+
(mkPackageName "base")
141+
(majorBoundVersion (mkVersion [4,13,0,0]))
142+
(Set.singleton LMainLibName)
143+
, mkDependency
144+
(mkPackageName "containers")
145+
(majorBoundVersion (mkVersion [5,7,0,0]))
146+
(Set.singleton LMainLibName)
147+
, mkDependency
148+
(mkPackageName "unordered-containers")
149+
(majorBoundVersion (mkVersion [2,7,0,0]))
150+
(Set.singleton LMainLibName)
151+
]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cabal-version: 2.4
2+
name: foo
3+
version: 3.2.1
4+
synopsis: The foo package
5+
homepage: https://github.com/foo/foo
6+
license: NONE
7+
author: me
8+
maintainer: [email protected]
9+
category: SomeCat
10+
extra-source-files: CHANGELOG.md
11+
12+
executable foo
13+
main-is: Main.hs
14+
build-depends: base ^>=4.13.0.0, containers ^>=5.7.0.0, unordered-containers ^>=2.7.0.0
15+
hs-source-dirs: app
16+
default-language: Haskell2010
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
cabal-version: 2.4
2+
name: foo
3+
version: 3.2.1
4+
synopsis: The foo package
5+
homepage: https://github.com/foo/foo
6+
license: NONE
7+
author: me
8+
maintainer: [email protected]
9+
category: SomeCat
10+
extra-source-files: CHANGELOG.md
11+
12+
library
13+
exposed-modules: A, B
14+
build-depends: base ^>=4.13.0.0, containers ^>=5.7.0.0, unordered-containers ^>=2.7.0.0
15+
hs-source-dirs: src
16+
default-language: Haskell2010
17+
18+
executable foo
19+
main-is: Main.hs
20+
build-depends: base ^>=4.13.0.0, containers ^>=5.7.0.0, unordered-containers ^>=2.7.0.0
21+
hs-source-dirs: app
22+
default-language: Haskell2010
23+
24+
test-suite foo-test
25+
default-language: Haskell2010
26+
type: exitcode-stdio-1.0
27+
hs-source-dirs: tests
28+
main-is: MyLibTest.hs
29+
build-depends: base ^>=4.13.0.0, containers ^>=5.7.0.0, unordered-containers ^>=2.7.0.0
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cabal-version: 2.4
2+
name: foo
3+
version: 3.2.1
4+
synopsis: The foo package
5+
homepage: https://github.com/foo/foo
6+
license: NONE
7+
author: me
8+
maintainer: [email protected]
9+
category: SomeCat
10+
extra-source-files: CHANGELOG.md
11+
12+
executable foo
13+
main-is: Main.hs
14+
build-depends: base ^>=4.13.0.0, containers ^>=5.7.0.0, unordered-containers ^>=2.7.0.0
15+
hs-source-dirs: app
16+
default-language: Haskell2010
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
cabal-version: 2.4
2+
name: foo
3+
version: 3.2.1
4+
synopsis: The foo package
5+
homepage: https://github.com/foo/foo
6+
license: NONE
7+
author: me
8+
maintainer: [email protected]
9+
category: SomeCat
10+
extra-source-files: CHANGELOG.md
11+
12+
library
13+
exposed-modules: A, B
14+
build-depends: base ^>=4.13.0.0, containers ^>=5.7.0.0, unordered-containers ^>=2.7.0.0
15+
hs-source-dirs: src
16+
default-language: Haskell2010
17+
18+
executable foo
19+
main-is: Main.hs
20+
build-depends: base ^>=4.13.0.0, containers ^>=5.7.0.0, unordered-containers ^>=2.7.0.0
21+
hs-source-dirs: app
22+
default-language: Haskell2010
23+
24+
test-suite foo-test
25+
default-language: Haskell2010
26+
type: exitcode-stdio-1.0
27+
hs-source-dirs: tests
28+
main-is: MyLibTest.hs
29+
build-depends: base ^>=4.13.0.0, containers ^>=5.7.0.0, unordered-containers ^>=2.7.0.0

0 commit comments

Comments
 (0)