From 34a510852717e854e5fd206f3e2185d34bfed144 Mon Sep 17 00:00:00 2001 From: Nathan Youngman Date: Wed, 19 Jun 2024 15:45:51 -0600 Subject: [PATCH] Resolve Dialyzer errors on Elixir 1.16 (#643) --- .github/workflows/test.yml | 72 ++++++++++++++++++++++++++++++++------ lib/tesla/multipart.ex | 8 ++++- mix.exs | 2 +- 3 files changed, 70 insertions(+), 12 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 698ceaf1..e5e69b8b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,16 +10,16 @@ jobs: strategy: matrix: include: - - otp: '25.3.2.12' - elixir: '1.15.8' + - elixir: '1.15.8' + otp: '25.3.2.12' experimental: false lint: false - - otp: '26.2.5' - elixir: '1.16.3' + - elixir: '1.16.3' + otp: '26.2.5' experimental: true lint: true - - otp: '27.0' - elixir: '1.17.1' + - elixir: '1.17.1' + otp: '27.0' experimental: true lint: false steps: @@ -32,11 +32,13 @@ jobs: version-type: strict - name: Restore dependencies cache uses: actions/cache@v4 + id: cache with: path: deps - key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }} - restore-keys: ${{ runner.os }}-mix- + key: ${{ runner.os }}-${{ steps.beam.outputs.elixir-version }}-${{ steps.beam.outputs.otp-version }}-mix-${{ hashFiles('**/mix.lock') }} + restore-keys: ${{ runner.os }}-${{ steps.beam.outputs.elixir-version }}-${{ steps.beam.outputs.otp-version }}-mix- - name: Install Dependencies + if: steps.cache.outputs.cache-hit != 'true' env: MIX_ENV: test run: | @@ -63,10 +65,11 @@ jobs: version-type: strict - name: Restore dependencies cache uses: actions/cache@v4 + id: cache with: path: deps - key: ${{ runner.os }}-mix-${{ hashFiles('test/lockfiles/gun1.lock') }} - restore-keys: ${{ runner.os }}-mix- + key: ${{ runner.os }}-${{ steps.beam.outputs.elixir-version }}-${{ steps.beam.outputs.otp-version }}-mix-gun-${{ hashFiles('test/lockfiles/gun1.lock') }} + restore-keys: ${{ runner.os }}-${{ steps.beam.outputs.elixir-version }}-${{ steps.beam.outputs.otp-version }}-mix-gun- - name: Install Dependencies env: MIX_ENV: test @@ -79,3 +82,52 @@ jobs: env: LOCKFILE: gun1 run: mix test test/tesla/adapter/gun_test.exs --trace + + dialyzer: + runs-on: ubuntu-latest + name: Dialyzer + steps: + - uses: actions/checkout@v4 + - name: Set up Elixir + uses: erlef/setup-beam@v1 + with: + elixir-version: '1.16.3' + otp-version: '26.2.5' + version-type: strict + - name: Restore dependencies cache + uses: actions/cache@v4 + id: cache + with: + path: | + deps + _build + dialyzer + key: ${{ runner.os }}-${{ steps.beam.outputs.elixir-version }}-${{ steps.beam.outputs.otp-version }}-dialyzer-${{ hashFiles('**/mix.lock') }} + restore-keys: | + ${{ runner.os }}-${{ steps.beam.outputs.elixir-version }}-${{ steps.beam.outputs.otp-version }}-dialyzer- + - name: Install Dependencies + if: steps.cache.outputs.cache-hit != 'true' + env: + MIX_ENV: test + run: | + mix local.rebar --force + mix local.hex --force + mix deps.get + # Doesn't cache PLTs based on mix.lock hash, as Dialyzer can incrementally update even old ones + # Cache key based on Elixir & Erlang version. + - name: Restore PLT cache + uses: actions/cache@v2 + id: plt_cache + with: + key: | + ${{ runner.os }}-${{ steps.beam.outputs.elixir-version }}-${{ steps.beam.outputs.otp-version }}-plt + restore-keys: | + ${{ runner.os }}-${{ steps.beam.outputs.elixir-version }}-${{ steps.beam.outputs.otp-version }}-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: Run dialyzer + run: mix dialyzer --format github diff --git a/lib/tesla/multipart.ex b/lib/tesla/multipart.ex index be8673dc..cce7fc9e 100644 --- a/lib/tesla/multipart.ex +++ b/lib/tesla/multipart.ex @@ -103,7 +103,7 @@ defmodule Tesla.Multipart do false -> headers end - data = File.stream!(path, [], 2048) + data = stream_file!(path, 2048) add_file_content(mp, data, filename, opts ++ [headers: headers]) end @@ -194,4 +194,10 @@ defmodule Tesla.Multipart do defp assert_part_value!(val) do raise(ArgumentError, "#{inspect(val)} is not a supported multipart value.") end + + if Version.compare(System.version(), "1.16.0") in [:gt, :eq] do + defp stream_file!(path, bytes), do: File.stream!(path, bytes) + else + defp stream_file!(path, bytes), do: File.stream!(path, [], bytes) + end end diff --git a/mix.exs b/mix.exs index cdf01a37..9dca78b0 100644 --- a/mix.exs +++ b/mix.exs @@ -18,7 +18,7 @@ defmodule Tesla.Mixfile do dialyzer: [ plt_core_path: "_build/#{Mix.env()}", plt_add_apps: [:mix, :inets, :idna, :ssl_verify_fun, :ex_unit], - plt_add_deps: :project + plt_add_deps: :apps_direct ], docs: docs(), preferred_cli_env: [coveralls: :test, "coveralls.html": :test]