|
| 1 | +import os |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from commitizen import cmd, commands |
| 6 | +from commitizen.cz.exceptions import CzException |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def staging_is_clean(mocker): |
| 11 | + is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean") |
| 12 | + is_staging_clean_mock.return_value = False |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.usefixtures("staging_is_clean") |
| 16 | +def test_commit(config, mocker): |
| 17 | + prompt_mock = mocker.patch("questionary.prompt") |
| 18 | + prompt_mock.return_value = { |
| 19 | + "prefix": "feat", |
| 20 | + "subject": "user created", |
| 21 | + "scope": "", |
| 22 | + "is_breaking_change": False, |
| 23 | + "body": "", |
| 24 | + "footer": "", |
| 25 | + } |
| 26 | + |
| 27 | + commit_mock = mocker.patch("commitizen.git.commit") |
| 28 | + commit_mock.return_value = cmd.Command("success", "", "", "") |
| 29 | + success_mock = mocker.patch("commitizen.out.success") |
| 30 | + |
| 31 | + commands.Commit(config, {})() |
| 32 | + success_mock.assert_called_once() |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.usefixtures("staging_is_clean") |
| 36 | +def test_commit_retry_fails_no_backup(config, mocker): |
| 37 | + commit_mock = mocker.patch("commitizen.git.commit") |
| 38 | + commit_mock.return_value = cmd.Command("success", "", "", "") |
| 39 | + |
| 40 | + with pytest.raises(SystemExit): |
| 41 | + commands.Commit(config, {"retry": True})() |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.usefixtures("staging_is_clean") |
| 45 | +def test_commit_retry_works(config, mocker): |
| 46 | + prompt_mock = mocker.patch("questionary.prompt") |
| 47 | + prompt_mock.return_value = { |
| 48 | + "prefix": "feat", |
| 49 | + "subject": "user created", |
| 50 | + "scope": "", |
| 51 | + "is_breaking_change": False, |
| 52 | + "body": "closes #21", |
| 53 | + "footer": "", |
| 54 | + } |
| 55 | + |
| 56 | + commit_mock = mocker.patch("commitizen.git.commit") |
| 57 | + commit_mock.return_value = cmd.Command("", "error", "", "") |
| 58 | + error_mock = mocker.patch("commitizen.out.error") |
| 59 | + |
| 60 | + with pytest.raises(SystemExit): |
| 61 | + commit_cmd = commands.Commit(config, {}) |
| 62 | + temp_file = commit_cmd.temp_file |
| 63 | + commit_cmd() |
| 64 | + |
| 65 | + prompt_mock.assert_called_once() |
| 66 | + error_mock.assert_called_once() |
| 67 | + assert os.path.isfile(temp_file) |
| 68 | + |
| 69 | + # Previous commit failed, so retry should pick up the backup commit |
| 70 | + # commit_mock = mocker.patch("commitizen.git.commit") |
| 71 | + commit_mock.return_value = cmd.Command("success", "", "", "") |
| 72 | + success_mock = mocker.patch("commitizen.out.success") |
| 73 | + |
| 74 | + commands.Commit(config, {"retry": True})() |
| 75 | + |
| 76 | + commit_mock.assert_called_with("feat: user created\n\ncloses #21") |
| 77 | + prompt_mock.assert_called_once() |
| 78 | + success_mock.assert_called_once() |
| 79 | + assert not os.path.isfile(temp_file) |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.usefixtures("staging_is_clean") |
| 83 | +def test_commit_command_with_dry_run_option(config, mocker): |
| 84 | + prompt_mock = mocker = mocker.patch("questionary.prompt") |
| 85 | + prompt_mock.return_value = { |
| 86 | + "prefix": "feat", |
| 87 | + "subject": "user created", |
| 88 | + "scope": "", |
| 89 | + "is_breaking_change": False, |
| 90 | + "body": "closes #57", |
| 91 | + "footer": "", |
| 92 | + } |
| 93 | + |
| 94 | + with pytest.raises(SystemExit): |
| 95 | + commit_cmd = commands.Commit(config, {"dry_run": True}) |
| 96 | + commit_cmd() |
| 97 | + |
| 98 | + |
| 99 | +def test_commit_when_nothing_to_commit(config, mocker): |
| 100 | + is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean") |
| 101 | + is_staging_clean_mock.return_value = True |
| 102 | + |
| 103 | + with pytest.raises(SystemExit) as err: |
| 104 | + commit_cmd = commands.Commit(config, {}) |
| 105 | + commit_cmd() |
| 106 | + |
| 107 | + assert err.value.code == commands.commit.NOTHING_TO_COMMIT |
| 108 | + |
| 109 | + |
| 110 | +@pytest.mark.usefixtures("staging_is_clean") |
| 111 | +def test_commit_when_customized_expected_raised(config, mocker, capsys): |
| 112 | + _err = ValueError() |
| 113 | + _err.__context__ = CzException("This is the root custom err") |
| 114 | + prompt_mock = mocker.patch("questionary.prompt") |
| 115 | + prompt_mock.side_effect = _err |
| 116 | + |
| 117 | + with pytest.raises(SystemExit) as err: |
| 118 | + commit_cmd = commands.Commit(config, {}) |
| 119 | + commit_cmd() |
| 120 | + |
| 121 | + assert err.value.code == commands.commit.CUSTOM_ERROR |
| 122 | + |
| 123 | + # Assert only the content in the formatted text |
| 124 | + captured = capsys.readouterr() |
| 125 | + assert "This is the root custom err" in captured.err |
0 commit comments