From 21cbaeb9d4073497382d06920973e8bd6e471b65 Mon Sep 17 00:00:00 2001 From: Steve Cohen Date: Fri, 14 Oct 2022 14:53:40 -0700 Subject: [PATCH] Fixed unit tests that were broken before 1.13 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 --- .../test/source_file/invalid_project_test.exs | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/apps/language_server/test/source_file/invalid_project_test.exs b/apps/language_server/test/source_file/invalid_project_test.exs index dffec009c..7ae7e46d7 100644 --- a/apps/language_server/test/source_file/invalid_project_test.exs +++ b/apps/language_server/test/source_file/invalid_project_test.exs @@ -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) @@ -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)