fix: issue #18 more visible git activities

This commit is contained in:
2026-07-06 15:52:28 +02:00
parent 8ee51c6626
commit 10fa174388
16 changed files with 958 additions and 134 deletions

View File

@@ -0,0 +1,92 @@
defmodule BDS.Desktop.ShellLive.GitRunTest do
use ExUnit.Case, async: true
import Phoenix.LiveViewTest
alias BDS.Desktop.ShellLive.GitRun
alias Phoenix.Component
defp socket(git_run) do
%Phoenix.LiveView.Socket{}
|> Component.assign(:git_run, git_run)
end
test "network_event? recognises fetch/pull/push but not prune" do
assert GitRun.network_event?("git_push")
assert GitRun.network_event?("git_pull")
assert GitRun.network_event?("git_fetch")
refute GitRun.network_event?("git_prune_lfs")
end
test "start with no project shows an error and a failed status" do
result = GitRun.start(socket(nil), "git_push", nil)
run = result.assigns.git_run
assert run.operation == :push
assert run.status == {:done, 1}
assert [{:stderr, text}] = run.lines
assert text =~ "No active project"
end
test "append accumulates chunks for the active ref only" do
ref = make_ref()
run = %{operation: :push, pid: nil, ref: ref, lines: [], status: :running}
socket =
socket(run)
|> GitRun.append(ref, :stdout, "a")
|> GitRun.append(ref, :stderr, "b")
|> GitRun.append(make_ref(), :stdout, "ignored")
assert socket.assigns.git_run.lines == [{:stderr, "b"}, {:stdout, "a"}]
end
test "done records the exit status and clears the pid" do
ref = make_ref()
run = %{operation: :push, pid: self(), ref: ref, lines: [], status: :running}
socket = GitRun.done(socket(run), ref, 128)
assert socket.assigns.git_run.status == {:done, 128}
assert socket.assigns.git_run.pid == nil
end
test "renders nothing when there is no active run" do
assert render_component(&GitRun.git_run_modal/1, git_run: nil) |> String.trim() == ""
end
test "renders the failed run with stderr coloured and a fail status" do
run = %{
operation: :push,
pid: nil,
ref: make_ref(),
lines: [{:stderr, "rejected"}, {:stdout, "counting"}],
status: {:done, 1}
}
html = render_component(&GitRun.git_run_modal/1, git_run: run)
assert html =~ "git push"
assert html =~ "git-run-stderr"
assert html =~ "rejected"
assert html =~ "git-run-status fail"
end
test "close cancels a running command and clears the modal" do
target = self()
pid =
spawn(fn ->
receive do
:cancel -> send(target, :cancelled)
end
end)
run = %{operation: :push, pid: pid, ref: make_ref(), lines: [], status: :running}
socket = GitRun.close(socket(run))
assert socket.assigns.git_run == nil
assert_receive :cancelled, 1_000
end
end

View File

@@ -0,0 +1,57 @@
defmodule BDS.Git.StreamTest do
use ExUnit.Case, async: true
alias BDS.Git.Stream
setup do
dir = Path.join(System.tmp_dir!(), "bds-git-stream-#{System.unique_integer([:positive])}")
File.mkdir_p!(dir)
on_exit(fn -> File.rm_rf(dir) end)
%{dir: dir}
end
defp write_script(dir, name, body) do
path = Path.join(dir, name)
File.write!(path, "#!/bin/sh\n" <> body)
File.chmod!(path, 0o755)
path
end
test "streams stdout and stderr separately and reports the exit status", %{dir: dir} do
script =
write_script(
dir,
"emit",
~s(printf 'OUT:%s\\n' "$1"; printf 'ERR:%s\\n' "$1" 1>&2\nexit 3\n)
)
{:ok, _pid, ref} = Stream.start(self(), dir, ["hello"], command: script)
assert_receive {:git_output, ^ref, :stdout, out}, 2_000
assert out =~ "OUT:hello"
assert_receive {:git_output, ^ref, :stderr, err}, 2_000
assert err =~ "ERR:hello"
assert_receive {:git_done, ^ref, 3}, 2_000
end
test "cancel kills a hanging process and still reports done", %{dir: dir} do
script = write_script(dir, "hang", "exec sleep 30\n")
{:ok, pid, ref} = Stream.start(self(), dir, [], command: script)
Stream.cancel(pid)
assert_receive {:git_done, ^ref, status}, 2_000
refute status == 0
end
test "reports a failure when the command is missing", %{dir: dir} do
{:ok, _pid, ref} = Stream.start(self(), dir, [], command: "definitely-not-a-real-binary-xyz")
assert_receive {:git_output, ^ref, :stderr, msg}, 2_000
assert msg =~ "definitely-not-a-real-binary-xyz"
assert_receive {:git_done, ^ref, status}, 2_000
refute status == 0
end
end

View File

@@ -250,6 +250,21 @@ defmodule BDS.GitTest do
end)
end
test "stream/4 runs a network operation for the project and streams output live", %{
project: project,
temp_root: temp_root
} do
script = Path.join(temp_root, "fake-git")
File.write!(script, "#!/bin/sh\nprintf 'pushing %s\\n' \"$1\" 1>&2\nexit 0\n")
File.chmod!(script, 0o755)
assert {:ok, _pid, ref} = Git.stream(project.id, :push, self(), command: script)
assert_receive {:git_output, ^ref, :stderr, chunk}, 2_000
assert chunk =~ "pushing push"
assert_receive {:git_done, ^ref, 0}, 2_000
end
defp fake_runner(handler) do
fn command, args, opts -> handler.(command, args, opts) end
end