mirror of https://github.com/ansible/ansible.git
Relocate and refactor ansible-test diff unit tests (#80113)
This provides the same test coverage as the previous tests, without the dependency on git. It also includes many more specific test cases with assertions, instead of simply relying on the code to not raise an exception.pull/80114/head
parent
67bafafbc0
commit
c2ea56e12e
@ -1,13 +0,0 @@
|
||||
all: sanity unit validate-modules-unit
|
||||
|
||||
.PHONY: sanity
|
||||
sanity:
|
||||
$(abspath ${CURDIR}/../../bin/ansible-test) sanity test/lib/ ${FLAGS}
|
||||
|
||||
.PHONY: unit
|
||||
unit:
|
||||
PYTHONPATH=$(abspath ${CURDIR}/../lib) pytest unit ${FLAGS}
|
||||
|
||||
.PHONY: validate-modules-unit
|
||||
validate-modules-unit:
|
||||
PYTHONPATH=$(abspath ${CURDIR}/../lib/ansible_test/_util/controller/sanity/validate-modules):$(abspath ${CURDIR}/../../lib) pytest validate-modules-unit ${FLAGS}
|
@ -1,100 +0,0 @@
|
||||
"""Tests for diff module."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import pytest
|
||||
|
||||
from ansible_test._internal.util import (
|
||||
to_text,
|
||||
to_bytes,
|
||||
)
|
||||
|
||||
from ansible_test._internal.diff import (
|
||||
parse_diff,
|
||||
FileDiff,
|
||||
)
|
||||
|
||||
|
||||
def get_diff(base, head=None):
|
||||
"""Return a git diff between the base and head revision.
|
||||
:type base: str
|
||||
:type head: str | None
|
||||
:rtype: list[str]
|
||||
"""
|
||||
if not head or head == 'HEAD':
|
||||
head = to_text(subprocess.check_output(['git', 'rev-parse', 'HEAD'])).strip()
|
||||
|
||||
cache = '/tmp/git-diff-cache-%s-%s.log' % (base, head)
|
||||
|
||||
if os.path.exists(cache):
|
||||
with open(cache, 'rb') as cache_fd:
|
||||
lines = to_text(cache_fd.read()).splitlines()
|
||||
else:
|
||||
lines = to_text(subprocess.check_output(['git', 'diff', base, head]), errors='replace').splitlines()
|
||||
|
||||
with open(cache, 'wb') as cache_fd:
|
||||
cache_fd.write(to_bytes('\n'.join(lines)))
|
||||
|
||||
assert lines
|
||||
|
||||
return lines
|
||||
|
||||
|
||||
def get_parsed_diff(base: str, head: str | None = None) -> list[FileDiff]:
|
||||
"""Return a parsed git diff between the base and head revision."""
|
||||
lines = get_diff(base, head)
|
||||
items = parse_diff(lines)
|
||||
|
||||
assert items
|
||||
|
||||
for item in items:
|
||||
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 items
|
||||
|
||||
|
||||
RANGES_TO_TEST = (
|
||||
('f31421576b00f0b167cdbe61217c31c21a41ac02', 'HEAD'),
|
||||
('b8125ac1a61f2c7d1de821c78c884560071895f1', '32146acf4e43e6f95f54d9179bf01f0df9814217')
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("base, head", RANGES_TO_TEST)
|
||||
def test_parse_diff(base, head):
|
||||
"""Integration test to verify parsing of ansible/ansible history."""
|
||||
get_parsed_diff(base, head)
|
||||
|
||||
|
||||
def test_parse_delete():
|
||||
"""Integration test to verify parsing of a deleted file."""
|
||||
commit = 'ee17b914554861470b382e9e80a8e934063e0860'
|
||||
items = get_parsed_diff(commit + '~', commit)
|
||||
deletes = [item for item in items if not item.new.exists]
|
||||
|
||||
assert len(deletes) == 1
|
||||
assert deletes[0].old.path == 'lib/ansible/plugins/connection/nspawn.py'
|
||||
assert deletes[0].new.path == 'lib/ansible/plugins/connection/nspawn.py'
|
||||
|
||||
|
||||
def test_parse_rename():
|
||||
"""Integration test to verify parsing of renamed files."""
|
||||
commit = '16a39639f568f4dd5cb233df2d0631bdab3a05e9'
|
||||
items = get_parsed_diff(commit + '~', commit)
|
||||
renames = [item for item in items if item.old.path != item.new.path and item.old.exists and item.new.exists]
|
||||
|
||||
assert len(renames) == 2
|
||||
assert renames[0].old.path == 'test/integration/targets/eos_eapi/tests/cli/badtransport.yaml'
|
||||
assert renames[0].new.path == 'test/integration/targets/eos_eapi/tests/cli/badtransport.1'
|
||||
assert renames[1].old.path == 'test/integration/targets/eos_eapi/tests/cli/zzz_reset.yaml'
|
||||
assert renames[1].new.path == 'test/integration/targets/eos_eapi/tests/cli/zzz_reset.1'
|
@ -0,0 +1,4 @@
|
||||
diff --git a/binary.dat b/binary.dat
|
||||
new file mode 100644
|
||||
index 0000000000..f76dd238ad
|
||||
Binary files /dev/null and b/binary.dat differ
|
@ -0,0 +1,8 @@
|
||||
diff --git a/test.txt b/test.txt
|
||||
new file mode 100644
|
||||
index 0000000000..814f4a4229
|
||||
--- /dev/null
|
||||
+++ b/test.txt
|
||||
@@ -0,0 +1,2 @@
|
||||
+one
|
||||
+two
|
@ -0,0 +1,9 @@
|
||||
diff --git a/test.txt b/test.txt
|
||||
index 9ed40b4425..814f4a4229 100644
|
||||
--- a/test.txt
|
||||
+++ b/test.txt
|
||||
@@ -1,2 +1,2 @@
|
||||
one
|
||||
-two
|
||||
\ No newline at end of file
|
||||
+two
|
@ -0,0 +1,16 @@
|
||||
diff --git a/one.txt b/one.txt
|
||||
new file mode 100644
|
||||
index 0000000000..99b976670b
|
||||
--- /dev/null
|
||||
+++ b/one.txt
|
||||
@@ -0,0 +1,2 @@
|
||||
+One
|
||||
+1
|
||||
diff --git a/two.txt b/two.txt
|
||||
new file mode 100644
|
||||
index 0000000000..da06cc0974
|
||||
--- /dev/null
|
||||
+++ b/two.txt
|
||||
@@ -0,0 +1,2 @@
|
||||
+Two
|
||||
+2
|
@ -0,0 +1,8 @@
|
||||
diff --git a/test.txt b/test.txt
|
||||
index 9ed40b4425..64c5e5885a 100644
|
||||
--- a/test.txt
|
||||
+++ b/test.txt
|
||||
@@ -1,2 +1 @@
|
||||
-one
|
||||
two
|
||||
\ No newline at end of file
|
@ -0,0 +1,10 @@
|
||||
diff --git a/test.txt b/test.txt
|
||||
index 949a655cb3..08c59a7cf1 100644
|
||||
--- a/test.txt
|
||||
+++ b/test.txt
|
||||
@@ -1,5 +1,3 @@
|
||||
One
|
||||
-Two
|
||||
Three
|
||||
-Four
|
||||
Five
|
@ -0,0 +1,16 @@
|
||||
diff --git a/changelogs/fragments/79263-runme-sh-logging-3cb482385bd59058.yaml b/changelogs/fragments/79263-runme-sh-logging-3cb482385bd59058.yaml
|
||||
deleted file mode 100644
|
||||
index a5bc88ffe3..0000000000
|
||||
--- a/changelogs/fragments/79263-runme-sh-logging-3cb482385bd59058.yaml
|
||||
+++ /dev/null
|
||||
@@ -1,10 +0,0 @@
|
||||
----
|
||||
-
|
||||
-trivial:
|
||||
- - >-
|
||||
- integration tests — added command invocation logging via ``set -x``
|
||||
- to ``runme.sh`` scripts where it was missing and improved failing
|
||||
- fast in those scripts that use pipes (via ``set -o pipefail``).
|
||||
- See `PR #79263` https://github.com/ansible/ansible/pull/79263>`__.
|
||||
-
|
||||
-...
|
@ -0,0 +1,8 @@
|
||||
diff --git a/packaging/debian/ansible-base.dirs b/packaging/debian/ansible-core.dirs
|
||||
similarity index 100%
|
||||
rename from packaging/debian/ansible-base.dirs
|
||||
rename to packaging/debian/ansible-core.dirs
|
||||
diff --git a/packaging/debian/ansible-base.install b/packaging/debian/ansible-core.install
|
||||
similarity index 100%
|
||||
rename from packaging/debian/ansible-base.install
|
||||
rename to packaging/debian/ansible-core.install
|
@ -0,0 +1,9 @@
|
||||
diff --git a/test.txt b/test.txt
|
||||
index 814f4a4229..9ed40b4425 100644
|
||||
--- a/test.txt
|
||||
+++ b/test.txt
|
||||
@@ -1,2 +1,2 @@
|
||||
one
|
||||
-two
|
||||
+two
|
||||
\ No newline at end of file
|
@ -0,0 +1,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'
|
Loading…
Reference in New Issue