otp version
authorAlexander Strizhakov <alex.strizhakov@gmail.com>
Wed, 4 Mar 2020 06:23:42 +0000 (09:23 +0300)
committerAlexander Strizhakov <alex.strizhakov@gmail.com>
Wed, 4 Mar 2020 06:23:42 +0000 (09:23 +0300)
lib/pleroma/application.ex
lib/pleroma/otp_version.ex
test/otp_version_test.exs

index d0b9c3c41dad575d6fd33567b887cec9e01e8f76..c8a0617a5a318044e7d18c37c1255464bf0e078d 100644 (file)
@@ -43,7 +43,25 @@ defmodule Pleroma.Application do
     load_custom_modules()
 
     if adapter() == Tesla.Adapter.Gun do
     load_custom_modules()
 
     if adapter() == Tesla.Adapter.Gun do
-      Pleroma.OTPVersion.check!()
+      if version = Pleroma.OTPVersion.version() do
+        [major, minor] =
+          version
+          |> String.split(".")
+          |> Enum.map(&String.to_integer/1)
+          |> Enum.take(2)
+
+        if (major == 22 and minor < 2) or major < 22 do
+          raise "
+            !!!OTP VERSION WARNING!!!
+            You are using gun adapter with OTP version #{version}, which doesn't support correct handling of unordered certificates chains.
+            "
+        end
+      else
+        raise "
+          !!!OTP VERSION WARNING!!!
+          To support correct handling of unordered certificates chains - OTP version must be > 22.2.
+          "
+      end
     end
 
     # Define workers and child supervisors to be supervised
     end
 
     # Define workers and child supervisors to be supervised
index 9ced2d27d593b7c3495cef9a1528be036a5dcdae..114d0054f8d41bda989eea26515e37abfd647d62 100644 (file)
@@ -3,71 +3,26 @@
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.OTPVersion do
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.OTPVersion do
-  @type check_status() :: :ok | :undefined | {:error, String.t()}
-
-  @spec check!() :: :ok | no_return()
-  def check! do
-    case check() do
-      :ok ->
-        :ok
-
-      {:error, version} ->
-        raise "
-            !!!OTP VERSION WARNING!!!
-            You are using gun adapter with OTP version #{version}, which doesn't support correct handling of unordered certificates chains.
-            "
-
-      :undefined ->
-        raise "
-            !!!OTP VERSION WARNING!!!
-            To support correct handling of unordered certificates chains - OTP version must be > 22.2.
-            "
-    end
-  end
-
-  @spec check() :: check_status()
-  def check do
+  @spec version() :: String.t() | nil
+  def version do
     # OTP Version https://erlang.org/doc/system_principles/versions.html#otp-version
     [
       Path.join(:code.root_dir(), "OTP_VERSION"),
       Path.join([:code.root_dir(), "releases", :erlang.system_info(:otp_release), "OTP_VERSION"])
     ]
     |> get_version_from_files()
     # OTP Version https://erlang.org/doc/system_principles/versions.html#otp-version
     [
       Path.join(:code.root_dir(), "OTP_VERSION"),
       Path.join([:code.root_dir(), "releases", :erlang.system_info(:otp_release), "OTP_VERSION"])
     ]
     |> get_version_from_files()
-    |> do_check()
-  end
-
-  @spec check([Path.t()]) :: check_status()
-  def check(paths) do
-    paths
-    |> get_version_from_files()
-    |> do_check()
   end
 
   end
 
-  defp get_version_from_files([]), do: nil
+  @spec get_version_from_files([Path.t()]) :: String.t() | nil
+  def get_version_from_files([]), do: nil
 
 
-  defp get_version_from_files([path | paths]) do
+  def get_version_from_files([path | paths]) do
     if File.exists?(path) do
     if File.exists?(path) do
-      File.read!(path)
+      path
+      |> File.read!()
+      |> String.replace(~r/\r|\n|\s/, "")
     else
       get_version_from_files(paths)
     end
   end
     else
       get_version_from_files(paths)
     end
   end
-
-  defp do_check(nil), do: :undefined
-
-  defp do_check(version) do
-    version = String.replace(version, ~r/\r|\n|\s/, "")
-
-    [major, minor] =
-      version
-      |> String.split(".")
-      |> Enum.map(&String.to_integer/1)
-      |> Enum.take(2)
-
-    if (major == 22 and minor >= 2) or major > 22 do
-      :ok
-    else
-      {:error, version}
-    end
-  end
 end
 end
index af278cc729e90aee2eb04399ebf2616fb6962a54..7d2538ec8e556e4d390947d93cd013e880fef9c1 100644 (file)
@@ -9,30 +9,34 @@ defmodule Pleroma.OTPVersionTest do
 
   describe "check/1" do
     test "22.4" do
 
   describe "check/1" do
     test "22.4" do
-      assert OTPVersion.check(["test/fixtures/warnings/otp_version/22.4"]) == :ok
+      assert OTPVersion.get_version_from_files(["test/fixtures/warnings/otp_version/22.4"]) ==
+               "22.4"
     end
 
     test "22.1" do
     end
 
     test "22.1" do
-      assert OTPVersion.check(["test/fixtures/warnings/otp_version/22.1"]) == {:error, "22.1"}
+      assert OTPVersion.get_version_from_files(["test/fixtures/warnings/otp_version/22.1"]) ==
+               "22.1"
     end
 
     test "21.1" do
     end
 
     test "21.1" do
-      assert OTPVersion.check(["test/fixtures/warnings/otp_version/21.1"]) == {:error, "21.1"}
+      assert OTPVersion.get_version_from_files(["test/fixtures/warnings/otp_version/21.1"]) ==
+               "21.1"
     end
 
     test "23.0" do
     end
 
     test "23.0" do
-      assert OTPVersion.check(["test/fixtures/warnings/otp_version/23.0"]) == :ok
+      assert OTPVersion.get_version_from_files(["test/fixtures/warnings/otp_version/23.0"]) ==
+               "23.0"
     end
 
     test "with non existance file" do
     end
 
     test "with non existance file" do
-      assert OTPVersion.check([
+      assert OTPVersion.get_version_from_files([
                "test/fixtures/warnings/otp_version/non-exising",
                "test/fixtures/warnings/otp_version/22.4"
                "test/fixtures/warnings/otp_version/non-exising",
                "test/fixtures/warnings/otp_version/22.4"
-             ]) == :ok
+             ]) == "22.4"
     end
 
     test "empty paths" do
     end
 
     test "empty paths" do
-      assert OTPVersion.check([]) == :undefined
+      assert OTPVersion.get_version_from_files([]) == nil
     end
   end
 end
     end
   end
 end