|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# *********************************COPYRIGHT************************************ |
| 3 | +# (C) Crown copyright Met Office. All rights reserved. |
| 4 | +# For further details please refer to the file COPYRIGHT.txt |
| 5 | +# which you should have received as part of this distribution. |
| 6 | +# *********************************COPYRIGHT************************************ |
| 7 | +""" |
| 8 | +Test suite for git_bdiff module. |
| 9 | +""" |
| 10 | + |
| 11 | +import os |
| 12 | +import subprocess |
| 13 | +import pytest |
| 14 | + |
| 15 | +from git_bdiff import GitBDiff, GitBDiffError, GitBDiffNotGit |
| 16 | + |
| 17 | + |
| 18 | +# Disable warnings caused by the use of pytest fixtures |
| 19 | +# pylint: disable=redefined-outer-name |
| 20 | + |
| 21 | + |
| 22 | +def add_to_repo(start, end, message, mode="wt"): |
| 23 | + """Add and commit dummy files to a repo.""" |
| 24 | + |
| 25 | + for i in range(start, end): |
| 26 | + with open(f"file{i}", mode, encoding="utf-8") as fd: |
| 27 | + print(f"Lorem ipsum dolor sit amet {i}", file=fd) |
| 28 | + |
| 29 | + subprocess.run(["git", "add", "-A"], check=True) |
| 30 | + subprocess.run(["git", "commit", "--no-gpg-sign", "-m", message], check=True) |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture(scope="session") |
| 34 | +def git_repo(tmpdir_factory): |
| 35 | + """Create and populate a test git repo.""" |
| 36 | + |
| 37 | + location = tmpdir_factory.mktemp("data") |
| 38 | + os.chdir(location) |
| 39 | + |
| 40 | + # Create the repo and add some files |
| 41 | + subprocess.run(["git", "init"], check=True) |
| 42 | + add_to_repo(0, 10, "Testing") |
| 43 | + |
| 44 | + # Create a branch and add some files |
| 45 | + subprocess.run(["git", "checkout", "-b", "mybranch"], check=True) |
| 46 | + add_to_repo(20, 30, "Commit to mybranch") |
| 47 | + |
| 48 | + # Create a branch-of-branch and add more files |
| 49 | + subprocess.run(["git", "checkout", "-b", "subbranch"], check=True) |
| 50 | + add_to_repo(40, 50, "Commit to subbranch") |
| 51 | + |
| 52 | + # Create a branch from main without any changes |
| 53 | + subprocess.run(["git", "checkout", "main"], check=True) |
| 54 | + subprocess.run(["git", "checkout", "-b", "unchanged"], check=True) |
| 55 | + |
| 56 | + # Create a branch from main and overwrite some things |
| 57 | + subprocess.run(["git", "checkout", "main"], check=True) |
| 58 | + subprocess.run(["git", "checkout", "-b", "overwrite"], check=True) |
| 59 | + add_to_repo(0, 10, "Overwriting", "at") |
| 60 | + |
| 61 | + # Switch back to the main branch ready for testing |
| 62 | + subprocess.run(["git", "checkout", "main"], check=True) |
| 63 | + |
| 64 | + return location |
| 65 | + |
| 66 | + |
| 67 | +def test_init(git_repo): |
| 68 | + """Test creation of a new GitBDiff instance""" |
| 69 | + |
| 70 | + os.chdir(git_repo) |
| 71 | + bdiff = GitBDiff() |
| 72 | + |
| 73 | + assert bdiff.branch is not None |
| 74 | + assert bdiff.branch == "main" |
| 75 | + assert not bdiff.is_branch |
| 76 | + assert not bdiff.has_diverged |
| 77 | + |
| 78 | + |
| 79 | +def test_repo_selection(git_repo): |
| 80 | + """Test selection of repository directory.""" |
| 81 | + |
| 82 | + os.chdir("/") |
| 83 | + bdiff = GitBDiff(repo=git_repo) |
| 84 | + |
| 85 | + assert bdiff.branch is not None |
| 86 | + assert bdiff.branch == "main" |
| 87 | + assert not bdiff.is_branch |
| 88 | + assert not bdiff.has_diverged |
| 89 | + |
| 90 | + |
| 91 | +def test_invalid_repo_selection(git_repo): |
| 92 | + """Test non-existent repo or plain file raises an error""" |
| 93 | + |
| 94 | + with pytest.raises(GitBDiffError): |
| 95 | + GitBDiff(repo="/nosuch") |
| 96 | + |
| 97 | + with pytest.raises(GitBDiffError): |
| 98 | + GitBDiff(repo="/etc/hosts") |
| 99 | + |
| 100 | + |
| 101 | +def test_branch_diff(git_repo): |
| 102 | + """Test a simple branch diff.""" |
| 103 | + |
| 104 | + os.chdir(git_repo) |
| 105 | + subprocess.run(["git", "checkout", "mybranch"], check=True) |
| 106 | + |
| 107 | + try: |
| 108 | + bdiff = GitBDiff() |
| 109 | + changes = list(bdiff.files()) |
| 110 | + finally: |
| 111 | + subprocess.run(["git", "checkout", "main"], check=True) |
| 112 | + |
| 113 | + assert bdiff.branch == "mybranch" |
| 114 | + assert bdiff.is_branch |
| 115 | + assert bdiff.has_diverged |
| 116 | + assert len(changes) == 10 |
| 117 | + assert changes[0] == "file20" |
| 118 | + |
| 119 | + |
| 120 | +def test_branch_of_branch_diff(git_repo): |
| 121 | + """Test a branch of branch diff. |
| 122 | +
|
| 123 | + This effectively tests whether all the commits since the branch |
| 124 | + point with main are picked up correctly. |
| 125 | + """ |
| 126 | + |
| 127 | + os.chdir(git_repo) |
| 128 | + subprocess.run(["git", "checkout", "subbranch"], check=True) |
| 129 | + |
| 130 | + try: |
| 131 | + bdiff = GitBDiff() |
| 132 | + changes = list(bdiff.files()) |
| 133 | + finally: |
| 134 | + subprocess.run(["git", "checkout", "main"], check=True) |
| 135 | + |
| 136 | + assert bdiff.branch == "subbranch" |
| 137 | + assert bdiff.is_branch |
| 138 | + assert bdiff.has_diverged |
| 139 | + assert len(changes) == 20 |
| 140 | + assert changes[0] == "file20" |
| 141 | + assert changes[-1] == "file49" |
| 142 | + |
| 143 | + |
| 144 | +def test_overwritten_branch(git_repo): |
| 145 | + """Test a diff of a branch with changed files.""" |
| 146 | + |
| 147 | + os.chdir(git_repo) |
| 148 | + subprocess.run(["git", "checkout", "overwrite"], check=True) |
| 149 | + try: |
| 150 | + bdiff = GitBDiff() |
| 151 | + changes = list(bdiff.files()) |
| 152 | + finally: |
| 153 | + subprocess.run(["git", "checkout", "main"], check=True) |
| 154 | + |
| 155 | + assert bdiff.branch == "overwrite" |
| 156 | + assert bdiff.is_branch |
| 157 | + assert bdiff.has_diverged |
| 158 | + assert len(changes) == 10 |
| 159 | + |
| 160 | + |
| 161 | +def test_unchanged_branch(git_repo): |
| 162 | + """Test a branch with no commits.""" |
| 163 | + |
| 164 | + os.chdir(git_repo) |
| 165 | + subprocess.run(["git", "checkout", "unchanged"], check=True) |
| 166 | + |
| 167 | + try: |
| 168 | + bdiff = GitBDiff() |
| 169 | + changes = list(bdiff.files()) |
| 170 | + finally: |
| 171 | + subprocess.run(["git", "checkout", "main"], check=True) |
| 172 | + |
| 173 | + assert bdiff.branch == "unchanged" |
| 174 | + assert bdiff.is_branch |
| 175 | + assert not bdiff.has_diverged |
| 176 | + assert not changes |
| 177 | + |
| 178 | + |
| 179 | +def test_non_repo(tmpdir): |
| 180 | + """Test exception if working directory is not a git repo.""" |
| 181 | + |
| 182 | + os.chdir(tmpdir) |
| 183 | + |
| 184 | + with pytest.raises(GitBDiffNotGit) as exc: |
| 185 | + GitBDiff() |
| 186 | + assert "not a repository" in str(exc.value) |
| 187 | + |
| 188 | + |
| 189 | +def test_nonexistent_parent(git_repo): |
| 190 | + """Test exception if parent branch does not exist. |
| 191 | +
|
| 192 | + This is a proxy test for the detection of all sorts of git |
| 193 | + errors. |
| 194 | + """ |
| 195 | + |
| 196 | + os.chdir(git_repo) |
| 197 | + |
| 198 | + with pytest.raises(GitBDiffError) as exc: |
| 199 | + GitBDiff(parent="nosuch") |
| 200 | + assert "Not a valid object name nosuch" in str(exc.value) |
| 201 | + |
| 202 | + |
| 203 | +def test_git_run(git_repo): |
| 204 | + """Test git interface and error handling.""" |
| 205 | + |
| 206 | + bdiff = GitBDiff() |
| 207 | + |
| 208 | + with pytest.raises(TypeError) as exc: |
| 209 | + # Use a string in place of a list |
| 210 | + list(i for i in bdiff.run_git("commit -m ''")) |
| 211 | + assert "args must be a list" in str(exc.value) |
| 212 | + |
| 213 | + with pytest.raises(GitBDiffError) as exc: |
| 214 | + # Run a command that should return non-zero |
| 215 | + list(i for i in bdiff.run_git(["commit", "-m", "''"])) |
| 216 | + assert "command returned 1" in str(exc.value) |
0 commit comments