From 528c6a55a3f45ad5b5a7a9c7e98cd1c906d62fc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Nystr=C3=B6m?= Date: Fri, 19 Jan 2018 00:03:43 +0100 Subject: [PATCH] Code formatting using Elixir 1.6.0 --- .formatter.exs | 3 +++ lib/librex.ex | 62 ++++++++++++++++++++++++++------------------ mix.exs | 40 ++++++++++++++++------------ test/librex_test.exs | 42 ++++++++++++++++++------------ 4 files changed, 88 insertions(+), 59 deletions(-) create mode 100644 .formatter.exs diff --git a/.formatter.exs b/.formatter.exs new file mode 100644 index 0000000..e248405 --- /dev/null +++ b/.formatter.exs @@ -0,0 +1,3 @@ +[ + inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"] +] \ No newline at end of file diff --git a/lib/librex.ex b/lib/librex.ex index 974ae1d..9c16e6a 100644 --- a/lib/librex.ex +++ b/lib/librex.ex @@ -2,32 +2,31 @@ defmodule Librex do @moduledoc """ Provides functions to convert office documents, spreadsheets & presentations to other formats. - LibreOffice must be installed. It's recommended that you add the soffice binary your PATH. Otherwise you have to specify the - absolute path to the soffice binary as the last parameter. + LibreOffice must be installed. It's recommended that you add the soffice binary your PATH. Otherwise you have to specify the absolute path to the soffice binary as the last parameter. - ## Examples + ## Examples - iex(1)> Librex.convert("test/fixtures/docx.docx", "/Users/ricn/docx.pdf") + iex(1)> Librex.convert("test/fixtures/docx.docx", "/Users/ricn/docx.pdf") - `{:ok, "/Users/ricn/docx.pdf"}` + `{:ok, "/Users/ricn/docx.pdf"}` - iex(2)> Librex.convert("non_existent_file", "/Users/ricn/docx.pdf") + iex(2)> Librex.convert("non_existent_file", "/Users/ricn/docx.pdf") - `{:error, :enoent}` + `{:error, :enoent}` - iex(3)> Librex.convert!("test/fixtures/docx.docx", "/Users/ricn/docx.pdf") + iex(3)> Librex.convert!("test/fixtures/docx.docx", "/Users/ricn/docx.pdf") - "/Users/ricn/docx.pdf" + "/Users/ricn/docx.pdf" - iex(4)> Librex.convert!("non_existent_file", "/Users/ricn/docx.pdf") + iex(4)> Librex.convert!("non_existent_file", "/Users/ricn/docx.pdf") - ** (File.Error) could not read non_existent_file: no such file or directory (librex) lib/librex.ex:13: Librex.convert!/3 + ** (File.Error) could not read non_existent_file: no such file or directory (librex) lib/librex.ex:13: Librex.convert!/3 - iex(5)> Librex.convert("test/fixtures/docx.docx", "/Users/ricn/docx.pdf", "/path_to/soffice") + iex(5)> Librex.convert("test/fixtures/docx.docx", "/Users/ricn/docx.pdf", "/path_to/soffice") - `{:ok, "/Users/ricn/docx.pdf"}` + `{:ok, "/Users/ricn/docx.pdf"}` - """ + """ @doc """ Converts in_file to out_file @@ -35,8 +34,8 @@ defmodule Librex do """ def convert(in_file, out_file, soffice_cmd \\ "soffice") do case validate(in_file, out_file, soffice_cmd) do - {:ok, _} -> do_convert(in_file, out_file, soffice_cmd) - {:error, reason } -> {:error, reason} + {:ok, _} -> do_convert(in_file, out_file, soffice_cmd) + {:error, reason} -> {:error, reason} end end @@ -45,7 +44,7 @@ defmodule Librex do """ def convert!(in_file, out_file, soffice_cmd \\ "soffice") do case convert(in_file, out_file, soffice_cmd) do - {:ok, _} -> out_file + {:ok, _} -> out_file {:error, reason} -> raise_error(reason, in_file) end end @@ -91,7 +90,8 @@ defmodule Librex do defp validate_output(result, output_file) do output_ext = String.replace(Path.extname(output_file), ".", "") - if Enum.any?(supported_formats(), fn(ext) -> ext == output_ext end) do + + if Enum.any?(supported_formats(), fn ext -> ext == output_ext end) do result else {:error, "#{output_ext} is not a supported output format"} @@ -99,7 +99,8 @@ defmodule Librex do end defp supported_formats do - supported_document_formats() ++ supported_presentation_formats() ++ supported_spreadsheet_formats() + supported_document_formats() ++ + supported_presentation_formats() ++ supported_spreadsheet_formats() end defp validate_soffice(result, soffice_cmd) do @@ -113,21 +114,32 @@ defmodule Librex do defp do_convert(in_file, out_file, soffice_cmd) do basename = Path.basename(in_file, Path.extname(in_file)) convert_to = String.replace(Path.extname(out_file), ".", "") - out_temp_dir = System.tmp_dir! <> "/" <> SecureRandom.uuid <> "/" + out_temp_dir = System.tmp_dir!() <> "/" <> SecureRandom.uuid() <> "/" out_temp_file = out_temp_dir <> basename <> Path.extname(out_file) run(Path.expand(in_file), out_temp_dir, convert_to, soffice_cmd) - File.cp! out_temp_file, Path.expand(out_file) - File.rm! out_temp_file - File.rmdir! out_temp_dir + File.cp!(out_temp_file, Path.expand(out_file)) + File.rm!(out_temp_file) + File.rmdir!(out_temp_dir) {:ok, out_file} end defp run(file_to_convert, out_dir, convert_to, soffice_cmd) do - user_installation = "-env:UserInstallation=file://" <> System.tmp_dir! <> "/" <> "librex_oouser" - opts = [user_installation, "--headless", "--convert-to", convert_to, "--outdir", out_dir, file_to_convert] + user_installation = + "-env:UserInstallation=file://" <> System.tmp_dir!() <> "/" <> "librex_oouser" + + opts = [ + user_installation, + "--headless", + "--convert-to", + convert_to, + "--outdir", + out_dir, + file_to_convert + ] + cmd = System.find_executable(soffice_cmd) System.cmd(cmd, opts, stderr_to_stdout: true) end diff --git a/mix.exs b/mix.exs index 907492d..b140f7a 100644 --- a/mix.exs +++ b/mix.exs @@ -2,13 +2,15 @@ defmodule Librex.Mixfile do use Mix.Project def project do - [app: :librex, - version: "1.0.2", - elixir: "~> 1.0", - description: description(), - package: package(), - deps: deps(), - test_coverage: [tool: ExCoveralls]] + [ + app: :librex, + version: "1.0.2", + elixir: "~> 1.0", + description: description(), + package: package(), + deps: deps(), + test_coverage: [tool: ExCoveralls] + ] end def application do @@ -16,11 +18,13 @@ defmodule Librex.Mixfile do end defp deps do - [{:secure_random, "~> 0.5"}, - {:inch_ex, "~> 0.5", only: :docs}, - {:earmark, "~> 1.2", only: :dev}, - {:ex_doc, "~> 0.16", only: :dev}, - {:excoveralls, "~> 0.6", only: [:dev, :test]}] + [ + {:secure_random, "~> 0.5"}, + {:inch_ex, "~> 0.5", only: :docs}, + {:earmark, "~> 1.2", only: :dev}, + {:ex_doc, "~> 0.16", only: :dev}, + {:excoveralls, "~> 0.6", only: [:dev, :test]} + ] end defp description do @@ -28,10 +32,12 @@ defmodule Librex.Mixfile do end defp package do - [files: ["lib", "mix.exs", "README*", "LICENSE*"], - contributors: ["Richard Nyström", "Sergey Chechaev"], - maintainers: ["Richard Nyström"], - licenses: ["MIT"], - links: %{"GitHub" => "https://github.com/ricn/librex", "Docs" => "http://hexdocs.pm/librex"}] + [ + files: ["lib", "mix.exs", "README*", "LICENSE*"], + contributors: ["Richard Nyström", "Sergey Chechaev"], + maintainers: ["Richard Nyström"], + licenses: ["MIT"], + links: %{"GitHub" => "https://github.com/ricn/librex", "Docs" => "http://hexdocs.pm/librex"} + ] end end diff --git a/test/librex_test.exs b/test/librex_test.exs index 16d6d09..36424e0 100644 --- a/test/librex_test.exs +++ b/test/librex_test.exs @@ -13,37 +13,37 @@ defmodule LibrexTest do @non_existent_file Path.join(__DIR__, "fixtures/non.existent") test ".convert docx to other supported formats" do - Enum.each(["pdf", "odt"], fn(format) -> test_conversion(@docx_file, format) end) + Enum.each(["pdf", "odt"], fn format -> test_conversion(@docx_file, format) end) end test ".convert odt to other supported formats" do - Enum.each(["pdf", "docx"], fn(format) -> test_conversion(@odt_file, format) end) + Enum.each(["pdf", "docx"], fn format -> test_conversion(@odt_file, format) end) end test ".convert pptx to other supported formats" do - Enum.each(["pdf", "odp"], fn(format) -> test_conversion(@pptx_file, format) end) + Enum.each(["pdf", "odp"], fn format -> test_conversion(@pptx_file, format) end) end test ".convert odp to other supported formats" do - Enum.each(["pdf", "pptx"], fn(format) -> test_conversion(@odp_file, format) end) + Enum.each(["pdf", "pptx"], fn format -> test_conversion(@odp_file, format) end) end test ".convert xlsx to other supported formats" do - Enum.each(["pdf", "ods"], fn(format) -> test_conversion(@xlsx_file, format) end) + Enum.each(["pdf", "ods"], fn format -> test_conversion(@xlsx_file, format) end) end test ".convert ods to other supported formats" do - Enum.each(["pdf", "xlsx"], fn(format) -> test_conversion(@ods_file, format) end) + Enum.each(["pdf", "xlsx"], fn format -> test_conversion(@ods_file, format) end) end defp test_conversion(input_file, output_format) do output_file = random_path() <> "." <> output_format - refute File.exists? output_file - assert { :ok, output_file } == Librex.convert(input_file, output_file) + refute File.exists?(output_file) + assert {:ok, output_file} == Librex.convert(input_file, output_file) end test ".convert must return error when file to convert does not exist" do - assert { :error, :enoent } == Librex.convert(@non_existent_file, "/tmp/output.pdf") + assert {:error, :enoent} == Librex.convert(@non_existent_file, "/tmp/output.pdf") end test ".convert! must return output file path" do @@ -53,20 +53,24 @@ defmodule LibrexTest do test ".convert! must raise error when file to convert does not exist" do msg = "could not read \"#{@non_existent_file}\": no such file or directory" + assert_raise File.Error, msg, fn -> Librex.convert!(@non_existent_file, "/tmp/output.pdf") end end test "convert must return error when LibreOffice executable can't be found" do - cmd = "sofice" # misspelled + # misspelled + cmd = "sofice" msg = "LibreOffice (#{cmd}) executable could not be found." assert {:error, msg} == Librex.convert(@docx_file, "/tmp/output.pdf", "sofice") end test "convert! must raise error when LibreOffice executable can't be found" do - cmd = "sofice" # misspelled + # misspelled + cmd = "sofice" msg = "LibreOffice (#{cmd}) executable could not be found." + assert_raise RuntimeError, msg, fn -> Librex.convert!(@docx_file, "/tmp/output.pdf", "sofice") end @@ -74,34 +78,38 @@ defmodule LibrexTest do test ".convert must have the possibility to specify LibreOffice command" do pdf_file = random_path() <> ".pdf" - assert { :ok, pdf_file } == Librex.convert(@docx_file, pdf_file, System.find_executable("soffice")) + + assert {:ok, pdf_file} == + Librex.convert(@docx_file, pdf_file, System.find_executable("soffice")) end test ".convert must return error when file to convert is directory" do pdf_file = random_path() <> ".pdf" - assert Librex.convert(System.tmp_dir!, pdf_file) == {:error, :eisdir} + assert Librex.convert(System.tmp_dir!(), pdf_file) == {:error, :eisdir} end test ".convert! must raise error when file to convert is directory" do - msg = "could not read \"#{System.tmp_dir!}\": illegal operation on a directory" + msg = "could not read \"#{System.tmp_dir!()}\": illegal operation on a directory" + assert_raise File.Error, msg, fn -> - Librex.convert!(System.tmp_dir!, "/tmp/output.pdf") + Librex.convert!(System.tmp_dir!(), "/tmp/output.pdf") end end test ".convert must return error when output file has wrong extension" do - { :error, reason } = Librex.convert(@docx_file, "/tmp/output.mp3") + {:error, reason} = Librex.convert(@docx_file, "/tmp/output.mp3") assert reason == "mp3 is not a supported output format" end test "convert! must raise error when output file has wrong extension" do msg = "mp3 is not a supported output format" + assert_raise RuntimeError, msg, fn -> Librex.convert!(@docx_file, "/tmp/output.mp3") end end defp random_path do - System.tmp_dir! <> "/" <> SecureRandom.uuid + System.tmp_dir!() <> "/" <> SecureRandom.uuid() end end