|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "test_helper" |
| 4 | +require "minitest/mock" |
| 5 | +require "git" |
| 6 | +require "git_client" |
| 7 | + |
| 8 | +class GitClientTest < Minitest::Test |
| 9 | + GitClient = Importmap::Update::GitClient |
| 10 | + |
| 11 | + AUTHOR_NAME = "Test Bot" |
| 12 | + AUTHOR_EMAIL = "bot@example.com" |
| 13 | + AUTHOR_STRING = "Test Bot <bot@example.com>" |
| 14 | + |
| 15 | + def setup |
| 16 | + @repo = Minitest::Mock.new |
| 17 | + @client = GitClient.new(repo: @repo, author_name: AUTHOR_NAME, author_email: AUTHOR_EMAIL) |
| 18 | + end |
| 19 | + |
| 20 | + def teardown |
| 21 | + assert_mock @repo |
| 22 | + end |
| 23 | + |
| 24 | + # ---- checkout_fresh_branch ---- |
| 25 | + |
| 26 | + def test_checkout_fresh_branch_creates_branch_when_it_does_not_exist |
| 27 | + @repo.expect(:fetch, nil, ["origin"], ref: "main") |
| 28 | + @repo.expect(:checkout, nil) { raise Git::Error } |
| 29 | + @repo.expect(:checkout, nil, ["importmap-updates/patch"], |
| 30 | + new_branch: true, start_point: "origin/main") |
| 31 | + |
| 32 | + assert_nil @client.checkout_fresh_branch(branch: "importmap-updates/patch", base: "main") |
| 33 | + end |
| 34 | + |
| 35 | + def test_checkout_fresh_branch_resets_existing_branch_to_base |
| 36 | + @repo.expect(:fetch, nil, ["origin"], ref: "main") |
| 37 | + @repo.expect(:checkout, nil, ["importmap-updates/patch"]) |
| 38 | + @repo.expect(:reset_hard, nil, ["origin/main"]) |
| 39 | + |
| 40 | + assert_nil @client.checkout_fresh_branch(branch: "importmap-updates/patch", base: "main") |
| 41 | + end |
| 42 | + |
| 43 | + # ---- commit_changes ---- |
| 44 | + |
| 45 | + def test_commit_changes_stages_and_commits_returning_true |
| 46 | + @repo.expect(:add, nil, [["config/importmap.rb", "vendor/javascript"]]) |
| 47 | + @repo.expect(:commit, nil, ["Bump lodash from 4.17.20 to 4.17.21"], |
| 48 | + author: AUTHOR_STRING) |
| 49 | + |
| 50 | + assert_equal true, @client.commit_changes(message: "Bump lodash from 4.17.20 to 4.17.21") |
| 51 | + end |
| 52 | + |
| 53 | + def test_commit_changes_returns_false_when_nothing_to_commit |
| 54 | + @repo.expect(:add, nil, [["config/importmap.rb", "vendor/javascript"]]) |
| 55 | + @repo.expect(:commit, nil) do |_msg, **_opts| |
| 56 | + raise git_failed_error("nothing to commit, working tree clean") |
| 57 | + end |
| 58 | + |
| 59 | + assert_equal false, @client.commit_changes(message: "irrelevant") |
| 60 | + end |
| 61 | + |
| 62 | + def test_commit_changes_re_raises_unexpected_git_errors |
| 63 | + @repo.expect(:add, nil, [["config/importmap.rb", "vendor/javascript"]]) |
| 64 | + @repo.expect(:commit, nil) do |_msg, **_opts| |
| 65 | + raise git_failed_error("lock file exists") |
| 66 | + end |
| 67 | + |
| 68 | + assert_raises(Git::FailedError) do |
| 69 | + @client.commit_changes(message: "irrelevant") |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + # ---- push ---- |
| 74 | + |
| 75 | + def test_push_without_force |
| 76 | + @repo.expect(:push, nil, ["origin", "importmap-updates/patch"], force: false) |
| 77 | + assert_nil @client.push(branch: "importmap-updates/patch") |
| 78 | + end |
| 79 | + |
| 80 | + def test_push_with_force |
| 81 | + @repo.expect(:push, nil, ["origin", "importmap-updates/patch"], force: true) |
| 82 | + assert_nil @client.push(branch: "importmap-updates/patch", force: true) |
| 83 | + end |
| 84 | + |
| 85 | + private |
| 86 | + |
| 87 | + # Git::FailedError wraps a Git::CommandLineResult which needs a status |
| 88 | + # object. We build the minimum required for e.result.stderr to work. |
| 89 | + def git_failed_error(stderr) |
| 90 | + fake_status = Struct.new(:exitstatus, :pid) { def to_s = "pid #{pid} exit #{exitstatus}" }.new(1, 0) |
| 91 | + result = Git::CommandLineResult.new(["git", "commit"], fake_status, "", stderr) |
| 92 | + Git::FailedError.new(result) |
| 93 | + end |
| 94 | +end |
0 commit comments