Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmi committed Feb 1, 2024
2 parents c097efb + 2237e88 commit b1a8917
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 221 deletions.
74 changes: 11 additions & 63 deletions lib/espec/diff.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,17 @@ defmodule ESpec.Diff do
edit_script(actual, expected)
end

if Version.match?(System.version(), ">= 1.10.0") do
defp edit_script(left, right) do
task =
Task.async(ExUnit.Diff, :compute, [
left,
right,
if(Version.match?(System.version(), ">= 1.11.0"), do: :===, else: :expr)
])

case Task.yield(task, 1_500) || Task.shutdown(task, :brutal_kill) do
{:ok, {script, _}} -> script
nil -> nil
end
end
else
defp edit_script(left, right) do
task = Task.async(ExUnit.Diff, :script, [left, right])

case Task.yield(task, 1_500) || Task.shutdown(task, :brutal_kill) do
{:ok, script} -> process_diff(script, {left, right})
nil -> nil
end
end

defp process_diff(diff, {actual, expected}) do
if is_nil(diff) do
{
[eq: inspect(expected, printable_limit: :infinity)],
[eq: inspect(actual, printable_limit: :infinity)]
}
else
diff
|> List.flatten()
|> split_flattened_diff()
end
end

defp split_flattened_diff(diff) do
split_flattened_diff(diff, %{
left: [],
right: []
})
end

defp split_flattened_diff([{:ins, text} | tail], processed) do
split_flattened_diff(tail, Map.update!(processed, :left, &(&1 ++ [ins: text])))
end

defp split_flattened_diff([{:del, text} | tail], processed) do
split_flattened_diff(tail, Map.update!(processed, :right, &(&1 ++ [del: text])))
end

defp split_flattened_diff([{:eq, text} | tail], processed) do
processed =
processed
|> Map.update!(:right, &(&1 ++ [eq: text]))
|> Map.update!(:left, &(&1 ++ [eq: text]))

split_flattened_diff(tail, processed)
end

defp split_flattened_diff([], processed) do
{processed.left, processed.right}
defp edit_script(left, right) do
task =
Task.async(ExUnit.Diff, :compute, [
left,
right,
:===
])

case Task.yield(task, 1_500) || Task.shutdown(task, :brutal_kill) do
{:ok, {script, _}} -> script
nil -> nil
end
end
end
108 changes: 38 additions & 70 deletions lib/espec/formatters/doc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -164,79 +164,47 @@ defmodule ESpec.Formatters.Doc do
""
end

if Version.match?(System.version(), ">= 1.10.0") do
defp format_diff(%ExUnit.Diff{
left: left,
right: right
}) do
left =
left
|> ExUnit.Diff.to_algebra(fn doc ->
Inspect.Algebra.color(
doc,
get_color_by_content(doc, :diff_delete, :diff_delete_whitespace),
%Inspect.Opts{syntax_colors: @diff_colors}
)
end)
|> Inspect.Algebra.nest(20)
|> Inspect.Algebra.format(80)

right =
right
|> ExUnit.Diff.to_algebra(fn doc ->
Inspect.Algebra.color(
doc,
get_color_by_content(doc, :diff_insert, :diff_insert_whitespace),
%Inspect.Opts{syntax_colors: @diff_colors}
)
end)
|> Inspect.Algebra.nest(20)
|> Inspect.Algebra.format(80)

"\n\t #{colorize(@main_colors[:diff_headers], "expected:")} " <>
IO.iodata_to_binary(left) <>
"\n\t #{colorize(@main_colors[:diff_headers], "actual:")} " <>
IO.iodata_to_binary(right)
end

defp get_color_by_content(content, color_if_normal, color_if_whitespace)
when is_binary(content) do
if String.trim_leading(content) == "", do: color_if_whitespace, else: color_if_normal
end

defp get_color_by_content(_content, color_if_normal, _color_if_whitespace) do
color_if_normal
end
else
defp format_diff({l, r}) do
[
"",
"\t #{colorize(@main_colors[:diff_headers], "expected:")} #{colorize_diff(r)}",
"\t #{colorize(@main_colors[:diff_headers], "actual:")} #{colorize_diff(l)}"
]
|> Enum.join("\n")
end

defp colorize_diff([{:eq, text} | rest]) do
text <> colorize_diff(rest)
end

defp colorize_diff([{:ins, text} | rest]) do
colorize(@diff_colors[:diff_insert], text) <> colorize_diff(rest)
end
defp format_diff(%ExUnit.Diff{
left: left,
right: right
}) do
left =
left
|> ExUnit.Diff.to_algebra(fn doc ->
Inspect.Algebra.color(
doc,
get_color_by_content(doc, :diff_delete, :diff_delete_whitespace),
%Inspect.Opts{syntax_colors: @diff_colors}
)
end)
|> Inspect.Algebra.nest(20)
|> Inspect.Algebra.format(80)

