Skip to content

Commit

Permalink
Allow source_date_epoch to be set from Config
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mobileoverlord committed Nov 18, 2019
1 parent 553de3a commit f876614
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/nerves/env.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down
57 changes: 57 additions & 0 deletions test/nerves/env_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f876614

Please sign in to comment.