Skip to content

Commit 366b3e0

Browse files
cydparsermergify[bot]
authored andcommitted
Require type field when spec < 3.8 (haskell#8115)
+ Update file-format-changelog.rst
1 parent b0be29a commit 366b3e0

File tree

9 files changed

+46
-23
lines changed

9 files changed

+46
-23
lines changed

Cabal-syntax/src/Distribution/PackageDescription/FieldGrammar.hs

+12-10
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@ testSuiteFieldGrammar = TestSuiteStanza
324324
<*> monoidalFieldAla "code-generators" (alaList' CommaFSep Token) testStanzaCodeGenerators
325325
^^^ availableSince CabalSpecV3_8 []
326326

327-
validateTestSuite :: Position -> TestSuiteStanza -> ParseResult TestSuite
328-
validateTestSuite pos stanza = case testSuiteType of
327+
validateTestSuite :: CabalSpecVersion -> Position -> TestSuiteStanza -> ParseResult TestSuite
328+
validateTestSuite cabalSpecVersion pos stanza = case testSuiteType of
329329
Nothing -> pure basicTestSuite
330330

331331
Just tt@(TestTypeUnknown _ _) ->
@@ -357,9 +357,10 @@ validateTestSuite pos stanza = case testSuiteType of
357357
{ testInterface = TestSuiteLibV09 ver module_ }
358358

359359
where
360-
testSuiteType =
361-
_testStanzaTestType stanza
362-
<|> testTypeExe <$ _testStanzaMainIs stanza
360+
testSuiteType = _testStanzaTestType stanza <|> do
361+
guard (cabalSpecVersion >= CabalSpecV3_8)
362+
363+
testTypeExe <$ _testStanzaMainIs stanza
363364
<|> testTypeLib <$ _testStanzaTestModule stanza
364365

365366
missingField name tt = "The '" ++ name ++ "' field is required for the "
@@ -446,8 +447,8 @@ benchmarkFieldGrammar = BenchmarkStanza
446447
<*> optionalField "benchmark-module" benchmarkStanzaBenchmarkModule
447448
<*> blurFieldGrammar benchmarkStanzaBuildInfo buildInfoFieldGrammar
448449

449-
validateBenchmark :: Position -> BenchmarkStanza -> ParseResult Benchmark
450-
validateBenchmark pos stanza = case benchmarkStanzaType of
450+
validateBenchmark :: CabalSpecVersion -> Position -> BenchmarkStanza -> ParseResult Benchmark
451+
validateBenchmark cabalSpecVersion pos stanza = case benchmarkStanzaType of
451452
Nothing -> pure emptyBenchmark
452453
{ benchmarkBuildInfo = _benchmarkStanzaBuildInfo stanza }
453454

@@ -474,9 +475,10 @@ validateBenchmark pos stanza = case benchmarkStanzaType of
474475
}
475476

476477
where
477-
benchmarkStanzaType =
478-
_benchmarkStanzaBenchmarkType stanza
479-
<|> benchmarkTypeExe <$ _benchmarkStanzaMainIs stanza
478+
benchmarkStanzaType = _benchmarkStanzaBenchmarkType stanza <|> do
479+
guard (cabalSpecVersion >= CabalSpecV3_8)
480+
481+
benchmarkTypeExe <$ _benchmarkStanzaMainIs stanza
480482

481483
missingField name tt = "The '" ++ name ++ "' field is required for the "
482484
++ prettyShow tt ++ " benchmark type."

Cabal-syntax/src/Distribution/PackageDescription/Parsec.hs

+25-6
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,23 @@ goSections specVer = traverse_ process
314314
commonStanzas <- use stateCommonStanzas
315315
name' <- parseUnqualComponentName pos args
316316
testStanza <- lift $ parseCondTree' testSuiteFieldGrammar (fromBuildInfo' name') commonStanzas fields
317-
testSuite <- lift $ traverse (validateTestSuite pos) testStanza
317+
testSuite <- lift $ traverse (validateTestSuite specVer pos) testStanza
318318

319319
let hasType ts = testInterface ts /= testInterface mempty
320320
unless (onAllBranches hasType testSuite) $ lift $ parseFailure pos $ concat
321321
[ "Test suite " ++ show (prettyShow name')
322-
, " is missing required field \"main-is\" or the field "
323-
, "is not present in all conditional branches."
322+
323+
, concat $ case specVer of
324+
v | v >= CabalSpecV3_8 ->
325+
[ " is missing required field \"main-is\" or the field "
326+
, "is not present in all conditional branches."
327+
]
328+
_ ->
329+
[ " is missing required field \"type\" or the field "
330+
, "is not present in all conditional branches. The "
331+
, "available test types are: "
332+
, intercalate ", " (map prettyShow knownTestTypes)
333+
]
324334
]
325335

326336
-- TODO check duplicate name here?
@@ -330,13 +340,22 @@ goSections specVer = traverse_ process
330340
commonStanzas <- use stateCommonStanzas
331341
name' <- parseUnqualComponentName pos args
332342
benchStanza <- lift $ parseCondTree' benchmarkFieldGrammar (fromBuildInfo' name') commonStanzas fields
333-
bench <- lift $ traverse (validateBenchmark pos) benchStanza
343+
bench <- lift $ traverse (validateBenchmark specVer pos) benchStanza
334344

335345
let hasType ts = benchmarkInterface ts /= benchmarkInterface mempty
336346
unless (onAllBranches hasType bench) $ lift $ parseFailure pos $ concat
337347
[ "Benchmark " ++ show (prettyShow name')
338-
, " is missing required field \"main-is\" or the field "
339-
, "is not present in all conditional branches."
348+
, concat $ case specVer of
349+
v | v >= CabalSpecV3_8 ->
350+
[ " is missing required field \"main-is\" or the field "
351+
, "is not present in all conditional branches."
352+
]
353+
_ ->
354+
[ " is missing required field \"type\" or the field "
355+
, "is not present in all conditional branches. The "
356+
, "available benchmark types are: "
357+
, intercalate ", " (map prettyShow knownBenchmarkTypes)
358+
]
340359
]
341360

342361
-- TODO check duplicate name here?

cabal-install/tests/IntegrationTests2/targets/benchmarks-disabled/p.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
cabal-version: 3.8
12
name: p
23
version: 0.1
34
build-type: Simple
4-
cabal-version: >= 1.10
55

66
benchmark solver-disabled
77
type: exitcode-stdio-1.0

cabal-install/tests/IntegrationTests2/targets/multiple-tests/p.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
cabal-version: 3.8
12
name: p
23
version: 0.1
34
build-type: Simple
4-
cabal-version: >= 1.10
55

66
test-suite p1
77
main-is: P1.hs

cabal-install/tests/IntegrationTests2/targets/variety/p.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
cabal-version: 3.8
12
name: p
23
version: 0.1
34
build-type: Simple
4-
cabal-version: >= 1.10
55

66
library
77
exposed-modules: P

cabal-testsuite/PackageTests/DuplicateModuleName/DuplicateModuleName.cabal

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
cabal-version: 3.8
12
name: DuplicateModuleName
23
version: 0.1.0.0
3-
license: BSD3
4+
license: BSD-3-Clause
45
author: Edward Z. Yang
56
maintainer: [email protected]
67
build-type: Simple
7-
cabal-version: >=1.10
88

99
library
1010
exposed-modules: Foo

cabal-testsuite/PackageTests/NewBuild/CmdBench/MultipleBenchmarks/MultipleBenchmarks.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
cabal-version: 3.8
12
name: MultipleBenchmarks
23
version: 1.0
34
build-type: Simple
4-
cabal-version: >= 1.10
55

66
benchmark foo
77
main-is: Foo.hs

changelog.d/pr-8115

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ prs: #8115
44
issues: #7459
55
description: {
66

7-
Allow the ommission of the `type` field in `test-suite` and `benchmark` stanzas
7+
Allow the omission of the `type` field in `test-suite` and `benchmark` stanzas
88
when the type can be inferred by the presence of `main-is` or `test-module`.
99

1010
}

doc/file-format-changelog.rst

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ relative to the respective preceding *published* version.
4949
allowed of the form ``foo/**/literalFile``. Prior, double-star
5050
wildcards required the trailing filename itself be a wildcard.
5151

52+
* Allow the omission of the `type` field in `test-suite` and `benchmark` stanzas
53+
when the type can be inferred by the presence of `main-is` or `test-module`.
5254

5355
``cabal-version: 3.6``
5456
----------------------

0 commit comments

Comments
 (0)