scouten

scouten

It’s been discussed a few times that code coverage tools do not see lines where a literal value are not flagged as executable lines. To wit:

I’m frustrated by this problem and I’m attempting to fix it in a library I’m building. (See Sign in to GitHub · GitHub)

I’ll start by saying I don’t like my fix, but I like even less the notion that I have to sacrifice insight into how well my library is covered.

I work that I’m doing in my day job, I have actually seen bugs get masked because we didn’t know the code was poorly covered.

I’m hoping that somebody can point me to a “better” solution that is less hacky, but doesn’t involve compromising on meaningful code coverage.

Showing Posts 1 to 8

dimitarvp

dimitarvp

Haven’t looked at your code yet. What is it that you don’t like about it currently?

scouten

scouten OP

What I don’t like is that I use a macro to translate a literal response into something more complicated in the test environment. For example, what was:

def valid?(0o040000), do: true

became:

def valid?(0o040000), do: cover(true)

In the test environment, this evaluates to:

defmacro cover(false = x) do
  quote do
    inspect(unquote(x))
    unquote(x)
  end
end

(There are other variations depending on the value passed to cover, but you get the point.)

Not ideal, but it was the result of a lot of trial-and-error to make the compiler, Credo, and dialyzer all happy and still make the original valid?/1 function (and dozens more like it) show up as executable.

Ultimately, I’d like to see the compiler change so that it is inherently marked as covered. But until / unless that happens, I’m looking for a less messy way to accomplish the goal of more accurate code coverage.

dimitarvp

dimitarvp

Seems you got quite far though. I didn’t even know defmacro cover(false = x) do is valid Elixir!

Recommendation: first try and achieve the goals you want. Then you should probably make an announcement to the community and ask for code reviews.

NobbZ

NobbZ

Why not?

What does make you think it were different from def foo(%Foo{} = foo) do … end?

dimitarvp

dimitarvp

I would assume that defmacro cover(false) do is the way to go, same as when pattern-matching function heads assert on specific values.

NobbZ

NobbZ

But they use the bound x in the body.

This is some heavy lifting to avoid problems in the coverage tools. So if they were just leaving off the binding of x and would use false literally, they might finaly have the same result of literals not beeing visible to the coverage tool or might cause warnings of unused variables.

dimitarvp

dimitarvp

I see. That’s why I said he actually got quite ahead. I would not have figured this out immediately. :slight_smile:

scouten

scouten OP

Thanks for the props. So, in the process, I learned that pattern matching works with macros. (I hadn’t expected that, either.)

Basically, the cover macro is an elaborate trick to prevent the compiler from inlining the literal value, which I think is the root cause for the lack of coverage.

So, to @dimitarvp’s comment, when I first posted this a couple of weeks ago, it was initially this was a call for suggestions. I’ve since merged the PR in question, so we could now consider it a call for reviews.

To @NobbZ’s comment:

This is some heavy lifting to avoid problems in the coverage tools.

Thanks. As I said before, I wish there were a cleaner solution. If there is, I’d love to hear about it. If not, consider this a plea for help from the tool providers. I suspect this is an issue in the compiler itself, but it might be something in excoveralls.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
AstonJ
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews