Haskell program coverage does not highlight a module that has no tests at all

I have the start of a haskell application and want to see how the building tools behave. One of the things I would like to see is Haskell coverage reports via hpc (Haskell Program Coverage β†’ I did not find this tag, therefore hpc points to high-performance calculations on the note side).

Structure of my application

Main src/ ModuleA ModuleB tests/ ModuleBTest 

I have unit tests for module B and I run these unit tests through the cabal test. I used to configure cabal to output hpc data via

 cabal configure --ghc-options=-fhpc --enable-tests 

Then i build and test

 cabal build cabal test unit-tests (that the name of the test suite in the cabal file) 

and I really see the report, and everything looks good. However, module A is not transferred from module B, it refers only to Main. I have no tests for the Main module.

The thing is, I was expecting to see the moduleA popup in the hpc output highlighted in yellow and really waving to me that there are no tests for this module, but that doesn't seem to be the case. I noticed that the .mix files are created for this β€œunused” module, so I suspect that the build step went fine, but it was wrong at the cache testing stage.

If I go through ghci and I compile unit tests, and module A is explicitly in the list of modules to compile, then I get hpc to show me that this module has no tests at all. Therefore, I suspect that the cable is optimizing this module (for example, "not used"), but I really do not see how and where.

Now I understand that this may not be the real situation, since moduleA refers only to the main method, module B does not refer to module A, and I do not test the main module (for now), but still I would feel much better if at least it would appear in the coverage of the program as a hole in my tests the size of a battleship. Does anyone have an idea?

Note. I understand that my question can be as follows: "How can I say that bondage does not need to optimize unused modules?" but I wanted to introduce a complete problem.

Kasper

+6
source share
1 answer

First, make sure all your modules are listed in the other-modules cabal field.
Despite the fact that in my experience sometimes applications seem to work without specifying everything there - this can often cause mysterious binding problems, and I assume that it can cause situations like yours.

Now, other than this, I don’t think cabal will optimize your modules like this, but eliminating the GHC code. Therefore, if your code is not used at all (only one actual use per module must exist), GHC does not even care about it.
Unfortunately, I did not see a flag to change this. You may want to make pointless use for each module in your test project, just to make everything visible.


2.1 Dead Code Exception

Does GHC remove code that you are not actually using?

Yes and no. If there is something not exported in the module and is not used by anything that is exported, it is ignored. (This makes your compiled program smaller.) Thus, at the module level, yes, the GHC does deletion of dead code.

On the other hand, if you import a module and use only one function from it, all the code for all functions in this module is bound in. So, in this sense, no, GHC does not eliminate dead code.

(There is a switch to force the GHC to spit out a separate object file for each individual function in the module. If you use this, the functions will actually be associated with your executable file. But this usually causes the linker program to get carried away ...)

If you want to be warned about unused code (why do you have it there if it's not in use? Have you forgotten to type something?), You can use -fwarn-unused-binds (or just -Wall).

- GHC Optimization - HaskellWiki

+1
source

Source: https://habr.com/ru/post/975114/


All Articles