|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: 2021 The Elixir Team |
| 3 | + |
| 4 | +Code.require_file("../../test_helper.exs", __DIR__) |
| 5 | + |
| 6 | +defmodule Mix.Tasks.SourceTest do |
| 7 | + use MixTest.Case |
| 8 | + |
| 9 | + import ExUnit.CaptureIO |
| 10 | + |
| 11 | + @editor System.get_env("ELIXIR_EDITOR") |
| 12 | + |
| 13 | + test "source MODULE", context do |
| 14 | + in_tmp(context.test, fn -> |
| 15 | + Mix.Tasks.Source.run(["Enum"]) |
| 16 | + assert_received {:mix_shell, :info, [location]} |
| 17 | + assert location =~ ~r"lib/elixir/lib/enum\.ex:\d+" |
| 18 | + end) |
| 19 | + end |
| 20 | + |
| 21 | + test "source MODULE.FUN", context do |
| 22 | + in_tmp(context.test, fn -> |
| 23 | + Mix.Tasks.Source.run(["Enum.map"]) |
| 24 | + assert_received {:mix_shell, :info, [location]} |
| 25 | + assert location =~ ~r"lib/elixir/lib/enum\.ex:\d+" |
| 26 | + end) |
| 27 | + end |
| 28 | + |
| 29 | + test "source MODULE.FUN/ARITY", context do |
| 30 | + in_tmp(context.test, fn -> |
| 31 | + Mix.Tasks.Source.run(["Enum.map/2"]) |
| 32 | + assert_received {:mix_shell, :info, [location]} |
| 33 | + assert location =~ ~r"lib/elixir/lib/enum\.ex:\d+" |
| 34 | + end) |
| 35 | + end |
| 36 | + |
| 37 | + test "source NESTED MODULE", context do |
| 38 | + in_tmp(context.test, fn -> |
| 39 | + Mix.Tasks.Source.run(["IO.ANSI"]) |
| 40 | + assert_received {:mix_shell, :info, [location]} |
| 41 | + assert location =~ ~r"lib/elixir/lib/io/ansi\.ex:\d+" |
| 42 | + end) |
| 43 | + end |
| 44 | + |
| 45 | + test "source Erlang MODULE", context do |
| 46 | + in_tmp(context.test, fn -> |
| 47 | + Mix.Tasks.Source.run([":math"]) |
| 48 | + assert_received {:mix_shell, :info, [location]} |
| 49 | + assert location =~ ~r"math\.erl:\d+" |
| 50 | + end) |
| 51 | + end |
| 52 | + |
| 53 | + test "source ERROR" do |
| 54 | + assert_raise Mix.Error, "Invalid expression: Foo.bar(~s[baz])", fn -> |
| 55 | + Mix.Tasks.Source.run(["Foo.bar(~s[baz])"]) |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + test "source unavailable module" do |
| 60 | + assert_raise Mix.Error, ~r/Could not find source/, fn -> |
| 61 | + Mix.Tasks.Source.run(["DoesNotExist"]) |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + test "source --open opens __FILE__ and __LINE__", context do |
| 66 | + System.put_env("ELIXIR_EDITOR", "echo __LINE__:__FILE__") |
| 67 | + |
| 68 | + in_tmp(context.test, fn -> |
| 69 | + output = |
| 70 | + capture_io(fn -> |
| 71 | + Mix.Tasks.Source.run(["--open", "Enum"]) |
| 72 | + end) |
| 73 | + |
| 74 | + assert output =~ ~r"\d+:.*lib/elixir/lib/enum\.ex" |
| 75 | + end) |
| 76 | + after |
| 77 | + if @editor, |
| 78 | + do: System.put_env("ELIXIR_EDITOR", @editor), |
| 79 | + else: System.delete_env("ELIXIR_EDITOR") |
| 80 | + end |
| 81 | + |
| 82 | + test "source -o opens with shortcut", context do |
| 83 | + System.put_env("ELIXIR_EDITOR", "echo __LINE__:__FILE__") |
| 84 | + |
| 85 | + in_tmp(context.test, fn -> |
| 86 | + output = |
| 87 | + capture_io(fn -> |
| 88 | + Mix.Tasks.Source.run(["-o", "Enum.map/2"]) |
| 89 | + end) |
| 90 | + |
| 91 | + assert output =~ ~r"\d+:.*lib/elixir/lib/enum\.ex" |
| 92 | + end) |
| 93 | + after |
| 94 | + if @editor, |
| 95 | + do: System.put_env("ELIXIR_EDITOR", @editor), |
| 96 | + else: System.delete_env("ELIXIR_EDITOR") |
| 97 | + end |
| 98 | + |
| 99 | + test "source --open without editor" do |
| 100 | + System.delete_env("ELIXIR_EDITOR") |
| 101 | + System.delete_env("EDITOR") |
| 102 | + |
| 103 | + assert_raise Mix.Error, ~r/ELIXIR_EDITOR/, fn -> |
| 104 | + Mix.Tasks.Source.run(["--open", "Enum"]) |
| 105 | + end |
| 106 | + after |
| 107 | + if @editor, |
| 108 | + do: System.put_env("ELIXIR_EDITOR", @editor), |
| 109 | + else: System.delete_env("ELIXIR_EDITOR") |
| 110 | + end |
| 111 | + |
| 112 | + test "bad arguments" do |
| 113 | + message = ~r/Unexpected arguments/ |
| 114 | + |
| 115 | + assert_raise Mix.Error, message, fn -> |
| 116 | + Mix.Tasks.Source.run(["foo", "bar"]) |
| 117 | + end |
| 118 | + |
| 119 | + assert_raise Mix.Error, message, fn -> |
| 120 | + Mix.Tasks.Source.run([]) |
| 121 | + end |
| 122 | + end |
| 123 | +end |
0 commit comments