Skip to content

Commit

Permalink
cleanup test system Mix.env/0 check (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
tfiedlerdejanze authored May 31, 2024
1 parent 265f188 commit 67e626d
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 42 deletions.
34 changes: 15 additions & 19 deletions lib/expublish/git.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,15 @@ defmodule Expublish.Git do
"""
@spec commit_and_tag(Version.t(), Options.t()) :: Version.t()
def commit_and_tag(%Version{} = version, options \\ %Options{}) do
syscall_module = if Mix.env() == :test, do: TestSystem, else: System

commit_prefix = Options.git_commit_prefix(options)
tag_prefix = Options.git_tag_prefix(options)

git_commit_message = "#{commit_prefix} #{version}"
git_tag = "#{tag_prefix}#{version}"

add(["add", "-u"], options, syscall_module)
commit(["commit", "-qm", git_commit_message], options, syscall_module)
tag(["tag", "-a", git_tag, "-m", git_commit_message], options, syscall_module)
add(["add", "-u"], options)
commit(["commit", "-qm", git_commit_message], options)
tag(["tag", "-a", git_tag, "-m", git_commit_message], options)

version
end
Expand All @@ -50,12 +48,10 @@ defmodule Expublish.Git do
def push(version, options \\ %Options{})

def push(%Version{} = version, %Options{dry_run: false, disable_push: false} = options) do
syscall_module = if Mix.env() == :test, do: TestSystem, else: System

%{branch: branch, remote: remote} = options
Logger.info("Pushing new package version with: \"git push #{remote} #{branch} --tags\".\n")

case syscall_module.cmd("git", ["push", remote, branch, "--tags"]) do
case Expublish.System.cmd("git", ["push", remote, branch, "--tags"]) do
{_, 0} -> :noop
_ -> Logger.error("Failed to push new version commit to git.")
end
Expand All @@ -68,31 +64,31 @@ defmodule Expublish.Git do
version
end

defp add(command, %Options{dry_run: true}, syscall_module) do
syscall_module.cmd("git", command ++ ["--dry-run"])
defp add(command, %Options{dry_run: true}) do
Expublish.System.cmd("git", command ++ ["--dry-run"])
end

defp add(command, _options, syscall_module) do
syscall_module.cmd("git", command)
defp add(command, _options) do
Expublish.System.cmd("git", command)
end

defp commit([_, _, git_commit_message] = command, %Options{dry_run: true}, syscall_module) do
defp commit([_, _, git_commit_message] = command, %Options{dry_run: true}) do
Logger.info(~s'Skipping new version commit: "#{git_commit_message}".')
syscall_module.cmd("git", command ++ ["--dry-run"])
Expublish.System.cmd("git", command ++ ["--dry-run"])
end

defp commit([_, _, git_commit_message] = command, _options, syscall_module) do
defp commit([_, _, git_commit_message] = command, _options) do
Logger.info(~s'Creating new version commit: "#{git_commit_message}".')
syscall_module.cmd("git", command)
Expublish.System.cmd("git", command)
end

defp tag([_, _, git_tag | _], %Options{dry_run: true}, _syscall_module) do
defp tag([_, _, git_tag | _], %Options{dry_run: true}) do
Logger.info(~s'Skipping new version tag: "#{git_tag}".')
end

defp tag([_, _, git_tag | _] = command, _options, syscall_module) do
defp tag([_, _, git_tag | _] = command, _options) do
Logger.info(~s'Creating new version tag: "#{git_tag}".')
syscall_module.cmd("git", command)
Expublish.System.cmd("git", command)
end

defp status(command) do
Expand Down
3 changes: 1 addition & 2 deletions lib/expublish/hex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ defmodule Expublish.Hex do
def publish(version, options \\ %Options{})

def publish(version, %Options{dry_run: false, disable_publish: false}) do
syscall_module = if Mix.env() == :test, do: TestSystem, else: System
Logger.info("Publishing new package version with: \"mix hex.publish --yes\".\n")

case syscall_module.cmd("mix", ["hex.publish", "--yes"]) do
case Expublish.System.cmd("mix", ["hex.publish", "--yes"]) do
{_, 0} ->
Logger.info("Successfully published new package version on hex.")

Expand Down
11 changes: 11 additions & 0 deletions lib/expublish/shell.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Expublish.Shell do
@moduledoc false

def cmd(command, opts \\ []) do
adapter().cmd(command, opts)
end

defp adapter do
Application.get_env(:expublish, :shell_adapter, Mix.Shell.IO)
end
end
10 changes: 10 additions & 0 deletions lib/expublish/system.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule Expublish.System do
@moduledoc false
def cmd(command, args, opts \\ []) do
adapter().cmd(command, args, opts)
end

defp adapter do
Application.get_env(:expublish, :system_adapter, System)
end
end
3 changes: 1 addition & 2 deletions lib/expublish/tests.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ defmodule Expublish.Tests do

def validate(_options, level) do
Logger.info("Starting test run for #{to_string(level)} release.")
syscall_module = if Mix.env() == :test, do: TestMixShell, else: Mix.Shell.IO

case syscall_module.cmd("mix test") do
case Expublish.Shell.cmd("mix test") do
0 -> :ok
_ -> "Test run failed. Abort."
end
Expand Down
15 changes: 0 additions & 15 deletions test/support/test_mix_shell.ex

This file was deleted.

15 changes: 15 additions & 0 deletions test/support/test_shell.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule Expublish.TestShell do
@moduledoc false

require Logger

def cmd(command, args \\ []) do
args_string = Enum.join(args, " ")

"#{command} #{args_string}"
|> String.trim()
|> Logger.info()

0
end
end
8 changes: 4 additions & 4 deletions test/support/test_system.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule TestSystem do
defmodule Expublish.TestSystem do
@moduledoc false

require Logger

def cmd(command, options \\ []) do
options_string = Enum.join(options, " ")
def cmd(command, args, _options \\ []) do
args_string = Enum.join(args, " ")

"#{command} #{options_string}"
"#{command} #{args_string}"
|> String.trim()
|> Logger.info()

Expand Down
3 changes: 3 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
ExUnit.start()

Application.put_env(:expublish, :system_adapter, Expublish.TestSystem)
Application.put_env(:expublish, :shell_adapter, Expublish.TestShell)

0 comments on commit 67e626d

Please sign in to comment.