| 1 |
"""Tests for the diff module.""" |
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
import pathlib |
| 5 |
import pytest |
| 6 |
import typing as t |
| 7 |
|
| 8 |
if t.TYPE_CHECKING: # pragma: nocover |
| 9 |
# noinspection PyProtectedMember |
| 10 |
from ansible_test._internal.diff import FileDiff |
| 11 |
|
| 12 |
|
| 13 |
@pytest.fixture() |
| 14 |
def diffs(request: pytest.FixtureRequest) -> list[FileDiff]: |
| 15 |
"""A fixture which returns the parsed diff associated with the current test.""" |
| 16 |
return get_parsed_diff(request.node.name.removeprefix('test_')) |
| 17 |
|
| 18 |
|
| 19 |
def get_parsed_diff(name: str) -> list[FileDiff]: |
| 20 |
"""Parse and return the named git diff.""" |
| 21 |
cache = pathlib.Path(__file__).parent / 'diff' / f'{name}.diff' |
| 22 |
content = cache.read_text() |
| 23 |
lines = content.splitlines() |
| 24 |
|
| 25 |
assert lines |
| 26 |
|
| 27 |
# noinspection PyProtectedMember |
| 28 |
from ansible_test._internal.diff import parse_diff |
| 29 |
|
| 30 |
diffs = parse_diff(lines) |
| 31 |
|
| 32 |
assert diffs |
| 33 |
|
| 34 |
for item in diffs: |
| 35 |
assert item.headers |
| 36 |
assert item.is_complete |
| 37 |
|
| 38 |
item.old.format_lines() |
| 39 |
item.new.format_lines() |
| 40 |
|
| 41 |
for line_range in item.old.ranges: |
| 42 |
assert line_range[1] >= line_range[0] > 0 |
| 43 |
|
| 44 |
for line_range in item.new.ranges: |
| 45 |
assert line_range[1] >= line_range[0] > 0 |
| 46 |
|
| 47 |
return diffs |
| 48 |
|
| 49 |
|
| 50 |
def test_add_binary_file(diffs: list[FileDiff]) -> None: |
| 51 |
"""Add a binary file.""" |
| 52 |
assert len(diffs) == 1 |
| 53 |
|
| 54 |
assert diffs[0].old.exists |
| 55 |
assert diffs[0].new.exists |
| 56 |
|
| 57 |
assert diffs[0].old.path == 'binary.dat' |
| 58 |
assert diffs[0].new.path == 'binary.dat' |
| 59 |
|
| 60 |
assert diffs[0].old.eof_newline |
| 61 |
assert diffs[0].new.eof_newline |
| 62 |
|
| 63 |
|
| 64 |
def test_add_text_file(diffs: list[FileDiff]) -> None: |
| 65 |
"""Add a new file.""" |
| 66 |
assert len(diffs) == 1 |
| 67 |
|
| 68 |
assert not diffs[0].old.exists |
| 69 |
assert diffs[0].new.exists |
| 70 |
|
| 71 |
assert diffs[0].old.path == 'test.txt' |
| 72 |
assert diffs[0].new.path == 'test.txt' |
| 73 |
|
| 74 |
assert diffs[0].old.eof_newline |
| 75 |
assert diffs[0].new.eof_newline |
| 76 |
|
| 77 |
|
| 78 |
def test_remove_trailing_newline(diffs: list[FileDiff]) -> None: |
| 79 |
"""Remove the trailing newline from a file.""" |
| 80 |
assert len(diffs) == 1 |
| 81 |
|
| 82 |
assert diffs[0].old.exists |
| 83 |
assert diffs[0].new.exists |
| 84 |
|
| 85 |
assert diffs[0].old.path == 'test.txt' |
| 86 |
assert diffs[0].new.path == 'test.txt' |
| 87 |
|
| 88 |
assert diffs[0].old.eof_newline |
| 89 |
assert not diffs[0].new.eof_newline |
| 90 |
|
| 91 |
|
| 92 |
def test_add_trailing_newline(diffs: list[FileDiff]) -> None: |
| 93 |
"""Add a trailing newline to a file.""" |
| 94 |
assert len(diffs) == 1 |
| 95 |
|
| 96 |
assert diffs[0].old.exists |
| 97 |
assert diffs[0].new.exists |
| 98 |
|
| 99 |
assert diffs[0].old.path == 'test.txt' |
| 100 |
assert diffs[0].new.path == 'test.txt' |
| 101 |
|
| 102 |
assert not diffs[0].old.eof_newline |
| 103 |
assert diffs[0].new.eof_newline |
| 104 |
|
| 105 |
|
| 106 |
def test_add_two_text_files(diffs: list[FileDiff]) -> None: |
| 107 |
"""Add two text files.""" |
| 108 |
assert len(diffs) == 2 |
| 109 |
|
| 110 |
assert not diffs[0].old.exists |
| 111 |
assert diffs[0].new.exists |
| 112 |
|
| 113 |
assert diffs[0].old.path == 'one.txt' |
| 114 |
assert diffs[0].new.path == 'one.txt' |
| 115 |
|
| 116 |
assert diffs[0].old.eof_newline |
| 117 |
assert diffs[0].new.eof_newline |
| 118 |
|
| 119 |
assert not diffs[1].old.exists |
| 120 |
assert diffs[1].new.exists |
| 121 |
|
| 122 |
assert diffs[1].old.path == 'two.txt' |
| 123 |
assert diffs[1].new.path == 'two.txt' |
| 124 |
|
| 125 |
assert diffs[1].old.eof_newline |
| 126 |
assert diffs[1].new.eof_newline |
| 127 |
|
| 128 |
|
| 129 |
def test_context_no_trailing_newline(diffs: list[FileDiff]) -> None: |
| 130 |
"""Context without a trailing newline.""" |
| 131 |
assert len(diffs) == 1 |
| 132 |
|
| 133 |
assert diffs[0].old.exists |
| 134 |
assert diffs[0].new.exists |
| 135 |
|
| 136 |
assert diffs[0].old.path == 'test.txt' |
| 137 |
assert diffs[0].new.path == 'test.txt' |
| 138 |
|
| 139 |
assert not diffs[0].old.eof_newline |
| 140 |
assert not diffs[0].new.eof_newline |
| 141 |
|
| 142 |
|
| 143 |
def test_multiple_context_lines(diffs: list[FileDiff]) -> None: |
| 144 |
"""Multiple context lines.""" |
| 145 |
assert len(diffs) == 1 |
| 146 |
|
| 147 |
assert diffs[0].old.exists |
| 148 |
assert diffs[0].new.exists |
| 149 |
|
| 150 |
assert diffs[0].old.path == 'test.txt' |
| 151 |
assert diffs[0].new.path == 'test.txt' |
| 152 |
|
| 153 |
assert diffs[0].old.eof_newline |
| 154 |
assert diffs[0].new.eof_newline |
| 155 |
|
| 156 |
|
| 157 |
def test_parse_delete(diffs: list[FileDiff]) -> None: |
| 158 |
"""Delete files.""" |
| 159 |
assert len(diffs) == 1 |
| 160 |
|
| 161 |
assert diffs[0].old.exists |
| 162 |
assert not diffs[0].new.exists |
| 163 |
|
| 164 |
assert diffs[0].old.path == 'changelogs/fragments/79263-runme-sh-logging-3cb482385bd59058.yaml' |
| 165 |
assert diffs[0].new.path == 'changelogs/fragments/79263-runme-sh-logging-3cb482385bd59058.yaml' |
| 166 |
|
| 167 |
|
| 168 |
def test_parse_rename(diffs) -> None: |
| 169 |
"""Rename files.""" |
| 170 |
assert len(diffs) == 2 |
| 171 |
|
| 172 |
assert all(item.old.path != item.new.path and item.old.exists and item.new.exists for item in diffs) |
| 173 |
|
| 174 |
assert diffs[0].old.path == 'packaging/debian/ansible-base.dirs' |
| 175 |
assert diffs[0].new.path == 'packaging/debian/ansible-core.dirs' |
| 176 |
|
| 177 |
assert diffs[1].old.path == 'packaging/debian/ansible-base.install' |
| 178 |
assert diffs[1].new.path == 'packaging/debian/ansible-core.install' |