right =
right
|> ExUnit.Diff.to_algebra(fn doc ->
Inspect.Algebra.color(
doc,
get_color_by_content(doc, :diff_insert, :diff_insert_whitespace),
%Inspect.Opts{syntax_colors: @diff_colors}
)
end)
|> Inspect.Algebra.nest(20)
|> Inspect.Algebra.format(80)

defp colorize_diff([{:del, text} | rest]) do
colorize(@diff_colors[:diff_delete], text) <> colorize_diff(rest)
end
"\n\t #{colorize(@main_colors[:diff_headers], "expected:")} " <>
IO.iodata_to_binary(left) <>
"\n\t #{colorize(@main_colors[:diff_headers], "actual:")} " <>
IO.iodata_to_binary(right)
end

defp colorize_diff([{:ins_whitespace, length} | rest]) do
colorize(@diff_colors[:diff_insert_whitespace], String.duplicate(" ", length)) <>
colorize_diff(rest)
end
defp get_color_by_content(content, color_if_normal, color_if_whitespace)
when is_binary(content) do
if String.trim_leading(content) == "", do: color_if_whitespace, else: color_if_normal
end

defp colorize_diff([]) do
""
end
defp get_color_by_content(_content, color_if_normal, _color_if_whitespace) do
color_if_normal
end

defp format_footer(examples, failed, pending) do
Expand Down
2 changes: 1 addition & 1 deletion lib/mix/tasks/espec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule Mix.Tasks.Espec do
end

defp cover_function(mod, output) do
case :cover.analyse_to_file(mod, '#{output}/#{mod}.html', [:html]) do
case :cover.analyse_to_file(mod, ~c"#{output}/#{mod}.html", [:html]) do
{:ok, _} -> nil
{:error, error} -> Mix.shell().info("#{error} while generating cover results for #{mod}")
end
Expand Down
55 changes: 0 additions & 55 deletions lib/mix/utils/stale_compatible.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,46 +31,6 @@ defmodule Mix.Utils.StaleCompatible do
## Test changed dependency resolution

cond do
Version.match?(System.version(), "< 1.10.0") ->
def tests_with_changed_references(%Version{major: 1, minor: minor} = version, test_sources)
when minor < 10 do
test_manifest = Stale.manifest()
[elixir_manifest] = Mix.Tasks.Compile.Elixir.manifests()

if Mix.Utils.stale?([elixir_manifest], [test_manifest]) do
elixir_manifest_entries =
apply(CE, :read_manifest, [elixir_manifest, Mix.Project.compile_path()])
|> Enum.group_by(&elem(&1, 0))

stale_modules =
for CE.module(module: module) <- elixir_manifest_entries.module,
# version 1.9 has this:
# for CE.module(module: module, beam: beam) <- elixir_manifest_entries.module
# but beam was removed in v1.10.
beam = Path.join(Mix.Project.compile_path(), Atom.to_string(module) <> ".beam"),
Mix.Utils.stale?([beam], [test_manifest]),
do: module,
into: MapSet.new()

stale_modules =
find_all_dependent_on(
version,
stale_modules,
elixir_manifest_entries.source,
elixir_manifest_entries.module
)

for module <- stale_modules,
source(source: source, runtime_references: r, compile_references: c) <-
test_sources,
module in r or module in c,
do: source,
into: MapSet.new()
else
MapSet.new()
end
end

Version.match?(System.version(), "< 1.16.0") ->
def tests_with_changed_references(%Version{major: 1, minor: minor} = version, test_sources)
when minor >= 10 and minor < 16 do
Expand Down Expand Up @@ -262,21 +222,6 @@ defmodule Mix.Utils.StaleCompatible do
end

cond do
Version.match?(System.version(), "< 1.11.0") ->
defp dependent_modules(%Version{major: 1, minor: minor}, module, modules, sources)
when minor >= 10 do
for CE.source(
source: source,
runtime_references: r,
compile_references: c,
struct_references: s
) <- sources,
module in r or module in c or module in s,
CE.module(sources: sources, module: dependent_module) <- modules,
source in sources,
do: dependent_module
end

Version.match?(System.version(), "< 1.16.0") ->
defp dependent_modules(%Version{major: 1, minor: minor}, module, modules, sources)
when minor >= 11 do
Expand Down
8 changes: 4 additions & 4 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule ESpec.Mixfile do
app: :espec,
name: "ESpec",
version: @version,
elixir: ">= 1.10.0",
elixir: ">= 1.13.0",
description: description(),
package: package(),
deps: deps(),
Expand All @@ -19,15 +19,15 @@ defmodule ESpec.Mixfile do
end

def application do
[applications: [], extra_applications: [:eex, :meck]]
[extra_applications: [:eex, :meck]]
end

defp deps do
[
{:meck, "~> 0.9"},
{:credo, "~> 1.6", only: [:dev, :test], runtime: false},
{:credo, "~> 1.7", only: [:dev, :test], runtime: false},
# Docs
{:ex_doc, "~> 0.28", only: [:docs, :dev]}
{:ex_doc, "~> 0.31", only: [:docs, :dev]}
]
end

