Skip to content

Commit 3a0989d

Browse files
authored
Merge pull request #8115 from cydparser/optional-type
Make `type` field optional for tests and benchmarks
2 parents 1b7162f + 0675627 commit 3a0989d

File tree

18 files changed

+58
-55
lines changed

18 files changed

+58
-55
lines changed

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ testSuiteFieldGrammar = TestSuiteStanza
325325
^^^ availableSince CabalSpecV3_6 [] -- TODO 3_8
326326

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

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

359359
where
360+
testSuiteType =
361+
_testStanzaTestType stanza
362+
<|> testTypeExe <$ _testStanzaMainIs stanza
363+
<|> testTypeLib <$ _testStanzaTestModule stanza
364+
360365
missingField name tt = "The '" ++ name ++ "' field is required for the "
361366
++ prettyShow tt ++ " test suite type."
362367

@@ -442,7 +447,7 @@ benchmarkFieldGrammar = BenchmarkStanza
442447
<*> blurFieldGrammar benchmarkStanzaBuildInfo buildInfoFieldGrammar
443448

444449
validateBenchmark :: Position -> BenchmarkStanza -> ParseResult Benchmark
445-
validateBenchmark pos stanza = case _benchmarkStanzaBenchmarkType stanza of
450+
validateBenchmark pos stanza = case benchmarkStanzaType of
446451
Nothing -> pure emptyBenchmark
447452
{ benchmarkBuildInfo = _benchmarkStanzaBuildInfo stanza }
448453

@@ -469,6 +474,10 @@ validateBenchmark pos stanza = case _benchmarkStanzaBenchmarkType stanza of
469474
}
470475

471476
where
477+
benchmarkStanzaType =
478+
_benchmarkStanzaBenchmarkType stanza
479+
<|> benchmarkTypeExe <$ _benchmarkStanzaMainIs stanza
480+
472481
missingField name tt = "The '" ++ name ++ "' field is required for the "
473482
++ prettyShow tt ++ " benchmark type."
474483

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

+4-8
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,8 @@ goSections specVer = traverse_ process
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 \"type\" or the field "
323-
, "is not present in all conditional branches. The "
324-
, "available test types are: "
325-
, intercalate ", " (map prettyShow knownTestTypes)
322+
, " is missing required field \"main-is\" or the field "
323+
, "is not present in all conditional branches."
326324
]
327325

