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