Skip to content

Commit

Permalink
feat: add dialyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedsoupe committed Jan 13, 2025
1 parent e17d8bd commit 81277f9
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 25 deletions.
Empty file added .dialyzerignore
Empty file.
150 changes: 148 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,30 @@ jobs:
elixir-version: ${{ matrix.elixir }}
otp-version: ${{ matrix.otp }}

- name: Install dependencies
run: mix deps.get
- name: Cache Elixir deps
uses: actions/cache@v1
id: deps-cache
with:
path: deps
key: ${{ runner.os }}-mix-${{ env.MIX_ENV }}-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}

- name: Cache Elixir _build
uses: actions/cache@v1
id: build-cache
with:
path: _build
key: ${{ runner.os }}-build-${{ env.MIX_ENV }}-${{ matrix.otp }}-${{ matrix.elixir }}-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}

- name: Install deps
if: steps.deps-cache.outputs.cache-hit != 'true'
run: |
mix local.rebar --force
mix local.hex --force
mix deps.get --only ${{ env.MIX_ENV }}
- name: Compile deps
if: steps.build-cache.outputs.cache-hit != 'true'
run: mix deps.compile --warnings-as-errors

- name: Clean build
run: mix clean
Expand All @@ -41,3 +63,127 @@ jobs:

- name: Run Credo
run: mix credo --strict

static-analisys:
runs-on: ubuntu-latest

env:
MIX_ENV: test

strategy:
matrix:
elixir: [1.18.1]
otp: [27.0]

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Elixir
uses: erlef/setup-beam@v1
with:
elixir-version: ${{ matrix.elixir }}
otp-version: ${{ matrix.otp }}

- name: Cache Elixir deps
uses: actions/cache@v1
id: deps-cache
with:
path: deps
key: ${{ runner.os }}-mix-${{ env.MIX_ENV }}-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}

- name: Cache Elixir _build
uses: actions/cache@v1
id: build-cache
with:
path: _build
key: ${{ runner.os }}-build-${{ env.MIX_ENV }}-${{ matrix.otp }}-${{ matrix.elixir }}-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}

- name: Install deps
if: steps.deps-cache.outputs.cache-hit != 'true'
run: |
mix local.rebar --force
mix local.hex --force
mix deps.get --only ${{ env.MIX_ENV }}
- name: Compile deps
if: steps.build-cache.outputs.cache-hit != 'true'
run: mix deps.compile --warnings-as-errors

