Skip to content

Commit 24bd19b

Browse files
authored
Merge pull request #122 from yknx4/fix/better-float-compare
fix: better float compare
2 parents 5cbf522 + b6a6672 commit 24bd19b

File tree

5 files changed

+25
-12
lines changed

5 files changed

+25
-12
lines changed

.formatter.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[
2+
import_deps: [:ecto, :phoenix],
23
inputs: [
34
"mix.exs",
45
"{config,lib,test,priv,web}/**/*.{ex,exs}"

lib/opencov/helpers/number.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
defmodule Librecov.Helpers.Number do
2+
defguard is_greater_than(a, b)
3+
when trunc(a * 1000) > trunc(b * 1000)
4+
5+
defguard is_less_than(a, b)
6+
when trunc(a * 1000) < trunc(b * 1000)
7+
8+
defguard is_equal(a, b) when trunc(a * 1000) == trunc(b * 1000)
9+
10+
defguard is_different(a, b)
11+
when trunc(a * 1000) != trunc(b * 1000)
12+
13+
defguard is_zero(a) when is_equal(a, 0)
14+
end

lib/opencov/services/github/checks.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defmodule Librecov.Services.Github.Checks do
66
alias Librecov.Project
77
alias Librecov.Services.Github.AuthData
88
import Librecov.Helpers.Coverage
9+
import Librecov.Helpers.Number
910

1011
def finish_check(
1112
%AuthData{token: token, owner: owner, repo: repo},
@@ -81,7 +82,7 @@ defmodule Librecov.Services.Github.Checks do
8182
)
8283
end
8384

84-
defp diff_conclusion(diff) when diff == 0, do: "neutral"
85+
defp diff_conclusion(diff) when is_zero(diff), do: "neutral"
8586
defp diff_conclusion(diff) when diff < 0, do: "failure"
8687
defp diff_conclusion(diff) when diff > 0, do: "success"
8788
end

lib/opencov/templates/comment_template.ex

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defmodule Librecov.Templates.CommentTemplate do
66
alias Librecov.File
77
import Librecov.Router.Helpers
88
import Librecov.Helpers.Coverage
9+
import Librecov.Helpers.Number
910
alias Librecov.Repo
1011
alias Librecov.Queries.BuildQueries
1112

@@ -62,7 +63,7 @@ defmodule Librecov.Templates.CommentTemplate do
6263
both_files =
6364
files
6465
|> Enum.map(fn f -> {f, base_files |> Enum.find(&(&1.name == f.name))} end)
65-
|> Enum.filter(fn {f1, f2} -> is_nil(f2) || f1.coverage != f2.coverage end)
66+
|> Enum.filter(fn {f1, f2} -> is_nil(f2) || is_different(f1.coverage, f2.coverage) end)
6667

6768
"""
6869
#{header}
@@ -81,12 +82,7 @@ defmodule Librecov.Templates.CommentTemplate do
8182
def get_jobs_files(jobs) do
8283
jobs
8384
|> JobManager.preload_files()
84-
|> Enum.flat_map(fn job ->
85-
job.files
86-
|> Enum.filter(fn %File{coverage: coverage, previous_coverage: previous_coverage} ->
87-
coverage_diff(coverage, previous_coverage) != 0
88-
end)
89-
end)
85+
|> Enum.flat_map(& &1.files)
9086
end
9187

9288
defp merge_message(_, nil, _, _), do: ""
@@ -134,11 +130,11 @@ defmodule Librecov.Templates.CommentTemplate do
134130
"| [#{filename}](#{file_url(Endpoint, :show, file_id)}) | `#{coverage |> format_coverage()} <#{cov_diff |> format_coverage()}> (#{cov_diff |> file_icon()})` | #{cov_diff |> diff_emoji()} |"
135131
end
136132

137-
defp diff_emoji(diff) when diff == 0, do: ""
133+
defp diff_emoji(diff) when is_zero(diff), do: ""
138134
defp diff_emoji(diff) when diff < 0, do: "⬇️"
139135
defp diff_emoji(diff) when diff > 0, do: "⬆️"
140136

141-
defp file_icon(diff) when diff >= 0.0 and diff <= 0.01, do: "ø"
137+
defp file_icon(diff) when is_zero(diff), do: "ø"
142138
defp file_icon(_diff), do: "Δ"
143139

144140
defp format_commit(commit), do: String.slice(commit, 0, 7)
@@ -148,7 +144,7 @@ defmodule Librecov.Templates.CommentTemplate do
148144
defp format_branch("refs/heads/" <> branch), do: branch
149145
defp format_branch(branch), do: branch
150146

151-
defp diff_verb(diff) when diff == 0, do: "mantain"
147+
defp diff_verb(diff) when is_zero(diff), do: "mantain"
152148
defp diff_verb(diff) when diff < 0, do: "decrease"
153149
defp diff_verb(diff) when diff > 0, do: "increase"
154150
end

lib/web/views/common_view.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
defmodule Librecov.CommonView do
2+
import Librecov.Helpers.Number
23
def format_coverage(num) when is_float(num), do: "#{Float.round(num, 1)}%"
34
def format_coverage(_), do: "NA"
45

56
def coverage_color(coverage) do
67
cond do
78
is_nil(coverage) -> "na"
8-
coverage == 0 -> "none"
9+
is_zero(coverage) -> "none"
910
coverage < 80 -> "low"
1011
coverage < 90 -> "normal"
1112
coverage < 100 -> "good"

0 commit comments

Comments
 (0)