Skip to content

Commit

Permalink
Fixed unit tests that were broken before 1.13 (#749)
Browse files Browse the repository at this point in the history
The unit tests on builds prior to 1.13 were failing because I was
patching the wrong function. Prior to 1.13, formatter_for_file didn't
exist, but the unit tests would patch that but the
`function_exported?` check in `source_file.ex` would skip that branch
and succeed, which would cause the tests to fail.

The change is simple, check to see if `formatter_for_file` exists, and
if it doesn't, patch the `formatter_opts_for_file` function
  • Loading branch information
scohen committed Oct 15, 2022
1 parent 45149aa commit b55fece
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions apps/language_server/test/source_file/invalid_project_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,22 @@ defmodule ElixirLS.LanguageServer.SourceFile.InvalidProjectTest do
alias ElixirLS.LanguageServer.SourceFile
import ExUnit.CaptureLog

def appropriate_formatter_function_name(_) do
formatter_function =
if function_exported?(Mix.Tasks.Format, :formatter_for_file, 1) do
:formatter_for_file
else
:formatter_opts_for_file
end

{:ok, formatter_name: formatter_function}
end

describe "formatter_for " do
test "should handle syntax errors" do
patch(Mix.Tasks.Format, :formatter_for_file, fn _ ->
setup [:appropriate_formatter_function_name]

test "should handle syntax errors", ctx do
patch(Mix.Tasks.Format, ctx.formatter_name, fn _ ->
raise %SyntaxError{file: ".formatter.exs", line: 1}
end)

Expand All @@ -19,8 +32,8 @@ defmodule ElixirLS.LanguageServer.SourceFile.InvalidProjectTest do
assert String.contains?(output, "Unable to get formatter options")
end

test "should handle compile errors" do
patch(Mix.Tasks.Format, :formatter_for_file, fn _ ->
test "should handle compile errors", ctx do
patch(Mix.Tasks.Format, ctx.formatter_name, fn _ ->
raise %SyntaxError{file: ".formatter.exs", line: 1}
end)

Expand Down

0 comments on commit b55fece

Please sign in to comment.