|
| 1 | +defmodule Mix.Tasks.GradientTest do |
| 2 | + use ExUnit.Case |
| 3 | + |
| 4 | + import ExUnit.CaptureIO |
| 5 | + |
| 6 | + @examples_path "test/examples/" |
| 7 | + @type_path Path.join([@examples_path, "type"]) |
| 8 | + |
| 9 | + @s_wrong_ret_beam "Elixir.SWrongRet.beam" |
| 10 | + @s_wrong_ret_ex "s_wrong_ret.ex" |
| 11 | + |
| 12 | + test "--no-compile option" do |
| 13 | + build_dir = Path.join(@type_path, "_build") |
| 14 | + run_task(@type_path, [@s_wrong_ret_beam]) |
| 15 | + assert was_dir?(build_dir) |
| 16 | + |
| 17 | + run_task(@type_path, ["--no-compile", "--", @s_wrong_ret_beam]) |
| 18 | + assert not was_dir?(build_dir) |
| 19 | + end |
| 20 | + |
| 21 | + test "path to the beam file" do |
| 22 | + output = run_task(@type_path, ["--no-compile", "--", @s_wrong_ret_beam]) |
| 23 | + assert 3 == String.split(output, @s_wrong_ret_ex) |> length() |
| 24 | + end |
| 25 | + |
| 26 | + test "path to the ex file" do |
| 27 | + output = run_task(@type_path, ["--no-compile", "--", @s_wrong_ret_ex]) |
| 28 | + assert 3 == String.split(output, @s_wrong_ret_ex) |> length() |
| 29 | + end |
| 30 | + |
| 31 | + test "no_fancy option" do |
| 32 | + output = run_task(@type_path, ["--no-compile", "--", @s_wrong_ret_beam]) |
| 33 | + assert String.contains?(output, "The integer on line") |
| 34 | + assert String.contains?(output, "The tuple on line") |
| 35 | + |
| 36 | + output = run_task(@type_path, ["--no-compile", "--no-fancy", "--", @s_wrong_ret_beam]) |
| 37 | + assert String.contains?(output, "The integer \e[33m1\e[0m on line") |
| 38 | + assert String.contains?(output, "The tuple \e[33m{:ok, []}\e[0m on line") |
| 39 | + end |
| 40 | + |
| 41 | + describe "colors" do |
| 42 | + test "no_colors option" do |
| 43 | + output = run_task(@type_path, ["--no-compile", "--", @s_wrong_ret_beam]) |
| 44 | + assert String.contains?(output, IO.ANSI.cyan()) |
| 45 | + assert String.contains?(output, IO.ANSI.red()) |
| 46 | + |
| 47 | + output = run_task(@type_path, ["--no-compile", "--no-colors", "--", @s_wrong_ret_beam]) |
| 48 | + assert not String.contains?(output, IO.ANSI.cyan()) |
| 49 | + assert not String.contains?(output, IO.ANSI.red()) |
| 50 | + end |
| 51 | + |
| 52 | + test "--expr-color and --type-color option" do |
| 53 | + output = |
| 54 | + run_task(@type_path, [ |
| 55 | + "--no-compile", |
| 56 | + "--no-fancy", |
| 57 | + "--expr-color", |
| 58 | + "green", |
| 59 | + "--type-color", |
| 60 | + "magenta", |
| 61 | + "--", |
| 62 | + @s_wrong_ret_beam |
| 63 | + ]) |
| 64 | + |
| 65 | + assert String.contains?(output, IO.ANSI.green()) |
| 66 | + assert String.contains?(output, IO.ANSI.magenta()) |
| 67 | + end |
| 68 | + |
| 69 | + test "--underscore_color option" do |
| 70 | + output = |
| 71 | + run_task(@type_path, [ |
| 72 | + "--no-compile", |
| 73 | + "--underscore-color", |
| 74 | + "green", |
| 75 | + "--", |
| 76 | + @s_wrong_ret_beam |
| 77 | + ]) |
| 78 | + |
| 79 | + assert String.contains?(output, IO.ANSI.green()) |
| 80 | + assert not String.contains?(output, IO.ANSI.red()) |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + test "--no-gradualizer-check option" do |
| 85 | + output = |
| 86 | + run_task(@type_path, ["--no-compile", "--no-gradualizer-check", "--", @s_wrong_ret_beam]) |
| 87 | + |
| 88 | + assert String.contains?(output, "No problems found!") |
| 89 | + end |
| 90 | + |
| 91 | + test "--no-ex-check option" do |
| 92 | + beam = "Elixir.SpecAfterSpec.beam" |
| 93 | + ex_spec_error_msg = "The spec convert_a/1 on line" |
| 94 | + |
| 95 | + output = run_task(@examples_path, ["--no-compile", "--", beam]) |
| 96 | + assert String.contains?(output, ex_spec_error_msg) |
| 97 | + |
| 98 | + output = run_task(@examples_path, ["--no-compile", "--no-ex-check", "--", beam]) |
| 99 | + assert not String.contains?(output, ex_spec_error_msg) |
| 100 | + end |
| 101 | + |
| 102 | + test "--no-specify option" do |
| 103 | + info = "Specifying froms..." |
| 104 | + |
| 105 | + output = run_task(@type_path, ["--no-compile", "--", @s_wrong_ret_beam]) |
| 106 | + assert String.contains?(output, info) |
| 107 | + |
| 108 | + output = run_task(@type_path, ["--no-compile", "--no-specify", "--", @s_wrong_ret_beam]) |
| 109 | + assert not String.contains?(output, info) |
| 110 | + end |
| 111 | + |
| 112 | + test "--stop-on-first-error option" do |
| 113 | + output = |
| 114 | + run_task(@type_path, ["--no-compile", "--stop-on-first-error", "--", @s_wrong_ret_beam]) |
| 115 | + |
| 116 | + assert 2 == String.split(output, @s_wrong_ret_ex) |> length() |
| 117 | + end |
| 118 | + |
| 119 | + test "--fmt-location option" do |
| 120 | + output = |
| 121 | + run_task(@type_path, ["--no-compile", "--fmt-location", "none", "--", @s_wrong_ret_beam]) |
| 122 | + |
| 123 | + assert String.contains?(output, "s_wrong_ret.ex: The integer is expected to have type") |
| 124 | + |
| 125 | + output = |
| 126 | + run_task(@type_path, ["--no-compile", "--fmt-location", "brief", "--", @s_wrong_ret_beam]) |
| 127 | + |
| 128 | + assert String.contains?(output, "s_wrong_ret.ex:3: The integer is expected to have type") |
| 129 | + |
| 130 | + output = |
| 131 | + run_task(@type_path, ["--no-compile", "--fmt-location", "verbose", "--", @s_wrong_ret_beam]) |
| 132 | + |
| 133 | + assert String.contains?( |
| 134 | + output, |
| 135 | + "s_wrong_ret.ex: The integer on line 3 is expected to have type" |
| 136 | + ) |
| 137 | + end |
| 138 | + |
| 139 | + test "--no-deps option" do |
| 140 | + info = "Loading deps..." |
| 141 | + |
| 142 | + output = run_task(@type_path, ["--no-compile", "--", @s_wrong_ret_beam]) |
| 143 | + assert String.contains?(output, info) |
| 144 | + |
| 145 | + output = run_task(@type_path, ["--no-compile", "--no-deps", "--", @s_wrong_ret_beam]) |
| 146 | + assert not String.contains?(output, info) |
| 147 | + end |
| 148 | + |
| 149 | + test "--infer option" do |
| 150 | + # FIXME provide implementation |
| 151 | + end |
| 152 | + |
| 153 | + def run_task(rel_path, args) do |
| 154 | + run_in_path(rel_path, fn -> |
| 155 | + capture_io(fn -> Mix.Tasks.Gradient.run(args) end) |
| 156 | + end) |
| 157 | + end |
| 158 | + |
| 159 | + def run_in_path(path, fun) do |
| 160 | + cwd = File.cwd!() |
| 161 | + File.cd(path) |
| 162 | + res = fun.() |
| 163 | + File.cd(cwd) |
| 164 | + res |
| 165 | + end |
| 166 | + |
| 167 | + def was_dir?(dir) do |
| 168 | + if File.dir?(dir) do |
| 169 | + :os.cmd(String.to_charlist("rm -Rf " <> dir)) |
| 170 | + true |
| 171 | + else |
| 172 | + false |
| 173 | + end |
| 174 | + end |
| 175 | +end |
0 commit comments