From be85de4abf7d783a89b9233dbeeee4f928d3d468 Mon Sep 17 00:00:00 2001 From: Julius Putra Tanu Setiaji Date: Thu, 19 Apr 2018 13:15:31 +0800 Subject: [PATCH 01/12] Add dialyzer --- .travis.yml | 1 + mix.exs | 1 + mix.lock | 1 + 3 files changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 7cbe3d46f..b35f8f435 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,3 +16,4 @@ before_script: script: - mix coveralls.travis - mix credo + - mix dialyzer --halt-exit-status diff --git a/mix.exs b/mix.exs index f4852fe31..485b2bd8e 100644 --- a/mix.exs +++ b/mix.exs @@ -56,6 +56,7 @@ defmodule Cadet.Mixfile do {:phoenix_live_reload, "~> 1.0", only: :dev}, {:credo, "~> 0.8", only: [:dev, :test], runtime: false}, {:excoveralls, "~> 0.8", only: :test}, + {:dialyxir, "~> 0.5", only: [:dev, :test], runtime: false}, {:arc, "~> 0.8.0"}, {:arc_ecto, "~> 0.7.0"}, {:timex, "~> 3.0"}, diff --git a/mix.lock b/mix.lock index 45a06ec69..25b8136ae 100644 --- a/mix.lock +++ b/mix.lock @@ -12,6 +12,7 @@ "credo": {:hex, :credo, "0.9.1", "f021affa11b32a94dc2e807a6472ce0914289c9132f99644a97fc84432b202a1", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:poison, ">= 0.0.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"}, "db_connection": {:hex, :db_connection, "1.1.2", "2865c2a4bae0714e2213a0ce60a1b12d76a6efba0c51fbda59c9ab8d1accc7a8", [], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"}, "decimal": {:hex, :decimal, "1.4.1", "ad9e501edf7322f122f7fc151cce7c2a0c9ada96f2b0155b8a09a795c2029770", [], [], "hexpm"}, + "dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm"}, "dotenv": {:hex, :dotenv, "2.0.0", "87e77a94e54c20eca49989876395e26878b109f016f9fd9c42b65bb9c59bca3f", [], [], "hexpm"}, "ecto": {:hex, :ecto, "2.2.6", "3fd1067661d6d64851a0d4db9acd9e884c00d2d1aa41cc09da687226cf894661", [], [{:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"}, "ecto_enum": {:hex, :ecto_enum, "1.1.0", "d44fe2ce6e1c0e907e7c3b6456a69e0f1d662348d8b4e2a662ba312223d8ff62", [], [{:ecto, ">= 2.0.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:mariaex, ">= 0.0.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:postgrex, ">= 0.0.0", [hex: :postgrex, repo: "hexpm", optional: true]}], "hexpm"}, From fb60e0897eff6a91870f6b1b89a2bf9db7a3d8a6 Mon Sep 17 00:00:00 2001 From: Julius Putra Tanu Setiaji Date: Thu, 19 Apr 2018 13:50:43 +0800 Subject: [PATCH 02/12] Fix dialyzer offenses --- lib/cadet/accounts/authorization.ex | 2 +- lib/cadet/accounts/form/registration.ex | 2 +- lib/cadet/accounts/user.ex | 2 +- lib/cadet/course.ex | 3 --- lib/cadet/course/material.ex | 2 +- lib/cadet/factory.ex | 1 + lib/cadet_web/controllers/session_controller.ex | 1 - lib/mix/tasks/digest.ex | 1 + lib/mix/tasks/server.ex | 1 + mix.exs | 3 ++- 10 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/cadet/accounts/authorization.ex b/lib/cadet/accounts/authorization.ex index 6c8b32f06..fb3493ea1 100644 --- a/lib/cadet/accounts/authorization.ex +++ b/lib/cadet/accounts/authorization.ex @@ -23,7 +23,7 @@ defmodule Cadet.Accounts.Authorization do @required_fields ~w(provider uid token)a @optional_fields ~w(refresh_token expires_at)a - def changeset(authorization, params \\ :empty) do + def changeset(authorization, params \\ %{}) do authorization |> cast(params, @required_fields ++ @optional_fields) |> validate_required(@required_fields) diff --git a/lib/cadet/accounts/form/registration.ex b/lib/cadet/accounts/form/registration.ex index 86340d3b0..e697439ea 100644 --- a/lib/cadet/accounts/form/registration.ex +++ b/lib/cadet/accounts/form/registration.ex @@ -22,7 +22,7 @@ defmodule Cadet.Accounts.Form.Registration do @email_format ~r/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/ - def changeset(registration, params \\ :empty) do + def changeset(registration, params \\ %{}) do registration |> cast(params, @required_fields ++ @optional_fields) |> validate_required(@required_fields) diff --git a/lib/cadet/accounts/user.ex b/lib/cadet/accounts/user.ex index 6204f79b0..8a2791241 100644 --- a/lib/cadet/accounts/user.ex +++ b/lib/cadet/accounts/user.ex @@ -20,7 +20,7 @@ defmodule Cadet.Accounts.User do @required_fields ~w(first_name role)a @optional_fields ~w(last_name)a - def changeset(user, params \\ :empty) do + def changeset(user, params \\ %{}) do user |> cast(params, @required_fields ++ @optional_fields) |> validate_required(@required_fields) diff --git a/lib/cadet/course.ex b/lib/cadet/course.ex index be602ad43..5dba6d23e 100644 --- a/lib/cadet/course.ex +++ b/lib/cadet/course.ex @@ -190,9 +190,6 @@ defmodule Cadet.Course do def delete_material(material = %Material{}) do cond do - material == nil -> - {:error, :not_found} - material.file != nil -> Upload.delete({material.file, material}) Repo.delete(material) diff --git a/lib/cadet/course/material.ex b/lib/cadet/course/material.ex index 4b248742e..efe907522 100644 --- a/lib/cadet/course/material.ex +++ b/lib/cadet/course/material.ex @@ -32,8 +32,8 @@ defmodule Cadet.Course.Material do def changeset(material, attrs \\ %{}) do material - |> cast(attrs, @required_fields ++ @optional_fields) |> cast_attachments(attrs, @optional_file_fields) + |> cast(attrs, @required_fields ++ @optional_fields) |> validate_changeset end diff --git a/lib/cadet/factory.ex b/lib/cadet/factory.ex index c916454d7..d5458a3bd 100644 --- a/lib/cadet/factory.ex +++ b/lib/cadet/factory.ex @@ -3,6 +3,7 @@ defmodule Cadet.Factory do Factory for testing """ use ExMachina.Ecto, repo: Cadet.Repo + @dialyzer {:no_return, fields_for: 1} # fields_for has been deprecated, only raising exception alias Cadet.Accounts.User alias Cadet.Accounts.Authorization diff --git a/lib/cadet_web/controllers/session_controller.ex b/lib/cadet_web/controllers/session_controller.ex index d0ec553e7..cf6bb6369 100644 --- a/lib/cadet_web/controllers/session_controller.ex +++ b/lib/cadet_web/controllers/session_controller.ex @@ -45,7 +45,6 @@ defmodule CadetWeb.SessionController do case reason do :not_found -> "E-mail not registered in the system" :invalid_password -> "Invalid e-mail or password" - _ -> "Unknown" end end end diff --git a/lib/mix/tasks/digest.ex b/lib/mix/tasks/digest.ex index 424a98c92..757a2d006 100644 --- a/lib/mix/tasks/digest.ex +++ b/lib/mix/tasks/digest.ex @@ -4,6 +4,7 @@ defmodule Mix.Tasks.Cadet.Digest do """ use Mix.Task + @spec run([any]) :: no_return def run(args) do Dotenv.load!() Mix.Shell.IO.cmd("cd frontend && npm run build") diff --git a/lib/mix/tasks/server.ex b/lib/mix/tasks/server.ex index f75c98489..49bc116dc 100644 --- a/lib/mix/tasks/server.ex +++ b/lib/mix/tasks/server.ex @@ -5,6 +5,7 @@ defmodule Mix.Tasks.Cadet.Server do """ use Mix.Task + @spec run([any]) :: no_return def run(args) do Dotenv.load!() :ok = Mix.Tasks.Phx.Server.run(args) diff --git a/mix.exs b/mix.exs index 485b2bd8e..1a98c2379 100644 --- a/mix.exs +++ b/mix.exs @@ -17,7 +17,8 @@ defmodule Cadet.Mixfile do "coveralls.html": :test ], aliases: aliases(), - deps: deps() + deps: deps(), + dialyzer: [plt_add_apps: [:mix]] ] end From cee377d283e6bac71fbd8ddec254ec1969d6ff28 Mon Sep 17 00:00:00 2001 From: Julius Putra Tanu Setiaji Date: Thu, 19 Apr 2018 13:55:16 +0800 Subject: [PATCH 03/12] Fix credo offense --- lib/cadet/course.ex | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/cadet/course.ex b/lib/cadet/course.ex index 5dba6d23e..3cbfbfb8a 100644 --- a/lib/cadet/course.ex +++ b/lib/cadet/course.ex @@ -189,14 +189,10 @@ defmodule Cadet.Course do end def delete_material(material = %Material{}) do - cond do - material.file != nil -> - Upload.delete({material.file, material}) - Repo.delete(material) - - true -> - Repo.delete(material) + if material.file do + Upload.delete({material.file, material}) end + Repo.delete(material) end @doc """ From b9ad812494284d8cc5a6ffe509fb2b5d5cc5ae73 Mon Sep 17 00:00:00 2001 From: Julius Putra Tanu Setiaji Date: Thu, 19 Apr 2018 14:09:11 +0800 Subject: [PATCH 04/12] Add try/rescue in mix tasks --- lib/mix/tasks/digest.ex | 7 +++++-- lib/mix/tasks/server.ex | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/mix/tasks/digest.ex b/lib/mix/tasks/digest.ex index 757a2d006..19024189c 100644 --- a/lib/mix/tasks/digest.ex +++ b/lib/mix/tasks/digest.ex @@ -4,9 +4,12 @@ defmodule Mix.Tasks.Cadet.Digest do """ use Mix.Task - @spec run([any]) :: no_return def run(args) do - Dotenv.load!() + try do + Dotenv.load!() + rescue + e in RuntimeError -> e + end Mix.Shell.IO.cmd("cd frontend && npm run build") :ok = Mix.Tasks.Phx.Digest.run(args) end diff --git a/lib/mix/tasks/server.ex b/lib/mix/tasks/server.ex index 49bc116dc..bf28ee563 100644 --- a/lib/mix/tasks/server.ex +++ b/lib/mix/tasks/server.ex @@ -5,9 +5,12 @@ defmodule Mix.Tasks.Cadet.Server do """ use Mix.Task - @spec run([any]) :: no_return def run(args) do - Dotenv.load!() + try do + Dotenv.load!() + rescue + e in RuntimeError -> e + end :ok = Mix.Tasks.Phx.Server.run(args) end end From 1d7486223915d74963dcf3c85b219c7fb814fc0b Mon Sep 17 00:00:00 2001 From: Julius Putra Tanu Setiaji Date: Thu, 19 Apr 2018 14:12:26 +0800 Subject: [PATCH 05/12] Add ex_unit to dialyzer --- mix.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mix.exs b/mix.exs index 1a98c2379..d4df6ce2a 100644 --- a/mix.exs +++ b/mix.exs @@ -18,7 +18,7 @@ defmodule Cadet.Mixfile do ], aliases: aliases(), deps: deps(), - dialyzer: [plt_add_apps: [:mix]] + dialyzer: [plt_add_apps: [:mix, :ex_unit]] ] end From f8c4b2b5821d31c11a59639eb667861a8966baf4 Mon Sep 17 00:00:00 2001 From: Julius Putra Tanu Setiaji Date: Thu, 19 Apr 2018 14:32:28 +0800 Subject: [PATCH 06/12] Add path to dialyzer --- mix.exs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mix.exs b/mix.exs index d4df6ce2a..d2ec406d9 100644 --- a/mix.exs +++ b/mix.exs @@ -18,7 +18,8 @@ defmodule Cadet.Mixfile do ], aliases: aliases(), deps: deps(), - dialyzer: [plt_add_apps: [:mix, :ex_unit]] + dialyzer: [plt_add_apps: [:mix, :ex_unit], + path: ["_build/dev/lib/cadet/ebin", "_build/test/lib/cadet/ebin"]] ] end From ef3bffcc0c30c01953657699d59ab35853a88a36 Mon Sep 17 00:00:00 2001 From: Julius Putra Tanu Setiaji Date: Thu, 19 Apr 2018 14:40:45 +0800 Subject: [PATCH 07/12] Use travis matrix instead --- .travis.yml | 8 ++++---- mix.exs | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index b35f8f435..ea52db338 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,9 @@ language: elixir elixir: 1.5.0 otp_release: '20.1' env: - - MIX_ENV=test + - MIX_ENV=test COMMAND="mix coveralls.travis" + - MIX_ENV=test COMMAND="mix credo" + - MIX_ENV=dev COMMAND="mix dialyzer --halt-exit-status" cache: directories: - _build @@ -14,6 +16,4 @@ services: before_script: - mix ecto.setup script: - - mix coveralls.travis - - mix credo - - mix dialyzer --halt-exit-status + - $COMMAND diff --git a/mix.exs b/mix.exs index d2ec406d9..d4df6ce2a 100644 --- a/mix.exs +++ b/mix.exs @@ -18,8 +18,7 @@ defmodule Cadet.Mixfile do ], aliases: aliases(), deps: deps(), - dialyzer: [plt_add_apps: [:mix, :ex_unit], - path: ["_build/dev/lib/cadet/ebin", "_build/test/lib/cadet/ebin"]] + dialyzer: [plt_add_apps: [:mix, :ex_unit]] ] end From 1cbb2d2ac92a3a33e32685032ecefaf50c04c705 Mon Sep 17 00:00:00 2001 From: Julius Putra Tanu Setiaji Date: Thu, 19 Apr 2018 14:53:15 +0800 Subject: [PATCH 08/12] Test override travis env --- .travis.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index ea52db338..c1d401f4c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,9 +2,7 @@ language: elixir elixir: 1.5.0 otp_release: '20.1' env: - - MIX_ENV=test COMMAND="mix coveralls.travis" - - MIX_ENV=test COMMAND="mix credo" - - MIX_ENV=dev COMMAND="mix dialyzer --halt-exit-status" + - MIX_ENV=test cache: directories: - _build @@ -16,4 +14,6 @@ services: before_script: - mix ecto.setup script: - - $COMMAND + - mix coveralls.travis + - mix credo + - MIX_ENV=dev mix dialyzer --halt-exit-status From 664628b9dee04f01b07d3d9493007819e6636457 Mon Sep 17 00:00:00 2001 From: Julius Putra Tanu Setiaji Date: Thu, 19 Apr 2018 15:12:31 +0800 Subject: [PATCH 09/12] Fix dialyzer conn_case --- test/support/conn_case.ex | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index 62b70fca7..3bf14ddc3 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -13,6 +13,8 @@ defmodule CadetWeb.ConnCase do of the test unless the test case is marked as async. """ + @dialyzer {:no_return, __ex_unit_setup_0: 1} + use ExUnit.CaseTemplate using do @@ -36,14 +38,11 @@ defmodule CadetWeb.ConnCase do conn = Phoenix.ConnTest.build_conn() if tags[:authenticate] != nil do - {:ok, conn: authenticate(conn, tags[:authenticate])} + user = Cadet.Factory.insert(:user, %{role: tags[:authenticate]}) + conn = Cadet.Auth.Guardian.Plug.sign_in(conn, user) + {:ok, conn: conn} else {:ok, conn: conn} end end - - defp authenticate(conn, role) do - user = Cadet.Factory.insert(:user, %{role: role}) - Cadet.Auth.Guardian.Plug.sign_in(conn, user) - end end From f986dd5d361ae0e538eb405c0b81396e6a239d12 Mon Sep 17 00:00:00 2001 From: Julius Putra Tanu Setiaji Date: Thu, 19 Apr 2018 15:17:02 +0800 Subject: [PATCH 10/12] Remove travis dev env --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c1d401f4c..b35f8f435 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,4 +16,4 @@ before_script: script: - mix coveralls.travis - mix credo - - MIX_ENV=dev mix dialyzer --halt-exit-status + - mix dialyzer --halt-exit-status From e643e0dfe809f278c29a2446e7c946a1e0281311 Mon Sep 17 00:00:00 2001 From: Julius Putra Tanu Setiaji Date: Thu, 19 Apr 2018 15:49:31 +0800 Subject: [PATCH 11/12] Remove dialyzer no_return --- test/support/conn_case.ex | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index 3bf14ddc3..aaa043369 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -13,8 +13,6 @@ defmodule CadetWeb.ConnCase do of the test unless the test case is marked as async. """ - @dialyzer {:no_return, __ex_unit_setup_0: 1} - use ExUnit.CaseTemplate using do From 9c66404c6ee0eab8be206d3689a3411c75385b7d Mon Sep 17 00:00:00 2001 From: Julius Putra Tanu Setiaji Date: Thu, 19 Apr 2018 20:17:20 +0800 Subject: [PATCH 12/12] Remove mix dialyzer from travis script --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index b35f8f435..7cbe3d46f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,4 +16,3 @@ before_script: script: - mix coveralls.travis - mix credo - - mix dialyzer --halt-exit-status