Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use .exs file for config #255

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions coveralls.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
%{
custom_stop_words: [

],

coverage_options: %{
output_dir: "cover/",
template_path: "lib/templates/html/htmlcov/",
minimum_coverage: 0
},

skip_files: [
"test/fixtures/test_missing.ex"
]
}
15 changes: 0 additions & 15 deletions coveralls.json

This file was deleted.

25 changes: 25 additions & 0 deletions lib/conf/coveralls.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
%{
default_stop_words: [
"defmodule",
"defrecord",
"defimpl",
"defexception",
"defprotocol",
"defstruct",
"def.+(.+\\\\.+).+do",
"^\\s+use\\s+"
],

custom_stop_words: [
],

coverage_options: %{
treat_no_relevant_lines_as_covered: false,
output_dir: "cover/",
minimum_coverage: 0
},

terminal_options: %{
file_column_width: 40
}
}
26 changes: 0 additions & 26 deletions lib/conf/coveralls.json

This file was deleted.

2 changes: 1 addition & 1 deletion lib/excoveralls/html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule ExCoveralls.Html do
output_dir
true ->
options = ExCoveralls.Settings.get_coverage_options()
case Map.fetch(options, "output_dir") do
case Map.fetch(options, :output_dir) do
{:ok, val} -> val
_ -> "cover/"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/excoveralls/html/view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule ExCoveralls.Html.View do

defp get_template_path() do
options = ExCoveralls.Settings.get_coverage_options
case Map.fetch(options, "template_path") do
case Map.fetch(options, :template_path) do
{:ok, path} -> path
_ -> Path.expand("excoveralls/lib/templates/html/htmlcov/", Mix.Project.deps_path())
end
Expand Down
2 changes: 1 addition & 1 deletion lib/excoveralls/json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule ExCoveralls.Json do
output_dir
true ->
options = ExCoveralls.Settings.get_coverage_options
case Map.fetch(options, "output_dir") do
case Map.fetch(options, :output_dir) do
{:ok, val} -> val
_ -> "cover/"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/excoveralls/local.ex
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ defmodule ExCoveralls.Local do

defp default_coverage_value do
options = ExCoveralls.Settings.get_coverage_options
case Map.fetch(options, "treat_no_relevant_lines_as_covered") do
case Map.fetch(options, :treat_no_relevant_lines_as_covered) do
{:ok, false} -> 0.0
_ -> 100.0
end
Expand Down
91 changes: 45 additions & 46 deletions lib/excoveralls/settings.ex
Original file line number Diff line number Diff line change
@@ -1,53 +1,54 @@
defmodule ExCoveralls.Settings do
@moduledoc """
Handles the configuration setting defined in json file.
Handles the configuration setting defined in exs file.
"""

defmodule Files do
@filename "coveralls.json"
@filename "coveralls.exs"

def default_file, do: "#{Path.dirname(__ENV__.file)}/../conf/#{@filename}"
def custom_file, do: Application.get_env(:excoveralls, :config_file, "#{File.cwd!}/#{@filename}")
def dot_file, do: Path.expand("~/.excoveralls/#{@filename}")
end

@doc """
Get stop words from the json file.
Get stop words from the exs file.
The words are taken as regular expression.
"""
def get_stop_words do
read_config("default_stop_words", []) ++ read_config("custom_stop_words", [])
read_config(:default_stop_words, []) ++ read_config(:custom_stop_words, [])
|> Enum.map(&Regex.compile!/1)
end

@doc """
Get coverage options from the json file.
Get coverage options from the exs file.
"""
def get_coverage_options do
read_config("coverage_options", []) |> Enum.into(Map.new)
read_config(:coverage_options, []) |> Enum.into(Map.new)
end

@doc """
Get default coverage value for lines marked as not relevant.
"""
def default_coverage_value do
case Map.fetch(get_coverage_options(), "treat_no_relevant_lines_as_covered") do
case Map.fetch(get_coverage_options(), :treat_no_relevant_lines_as_covered) do
{:ok, true} -> 100.0
_ -> 0.0
end
end

@doc """
Get terminal output options from the json file.
Get terminal output options from the exs file.
"""
def get_terminal_options do
read_config("terminal_options", []) |> Enum.into(Map.new)
read_config(:terminal_options, []) |> Enum.into(Map.new)
end

@doc """
Get column width to use for the report from the json file
Get column width to use for the report from the exs file
"""
def get_file_col_width do
case Map.fetch(get_terminal_options(), "file_column_width") do
case Map.fetch(get_terminal_options(), :file_column_width) do
{:ok, val} when is_binary(val) ->
case Integer.parse(val) do
:error -> 40
Expand All @@ -59,69 +60,67 @@ defmodule ExCoveralls.Settings do
end

def get_print_files do
case Map.fetch(get_terminal_options(), "print_files") do
case Map.fetch(get_terminal_options(), :print_files) do
{:ok, val} when is_boolean(val) -> val
_ -> true
end
end

defp read_config_file(file_name) do
if File.exists?(file_name) do
case File.read!(file_name) |> Jason.decode do
{:ok, config} -> config
_ -> raise "Failed to parse config file as JSON : #{file_name}"
end
else
Map.new
end
end

@doc """
Get xml base dir
"""
def get_xml_base_dir do
Map.get(get_coverage_options(), "xml_base_dir", "")
Map.get(get_coverage_options(), :xml_base_dir, "")
end

