From e243645abb9ab59164ebc004065875e71d5924aa Mon Sep 17 00:00:00 2001 From: Adrian Kumpf <8999358+adriankumpf@users.noreply.github.com> Date: Sat, 10 Jun 2023 18:37:47 +0200 Subject: [PATCH] Add Dialyzer --- .dialyzer_ignore.exs | 1 + .gitignore | 5 ++- lib/teslamate/convert.ex | 7 ++-- lib/teslamate/log.ex | 5 +-- lib/teslamate/repair.ex | 2 +- lib/teslamate/updater.ex | 4 +-- lib/teslamate/vehicles/vehicle.ex | 2 +- mix.exs | 15 ++++++++- mix.lock | 2 ++ .../vehicles/vehicle/charging_sync_test.exs | 5 ++- .../vehicles/vehicle/charging_test.exs | 2 +- .../vehicles/vehicle/streaming_test.exs | 32 +++++++++---------- .../live/car_index_live_test.exs | 2 +- .../teslamate_web/live/geofence_live_test.exs | 2 +- 14 files changed, 51 insertions(+), 35 deletions(-) create mode 100644 .dialyzer_ignore.exs diff --git a/.dialyzer_ignore.exs b/.dialyzer_ignore.exs new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/.dialyzer_ignore.exs @@ -0,0 +1 @@ +[] diff --git a/.gitignore b/.gitignore index 8487e829..5583cbfd 100644 --- a/.gitignore +++ b/.gitignore @@ -57,4 +57,7 @@ config/*.env /.project # Mac files -.DS_Store \ No newline at end of file +.DS_Store + +/priv/plts/*.plt +/priv/plts/*.plt.hash diff --git a/lib/teslamate/convert.ex b/lib/teslamate/convert.ex index 06210b30..1b6350e6 100644 --- a/lib/teslamate/convert.ex +++ b/lib/teslamate/convert.ex @@ -2,11 +2,12 @@ defmodule TeslaMate.Convert do @km_factor 0.62137119223733 @km_factor_d Decimal.from_float(@km_factor) @ft_factor 3.28084 + @ft_factor_d Decimal.from_float(@ft_factor) alias Decimal, as: D def mph_to_kmh(nil), do: nil - def mph_to_kmh(mph = %D{}), do: mph |> D.div(@km_factor) |> D.round() + def mph_to_kmh(mph = %D{}), do: mph |> D.div(@km_factor_d) |> D.round() def mph_to_kmh(mph), do: round(mph / @km_factor) def miles_to_km(nil, _precision), do: nil @@ -20,11 +21,11 @@ defmodule TeslaMate.Convert do def km_to_miles(km, precision), do: Float.round(km * @km_factor, precision) def m_to_ft(nil), do: nil - def m_to_ft(m = %D{}), do: D.mult(m, @ft_factor) + def m_to_ft(m = %D{}), do: D.mult(m, @ft_factor_d) def m_to_ft(m), do: m * @ft_factor def ft_to_m(nil), do: nil - def ft_to_m(ft = %D{}), do: D.div(ft, @ft_factor) + def ft_to_m(ft = %D{}), do: D.div(ft, @ft_factor_d) def ft_to_m(ft), do: ft / @ft_factor def celsius_to_fahrenheit(nil, _precision), do: nil diff --git a/lib/teslamate/log.ex b/lib/teslamate/log.ex index 069306e1..8875f0a1 100644 --- a/lib/teslamate/log.ex +++ b/lib/teslamate/log.ex @@ -568,10 +568,7 @@ defmodule TeslaMate.Log do }} when is_number(minutes) -> cost = Decimal.mult(minutes, cost_per_minute) - - if match?(%Decimal{}, cost) or match?(%Decimal{}, session_fee) do - Decimal.add(session_fee || 0, cost || 0) - end + Decimal.add(session_fee || 0, cost) {_, _} -> nil diff --git a/lib/teslamate/repair.ex b/lib/teslamate/repair.ex index 1f79e5ac..04f8c2cd 100644 --- a/lib/teslamate/repair.ex +++ b/lib/teslamate/repair.ex @@ -15,7 +15,7 @@ defmodule TeslaMate.Repair do # API def start_link(opts) do - GenServer.start_link(__MODULE__, opts, name: __MODULE__, fullsweep_after: 10) + GenServer.start_link(__MODULE__, opts, name: __MODULE__) end def trigger_run do diff --git a/lib/teslamate/updater.ex b/lib/teslamate/updater.ex index 6c637f2d..ee5b8a54 100644 --- a/lib/teslamate/updater.ex +++ b/lib/teslamate/updater.ex @@ -7,7 +7,7 @@ defmodule TeslaMate.Updater do @version Mix.Project.config()[:version] @name __MODULE__ - adapter Tesla.Adapter.Finch, name: TeslaMate.HTTP + adapter Tesla.Adapter.Finch, name: TeslaMate.HTTP, receive_timeout: 30_000 plug Tesla.Middleware.BaseUrl, "https://api.github.com" plug Tesla.Middleware.Headers, [{"user-agent", "TeslaMate/#{@version}"}] @@ -85,7 +85,7 @@ defmodule TeslaMate.Updater do ## Private defp fetch_release do - case get("/repos/adriankumpf/teslamate/releases/latest", receive_timeout: 30_000) do + case get("/repos/adriankumpf/teslamate/releases/latest") do {:ok, %Tesla.Env{status: 200, body: body}} -> parse_release(body) diff --git a/lib/teslamate/vehicles/vehicle.ex b/lib/teslamate/vehicles/vehicle.ex index d68a1803..4e3addc4 100644 --- a/lib/teslamate/vehicles/vehicle.ex +++ b/lib/teslamate/vehicles/vehicle.ex @@ -292,7 +292,7 @@ defmodule TeslaMate.Vehicles.Vehicle do {%Vehicle{drive_state: %Drive{timestamp: now}}, %Data{last_response: %Vehicle{drive_state: %Drive{timestamp: last}}}} when is_number(now) and is_number(last) and now < last -> - drive_states = %{now: vehicle.drive_state, last: data.last_response.drive_state} + drive_states = [now: vehicle.drive_state, last: data.last_response.drive_state] Logger.warning( "Discarded stale fetch result: #{inspect(drive_states, pretty: true)}", diff --git a/mix.exs b/mix.exs index 43d3ccc1..520b85a6 100644 --- a/mix.exs +++ b/mix.exs @@ -12,6 +12,7 @@ defmodule TeslaMate.MixProject do aliases: aliases(), releases: releases(), deps: deps(), + dialyzer: dialyzer(), test_coverage: [tool: ExCoveralls], preferred_cli_env: [ coveralls: :test, @@ -64,7 +65,8 @@ defmodule TeslaMate.MixProject do {:websockex, "~> 0.4"}, {:cloak_ecto, "~> 1.2"}, # Necessary until v1.1.7+ is released - {:ssl_verify_fun, "~> 1.1.0", manager: :rebar3, override: true} + {:ssl_verify_fun, "~> 1.1.0", manager: :rebar3, override: true}, + {:dialyxir, "~> 1.3", only: [:dev], runtime: false} ] end @@ -79,6 +81,17 @@ defmodule TeslaMate.MixProject do ] end + defp dialyzer do + [ + plt_file: {:no_warn, "priv/plts/dialyzer.plt"}, + plt_core_path: "priv/plts/", + plt_add_apps: [:mix, :ex_unit], + plt_ignore_apps: [], + ignore_warnings: ".dialyzer_ignore.exs", + list_unused_filters: true + ] + end + defp releases do [ teslamate: [ diff --git a/mix.lock b/mix.lock index cf8603ef..2507890f 100644 --- a/mix.lock +++ b/mix.lock @@ -10,8 +10,10 @@ "cowlib": {:hex, :cowlib, "2.12.1", "a9fa9a625f1d2025fe6b462cb865881329b5caff8f1854d1cbc9f9533f00e1e1", [:make, :rebar3], [], "hexpm", "163b73f6367a7341b33c794c4e88e7dbfe6498ac42dcd69ef44c5bc5507c8db0"}, "db_connection": {:hex, :db_connection, "2.5.0", "bb6d4f30d35ded97b29fe80d8bd6f928a1912ca1ff110831edcd238a1973652c", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c92d5ba26cd69ead1ff7582dbb860adeedfff39774105a4f1c92cbb654b55aa2"}, "decimal": {:hex, :decimal, "2.1.1", "5611dca5d4b2c3dd497dec8f68751f1f1a54755e8ed2a966c2633cf885973ad6", [:mix], [], "hexpm", "53cfe5f497ed0e7771ae1a475575603d77425099ba5faef9394932b35020ffcc"}, + "dialyxir": {:hex, :dialyxir, "1.3.0", "fd1672f0922b7648ff9ce7b1b26fcf0ef56dda964a459892ad15f6b4410b5284", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "00b2a4bcd6aa8db9dcb0b38c1225b7277dca9bc370b6438715667071a304696f"}, "ecto": {:hex, :ecto, "3.10.2", "6b887160281a61aa16843e47735b8a266caa437f80588c3ab80a8a960e6abe37", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "6a895778f0d7648a4b34b486af59a1c8009041fbdf2b17f1ac215eb829c60235"}, "ecto_sql": {:hex, :ecto_sql, "3.10.1", "6ea6b3036a0b0ca94c2a02613fd9f742614b5cfe494c41af2e6571bb034dd94c", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.10.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.16.0 or ~> 0.17.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f6a25bdbbd695f12c8171eaff0851fa4c8e72eec1e98c7364402dda9ce11c56b"}, + "erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"}, "ex_cldr": {:hex, :ex_cldr, "2.37.1", "6091fa719a7a96f9abee7aba186e63a906d504d08039cc8f0c683a0e71ee1bd7", [:mix], [{:cldr_utils, "~> 2.21", [hex: :cldr_utils, repo: "hexpm", optional: false]}, {:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:gettext, "~> 0.19", [hex: :gettext, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: true]}], "hexpm", "5d60c3288454bc966e404ea4f59531f7dbb570d7e927dce62f0ab8466713bf78"}, "ex_cldr_plugs": {:hex, :ex_cldr_plugs, "1.3.0", "72a2064cb36c390dd0b212e8a172f643d455c8d362ee9c4bda29a96b42204df6", [:mix], [{:ex_cldr, "~> 2.37", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:gettext, "~> 0.19", [hex: :gettext, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "699a98543ea14a7c849fae768041c40f49aa611aa55866025d227796e4858bff"}, "excoveralls": {:hex, :excoveralls, "0.16.1", "0bd42ed05c7d2f4d180331a20113ec537be509da31fed5c8f7047ce59ee5a7c5", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "dae763468e2008cf7075a64cb1249c97cb4bc71e236c5c2b5e5cdf1cfa2bf138"}, diff --git a/test/teslamate/vehicles/vehicle/charging_sync_test.exs b/test/teslamate/vehicles/vehicle/charging_sync_test.exs index af4926f1..9ceea1c1 100644 --- a/test/teslamate/vehicles/vehicle/charging_sync_test.exs +++ b/test/teslamate/vehicles/vehicle/charging_sync_test.exs @@ -38,9 +38,8 @@ defmodule TeslaMate.Vehicles.Vehicle.ChargingSyncTest do refute_receive _ end) =~ - ~r""" - \[warn.*\] Invalid charge data: %{ideal_battery_range_km: \[\"can't be blank\"\]} - \[warn.*\] Invalid charge data: %{ideal_battery_range_km: \[\"can't be blank\"\]} + """ + [warning] Invalid charge data: %{ideal_battery_range_km: [\"can't be blank\"]} """ end diff --git a/test/teslamate/vehicles/vehicle/charging_test.exs b/test/teslamate/vehicles/vehicle/charging_test.exs index b825c0a6..5b916829 100644 --- a/test/teslamate/vehicles/vehicle/charging_test.exs +++ b/test/teslamate/vehicles/vehicle/charging_test.exs @@ -234,7 +234,7 @@ defmodule TeslaMate.Vehicles.Vehicle.ChargingTest do end @tag :capture_log - test "transisitions into asleep state", %{test: name} do + test "transitions into asleep state", %{test: name} do now_ts = DateTime.utc_now() |> DateTime.to_unix(:millisecond) events = [ diff --git a/test/teslamate/vehicles/vehicle/streaming_test.exs b/test/teslamate/vehicles/vehicle/streaming_test.exs index 66f3e5ec..72fb1501 100644 --- a/test/teslamate/vehicles/vehicle/streaming_test.exs +++ b/test/teslamate/vehicles/vehicle/streaming_test.exs @@ -234,21 +234,7 @@ defmodule TeslaMate.Vehicles.Vehicle.StreamingTest do assert_receive :continue? refute_receive _ end) =~ """ - Discarded stale fetch result: %{ - last: %TeslaApi.Vehicle.State.Drive{ - gps_as_of: nil, - heading: 120, - latitude: 42.1, - longitude: 42.0, - native_latitude: nil, - native_location_supported: nil, - native_longitude: nil, - native_type: nil, - power: 0, - shift_state: \"D\", - speed: 0, - timestamp: #{DateTime.to_unix(d1, :millisecond)} - }, + Discarded stale fetch result: [ now: %TeslaApi.Vehicle.State.Drive{ gps_as_of: nil, heading: nil, @@ -262,8 +248,22 @@ defmodule TeslaMate.Vehicles.Vehicle.StreamingTest do shift_state: \"P\", speed: 0, timestamp: #{now_ts} + }, + last: %TeslaApi.Vehicle.State.Drive{ + gps_as_of: nil, + heading: 120, + latitude: 42.1, + longitude: 42.0, + native_latitude: nil, + native_location_supported: nil, + native_longitude: nil, + native_type: nil, + power: 0, + shift_state: \"D\", + speed: 0, + timestamp: #{DateTime.to_unix(d1, :millisecond)} } - } + ] """ send(:"api_#{name}", :continue) diff --git a/test/teslamate_web/live/car_index_live_test.exs b/test/teslamate_web/live/car_index_live_test.exs index 6a181194..3212581d 100644 --- a/test/teslamate_web/live/car_index_live_test.exs +++ b/test/teslamate_web/live/car_index_live_test.exs @@ -7,7 +7,7 @@ defmodule TeslaMateWeb.CarLive.Indextest do describe "base URL" do @tag :signed_in - test "initiall sets the base URL", %{conn: conn} do + test "sets the base URL", %{conn: conn} do :ok = start_vehicles([{:ok, online_event()}]) assert %GlobalSettings{base_url: nil} = Settings.get_global_settings!() diff --git a/test/teslamate_web/live/geofence_live_test.exs b/test/teslamate_web/live/geofence_live_test.exs index 628aabbf..c4f48923 100644 --- a/test/teslamate_web/live/geofence_live_test.exs +++ b/test/teslamate_web/live/geofence_live_test.exs @@ -369,7 +369,7 @@ defmodule TeslaMateWeb.GeoFenceLiveTest do describe "grafana URL" do alias TeslaMate.Settings.GlobalSettings - test "initiall sets the base URL", %{conn: conn} do + test "sets the base URL", %{conn: conn} do assert %GlobalSettings{grafana_url: nil} = Settings.get_global_settings!() assert {:ok, _parent_view, _html} =