Expand Down
20 changes: 10 additions & 10 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
%{
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
"credo": {:hex, :credo, "1.6.4", "ddd474afb6e8c240313f3a7b0d025cc3213f0d171879429bf8535d7021d9ad78", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "c28f910b61e1ff829bffa056ef7293a8db50e87f2c57a9b5c3f57eee124536b7"},
"bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"},
"credo": {:hex, :credo, "1.7.3", "05bb11eaf2f2b8db370ecaa6a6bda2ec49b2acd5e0418bc106b73b07128c0436", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "35ea675a094c934c22fb1dca3696f3c31f2728ae6ef5a53b5d648c11180a4535"},
"earmark": {:hex, :earmark, "1.3.1", "73812f447f7a42358d3ba79283cfa3075a7580a3a2ed457616d6517ac3738cb9", [:mix], [], "hexpm", "000aaeff08919e95e7aea13e4af7b2b9734577b3e6a7c50ee31ee88cab6ec4fb"},
"earmark_parser": {:hex, :earmark_parser, "1.4.25", "2024618731c55ebfcc5439d756852ec4e85978a39d0d58593763924d9a15916f", [:mix], [], "hexpm", "56749c5e1c59447f7b7a23ddb235e4b3defe276afc220a6227237f3efe83f51e"},
"ex_doc": {:hex, :ex_doc, "0.28.4", "001a0ea6beac2f810f1abc3dbf4b123e9593eaa5f00dd13ded024eae7c523298", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "bf85d003dd34911d89c8ddb8bda1a958af3471a274a4c2150a9c01c78ac3f8ed"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.0", "f8c570a0d33f8039513fbccaf7108c5d750f47d8defd44088371191b76492b0b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "28b2cbdc13960a46ae9a8858c4bebdec3c9a6d7b4b9e7f4ed1502f8159f338e7"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
"earmark_parser": {:hex, :earmark_parser, "1.4.39", "424642f8335b05bb9eb611aa1564c148a8ee35c9c8a8bba6e129d51a3e3c6769", [:mix], [], "hexpm", "06553a88d1f1846da9ef066b87b57c6f605552cfbe40d20bd8d59cc6bde41944"},
"ex_doc": {:hex, :ex_doc, "0.31.1", "8a2355ac42b1cc7b2379da9e40243f2670143721dd50748bf6c3b1184dae2089", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.1", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "3178c3a407c557d8343479e1ff117a96fd31bafe52a039079593fb0524ef61b0"},
"file_system": {:hex, :file_system, "1.0.0", "b689cc7dcee665f774de94b5a832e578bd7963c8e637ef940cd44327db7de2cd", [:mix], [], "hexpm", "6752092d66aec5a10e662aefeed8ddb9531d79db0bc145bb8c40325ca1d8536d"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"makeup": {:hex, :makeup, "1.1.1", "fa0bc768698053b2b3869fa8a62616501ff9d11a562f3ce39580d60860c3a55e", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "5dc62fbdd0de44de194898b6710692490be74baa02d9d108bc29f007783b0b48"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.3", "d684f4bac8690e70b06eb52dad65d26de2eefa44cd19d64a8095e1417df7c8fd", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "b78dc853d2e670ff6390b605d807263bf606da3c82be37f9d7f68635bd886fc9"},
"meck": {:hex, :meck, "0.9.0", "cb40c223cf403db2d09def59d32d3682074ebecceb64f3e6f6c4477458df124d", [:rebar3], [], "hexpm", "f813e90dd0b89b2516a0201a355e84b1abc78b5751aa0cbf669a9d85a810ac63"},
"nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"},
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
}
13 changes: 3 additions & 10 deletions spec/diff_spec.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ defmodule DiffSpec do
use ESpec, async: true

defmacrop diff_1_2 do
if Version.match?(System.version(), ">= 1.10.0") and
Version.match?(System.version(), "< 1.15.0") do
if Version.match?(System.version(), "< 1.15.0") do
quote do
%ExUnit.Diff{
equivalent?: false,
Expand All @@ -23,14 +22,8 @@ defmodule DiffSpec do
end

defmacrop diff_1_1 do
if Version.match?(System.version(), ">= 1.10.0") do
quote do
%ExUnit.Diff{equivalent?: true, left: 1, right: 1}
end
else
quote do
{[eq: "1"], [eq: "1"]}
end
quote do
%ExUnit.Diff{equivalent?: true, left: 1, right: 1}
end
end

Expand Down
8 changes: 0 additions & 8 deletions test/assertions/match_pattern_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,6 @@ defmodule MatchPatternTest do
expect({:ok, 1}) |> to_not(match_pattern(^pattern))
end
end

context "with let functions" do
let foo: "bar"

it do
expect("bar") |> to(match_pattern foo())
end
end
end
end

Expand Down

0 comments on commit b1a8917

Please sign in to comment.