1

I'm working on proto-lens#400 tweaking a Haskell code generator. In one of the tests I'd like to verify that certain API has not been built. Specifically, I want to ensure that a certain type of program will not type check successfully. I'd also have a similar program with one identifier changed which should compile, to guard against a typo breaking the test. Reading Extending and using GHC as a Library I have managed to have my test write a small file and compile it using GHC as a library.

But I need the code emitted by the test to load some other modules. Specifically the output of the code generator of that project and its runtime environment with transitive dependencies. I have at best a very rough understanding of stack and hpack, which is providing the build time system. I know I can add dependencies to some package.yaml file to make them available to individual tests, but I have no clue how to access such dependencies from the GHC session set up as part of running the test. I imagine I might find some usable data in some environment variables, but I also believe such an approach might be undocumented and prone to break without warning.

How can I have a test case use GHC as a library and have it access dependencies expressed in package.yaml? Or alternatively, can I use some construct other than a regular test case to express a file with dependencies but check that the file won't compile?

MvG
  • 57,380
  • 22
  • 148
  • 276
  • Maybe this package could be useful for you? https://hackage.haskell.org/package/should-not-typecheck – sjakobi Nov 20 '20 at 18:13
  • Alternatively, the `type-spec` package might be useful here: https://hackage.haskell.org/package/type-spec-0.4.0.0/docs/Test-TypeSpec-ShouldBe.html#t:ShouldNotBe – sjakobi Nov 20 '20 at 18:20

1 Answers1

0

I don't know if this applies to you because there are too many details going way over my head, but one way to test for type errors is to build your test suite with -fdefer-type-errors and to catch the exception at run-time (of type TypeError).

Li-yao Xia
  • 31,896
  • 2
  • 33
  • 56