328326
-- TODO check duplicate name here?
@@ -337,10 +335,8 @@ goSections specVer = traverse_ process
337335
let hasType ts = benchmarkInterface ts /= benchmarkInterface mempty
338336
unless (onAllBranches hasType bench) $ lift $ parseFailure pos $ concat
339337
[ "Benchmark " ++ show (prettyShow name')
340-
, " is missing required field \"type\" or the field "
341-
, "is not present in all conditional branches. The "
342-
, "available benchmark types are: "
343-
, intercalate ", " (map prettyShow knownBenchmarkTypes)
338+
, " is missing required field \"main-is\" or the field "
339+
, "is not present in all conditional branches."
344340
]
345341

346342
-- TODO check duplicate name here?

Cabal-syntax/src/Distribution/Types/BenchmarkInterface.hs

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import Distribution.Compat.Prelude
1111
import Distribution.Types.BenchmarkType
1212
import Distribution.Version
1313

14-
-- | The benchmark interfaces that are currently defined. Each
15-
-- benchmark must specify which interface it supports.
14+
-- | The benchmark interfaces that are currently defined.
1615
--
1716
-- More interfaces may be defined in future, either new revisions or
1817
-- totally new interfaces.

Cabal-syntax/src/Distribution/Types/BenchmarkType.hs

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
module Distribution.Types.BenchmarkType (
66
BenchmarkType(..),
77
knownBenchmarkTypes,
8+
benchmarkTypeExe,
89
) where
910

1011
import Distribution.Compat.Prelude
@@ -28,7 +29,10 @@ instance Structured BenchmarkType
2829
instance NFData BenchmarkType where rnf = genericRnf
2930

3031
knownBenchmarkTypes :: [BenchmarkType]
31-
knownBenchmarkTypes = [ BenchmarkTypeExe (mkVersion [1,0]) ]
32+
knownBenchmarkTypes = [ benchmarkTypeExe ]
33+
34+
benchmarkTypeExe :: BenchmarkType
35+
benchmarkTypeExe = BenchmarkTypeExe (mkVersion [1,0])
3236

3337
instance Pretty BenchmarkType where
3438
pretty (BenchmarkTypeExe ver) = text "exitcode-stdio-" <<>> pretty ver

Cabal-syntax/src/Distribution/Types/TestSuiteInterface.hs

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import Distribution.Types.TestType
1212
import Distribution.ModuleName
1313
import Distribution.Version
1414

15-
-- | The test suite interfaces that are currently defined. Each test suite must
16-
-- specify which interface it supports.
15+
-- | The test suite interfaces that are currently defined.
1716
--
1817
-- More interfaces may be defined in future, either new revisions or totally
1918
-- new interfaces.

Cabal-syntax/src/Distribution/Types/TestType.hs

+11-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
module Distribution.Types.TestType (
66
TestType(..),
77
knownTestTypes,
8+
testTypeExe,
9+
testTypeLib,
810
) where
911

1012
import Distribution.Compat.Prelude
@@ -28,8 +30,15 @@ instance Structured TestType
2830
instance NFData TestType where rnf = genericRnf
2931

3032
knownTestTypes :: [TestType]
31-
knownTestTypes = [ TestTypeExe (mkVersion [1,0])
32-
, TestTypeLib (mkVersion [0,9]) ]
33+
knownTestTypes = [ testTypeExe
34+
, testTypeLib
35+
]
36+
37+
testTypeExe :: TestType
38+
testTypeExe = TestTypeExe (mkVersion [1,0])
39+
40+
testTypeLib :: TestType
41+
testTypeLib = TestTypeLib (mkVersion [0,9])
3342

3443
instance Pretty TestType where
3544
pretty (TestTypeExe ver) = text "exitcode-stdio-" <<>> pretty ver
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
VERSION: Just (mkVersion [2,0])
2-
issue-5055-2.cabal:15:1: Test suite "flag-cabal-test" is missing required field "type" or the field is not present in all conditional branches. The available test types are: exitcode-stdio-1.0, detailed-0.9
2+
issue-5055-2.cabal:15:1: Test suite "flag-cabal-test" is missing required field "main-is" or the field is not present in all conditional branches.
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
VERSION: Just (mkVersion [2,0])
2-
issue-5055.cabal:15:1: Test suite "flag-cabal-test" is missing required field "type" or the field is not present in all conditional branches. The available test types are: exitcode-stdio-1.0, detailed-0.9
2+
issue-5055.cabal:15:1: Test suite "flag-cabal-test" is missing required field "main-is" or the field is not present in all conditional branches.

Cabal-tests/tests/ParserTests/regressions/issue-5055.expr

+3-5
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,9 @@ GenericPackageDescription {
213213
condTreeData = TestSuite {
214214
testName = UnqualComponentName
215215
"",
216-
testInterface =
217-
TestSuiteUnsupported
218-
(TestTypeUnknown
219-
""
220-
(mkVersion [])),
216+
testInterface = TestSuiteExeV10
217+
(mkVersion [1, 0])
218+
"FirstMain.hs",
221219
testBuildInfo = BuildInfo {
222220
buildable = True,
223221
buildTools = [],

Cabal-tests/tests/ParserTests/regressions/issue-5055.format

+2
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ test-suite flag-cabal-test
1919
build-depends: base >=4.8 && <5
2020

2121
if os(windows)
22+
type: exitcode-stdio-1.0
23+
main-is: FirstMain.hs

Cabal/src/Distribution/PackageDescription/Check.hs

+4
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,14 @@ checkTestSuite pkg test =
322322
TestSuiteUnsupported tt@(TestTypeUnknown _ _) -> Just $
323323
PackageBuildWarning $
324324
quote (prettyShow tt) ++ " is not a known type of test suite. "
325+
++ "Either remove the 'type' field or use a known type. "
325326
++ "The known test suite types are: "
326327
++ commaSep (map prettyShow knownTestTypes)
327328

328329
TestSuiteUnsupported tt -> Just $
329330
PackageBuildWarning $
330331
quote (prettyShow tt) ++ " is not a supported test suite version. "
332+
++ "Either remove the 'type' field or use a known type. "
331333
++ "The known test suite types are: "
332334
++ commaSep (map prettyShow knownTestTypes)
333335
_ -> Nothing
@@ -372,12 +374,14 @@ checkBenchmark _pkg bm =
372374
BenchmarkUnsupported tt@(BenchmarkTypeUnknown _ _) -> Just $
373375
PackageBuildWarning $
374376
quote (prettyShow tt) ++ " is not a known type of benchmark. "
377+
++ "Either remove the 'type' field or use a known type. "
375378
++ "The known benchmark types are: "
376379
++ commaSep (map prettyShow knownBenchmarkTypes)
377380

378381
BenchmarkUnsupported tt -> Just $
379382
PackageBuildWarning $
380383
quote (prettyShow tt) ++ " is not a supported benchmark version. "
384+
++ "Either remove the 'type' field or use a known type. "
381385
++ "The known benchmark types are: "
382386
++ commaSep (map prettyShow knownBenchmarkTypes)
383387
_ -> Nothing

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

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ benchmark solver-disabled
99
build-depends: a-package-that-does-not-exist
1010

1111
benchmark user-disabled
12-
type: exitcode-stdio-1.0
1312
main-is: Test.hs
1413
build-depends: base
1514

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

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ build-type: Simple
44
cabal-version: >= 1.10
55

66
test-suite p1
7-
type: exitcode-stdio-1.0
87
main-is: P1.hs
98
build-depends: base
109

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

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ executable an-exe
1616
other-modules: AModule
1717

1818
test-suite a-testsuite
19-
type: exitcode-stdio-1.0
2019
main-is: Test.hs
2120
other-modules: AModule
2221

cabal-testsuite/PackageTests/DuplicateModuleName/DuplicateModuleName.cabal

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ library
1313
default-language: Haskell2010
1414

1515
test-suite foo
16-
type: detailed-0.9
1716
test-module: Foo
1817
hs-source-dirs: tests
1918
build-depends: base, Cabal, DuplicateModuleName

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

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ build-type: Simple
44
cabal-version: >= 1.10
55

66
benchmark foo
7-
type: exitcode-stdio-1.0
87
main-is: Foo.hs
98
build-depends: base
109
default-language: Haskell2010

changelog.d/pr-8115

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
synopsis: Make type field optional for tests and benchmarks
2+
packages: Cabal, Cabal-syntax
3+
prs: #8115
4+
issues: #7459
5+
description: {
6+
7+
Allow the ommission of the `type` field in `test-suite` and `benchmark` stanzas
8+
when the type can be inferred by the presence of `main-is` or `test-module`.
9+
10+
}

doc/cabal-package.rst

+4-26
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,6 @@ look something like this:
926926
default-language: Haskell2010
927927

928928
test-suite test-foo
929-
type: exitcode-stdio-1.0
930929
main-is: test-foo.hs
931930
-- NOTE: no constraints on 'foo-internal' as same-package
932931
-- dependencies implicitly refer to the same package instance
@@ -1188,14 +1187,14 @@ Test suites
11881187
The test suite may be described using the following fields, as well as
11891188
build information fields (see the section on `build information`_).
11901189

1191-
.. pkg-field:: type: interface (required)
1190+
.. pkg-field:: type: interface
11921191

11931192
The interface type and version of the test suite. Cabal supports two
1194-
test suite interfaces, called ``exitcode-stdio-1.0`` and
1193+
test suite interfaces, called ``exitcode-stdio-1.0`` (default) and
11951194
``detailed-0.9``. Each of these types may require or disallow other
11961195
fields as described below.
11971196

1198-
Test suites using the ``exitcode-stdio-1.0`` interface are executables
1197+
Test suites using the ``exitcode-stdio-1.0`` (default) interface are executables
11991198
that indicate test failure with a non-zero exit code when run; they may
12001199
provide human-readable log information through the standard output and
12011200
error channels. The ``exitcode-stdio-1.0`` type requires the ``main-is``
@@ -1204,7 +1203,6 @@ field.
12041203
.. pkg-field:: main-is: filename
12051204
:synopsis: Module containing tests main function.
12061205

1207-
:required: ``exitcode-stdio-1.0``
12081206
:disallowed: ``detailed-0.9``
12091207

12101208
The name of the ``.hs`` or ``.lhs`` file containing the ``Main``
@@ -1226,7 +1224,6 @@ the :pkg-field:`test-module` field.
12261224

12271225
.. pkg-field:: test-module: identifier
12281226

1229-
:required: ``detailed-0.9``
12301227
:disallowed: ``exitcode-stdio-1.0``
12311228

12321229
The module exporting the ``tests`` symbol.
@@ -1247,7 +1244,6 @@ demonstrate the use of the ``exitcode-stdio-1.0`` interface.
12471244
Build-Type: Simple
12481245
12491246
Test-Suite test-foo
1250-
type: exitcode-stdio-1.0
12511247
main-is: test-foo.hs
12521248
build-depends: base >= 4 && < 5
12531249
default-language: Haskell2010
@@ -1282,7 +1278,6 @@ be provided by the library that provides the testing facility.
12821278
Build-Type: Simple
12831279
12841280
Test-Suite test-bar
1285-
type: detailed-0.9
12861281
test-module: Bar
12871282
build-depends: base >= 4 && < 5, Cabal >= 1.9.2 && < 2
12881283
default-language: Haskell2010
@@ -1345,20 +1340,8 @@ Benchmarks
13451340
The benchmark may be described using the following fields, as well as
13461341
build information fields (see the section on `build information`_).
13471342

1348-
.. pkg-field:: type: interface (required)
1349-
1350-
The interface type and version of the benchmark. At the moment Cabal
1351-
only support one benchmark interface, called ``exitcode-stdio-1.0``.
1352-
1353-
Benchmarks using the ``exitcode-stdio-1.0`` interface are executables
1354-
that indicate failure to run the benchmark with a non-zero exit code
1355-
when run; they may provide human-readable information through the
1356-
standard output and error channels.
1357-
13581343
.. pkg-field:: main-is: filename
13591344

1360-
:required: ``exitcode-stdio-1.0``
1361-
13621345
The name of the ``.hs`` or ``.lhs`` file containing the ``Main``
13631346
module. Note that it is the ``.hs`` filename that must be listed,
13641347
even if that file is generated using a preprocessor. The source file
@@ -1367,12 +1350,9 @@ standard output and error channels.
13671350
field of an executable section. Further, while the name of the file may
13681351
vary, the module itself must be named ``Main``.
13691352

1370-
Example: Package using ``exitcode-stdio-1.0`` interface
1353+
Example:
13711354
"""""""""""""""""""""""""""""""""""""""""""""""""""""""
13721355

1373-
The example package description and executable source file below
1374-
demonstrate the use of the ``exitcode-stdio-1.0`` interface.
1375-
13761356
.. code-block:: cabal
13771357
:caption: foo.cabal
13781358
:name: foo-bench.cabal
@@ -1384,7 +1364,6 @@ demonstrate the use of the ``exitcode-stdio-1.0`` interface.
13841364
Build-Type: Simple
13851365
13861366
Benchmark bench-foo
1387-
type: exitcode-stdio-1.0
13881367
main-is: bench-foo.hs
13891368
build-depends: base >= 4 && < 5, time >= 1.1 && < 1.7
13901369
default-language: Haskell2010
@@ -2719,7 +2698,6 @@ Starting with Cabal-2.2 it's possible to use common build info stanzas.
27192698

27202699
test-suite tests
27212700
import: deps, test-deps
2722-
type: exitcode-stdio-1.0
27232701
main-is: Tests.hs
27242702
build-depends: foo
27252703
default-language: Haskell2010

0 commit comments

Comments
 (0)