简体   繁体   English

测试 GHC 编译时错误

[英]Test for GHC compile time errors

I'm working on proto-lens#400 tweaking a Haskell code generator.我正在研究proto-lens#400调整 Haskell 代码生成器。 In one of the tests I'd like to verify that certain API has not been built.在测试中的一个,我想,以验证某些API尚未建成。 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.阅读扩展和使用 GHC 作为库我设法让我的测试编写了一个小文件并使用 GHC 作为库编译它。

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.我充其量对堆栈和 hpack 有一个非常粗略的了解,它们提供了构建时间系统。 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.我知道我可以将依赖项添加到某个package.yaml文件中,以使它们可用于单个测试,但我不知道如何从作为运行测试的一部分设置的 GHC 会话访问这些依赖项。 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 ?如何让测试用例使用 GHC 作为库并让它访问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?或者,我是否可以使用常规测试用例以外的其他构造来表达具有依赖项的文件,但检查该文件是否无法编译?

我不知道这是否适用于您,因为有太多细节让我无法理解,但是测试类型错误的一种方法是使用-fdefer-type-errors构建您的测试套件并在运行时捕获异常-time(类型为TypeError )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM