Skip to content

Commit 8ed1b70

Browse files
committed
Add FileCreators.generateCabalFile unit tests.
1 parent a09afa7 commit 8ed1b70

File tree

9 files changed

+254
-1
lines changed

9 files changed

+254
-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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ Extra-Source-Files:
9090
tests/IntegrationTests2/targets/tests-disabled/q/q.cabal
9191
tests/IntegrationTests2/targets/variety/cabal.project
9292
tests/IntegrationTests2/targets/variety/p.cabal
93+
tests/fixtures/init/exe-only-golden.cabal
94+
tests/fixtures/init/lib-exe-and-test-golden.cabal
9395
-- END gen-extra-source-files
9496

9597
-- Additional manual extra-source-files:

cabal-install/cabal-install.cabal.pp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@
370370
tests/IntegrationTests2/targets/tests-disabled/q/q.cabal
371371
tests/IntegrationTests2/targets/variety/cabal.project
372372
tests/IntegrationTests2/targets/variety/p.cabal
373+
tests/fixtures/init/exe-only-golden.cabal
374+
tests/fixtures/init/lib-exe-and-test-golden.cabal
373375
-- END gen-extra-source-files
374376
375377
-- Additional manual extra-source-files:
@@ -475,6 +477,7 @@
475477
UnitTests.Distribution.Client.GenericInstances
476478
UnitTests.Distribution.Client.Glob
477479
UnitTests.Distribution.Client.GZipUtils
480+
UnitTests.Distribution.Client.Init.FileCreators
478481
UnitTests.Distribution.Client.Sandbox
479482
UnitTests.Distribution.Client.Sandbox.Timestamp
480483
UnitTests.Distribution.Client.Store
@@ -515,6 +518,7 @@
515518
network-uri < 2.6.2.0,
516519
network,
517520
tasty >= 1.2.3 && <1.3,
521+
tasty-golden >=2.3.1.1 && <2.4,
518522
tasty-hunit >= 0.10,
519523
tasty-quickcheck,
520524
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: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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(..) )
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 = InitFlags {
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+
, initVerbosity = NoFlag
85+
, overwrite = NoFlag
86+
87+
-- Commonly overridden values in test InitFlags.
88+
-- It is fine to provide the same value in an overridden InitFlags
89+
-- to make it clear what that particular test case is differentiating
90+
-- from others.
91+
, packageType = Flag Executable
92+
, mainIs = Flag "Main.hs"
93+
, applicationDirs = Just ["app"]
94+
, sourceDirs = Nothing
95+
, exposedModules = Nothing
96+
, initializeTestSuite = Flag False
97+
, testDirs = Nothing
98+
}
99+
100+
101+
-- ==================================================
102+
-- Simple exe.
103+
104+
exeFlags :: InitFlags
105+
exeFlags = baseFlags {
106+
-- Create an executable only, with main living in app/Main.hs.
107+
packageType = Flag Executable
108+
, mainIs = Flag "Main.hs"
109+
, applicationDirs = Just ["app"]
110+
}
111+
112+
113+
-- ==================================================
114+
-- Lib, exe, and test suite
115+
116+
libExeAndTestFlags :: InitFlags
117+
libExeAndTestFlags = baseFlags {
118+
-- Create a library and executable
119+
packageType = Flag LibraryAndExecutable
120+
121+
-- Main living in app/Main.hs.
122+
, mainIs = Flag "Main.hs"
123+
, applicationDirs = Just ["app"]
124+
125+
-- Library sources live in src/ and expose the modules A and B.
126+
, sourceDirs = Just ["src"]
127+
, exposedModules = Just (map ModuleName.fromString ["A", "B"])
128+
129+
-- Create a test suite living in tests/
130+
, initializeTestSuite = Flag True
131+
, testDirs = Just ["tests"]
132+
}
133+
134+
135+
-- ==================================================
136+
-- Test dependency.
137+
138+
testDependencies :: [Dependency]
139+
testDependencies =
140+
[ mkDependency
141+
(mkPackageName "base")
142+
(majorBoundVersion (mkVersion [4,13,0,0]))
143+
(Set.singleton LMainLibName)
144+
, mkDependency
145+
(mkPackageName "containers")
146+
(majorBoundVersion (mkVersion [5,7,0,0]))
147+
(Set.singleton LMainLibName)
148+
, mkDependency
149+
(mkPackageName "unordered-containers")
150+
(majorBoundVersion (mkVersion [2,7,0,0]))
151+
(Set.singleton LMainLibName)
152+
]
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)