# Don't cache PLTs based on mix.lock hash, as Dialyzer can incrementally update even old ones
# Cache key based on Elixir & Erlang version (also useful when running in matrix)
- name: Restore PLT cache
uses: actions/cache/restore@v3
id: plt_cache
with:
key: ${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-plt
restore-keys: ${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-plt
path: priv/plts

# Create PLTs if no cache was found
- name: Create PLTs
if: steps.plt_cache.outputs.cache-hit != 'true'
run: mix dialyzer --plt

- name: Save PLT cache
uses: actions/cache/save@v3
if: steps.plt_cache.outputs.cache-hit != 'true'
id: plt_cache_save
with:
key: ${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-plt
path: priv/plts

- name: Run dialyzer
run: mix dialyzer --format github

test:
runs-on: ubuntu-latest

env:
MIX_ENV: test

strategy:
matrix:
elixir: [1.18.1]
otp: [27.0]

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Elixir
uses: erlef/setup-beam@v1
with:
elixir-version: ${{ matrix.elixir }}
otp-version: ${{ matrix.otp }}

- name: Cache Elixir deps
uses: actions/cache@v1
id: deps-cache
with:
path: deps
key: ${{ runner.os }}-mix-${{ env.MIX_ENV }}-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}

- name: Cache Elixir _build
uses: actions/cache@v1
id: build-cache
with:
path: _build
key: ${{ runner.os }}-build-${{ env.MIX_ENV }}-${{ matrix.otp }}-${{ matrix.elixir }}-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}

- name: Install deps
if: steps.deps-cache.outputs.cache-hit != 'true'
run: |
mix local.rebar --force
mix local.hex --force
mix deps.get --only ${{ env.MIX_ENV }}
- name: Compile deps
if: steps.build-cache.outputs.cache-hit != 'true'
run: mix deps.compile --warnings-as-errors

- name: Clean build
run: mix clean

- name: Run tests
run: mix test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ result
.elixir_ls/
.elixir-tools/
.lexical/

# Dialyzer
/priv/plt/
4 changes: 2 additions & 2 deletions lib/supabase.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ defmodule Supabase do
{:ok, Client.t()} | {:error, changeset}
when supabase_url: String.t(),
supabase_key: String.t(),
options: Enumerable.t()
options: Client.options()
def init_client(url, api_key, opts \\ %{})
when is_binary(url) and is_binary(api_key) do
opts
|> Map.new()
|> Map.put(:base_url, url)
|> Map.put(:api_key, api_key)
|> then(&Client.changeset(%Client{}, &1))
|> Client.changeset()
|> Ecto.Changeset.apply_action(:parse)
|> then(&maybe_put_storage_key/1)
|> then(&put_default_headers/1)
Expand Down
12 changes: 8 additions & 4 deletions lib/supabase/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ defmodule Supabase.Application do
Supervisor.start_link(children, opts)
end

@spec maybe_append_child(list(Supervisor.child_spec()), (env -> bool), Supervisor.child_spec()) ::
list(Supervisor.child_spec())
when env: :dev | :prod | :test
@spec maybe_append_child([child], (env -> boolean()), child) :: [child]
when env: :dev | :prod | :test | nil, child: Supervisor.module_spec()
defp maybe_append_child(children, pred, child) do
env = get_env()

if pred.(env), do: children ++ [child], else: children
cond do
is_nil(env) -> children
pred.(env) -> children ++ [child]
not pred.(env) -> children
end
end

@spec get_env :: :dev | :prod | :test | nil
defp get_env, do: Application.get_env(:supabase_potion, :env)
end
21 changes: 6 additions & 15 deletions lib/supabase/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ defmodule Supabase.Client do
Note that these options can be passed to `Supabase.init_client/3` as `Enumerable`, which means it can be either a `Keyword.t()` or a `Map.t()`, but internally it will be passed as a map.
"""
@type options :: %{
db: Db.params(),
global: Global.params(),
auth: Auth.params()
optional(:db) => Db.params(),
optional(:global) => Global.params(),
optional(:auth) => Auth.params()
}

defmacro __using__(otp_app: otp_app) do
Expand Down Expand Up @@ -265,18 +265,9 @@ defmodule Supabase.Client do
embeds_one(:auth, Auth, defaults_to_struct: true, on_replace: :update)
end

@spec changeset(source, attrs) :: {:ok, t} | {:error, changeset}
when source: t(),
changeset: Ecto.Changeset.t(),
attrs: %{
base_url: String.t(),
api_key: String.t(),
db: Db.params(),
global: Global.params(),
auth: Auth.params()
}
def changeset(%__MODULE__{} = source, %{base_url: base_url, api_key: api_key} = attrs) do
source
@spec changeset(attrs :: map) :: Ecto.Changeset.t()
def changeset(%{base_url: base_url, api_key: api_key} = attrs) do
%__MODULE__{}
|> cast(attrs, [:api_key, :base_url, :access_token])
|> put_change(:access_token, attrs[:access_token] || api_key)
|> cast_embed(:db, required: false)
Expand Down
2 changes: 1 addition & 1 deletion lib/supabase/fetcher/adapter/finch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ defmodule Supabase.Fetcher.Adapter.Finch do
@impl true
def upload(%Request{} = b, file, opts \\ []) do
mime_type = MIME.from_path(file)
body_stream = File.stream!(file, 2048, [:raw])
body_stream = File.stream!(file, 2048)
%File.Stat{size: content_length} = File.stat!(file)
content_headers = [{"content-length", to_string(content_length)}, {"content-type", mime_type}]

Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ defmodule Supabase.MixProject do
docs: docs(),
package: package(),
description: description(),
elixirc_paths: elixirc_paths(Mix.env())
elixirc_paths: elixirc_paths(Mix.env()),
dialyzer: [plt_local_path: "priv/plt", ignore_warnings: ".dialyzerignore"]
]
end

Expand Down

0 comments on commit 81277f9

Please sign in to comment.