Skip to content

Commit ce460b7

Browse files
authored
Adds credo checks to CI (#66)
* Adds credo * Fix all credo issues and warnings * Update CI to use credo
1 parent f544443 commit ce460b7

File tree

14 files changed

+301
-91
lines changed

14 files changed

+301
-91
lines changed

.credo.exs

+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
# This file contains the configuration for Credo and you are probably reading
2+
# this after creating it with `mix credo.gen.config`.
3+
#
4+
# If you find anything wrong or unclear in this file, please report an
5+
# issue on GitHub: https://github.com/rrrene/credo/issues
6+
#
7+
%{
8+
#
9+
# You can have as many configs as you like in the `configs:` field.
10+
configs: [
11+
%{
12+
#
13+
# Run any config using `mix credo -C <name>`. If no config name is given
14+
# "default" is used.
15+
#
16+
name: "default",
17+
#
18+
# These are the files included in the analysis:
19+
files: %{
20+
#
21+
# You can give explicit globs or simply directories.
22+
# In the latter case `**/*.{ex,exs}` will be used.
23+
#
24+
included: [
25+
"lib/",
26+
"src/",
27+
"test/",
28+
"web/",
29+
"apps/*/lib/",
30+
"apps/*/src/",
31+
"apps/*/test/",
32+
"apps/*/web/"
33+
],
34+
excluded: [~r"/_build/", ~r"/deps/", ~r"/node_modules/"]
35+
},
36+
#
37+
# Load and configure plugins here:
38+
#
39+
plugins: [],
40+
#
41+
# If you create your own checks, you must specify the source files for
42+
# them here, so they can be loaded by Credo before running the analysis.
43+
#
44+
requires: [],
45+
#
46+
# If you want to enforce a style guide and need a more traditional linting
47+
# experience, you can change `strict` to `true` below:
48+
#
49+
strict: false,
50+
#
51+
# To modify the timeout for parsing files, change this value:
52+
#
53+
parse_timeout: 5000,
54+
#
55+
# If you want to use uncolored output by default, you can change `color`
56+
# to `false` below:
57+
#
58+
color: true,
59+
#
60+
# You can customize the parameters of any check by adding a second element
61+
# to the tuple.
62+
#
63+
# To disable a check put `false` as second element:
64+
#
65+
# {Credo.Check.Design.DuplicatedCode, false}
66+
#
67+
checks: %{
68+
enabled: [
69+
#
70+
## Consistency Checks
71+
#
72+
{Credo.Check.Consistency.ExceptionNames, []},
73+
{Credo.Check.Consistency.LineEndings, []},
74+
{Credo.Check.Consistency.ParameterPatternMatching, []},
75+
{Credo.Check.Consistency.SpaceAroundOperators, []},
76+
{Credo.Check.Consistency.SpaceInParentheses, []},
77+
{Credo.Check.Consistency.TabsOrSpaces, []},
78+
79+
#
80+
## Design Checks
81+
#
82+
# You can customize the priority of any check
83+
# Priority values are: `low, normal, high, higher`
84+
#
85+
{Credo.Check.Design.AliasUsage,
86+
[priority: :low, if_nested_deeper_than: 2, if_called_more_often_than: 0]},
87+
88+
#
89+
## Readability Checks
90+
#
91+
{Credo.Check.Readability.AliasOrder, []},
92+
{Credo.Check.Readability.FunctionNames, []},
93+
{Credo.Check.Readability.LargeNumbers, []},
94+
{Credo.Check.Readability.MaxLineLength, [priority: :low, max_length: 120]},
95+
{Credo.Check.Readability.ModuleAttributeNames, []},
96+
{Credo.Check.Readability.ModuleDoc, []},
97+
{Credo.Check.Readability.ModuleNames, []},
98+
{Credo.Check.Readability.ParenthesesInCondition, []},
99+
{Credo.Check.Readability.ParenthesesOnZeroArityDefs, []},
100+
{Credo.Check.Readability.PipeIntoAnonymousFunctions, []},
101+
{Credo.Check.Readability.PredicateFunctionNames, []},
102+
{Credo.Check.Readability.PreferImplicitTry, []},
103+
{Credo.Check.Readability.RedundantBlankLines, []},
104+
{Credo.Check.Readability.Semicolons, []},
105+
{Credo.Check.Readability.SpaceAfterCommas, []},
106+
{Credo.Check.Readability.StringSigils, []},
107+
{Credo.Check.Readability.TrailingBlankLine, []},
108+
{Credo.Check.Readability.TrailingWhiteSpace, []},
109+
{Credo.Check.Readability.UnnecessaryAliasExpansion, []},
110+
{Credo.Check.Readability.VariableNames, []},
111+
{Credo.Check.Readability.WithSingleClause, []},
112+
113+
#
114+
## Refactoring Opportunities
115+
#
116+
{Credo.Check.Refactor.Apply, []},
117+
{Credo.Check.Refactor.CondStatements, []},
118+
{Credo.Check.Refactor.CyclomaticComplexity, []},
119+
{Credo.Check.Refactor.FunctionArity, []},
120+
{Credo.Check.Refactor.LongQuoteBlocks, []},
121+
{Credo.Check.Refactor.MatchInCondition, []},
122+
{Credo.Check.Refactor.MapJoin, []},
123+
{Credo.Check.Refactor.NegatedConditionsInUnless, []},
124+
{Credo.Check.Refactor.NegatedConditionsWithElse, []},
125+
{Credo.Check.Refactor.Nesting, []},
126+
{Credo.Check.Refactor.UnlessWithElse, []},
127+
{Credo.Check.Refactor.WithClauses, []},
128+
{Credo.Check.Refactor.FilterFilter, []},
129+
{Credo.Check.Refactor.RejectReject, []},
130+
{Credo.Check.Refactor.RedundantWithClauseResult, []},
131+
132+
#
133+
## Warnings
134+
#
135+
{Credo.Check.Warning.ApplicationConfigInModuleAttribute, []},
136+
{Credo.Check.Warning.BoolOperationOnSameValues, []},
137+
{Credo.Check.Warning.ExpensiveEmptyEnumCheck, []},
138+
{Credo.Check.Warning.IExPry, []},
139+
{Credo.Check.Warning.IoInspect, []},
140+
{Credo.Check.Warning.OperationOnSameValues, []},
141+
{Credo.Check.Warning.OperationWithConstantResult, []},
142+
{Credo.Check.Warning.RaiseInsideRescue, []},
143+
{Credo.Check.Warning.SpecWithStruct, []},
144+
{Credo.Check.Warning.WrongTestFileExtension, []},
145+
{Credo.Check.Warning.UnusedEnumOperation, []},
146+
{Credo.Check.Warning.UnusedFileOperation, []},
147+
{Credo.Check.Warning.UnusedKeywordOperation, []},
148+
{Credo.Check.Warning.UnusedListOperation, []},
149+
{Credo.Check.Warning.UnusedPathOperation, []},
150+
{Credo.Check.Warning.UnusedRegexOperation, []},
151+
{Credo.Check.Warning.UnusedStringOperation, []},
152+
{Credo.Check.Warning.UnusedTupleOperation, []},
153+
{Credo.Check.Warning.UnsafeExec, []}
154+
],
155+
disabled: [
156+
#
157+
# Checks scheduled for next check update (opt-in for now, just replace `false` with `[]`)
158+
159+
# You can also customize the exit_status of each check.
160+
# If you don't want TODO comments to cause `mix credo` to fail, just
161+
# set this value to 0 (zero).
162+
#
163+
{Credo.Check.Design.TagTODO, [exit_status: 2]},
164+
{Credo.Check.Design.TagFIXME, []},
165+
166+
#
167+
# Controversial and experimental checks (opt-in, just move the check to `:enabled`
168+
# and be sure to use `mix credo --strict` to see low priority checks)
169+
#
170+
{Credo.Check.Consistency.MultiAliasImportRequireUse, []},
171+
{Credo.Check.Consistency.UnusedVariableNames, []},
172+
{Credo.Check.Design.DuplicatedCode, []},
173+
{Credo.Check.Design.SkipTestWithoutComment, []},
174+
{Credo.Check.Readability.AliasAs, []},
175+
{Credo.Check.Readability.BlockPipe, []},
176+
{Credo.Check.Readability.ImplTrue, []},
177+
{Credo.Check.Readability.MultiAlias, []},
178+
{Credo.Check.Readability.NestedFunctionCalls, []},
179+
{Credo.Check.Readability.SeparateAliasRequire, []},
180+
{Credo.Check.Readability.SingleFunctionToBlockPipe, []},
181+
{Credo.Check.Readability.SinglePipe, []},
182+
{Credo.Check.Readability.Specs, []},
183+
{Credo.Check.Readability.StrictModuleLayout, []},
184+
{Credo.Check.Readability.WithCustomTaggedTuple, []},
185+
{Credo.Check.Refactor.ABCSize, []},
186+
{Credo.Check.Refactor.AppendSingleItem, []},
187+
{Credo.Check.Refactor.DoubleBooleanNegation, []},
188+
{Credo.Check.Refactor.FilterReject, []},
189+
{Credo.Check.Refactor.IoPuts, []},
190+
{Credo.Check.Refactor.MapMap, []},
191+
{Credo.Check.Refactor.ModuleDependencies, []},
192+
{Credo.Check.Refactor.NegatedIsNil, []},
193+
{Credo.Check.Refactor.PipeChainStart, []},
194+
{Credo.Check.Refactor.RejectFilter, []},
195+
{Credo.Check.Refactor.VariableRebinding, []},
196+
{Credo.Check.Warning.LazyLogging, []},
197+
{Credo.Check.Warning.LeakyEnvironment, []},
198+
{Credo.Check.Warning.MapGetUnsafePass, []},
199+
{Credo.Check.Warning.MixEnv, []},
200+
{Credo.Check.Warning.UnsafeToAtom, []}
201+
202+
# {Credo.Check.Refactor.MapInto, []},
203+
204+
#
205+
# Custom checks can be created using `mix credo.gen.check`.
206+
#
207+
]
208+
}
209+
}
210+
]
211+
}

.github/workflows/ci.yml

+34-28
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,60 @@
11
name: CI
22

33
on:
4-
pull_request:
54
push:
65
branches:
7-
- main
6+
- "*"
87

98
jobs:
109
lint:
11-
runs-on: ubuntu-20.04
10+
runs-on: ${{ matrix.os }}
1211
env:
13-
MIX_ENV: test
12+
MIX_ENV: dev
1413
name: Lint
14+
strategy:
15+
matrix:
16+
os: ["ubuntu-20.04"]
17+
elixir: ["1.13"]
18+
otp: ["24"]
1519
steps:
1620
- uses: actions/checkout@v2
17-
- uses: erlef/setup-elixir@v1
21+
- uses: erlef/setup-beam@v1
22+
with:
23+
otp-version: ${{ matrix.otp }}
24+
elixir-version: ${{ matrix.elixir }}
25+
- uses: actions/cache@v2
1826
with:
19-
otp-version: "24"
20-
elixir-version: "1.12"
21-
- run: mix deps.get && mix deps.unlock --check-unused
27+
path: deps
28+
key: ${{ matrix.os }}-otp_${{ matrix.otp }}-elixir_${{ matrix.elixir }}-mix_${{ hashFiles('**/mix.lock') }}
29+
restore-keys: ${{ matrix.os }}-otp_${{ matrix.otp }}-elixir_${{ matrix.elixir }}-mix_
30+
- run: mix deps.get
31+
- run: mix deps.compile
2232
- run: mix format --check-formatted
33+
- run: mix deps.unlock --check-unused
34+
- run: mix credo --strict --all
35+
2336
test:
24-
runs-on: ubuntu-20.04
37+
runs-on: ${{ matrix.os }}
38+
name: Test Elixir ${{ matrix.elixir }}, OTP ${{ matrix.otp }}, OS ${{ matrix.os }}
2539
env:
2640
MIX_ENV: test
27-
name: Ubuntu / OTP ${{matrix.otp}} / Elixir ${{matrix.elixir}}
2841
strategy:
2942
fail-fast: false
3043
matrix:
31-
include:
32-
- elixir: "1.12"
33-
otp: "24"
34-
- elixir: "1.12"
35-
otp: "23"
36-
- elixir: "1.11"
37-
otp: "24"
38-
- elixir: "1.11"
39-
otp: "23"
40-
- elixir: "1.10"
41-
otp: "24"
42-
- elixir: "1.10"
43-
otp: "23"
44-
- elixir: "1.8"
45-
otp: "21"
44+
os: ["ubuntu-20.04"]
45+
elixir: ["1.13", "1.12", "1.11"]
46+
otp: ["24", "23", "22"]
4647
steps:
4748
- uses: actions/checkout@v2
48-
- uses: erlef/setup-elixir@v1
49+
- uses: erlef/setup-beam@v1
50+
with:
51+
otp-version: ${{ matrix.otp }}
52+
elixir-version: ${{ matrix.elixir }}
53+
- uses: actions/cache@v2
4954
with:
50-
otp-version: ${{matrix.otp}}
51-
elixir-version: ${{matrix.elixir}}
55+
path: deps
56+
key: ${{ matrix.os }}-otp_${{ matrix.otp }}-elixir_${{ matrix.elixir }}-mix_${{ hashFiles('**/mix.lock') }}
57+
restore-keys: ${{ matrix.os }}-otp_${{ matrix.otp }}-elixir_${{ matrix.elixir }}-mix_
5258
- run: mix deps.get --only test
5359
- run: mix deps.compile
5460
- run: mix compile

lib/ecto/adapters/sqlite3.ex

+9-7
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,14 @@ defmodule Ecto.Adapters.SQLite3 do
165165
def storage_down(options) do
166166
db_path = Keyword.fetch!(options, :database)
167167

168-
with :ok <- File.rm(db_path) do
169-
File.rm(db_path <> "-shm")
170-
File.rm(db_path <> "-wal")
171-
:ok
172-
else
173-
_ -> {:error, :already_down}
168+
case File.rm(db_path) do
169+
:ok ->
170+
File.rm(db_path <> "-shm")
171+
File.rm(db_path <> "-wal")
172+
:ok
173+
174+
_otherwise ->
175+
{:error, :already_down}
174176
end
175177
end
176178

@@ -194,7 +196,7 @@ defmodule Ecto.Adapters.SQLite3 do
194196
end
195197

196198
@impl Ecto.Adapter.Migration
197-
def supports_ddl_transaction?(), do: false
199+
def supports_ddl_transaction?, do: false
198200

199201
@impl Ecto.Adapter.Migration
200202
def lock_for_migrations(_meta, _options, fun) do

lib/ecto/adapters/sqlite3/codec.ex

+6-10
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,15 @@ defmodule Ecto.Adapters.SQLite3.Codec do
2626
def decimal_decode(nil), do: {:ok, nil}
2727

2828
def decimal_decode(x) when is_float(x) do
29-
try do
30-
{:ok, Decimal.from_float(x)}
31-
catch
32-
Decimal.Error -> :error
33-
end
29+
{:ok, Decimal.from_float(x)}
30+
catch
31+
Decimal.Error -> :error
3432
end
3533

3634
def decimal_decode(x) when is_binary(x) or is_integer(x) do
37-
try do
38-
{:ok, Decimal.new(x)}
39-
catch
40-
Decimal.Error -> :error
41-
end
35+
{:ok, Decimal.new(x)}
36+
catch
37+
Decimal.Error -> :error
4238
end
4339

4440
def decimal_decode(_), do: :error

lib/ecto/adapters/sqlite3/connection.ex

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule Ecto.Adapters.SQLite3.Connection do
33

44
@behaviour Ecto.Adapters.SQL.Connection
55

6+
alias Ecto.Adapters.SQL
67
alias Ecto.Migration.Constraint
78
alias Ecto.Migration.Index
89
alias Ecto.Migration.Reference
@@ -19,7 +20,7 @@ defmodule Ecto.Adapters.SQLite3.Connection do
1920
defp default_opts(opts) do
2021
opts
2122
|> Keyword.put_new(:journal_mode, :wal)
22-
|> Keyword.put_new(:cache_size, -64000)
23+
|> Keyword.put_new(:cache_size, -64_000)
2324
|> Keyword.put_new(:temp_store, :memory)
2425
|> Keyword.put_new(:pool_size, 5)
2526
end
@@ -320,7 +321,7 @@ defmodule Ecto.Adapters.SQLite3.Connection do
320321
def explain_query(conn, query, params, opts) do
321322
case query(conn, build_explain_query(query), params, opts) do
322323
{:ok, %Exqlite.Result{} = result} ->
323-
{:ok, Ecto.Adapters.SQL.format_table(result)}
324+
{:ok, SQL.format_table(result)}
324325

325326
error ->
326327
error

0 commit comments

Comments
 (0)