Depend on test from bench

ぐ巨炮叔叔 提交于 2019-12-10 14:57:19

问题


I have a test project

test-suite spec
  ...

benchmark bench
  build-depends:
    library-test-spec

... how do I depend on the test-suite code from benchmark? The above doesn't work, because the package with name library-test-spec doesn't exist.


回答1:


From Cabal 2.0 onwards, you can put the common code in a named "internal library" on which both the test suite and the benchmark can depend. According to the documentation:

Cabal 2.0 and later support “internal libraries”, which are extra named libraries (as opposed to the usual unnamed library section). For example, suppose that your test suite needs access to some internal modules in your library, which you do not otherwise want to export. You could put these modules in an internal library, which the main library and the test suite build-depends upon.

A convenience internal library looks like this:

library foo-internal
    exposed-modules: Foo.Internal
    build-depends: base

When depending on it, you don't need to put version constraints because same-package dependencies implicitly refer to the same package instance.

With internal libraries you can avoid both double compilation (as when you include the same sources twice) and encumbering your main library (as when you put the common code there).

Remember to include cabal-version: >=2 in your .cabal file.




回答2:


You can't depend on test-suite inside benchmarks. The general solution to this problem is to move functions you want to use in test-suite and in benchmarks into your library target. This, unfortunately, implies that if common functionality needs some testing libraries, your library target will have those testing frameworks in dependencies. If you don't want such behavior then you can move these functions into separate package and depend on this package in your tests and benchmarks.

Or just copy-paste code from tests to benchmarks.



来源:https://stackoverflow.com/questions/48227751/depend-on-test-from-bench

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!