How to run (expensive integration) tests in a monorepo behind a feature flag

I’m not able to run a test-suite inside a subpackage in a multi-package project that is hidden behind a Cabal flag. I would like for that test-suite to be disabled by default, e.g. when running cabal test all, but enabled when explicitly called for. However,

$ cabal test -fintegration-tests all

and its variations like cabal test --flags="+integration-tests" all (see 2. Project Description — cabal.project File — Cabal 3.14.1.1 User's Guide) don’t seem to forward the flag to the subpackage(s). What am i doing wrong, or is it even the right approach?

Here some context:

-- subpackage.cabal
flag integration-tests
  description: Enable integration tests
  default: False

if flag(integration-tests)
  test-suite test-integration
    type:                exitcode-stdio-1.0
    hs-source-dirs:      tests
    main-is:             TestIntegration.hs
    build-depends:       base
    ...
...
├── cabal.project
└── subpackage
    ├── src
    │     ...
    ├── tests
    │   └── TestIntegration.hs
    └── subpackage.cabal

You can’t have flags at the top level. If you run cabal check, you should see output containing this:

Warning: [unused-flag] Declared and used flag sets differ: integration-tests /= .
Warning: [parser-warning] subpackage.cabal:5:1: Ignoring section: "if"

I think something like this should work instead:

test-suite test-integration
  ...
  if !flag(integration-tests)
    buildable: False
4 Likes

Thanks a lot, this works. If that is indeed the current best approach, i will add a “How to run tests” guide page to https://cabal.readthedocs.io as i was not able to find such a hint via Google, Reddit and here on Discourse.

1 Like