Skip to content

Commit cf628ce

Browse files
committed
copy tag
1 parent de2ceb2 commit cf628ce

File tree

4 files changed

+250
-4
lines changed

4 files changed

+250
-4
lines changed

.credo.exs

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

lib/ex_example.ex

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ defmodule ExExample do
1212
# module attribute that holds all the examples
1313
Module.register_attribute(__MODULE__, :example_dependencies, accumulate: true)
1414
Module.register_attribute(__MODULE__, :examples, accumulate: true)
15+
Module.register_attribute(__MODULE__, :copies, accumulate: true)
16+
Module.register_attribute(__MODULE__, :copy, accumulate: false)
1517

1618
@before_compile unquote(__MODULE__)
1719
end
@@ -69,6 +71,11 @@ defmodule ExExample do
6971
end)
7072
|> Graph.topsort()
7173
end
74+
75+
def __example_copy__(example_name) do
76+
@copies
77+
|> Keyword.get(example_name, nil)
78+
end
7279
end
7380
end
7481

@@ -84,16 +91,24 @@ defmodule ExExample do
8491
hidden_example_name = String.to_atom("__#{example_name}__")
8592

8693
quote do
94+
# fetch the attribute value, and then clear it for the next examples.
95+
example_copy_tag = Module.get_attribute(unquote(__CALLER__.module), :copy)
96+
Module.delete_attribute(unquote(__CALLER__.module), :copy)
97+
8798
def unquote({hidden_example_name, context, args}) do
8899
unquote(body)
89100
end
90101

102+
@copies {unquote(example_name), {unquote(__CALLER__.module), example_copy_tag}}
91103
@example_dependencies {unquote(example_name), unquote(called_functions)}
92104
@examples unquote(example_name)
93105
def unquote(name) do
94106
example_dependencies = __example_dependencies__(unquote(example_name))
107+
example_copy = __example_copy__(unquote(example_name))
95108

96-
Executor.maybe_run_example(__MODULE__, unquote(example_name), example_dependencies)
109+
Executor.maybe_run_example(__MODULE__, unquote(example_name), example_dependencies,
110+
copy: example_copy
111+
)
97112
end
98113
end
99114
end

lib/ex_example/executor.ex

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ defmodule ExExample.Executor do
4343
If any of the example its dependencies either failed or were skipped,
4444
I will skip the example.
4545
"""
46-
@spec maybe_run_example(atom(), atom(), list(dependency)) :: any()
47-
def maybe_run_example(module, func, dependencies) do
46+
@spec maybe_run_example(atom(), atom(), list(dependency), Keyword.t()) :: any()
47+
def maybe_run_example(module, func, dependencies, copy: copy) do
4848
dependency_results =
4949
dependencies
5050
|> Enum.map(fn {{module, func}, _arity} ->
@@ -64,7 +64,7 @@ defmodule ExExample.Executor do
6464
# cached result, no recompile
6565
{:ok, result} ->
6666
Logger.debug("found cached result for #{inspect(module)}.#{func}")
67-
result.result
67+
copy_result(result, copy).result
6868

6969
{:error, :no_result} ->
7070
Logger.debug("running #{inspect(module)}.#{func} for the first time")
@@ -110,4 +110,13 @@ defmodule ExExample.Executor do
110110

111111
result
112112
end
113+
114+
# @doc """
115+
# Given a result from a previous invocation and a copy function, I create a copy of the result.
116+
# """
117+
defp copy_result(%Cache.Result{} = result, {_, nil}), do: result
118+
119+
defp copy_result(%Cache.Result{} = result, {module, func}) do
120+
%{result | result: apply(module, func, [result.result])}
121+
end
113122
end

lib/examples/stack_examples.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defmodule Examples.Stack do
66

77
import ExUnit.Assertions
88

9+
@copy :do_copy
910
example new_stack do
1011
{:ok, stack} = Stack.create()
1112
assert stack == %Stack{}
@@ -29,4 +30,8 @@ defmodule Examples.Stack do
2930
{:ok, stack, 1} = Stack.pop(stack)
3031
stack
3132
end
33+
34+
def do_copy(stack) do
35+
%Stack{elements: stack.elements}
36+
end
3237
end

0 commit comments

Comments
 (0)