Skip to content

Commit 797c992

Browse files
authored
Running credo (#122)
1 parent 4ee808a commit 797c992

32 files changed

+388
-162
lines changed

.credo.exs

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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+
# You can also customize the exit_status of each check.
88+
# If you don't want TODO comments to cause `mix credo` to fail, just
89+
# set this value to 0 (zero).
90+
#
91+
{Credo.Check.Design.TagTODO, [exit_status: 2]},
92+
{Credo.Check.Design.TagFIXME, []},
93+
94+
#
95+
## Readability Checks
96+
#
97+
{Credo.Check.Readability.AliasOrder, []},
98+
{Credo.Check.Readability.FunctionNames, []},
99+
{Credo.Check.Readability.LargeNumbers, []},
100+
{Credo.Check.Readability.MaxLineLength, [priority: :low, max_length: 120]},
101+
{Credo.Check.Readability.ModuleAttributeNames, []},
102+
{Credo.Check.Readability.ModuleDoc, []},
103+
{Credo.Check.Readability.ModuleNames, []},
104+
{Credo.Check.Readability.ParenthesesInCondition, []},
105+
{Credo.Check.Readability.ParenthesesOnZeroArityDefs, []},
106+
{Credo.Check.Readability.PipeIntoAnonymousFunctions, []},
107+
{Credo.Check.Readability.PredicateFunctionNames, []},
108+
{Credo.Check.Readability.PreferImplicitTry, []},
109+
{Credo.Check.Readability.RedundantBlankLines, []},
110+
{Credo.Check.Readability.Semicolons, []},
111+
{Credo.Check.Readability.SpaceAfterCommas, []},
112+
{Credo.Check.Readability.StringSigils, []},
113+
{Credo.Check.Readability.TrailingBlankLine, []},
114+
{Credo.Check.Readability.TrailingWhiteSpace, []},
115+
{Credo.Check.Readability.UnnecessaryAliasExpansion, []},
116+
{Credo.Check.Readability.VariableNames, []},
117+
{Credo.Check.Readability.WithSingleClause, []},
118+
119+
#
120+
## Refactoring Opportunities
121+
#
122+
{Credo.Check.Refactor.Apply, []},
123+
{Credo.Check.Refactor.CondStatements, []},
124+
{Credo.Check.Refactor.FunctionArity, []},
125+
{Credo.Check.Refactor.LongQuoteBlocks, []},
126+
{Credo.Check.Refactor.MatchInCondition, []},
127+
{Credo.Check.Refactor.MapJoin, []},
128+
{Credo.Check.Refactor.NegatedConditionsInUnless, []},
129+
{Credo.Check.Refactor.NegatedConditionsWithElse, []},
130+
{Credo.Check.Refactor.Nesting, []},
131+
{Credo.Check.Refactor.UnlessWithElse, []},
132+
{Credo.Check.Refactor.WithClauses, []},
133+
{Credo.Check.Refactor.FilterFilter, []},
134+
{Credo.Check.Refactor.RejectReject, []},
135+
{Credo.Check.Refactor.RedundantWithClauseResult, []},
136+
137+
#
138+
## Warnings
139+
#
140+
{Credo.Check.Warning.ApplicationConfigInModuleAttribute, []},
141+
{Credo.Check.Warning.BoolOperationOnSameValues, []},
142+
{Credo.Check.Warning.ExpensiveEmptyEnumCheck, []},
143+
{Credo.Check.Warning.IExPry, []},
144+
{Credo.Check.Warning.IoInspect, []},
145+
{Credo.Check.Warning.OperationOnSameValues, []},
146+
{Credo.Check.Warning.OperationWithConstantResult, []},
147+
{Credo.Check.Warning.RaiseInsideRescue, []},
148+
{Credo.Check.Warning.SpecWithStruct, []},
149+
{Credo.Check.Warning.WrongTestFileExtension, []},
150+
{Credo.Check.Warning.UnusedEnumOperation, []},
151+
{Credo.Check.Warning.UnusedFileOperation, []},
152+
{Credo.Check.Warning.UnusedKeywordOperation, []},
153+
{Credo.Check.Warning.UnusedListOperation, []},
154+
{Credo.Check.Warning.UnusedPathOperation, []},
155+
{Credo.Check.Warning.UnusedRegexOperation, []},
156+
{Credo.Check.Warning.UnusedStringOperation, []},
157+
{Credo.Check.Warning.UnusedTupleOperation, []},
158+
{Credo.Check.Warning.UnsafeExec, []}
159+
],
160+
disabled: [
161+
#
162+
# Checks scheduled for next check update (opt-in for now, just replace `false` with `[]`)
163+
164+
#
165+
# Controversial and experimental checks (opt-in, just move the check to `:enabled`
166+
# and be sure to use `mix credo --strict` to see low priority checks)
167+
#
168+
{Credo.Check.Refactor.CyclomaticComplexity, [max_complexity: 15]},
169+
{Credo.Check.Consistency.MultiAliasImportRequireUse, []},
170+
{Credo.Check.Consistency.UnusedVariableNames, []},
171+
{Credo.Check.Design.DuplicatedCode, []},
172+
{Credo.Check.Design.SkipTestWithoutComment, []},
173+
{Credo.Check.Readability.AliasAs, []},
174+
{Credo.Check.Readability.BlockPipe, []},
175+
{Credo.Check.Readability.ImplTrue, []},
176+
{Credo.Check.Readability.MultiAlias, []},
177+
{Credo.Check.Readability.NestedFunctionCalls, []},
178+
{Credo.Check.Readability.SeparateAliasRequire, []},
179+
{Credo.Check.Readability.SingleFunctionToBlockPipe, []},
180+
{Credo.Check.Readability.SinglePipe, []},
181+
{Credo.Check.Readability.Specs, []},
182+
{Credo.Check.Readability.StrictModuleLayout, []},
183+
{Credo.Check.Readability.WithCustomTaggedTuple, []},
184+
{Credo.Check.Refactor.ABCSize, []},
185+
{Credo.Check.Refactor.AppendSingleItem, []},
186+
{Credo.Check.Refactor.DoubleBooleanNegation, []},
187+
{Credo.Check.Refactor.FilterReject, []},
188+
{Credo.Check.Refactor.IoPuts, []},
189+
{Credo.Check.Refactor.MapMap, []},
190+
{Credo.Check.Refactor.ModuleDependencies, []},
191+
{Credo.Check.Refactor.NegatedIsNil, []},
192+
{Credo.Check.Refactor.PipeChainStart, []},
193+
{Credo.Check.Refactor.RejectFilter, []},
194+
{Credo.Check.Refactor.VariableRebinding, []},
195+
{Credo.Check.Warning.LazyLogging, []},
196+
{Credo.Check.Warning.LeakyEnvironment, []},
197+
{Credo.Check.Warning.MapGetUnsafePass, []},
198+
{Credo.Check.Warning.MixEnv, []},
199+
{Credo.Check.Warning.UnsafeToAtom, []}
200+
201+
# {Credo.Check.Refactor.MapInto, []},
202+
203+
#
204+
# Custom checks can be created using `mix credo.gen.check`.
205+
#
206+
]
207+
}
208+
}
209+
]
210+
}

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,9 @@ run the test cases against other MongoDB deployments or older versions, you can
604604
```bash
605605
pyenv global 3.6
606606
pip3 install --upgrade pip
607-
pip3 install mtools[all]
607+
pip3 install 'mtools[all]'
608608
export PATH=to-your-mongodb/bin/:$PATH
609+
ulimit -S -n 2048 ## in case of Mac OS X
609610
mlaunch init --setParameter enableTestCommands=1 --replicaset --name "rs_1"
610611
mix test --exclude ssl --exclude socket
611612
```
@@ -640,7 +641,7 @@ Special thanks to [JetBrains](https://www.jetbrains.com/?from=elixir-mongodb-dri
640641
## Copyright and License
641642

642643
Copyright 2015 Eric Meadows-Jönsson and Justin Wood \
643-
Copyright 2019 - 2021 Michael Maier
644+
Copyright 2019 - 2022 Michael Maier
644645

645646
Licensed under the Apache License, Version 2.0 (the "License");
646647
you may not use this file except in compliance with the License.

lib/bson/decimal128.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ defmodule BSON.Decimal128 do
6969
end
7070

7171
def encode(%Decimal{sign: sign, coef: significand, exp: exponent}) when exponent >= @min_exponent and exponent <= @max_exponent do
72-
biasedExponent = exponent + @exponent_bias
72+
biased_exponent = exponent + @exponent_bias
7373
low = significand &&& @low_mask
7474
## mask max significand
7575
high = significand >>> 64 &&& @significand_mask
76-
high = bor(high, biasedExponent <<< 49)
76+
high = bor(high, biased_exponent <<< 49)
7777

7878
high =
7979
case sign do
@@ -89,7 +89,7 @@ defmodule BSON.Decimal128 do
8989
raise ArgumentError, message
9090
end
9191

92-
defp exponent(high, _two_highest_bits_set = true) do
92+
defp exponent(high, true) do
9393
biased_exponent = high >>> 47 &&& @exponent_mask
9494
biased_exponent - @exponent_bias
9595
end

lib/mongo.ex

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ defmodule Mongo do
6666
alias Mongo.Events.CommandFailedEvent
6767
alias Mongo.Error
6868

69-
# 5000
70-
@timeout 15000
69+
@timeout 15_000
7170

7271
@type conn :: DbConnection.Conn
7372
@type collection :: String.t()
@@ -179,7 +178,7 @@ defmodule Mongo do
179178
iex> Mongo.uuid("848e90e9-5750-4e0a-ab73-66-c6b328242")
180179
{:error, %ArgumentError{message: "non-alphabet digit found: \"-\" (byte 45)"}}
181180
"""
182-
@spec uuid(String.t()) :: {:ok, BSON.Binary.t()} | {:error, %ArgumentError{}}
181+
@spec uuid(String.t()) :: {:ok, BSON.Binary.t()} | {:error, ArgumentError.t()}
183182
def uuid(string) when is_binary(string) and byte_size(string) == 36 do
184183
try do
185184
p1 = binary_part(string, 0, 8) |> Base.decode16!(case: :lower)
@@ -213,9 +212,8 @@ defmodule Mongo do
213212
(mongodb_driver 0.6.4) lib/mongo.ex:205: Mongo.uuid!/1
214213
"""
215214
def uuid!(string) do
216-
with {:ok, result} <- uuid(string) do
217-
result
218-
else
215+
case uuid(string) do
216+
{:ok, result} -> result
219217
{:error, reason} -> raise reason
220218
end
221219
end
@@ -791,9 +789,7 @@ defmodule Mongo do
791789
"""
792790
@spec command(GenServer.server(), BSON.document(), Keyword.t()) :: result(BSON.document())
793791
def command(topology_pid, cmd, opts \\ []) do
794-
with {:ok, doc} <- issue_command(topology_pid, cmd, :write, opts) do
795-
{:ok, doc}
796-
end
792+
issue_command(topology_pid, cmd, :write, opts)
797793
end
798794

799795
@doc false
@@ -902,9 +898,7 @@ defmodule Mongo do
902898
"""
903899
@spec wire_version(GenServer.server()) :: {:ok, integer} | {:error, Mongo.Error.t()}
904900
def wire_version(topology_pid) do
905-
with {:ok, wire_version} <- Topology.wire_version(topology_pid) do
906-
{:ok, wire_version}
907-
end
901+
Topology.wire_version(topology_pid)
908902
end
909903

910904
@doc """
@@ -927,9 +921,7 @@ defmodule Mongo do
927921
"""
928922
@spec limits(GenServer.server()) :: {:ok, BSON.document()} | {:error, Mongo.Error.t()}
929923
def limits(topology_pid) do
930-
with {:ok, limits} <- Topology.limits(topology_pid) do
931-
{:ok, limits}
932-
end
924+
Topology.limits(topology_pid)
933925
end
934926

935927
@doc """
@@ -1335,7 +1327,7 @@ defmodule Mongo do
13351327
@doc """
13361328
Convenient function that returns a cursor with the names of the indexes.
13371329
"""
1338-
@spec list_index_names(GenServer.server(), String.t(), Keyword.t()) :: %Stream{}
1330+
@spec list_index_names(GenServer.server(), String.t(), Keyword.t()) :: Stream.t()
13391331
def list_index_names(topology_pid, coll, opts \\ []) do
13401332
list_indexes(topology_pid, coll, opts)
13411333
|> Stream.map(fn %{"name" => name} -> name end)

lib/mongo/auth/x509.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ defmodule Mongo.Auth.X509 do
55
def auth({username, _password}, _db, s) do
66
cmd = [authenticate: 1, user: username, mechanism: "MONGODB-X509"]
77

8-
with {:ok, _flags, _message} <- Utils.command(-2, cmd, s) do
9-
:ok
10-
else
8+
case Utils.command(-2, cmd, s) do
9+
{:ok, _flags, _message} -> :ok
1110
_error -> {:error, "X509 auth failed"}
1211
end
1312
end

lib/mongo/bulk_write.ex

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,9 @@ defmodule Mongo.BulkWrite do
252252
# Executes the command `cmd` and collects the result.
253253
#
254254
defp one_bulk_write_operation(session, cmd, coll, docs, max_batch_size, opts) do
255-
with result <-
256-
session
257-
|> run_commands(get_cmds(cmd, coll, docs, max_batch_size, opts), opts)
258-
|> collect(cmd) do
259-
result
260-
end
255+
session
256+
|> run_commands(get_cmds(cmd, coll, docs, max_batch_size, opts), opts)
257+
|> collect(cmd)
261258
end
262259

263260
##

lib/mongo/change_stream.ex

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
defmodule Mongo.ChangeStream do
2+
@moduledoc false
3+
24
alias Mongo.Session
35
alias Mongo.Error
46

@@ -132,18 +134,19 @@ defmodule Mongo.ChangeStream do
132134
]
133135
|> filter_nils()
134136

135-
with {:ok, %{"operationTime" => op_time, "cursor" => %{"id" => new_cursor_id, "nextBatch" => docs} = cursor, "ok" => ok}} when ok == 1 <- Mongo.exec_command_session(session, get_more, opts) do
136-
old_token = change_stream(change_stream, :resume_token)
137-
change_stream = update_change_stream(change_stream, cursor["postBatchResumeToken"], op_time, List.last(docs))
138-
new_token = change_stream(change_stream, :resume_token)
137+
case Mongo.exec_command_session(session, get_more, opts) do
138+
{:ok, %{"operationTime" => op_time, "cursor" => %{"id" => new_cursor_id, "nextBatch" => docs} = cursor, "ok" => ok}} when ok == 1 ->
139+
old_token = change_stream(change_stream, :resume_token)
140+
change_stream = update_change_stream(change_stream, cursor["postBatchResumeToken"], op_time, List.last(docs))
141+
new_token = change_stream(change_stream, :resume_token)
139142

140-
case token_changes(old_token, new_token) do
141-
true -> fun.(new_token)
142-
false -> :noop
143-
end
143+
case token_changes(old_token, new_token) do
144+
true -> fun.(new_token)
145+
false -> :noop
146+
end
147+
148+
{:ok, %{cursor_id: new_cursor_id, docs: docs, change_stream: change_stream}}
144149

145-
{:ok, %{cursor_id: new_cursor_id, docs: docs, change_stream: change_stream}}
146-
else
147150
{:error, %Mongo.Error{resumable: false} = not_resumable} ->
148151
{:error, not_resumable}
149152

0 commit comments

Comments
 (0)