From bc08592466e3fb439919caad756ffeaa9fd610c0 Mon Sep 17 00:00:00 2001 From: Jon Parise Date: Sun, 2 Oct 2016 09:02:26 -0700 Subject: [PATCH] Full test coverage for Mix.Tasks.Compile.Thrift Add test coverage for the last two remaining error cases. This brings this module's unit test coverage up to 100%. This also includes one functional change: we now redirect :stderr output to :stdout so we can more consistently capture and display output from the Thrift compiler executable. --- lib/mix/tasks/compile.thrift.ex | 4 ++-- test/mix/tasks/compile.thrift_test.exs | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/mix/tasks/compile.thrift.ex b/lib/mix/tasks/compile.thrift.ex index bba8dd60..47411f99 100644 --- a/lib/mix/tasks/compile.thrift.ex +++ b/lib/mix/tasks/compile.thrift.ex @@ -62,7 +62,7 @@ defmodule Mix.Tasks.Compile.Thrift do end defp get_thrift_version(exec) do - case System.cmd(exec, ~w[-version]) do + case System.cmd(exec, ~w[-version], stderr_to_stdout: true) do {s, 0} -> hd(Regex.run(~r/\b(\d+\.\d+\.\d+)\b/, s, capture: :first) || []) {_, e} -> Mix.raise "Failed to execute `#{exec} -version` (error #{e})" end @@ -89,7 +89,7 @@ defmodule Mix.Tasks.Compile.Thrift do defp generate(exec, options, thrift_file) do args = options ++ [thrift_file] - case System.cmd(exec, args) do + case System.cmd(exec, args, stderr_to_stdout: true) do {_, 0} -> Mix.shell.info "Compiled #{thrift_file}" {_, e} -> Mix.shell.error "Failed to compile #{thrift_file} (error #{e})" end diff --git a/test/mix/tasks/compile.thrift_test.exs b/test/mix/tasks/compile.thrift_test.exs index b8eb2cbb..72e4e620 100644 --- a/test/mix/tasks/compile.thrift_test.exs +++ b/test/mix/tasks/compile.thrift_test.exs @@ -52,7 +52,7 @@ defmodule Mix.Tasks.Compile.ThriftTest do end end - test "specifying an empty :thrift_files list " do + test "specifying an empty :thrift_files list" do in_fixture fn -> with_project_config [thrift_files: []], fn -> assert capture_io(fn -> run([]) end) == "" @@ -60,6 +60,16 @@ defmodule Mix.Tasks.Compile.ThriftTest do end end + test "specifying a non-existent Thrift file" do + in_fixture fn -> + with_project_config [thrift_files: ~w("missing.thrift")], fn -> + capture_io fn -> + assert capture_io(:stderr, fn -> run([]) end) =~ "Failed to compile" + end + end + end + end + test "attempting to use a non-existent Thrift executable" do in_fixture fn -> with_project_config [thrift_executable: "nonexistentthrift"], fn -> @@ -70,6 +80,16 @@ defmodule Mix.Tasks.Compile.ThriftTest do end end + test "attempting to use a broken Thrift executable" do + in_fixture fn -> + with_project_config [thrift_executable: "mix", thrift_version: "0.0.0"], fn -> + assert_raise Mix.Error, ~r/Failed to execute/, fn -> + run([]) + end + end + end + end + test "attempting to use an unsupported Thrift version" do in_fixture fn -> with_project_config [thrift_version: "< 0.0.0"], fn ->