From f87661418d5abcca9ce951fe2fbf5e06a57213c4 Mon Sep 17 00:00:00 2001 From: Justin Schneck Date: Mon, 18 Nov 2019 12:30:15 -0500 Subject: [PATCH] Allow source_date_epoch to be set from Config This will set `SOURCE_DATE_EPOCH` to a value from `config :nerves, source_date_epoch: 1234` or can be overridden if `SOURCE_DATE_EPOCH` is already defined. --- lib/nerves/env.ex | 28 ++++++++++++++++++++ test/nerves/env_test.exs | 57 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/lib/nerves/env.ex b/lib/nerves/env.ex index 4c486d49..bdedd936 100644 --- a/lib/nerves/env.ex +++ b/lib/nerves/env.ex @@ -17,6 +17,7 @@ defmodule Nerves.Env do """ @spec start() :: Agent.on_start() def start do + set_source_date_epoch() Agent.start_link(fn -> load_packages() end, name: __MODULE__) end @@ -386,6 +387,33 @@ defmodule Nerves.Env do end end + def source_date_epoch() do + (System.get_env("SOURCE_DATE_EPOCH") || Application.get_env(:nerves, :source_date_epoch)) + |> validate_source_date_epoch() + end + + defp set_source_date_epoch() do + case source_date_epoch() do + {:ok, nil} -> :ok + {:ok, sde} -> System.put_env("SOURCE_DATE_EPOCH", sde) + {:error, error} -> Mix.raise(error) + end + end + + defp validate_source_date_epoch(nil), do: {:ok, nil} + defp validate_source_date_epoch(sde) when is_integer(sde), do: {:ok, Integer.to_string(sde)} + defp validate_source_date_epoch(""), do: {:error, "SOURCE_DATE_EPOCH cannot be empty"} + + defp validate_source_date_epoch(sde) when is_binary(sde) do + case Integer.parse(sde) do + {_sde, _rem} -> + {:ok, sde} + + :error -> + {:error, "SOURCE_DATE_EPOCH should be a positive integer, received: #{inspect(sde)}"} + end + end + @doc false defp load_packages do Mix.Project.deps_paths() diff --git a/test/nerves/env_test.exs b/test/nerves/env_test.exs index 01668d7b..bcfeb637 100644 --- a/test/nerves/env_test.exs +++ b/test/nerves/env_test.exs @@ -79,4 +79,61 @@ defmodule Nerves.EnvTest do assert Path.expand("~/.nerves") == Nerves.Env.data_dir() end end + + describe "source_date_epoch" do + setup do + on_exit(fn -> + System.delete_env("SOURCE_DATE_EPOCH") + Application.delete_env(:nerves, :source_date_epoch) + end) + end + + test "from environment" do + in_fixture("simple_app", fn -> + packages = ~w(system toolchain system_platform toolchain_platform) + System.put_env("SOURCE_DATE_EPOCH", "1234") + load_env(packages) + assert System.get_env("SOURCE_DATE_EPOCH") == "1234" + end) + end + + test "from config" do + in_fixture("simple_app", fn -> + packages = ~w(system toolchain system_platform toolchain_platform) + Application.put_env(:nerves, :source_date_epoch, "1234") + load_env(packages) + assert System.get_env("SOURCE_DATE_EPOCH") == "1234" + end) + end + + test "nil" do + in_fixture("simple_app", fn -> + packages = ~w(system toolchain system_platform toolchain_platform) + load_env(packages) + assert System.get_env("SOURCE_DATE_EPOCH") == nil + end) + end + + test "invalid value" do + System.put_env("SOURCE_DATE_EPOCH", "foo") + assert {:error, _} = Nerves.Env.source_date_epoch() + System.put_env("SOURCE_DATE_EPOCH", "") + assert {:error, _} = Nerves.Env.source_date_epoch() + System.delete_env("SOURCE_DATE_EPOCH") + Application.put_env(:nerves, :source_date_epoch, "foo") + assert {:error, _} = Nerves.Env.source_date_epoch() + Application.put_env(:nerves, :source_date_epoch, "") + assert {:error, _} = Nerves.Env.source_date_epoch() + end + + test "mix raises when invalid" do + in_fixture("simple_app", fn -> + System.put_env("SOURCE_DATE_EPOCH", "") + + assert_raise Mix.Error, fn -> + Nerves.Env.start() + end + end) + end + end end