@doc """
Get skip files from the json file.
Get skip files from the exs file.
"""
def get_skip_files do
read_config("skip_files", [])
read_config(:skip_files, [])
|> Enum.map(&Regex.compile!/1)
end

def get_print_summary do
read_config("print_summary", true)
read_config(:print_summary, true)
end

@doc """
Reads the value for the specified key defined in the json file.
Reads the value for the specified key defined in the exs file.
"""
def read_config(key, default \\ nil) do
case (read_merged_config(Files.dot_file, Files.custom_file) |> Map.get(key)) do
nil -> read_config_file(Files.default_file()) |> Map.get(key, default)
def read_config(key, default \\ nil)

def read_config(key, default) when is_atom(key) do
case Map.get(config(), key) do
nil -> Map.get(default_config(), key, default)
config -> config
end
end

defp read_merged_config(dot, custom) do
read_config_file(dot)
|> merge(read_config_file(custom))
def read_config(key, default) do
key
|> String.to_atom()
|> read_config(default)
end

defp merge(left, right) when is_map(left) and is_map(right) do
keys = Map.keys(left) ++ Map.keys(right)
Enum.reduce(keys, %{}, fn k, new_map ->
merged = cond do
Map.has_key?(left, k) and Map.has_key?(right, k) -> merge(Map.get(left, k), Map.get(right, k))
Map.has_key?(left, k) == false and Map.has_key?(right, k) -> Map.get(right, k)
Map.has_key?(left, k) and Map.has_key?(right, k) == false -> Map.get(left, k)
true -> %{}
end
Map.put(new_map, k, merged)
def config() do
dot_file_config = read_config_file(Files.dot_file())
custom_file_config = read_config_file(Files.custom_file())

Map.merge(dot_file_config, custom_file_config, fn
_k, v1, v2 when is_list(v1) and is_list(v2) -> Enum.uniq(v1 ++ v2)
_k, v1, v2 -> Map.merge(v1, v2)
end)
end
defp merge(left, right) when is_list(left) and is_list(right), do: Enum.uniq(left ++ right)
defp merge(_left, right), do: right

def default_config(), do: read_config_file(Files.default_file())

defp read_config_file(file_name) do
if File.exists?(file_name) do
{map, _} = Code.eval_file(file_name)
map
else
Map.new
end
end
end
6 changes: 3 additions & 3 deletions lib/excoveralls/stats.ex
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ defmodule ExCoveralls.Stats do
Regex.replace(~r/\A\n/m, string, "")
end

def skip_files(converage) do
def skip_files(coverage) do
skip = Settings.get_skip_files
Enum.reject(converage, fn cov ->
Enum.reject(coverage, fn cov ->
Enum.any?(skip, &Regex.match?(&1, cov[:name]))
end)
end
Expand Down Expand Up @@ -208,7 +208,7 @@ defmodule ExCoveralls.Stats do
"""
def ensure_minimum_coverage(stats) do
coverage_options = ExCoveralls.Settings.get_coverage_options
minimum_coverage = coverage_options["minimum_coverage"] || 0
minimum_coverage = coverage_options[:minimum_coverage] || 0
if minimum_coverage > 0, do: check_coverage_threshold(stats, minimum_coverage)
end

Expand Down
8 changes: 4 additions & 4 deletions lib/excoveralls/xml.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule ExCoveralls.Xml do
"""
def execute(stats, options \\ []) do
stats
|> generate_xml(Enum.into(options, %{}))
|> generate_xml(Enum.into(options, %{}))
|> write_file(options[:output_dir])

ExCoveralls.Local.print_summary(stats)
Expand All @@ -23,12 +23,12 @@ defmodule ExCoveralls.Xml do
"<coverage version=\"1\">" <> Enum.map_join(stats, fn %{name: name, coverage: coverage} ->
path = String.replace("#{base_dir}/#{name}", ~r/(\/)+/, "/", global: true)
"<file path=\"#{path}\">" <>
Enum.map_join(Enum.with_index(coverage), fn
Enum.map_join(Enum.with_index(coverage), fn
{nil, _line} -> ""
{count, line} ->
"<lineToCover lineNumber=\"#{line + 1}\" covered=\"#{count != 0}\"/>"
end)
<> "</file>"
<> "</file>"
end) <> "</coverage>"
end

Expand All @@ -38,7 +38,7 @@ defmodule ExCoveralls.Xml do
output_dir
true ->
options = Settings.get_coverage_options
case Map.fetch(options, "output_dir") do
case Map.fetch(options, :output_dir) do
{:ok, val} -> val
_ -> "cover/"
end
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/custom.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
%{
custom_stop_words: [
"aa",
"bb"
]
}
6 changes: 0 additions & 6 deletions test/fixtures/custom.json

This file was deleted.

15 changes: 15 additions & 0 deletions test/fixtures/default.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
%{
default_stop_words: [
"a",
"b"
],

custom_stop_words: [
"d",
"e"
],

coverage_options: %{
f: true
}
}
15 changes: 0 additions & 15 deletions test/fixtures/default.json

This file was deleted.

9 changes: 9 additions & 0 deletions test/fixtures/dotfile.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
%{
coverage_options: %{
template_path: "/users/test/.excoveralls/templates/html/htmlcov"
},
custom_stop_words: [
"cc",
"dd"
]
}