[diffoscope] 01/05: Avoid more DRY violations by factoring-out non-existing file tests.
Chris Lamb
chris at chris-lamb.co.uk
Sun Aug 21 00:51:07 CEST 2016
This is an automated email from the git hooks/post-receive script.
lamby pushed a commit to branch master
in repository diffoscope.
commit b3501ec9d751f860dabec8382104d2bfdb23f52d
Author: Chris Lamb <lamby at debian.org>
Date: Sat Aug 20 23:24:59 2016 +0100
Avoid more DRY violations by factoring-out non-existing file tests.
Signed-off-by: Chris Lamb <lamby at debian.org>
---
tests/comparators/test_bzip2.py | 13 +++++--------
tests/comparators/test_cbfs.py | 10 +++-------
tests/comparators/test_cpio.py | 10 +++-------
tests/comparators/test_debian.py | 8 ++------
tests/comparators/test_git.py | 1 -
tests/comparators/test_pdf.py | 10 +++-------
tests/comparators/test_png.py | 10 +++-------
tests/comparators/test_ppu.py | 10 +++-------
tests/comparators/test_ps.py | 11 +++--------
tests/comparators/test_rlib.py | 11 +++--------
tests/comparators/test_rpm.py | 11 +++--------
tests/comparators/test_sqlite.py | 10 +++-------
tests/comparators/test_squashfs.py | 10 +++-------
tests/comparators/test_tar.py | 9 +++------
tests/comparators/test_text.py | 9 +++------
tests/comparators/test_xz.py | 11 ++++-------
tests/comparators/test_zip.py | 16 ++++------------
tests/comparators/utils.py | 12 +++++++++++-
18 files changed, 62 insertions(+), 120 deletions(-)
diff --git a/tests/comparators/test_bzip2.py b/tests/comparators/test_bzip2.py
index 706efa7..75356b4 100644
--- a/tests/comparators/test_bzip2.py
+++ b/tests/comparators/test_bzip2.py
@@ -20,17 +20,17 @@
import shutil
import pytest
-from diffoscope.config import Config
from diffoscope.comparators import specialize
from diffoscope.comparators.bzip2 import Bzip2File
-from diffoscope.comparators.binary import FilesystemFile, NonExistingFile
+from diffoscope.comparators.binary import FilesystemFile
+
+from utils import skip_unless_tool_exists, data, load_fixture, \
+ assert_non_existing
-from utils import skip_unless_tool_exists, data, load_fixture
TEST_FILE1_PATH = data('test1.bz2')
TEST_FILE2_PATH = data('test2.bz2')
-
bzip1 = load_fixture(TEST_FILE1_PATH)
bzip2 = load_fixture(TEST_FILE2_PATH)
@@ -69,7 +69,4 @@ def test_content_diff(differences):
@skip_unless_tool_exists('bzip2')
def test_compare_non_existing(monkeypatch, bzip1):
- monkeypatch.setattr(Config, 'new_file', True)
- difference = bzip1.compare(NonExistingFile('/nonexisting', bzip1))
- assert difference.source2 == '/nonexisting'
- assert difference.details[-1].source2 == '/dev/null'
+ assert_non_existing(monkeypatch, bzip1)
diff --git a/tests/comparators/test_cbfs.py b/tests/comparators/test_cbfs.py
index 4ec06b2..24d676d 100644
--- a/tests/comparators/test_cbfs.py
+++ b/tests/comparators/test_cbfs.py
@@ -21,13 +21,12 @@ import struct
import pytest
import subprocess
-from diffoscope.config import Config
from diffoscope.comparators import specialize
from diffoscope.presenters.text import output_text
from diffoscope.comparators.cbfs import CbfsFile
-from diffoscope.comparators.binary import FilesystemFile, NonExistingFile
+from diffoscope.comparators.binary import FilesystemFile
-from utils import skip_unless_tool_exists, data
+from utils import skip_unless_tool_exists, data, assert_non_existing
TEST_FILE1_PATH = data('text_ascii1')
TEST_FILE2_PATH = data('text_ascii2')
@@ -88,7 +87,4 @@ def test_content(differences):
@skip_unless_tool_exists('cbfstool')
def test_compare_non_existing(monkeypatch, rom1):
- monkeypatch.setattr(Config.general, 'new_file', True)
- difference = rom1.compare(NonExistingFile('/nonexisting', rom1))
- assert difference.source2 == '/nonexisting'
- assert difference.details[-1].source2 == '/dev/null'
+ assert_non_existing(monkeypatch, rom1)
diff --git a/tests/comparators/test_cpio.py b/tests/comparators/test_cpio.py
index 3415d43..761533e 100644
--- a/tests/comparators/test_cpio.py
+++ b/tests/comparators/test_cpio.py
@@ -19,11 +19,10 @@
import pytest
-from diffoscope.config import Config
from diffoscope.comparators.cpio import CpioFile
-from diffoscope.comparators.binary import NonExistingFile
-from utils import skip_unless_tool_exists, data, load_fixture
+from utils import skip_unless_tool_exists, data, load_fixture, \
+ assert_non_existing
cpio1 = load_fixture(data('test1.cpio'))
@@ -61,7 +60,4 @@ def test_compressed_files(differences):
@skip_unless_tool_exists('cpio')
def test_compare_non_existing(monkeypatch, cpio1):
- monkeypatch.setattr(Config.general, 'new_file', True)
- difference = cpio1.compare(NonExistingFile('/nonexisting', cpio1))
- assert difference.source2 == '/nonexisting'
- assert difference.details[-1].source2 == '/dev/null'
+ assert_non_existing(monkeypatch, cpio1)
diff --git a/tests/comparators/test_debian.py b/tests/comparators/test_debian.py
index acb9868..2b08194 100644
--- a/tests/comparators/test_debian.py
+++ b/tests/comparators/test_debian.py
@@ -25,7 +25,7 @@ from diffoscope.comparators import specialize
from diffoscope.presenters.text import output_text
from diffoscope.comparators.binary import FilesystemFile, NonExistingFile
-from utils import data
+from utils import data, assert_non_existing
try:
from diffoscope.comparators.debian import DotChangesFile, DotDscFile, \
@@ -200,8 +200,4 @@ def test_dot_buildinfo_internal_diff(dot_buildinfo_differences):
@pytest.mark.skipif(miss_debian_module, reason='debian module is not installed')
def test_dot_buildinfo_compare_non_existing(monkeypatch, dot_buildinfo1):
- monkeypatch.setattr(Config.general, 'new_file', True)
- difference = dot_buildinfo1.compare(NonExistingFile('/nonexisting', dot_buildinfo1))
- output_text(difference, print_func=print)
- assert difference.source2 == '/nonexisting'
- assert difference.details[-1].source2 == '/dev/null'
+ assert_non_existing(monkeypatch, dot_buildinfo1)
diff --git a/tests/comparators/test_git.py b/tests/comparators/test_git.py
index 2939bae..4805e63 100644
--- a/tests/comparators/test_git.py
+++ b/tests/comparators/test_git.py
@@ -20,7 +20,6 @@
import pytest
from diffoscope.comparators.git import GitIndexFile
-from diffoscope.comparators.binary import FilesystemFile
from utils import data, load_fixture
diff --git a/tests/comparators/test_pdf.py b/tests/comparators/test_pdf.py
index 7e49d6c..8ba8161 100644
--- a/tests/comparators/test_pdf.py
+++ b/tests/comparators/test_pdf.py
@@ -19,11 +19,10 @@
import pytest
-from diffoscope.config import Config
from diffoscope.comparators.pdf import PdfFile
-from diffoscope.comparators.binary import NonExistingFile
-from utils import skip_unless_tool_exists, data, load_fixture
+from utils import skip_unless_tool_exists, data, load_fixture, \
+ assert_non_existing
pdf1 = load_fixture(data('test1.pdf'))
pdf2 = load_fixture(data('test2.pdf'))
@@ -51,7 +50,4 @@ def test_internal_diff(differences):
@skip_unless_tool_exists('pdftk', 'pdftotext')
def test_compare_non_existing(monkeypatch, pdf1):
- monkeypatch.setattr(Config, 'new_file', True)
- difference = pdf1.compare(NonExistingFile('/nonexisting', pdf1))
- assert difference.source2 == '/nonexisting'
- assert len(difference.details) > 0
+ assert_non_existing(monkeypatch, pdf1, has_null_source=False)
diff --git a/tests/comparators/test_png.py b/tests/comparators/test_png.py
index 2c6e1ea..5fb4781 100644
--- a/tests/comparators/test_png.py
+++ b/tests/comparators/test_png.py
@@ -19,11 +19,10 @@
import pytest
-from diffoscope.config import Config
from diffoscope.comparators.png import PngFile
-from diffoscope.comparators.binary import FilesystemFile, NonExistingFile
-from utils import skip_unless_tool_exists, data, load_fixture
+from utils import skip_unless_tool_exists, data, load_fixture, \
+ assert_non_existing
png1 = load_fixture(data('test1.png'))
png2 = load_fixture(data('test2.png'))
@@ -46,7 +45,4 @@ def test_diff(differences):
@skip_unless_tool_exists('sng')
def test_compare_non_existing(monkeypatch, png1):
- monkeypatch.setattr(Config, 'new_file', True)
- difference = png1.compare(NonExistingFile('/nonexisting', png1))
- assert difference.source2 == '/nonexisting'
- assert len(difference.details) > 0
+ assert_non_existing(monkeypatch, png1, has_null_source=False)
diff --git a/tests/comparators/test_ppu.py b/tests/comparators/test_ppu.py
index 8ef5896..a3e4766 100644
--- a/tests/comparators/test_ppu.py
+++ b/tests/comparators/test_ppu.py
@@ -20,11 +20,10 @@
import pytest
-from diffoscope.config import Config
from diffoscope.comparators.ppu import PpuFile
-from diffoscope.comparators.binary import NonExistingFile
-from utils import skip_unless_tool_exists, data, load_fixture
+from utils import skip_unless_tool_exists, data, load_fixture, \
+ assert_non_existing
# These test files were taken from two different builds of the Debian package
# fp-units-castle-game-engine (version 5.1.1-2 on amd64) on the Debian
@@ -55,7 +54,4 @@ def test_diff(differences):
@skip_unless_tool_exists('ppudump')
def test_compare_non_existing(monkeypatch, file1):
- monkeypatch.setattr(Config, 'new_file', True)
- difference = file1.compare(NonExistingFile('/nonexisting', file1))
- assert difference.source2 == '/nonexisting'
- assert len(difference.details) > 0
+ assert_non_existing(monkeypatch, file1, has_null_source=False)
diff --git a/tests/comparators/test_ps.py b/tests/comparators/test_ps.py
index 804532c..7006abe 100644
--- a/tests/comparators/test_ps.py
+++ b/tests/comparators/test_ps.py
@@ -19,12 +19,10 @@
import pytest
-from diffoscope.config import Config
-from diffoscope.comparators import specialize
from diffoscope.comparators.ps import PsFile
-from diffoscope.comparators.binary import NonExistingFile
-from utils import skip_unless_tool_exists, data, load_fixture
+from utils import skip_unless_tool_exists, data, load_fixture, \
+ assert_non_existing
ps1 = load_fixture(data('test1.ps'))
ps2 = load_fixture(data('test2.ps'))
@@ -52,7 +50,4 @@ def test_text_diff(differences):
@skip_unless_tool_exists('ps2ascii')
def test_compare_non_existing(monkeypatch, ps1):
- monkeypatch.setattr(Config, 'new_file', True)
- difference = ps1.compare(NonExistingFile('/nonexisting', ps1))
- assert difference.source2 == '/nonexisting'
- assert len(difference.details) > 0
+ assert_non_existing(monkeypatch, ps1, has_null_source=False)
diff --git a/tests/comparators/test_rlib.py b/tests/comparators/test_rlib.py
index 8a3a9c9..4ae5735 100644
--- a/tests/comparators/test_rlib.py
+++ b/tests/comparators/test_rlib.py
@@ -20,13 +20,11 @@
import pytest
-from diffoscope.config import Config
-from diffoscope.comparators import specialize
from diffoscope.comparators.ar import ArFile
from diffoscope.comparators.utils import diff_ignore_line_numbers
-from diffoscope.comparators.binary import FilesystemFile, NonExistingFile
-from utils import skip_unless_tool_exists, tool_older_than, data, load_fixture
+from utils import skip_unless_tool_exists, tool_older_than, data, \
+ load_fixture, assert_non_existing
rlib1 = load_fixture(data('test1.rlib'))
rlib2 = load_fixture(data('test2.rlib'))
@@ -71,7 +69,4 @@ def test_item3_deflate_llvm_bitcode(differences):
assert diff_ignore_line_numbers(actual_diff) == diff_ignore_line_numbers(expected_diff)
def test_compare_non_existing(monkeypatch, rlib1):
- monkeypatch.setattr(Config.general, 'new_file', True)
- difference = rlib1.compare(NonExistingFile('/nonexisting', rlib1))
- assert difference.source2 == '/nonexisting'
- assert difference.details[-1].source2 == '/dev/null'
+ assert_non_existing(monkeypatch, rlib1)
diff --git a/tests/comparators/test_rpm.py b/tests/comparators/test_rpm.py
index 36bca5e..74032b4 100644
--- a/tests/comparators/test_rpm.py
+++ b/tests/comparators/test_rpm.py
@@ -19,10 +19,8 @@
import pytest
-from diffoscope.config import Config
-from diffoscope.comparators.binary import NonExistingFile
-
-from utils import skip_unless_tool_exists, data, load_fixture
+from utils import skip_unless_tool_exists, data, load_fixture, \
+ assert_non_existing
try:
from diffoscope.comparators.rpm import RpmFile
@@ -72,7 +70,4 @@ def test_content(differences):
@pytest.mark.skipif(miss_rpm_module, reason='rpm module is not installed')
@skip_unless_tool_exists('rpm2cpio')
def test_compare_non_existing(monkeypatch, rpm1):
- monkeypatch.setattr(Config.general, 'new_file', True)
- difference = rpm1.compare(NonExistingFile('/nonexisting', rpm1))
- assert difference.source2 == '/nonexisting'
- assert difference.details[-1].source2 == '/dev/null'
+ assert_non_existing(monkeypatch, rpm1)
diff --git a/tests/comparators/test_sqlite.py b/tests/comparators/test_sqlite.py
index 5dabc56..a185bd0 100644
--- a/tests/comparators/test_sqlite.py
+++ b/tests/comparators/test_sqlite.py
@@ -19,11 +19,10 @@
import pytest
-from diffoscope.config import Config
-from diffoscope.comparators.binary import NonExistingFile
from diffoscope.comparators.sqlite import Sqlite3Database
-from utils import skip_unless_tool_exists, data, load_fixture
+from utils import skip_unless_tool_exists, data, load_fixture, \
+ assert_non_existing
sqlite3db1 = load_fixture(data('test1.sqlite3'))
sqlite3db2 = load_fixture(data('test2.sqlite3'))
@@ -46,7 +45,4 @@ def test_diff(differences):
@skip_unless_tool_exists('sqlite3')
def test_compare_non_existing(monkeypatch, sqlite3db1):
- monkeypatch.setattr(Config.general, 'new_file', True)
- difference = sqlite3db1.compare(NonExistingFile('/nonexisting', sqlite3db1))
- assert difference.source2 == '/nonexisting'
- assert len(difference.details) > 0
+ assert_non_existing(monkeypatch, sqlite3db1, has_null_source=False)
diff --git a/tests/comparators/test_squashfs.py b/tests/comparators/test_squashfs.py
index ce054c1..41c1c85 100644
--- a/tests/comparators/test_squashfs.py
+++ b/tests/comparators/test_squashfs.py
@@ -19,11 +19,10 @@
import pytest
-from diffoscope.config import Config
-from diffoscope.comparators.binary import NonExistingFile
from diffoscope.comparators.squashfs import SquashfsFile
-from utils import skip_unless_tool_exists, data, load_fixture
+from utils import skip_unless_tool_exists, data, load_fixture, \
+ assert_non_existing
squashfs1 = load_fixture(data('test1.squashfs'))
squashfs2 = load_fixture(data('test2.squashfs'))
@@ -64,7 +63,4 @@ def test_compressed_files(differences):
@skip_unless_tool_exists('unsquashfs')
def test_compare_non_existing(monkeypatch, squashfs1):
- monkeypatch.setattr(Config.general, 'new_file', True)
- difference = squashfs1.compare(NonExistingFile('/nonexisting', squashfs1))
- assert difference.source2 == '/nonexisting'
- assert difference.details[-1].source2 == '/dev/null'
+ assert_non_existing(monkeypatch, squashfs1)
diff --git a/tests/comparators/test_tar.py b/tests/comparators/test_tar.py
index 4d6d176..0b205aa 100644
--- a/tests/comparators/test_tar.py
+++ b/tests/comparators/test_tar.py
@@ -21,9 +21,9 @@ import pytest
from diffoscope.config import Config
from diffoscope.comparators.tar import TarFile
-from diffoscope.comparators.binary import FilesystemFile, NonExistingFile
+from diffoscope.comparators.binary import NonExistingFile
-from utils import data, load_fixture
+from utils import data, load_fixture, assert_non_existing
tar1 = load_fixture(data('test1.tar'))
tar2 = load_fixture(data('test2.tar'))
@@ -57,10 +57,7 @@ def test_text_file(differences):
assert differences[1].unified_diff == expected_diff
def test_compare_non_existing(monkeypatch, tar1):
- monkeypatch.setattr(Config.general, 'new_file', True)
- difference = tar1.compare(NonExistingFile('/nonexisting', tar1))
- assert difference.source2 == '/nonexisting'
- assert difference.details[-1].source2 == '/dev/null'
+ assert_non_existing(monkeypatch, tar1)
no_permissions_tar = load_fixture(data('no-perms.tar'))
diff --git a/tests/comparators/test_text.py b/tests/comparators/test_text.py
index 23d63ba..5718311 100644
--- a/tests/comparators/test_text.py
+++ b/tests/comparators/test_text.py
@@ -20,11 +20,10 @@
import codecs
import pytest
-from diffoscope.config import Config
from diffoscope.comparators import specialize
-from diffoscope.comparators.binary import FilesystemFile, NonExistingFile
+from diffoscope.comparators.binary import FilesystemFile
-from utils import data, load_fixture
+from utils import data, load_fixture, assert_non_existing
ascii1 = load_fixture(data('text_ascii1'))
ascii2 = load_fixture(data('text_ascii2'))
@@ -66,6 +65,4 @@ def test_difference_between_iso88591_and_unicode_only(iso8859, tmpdir):
assert difference.details[0].source1 == 'encoding'
def test_compare_non_existing(monkeypatch, ascii1):
- monkeypatch.setattr(Config.general, 'new_file', True)
- difference = ascii1.compare(NonExistingFile('/nonexisting', ascii1))
- assert difference.source2 == '/nonexisting'
+ assert_non_existing(monkeypatch, ascii1, has_null_source=False, has_details=False)
diff --git a/tests/comparators/test_xz.py b/tests/comparators/test_xz.py
index 1a2223c..b84f8bf 100644
--- a/tests/comparators/test_xz.py
+++ b/tests/comparators/test_xz.py
@@ -20,12 +20,12 @@
import shutil
import pytest
-from diffoscope.config import Config
from diffoscope.comparators import specialize
from diffoscope.comparators.xz import XzFile
-from diffoscope.comparators.binary import FilesystemFile, NonExistingFile
+from diffoscope.comparators.binary import FilesystemFile
-from utils import skip_unless_tool_exists, data, load_fixture
+from utils import skip_unless_tool_exists, data, load_fixture, \
+ assert_non_existing
TEST_FILE1_PATH = data('test1.xz')
TEST_FILE2_PATH = data('test2.xz')
@@ -68,7 +68,4 @@ def test_content_diff(differences):
@skip_unless_tool_exists('xz')
def test_compare_non_existing(monkeypatch, xz1):
- monkeypatch.setattr(Config.general, 'new_file', True)
- difference = xz1.compare(NonExistingFile('/nonexisting', xz1))
- assert difference.source2 == '/nonexisting'
- assert difference.details[-1].source2 == '/dev/null'
+ assert_non_existing(monkeypatch, xz1)
diff --git a/tests/comparators/test_zip.py b/tests/comparators/test_zip.py
index 4428b61..bd8b26b 100644
--- a/tests/comparators/test_zip.py
+++ b/tests/comparators/test_zip.py
@@ -19,12 +19,10 @@
import pytest
-from diffoscope.config import Config
-from diffoscope.comparators import specialize
from diffoscope.comparators.zip import ZipFile, MozillaZipFile
-from diffoscope.comparators.binary import FilesystemFile, NonExistingFile
-from utils import skip_unless_tool_exists, data, load_fixture
+from utils import skip_unless_tool_exists, data, load_fixture, \
+ assert_non_existing
zip1 = load_fixture(data('test1.zip'))
zip2 = load_fixture(data('test2.zip'))
@@ -54,10 +52,7 @@ def test_compressed_files(differences):
@skip_unless_tool_exists('zipinfo')
def test_compare_non_existing(monkeypatch, zip1):
- monkeypatch.setattr(Config.general, 'new_file', True)
- difference = zip1.compare(NonExistingFile('/nonexisting', zip1))
- assert difference.source2 == '/nonexisting'
- assert difference.details[-1].source2 == '/dev/null'
+ assert_non_existing(monkeypatch, zip1)
TEST_MOZZIP1_PATH = data('test1.mozzip')
TEST_MOZZIP2_PATH = data('test2.mozzip')
@@ -92,7 +87,4 @@ def test_mozzip_compressed_files(mozzip_differences):
@skip_unless_tool_exists('zipinfo')
def test_mozzip_compare_non_existing(monkeypatch, mozzip1):
- monkeypatch.setattr(Config.general, 'new_file', True)
- difference = mozzip1.compare(NonExistingFile('/nonexisting', mozzip1))
- assert difference.source2 == '/nonexisting'
- assert difference.details[-1].source2 == '/dev/null'
+ assert_non_existing(monkeypatch, mozzip1)
diff --git a/tests/comparators/utils.py b/tests/comparators/utils.py
index ac2e96e..64502d8 100644
--- a/tests/comparators/utils.py
+++ b/tests/comparators/utils.py
@@ -25,8 +25,9 @@ import subprocess
from distutils.spawn import find_executable
from distutils.version import StrictVersion
+from diffoscope.config import Config
from diffoscope.comparators import specialize
-from diffoscope.comparators.binary import FilesystemFile
+from diffoscope.comparators.binary import FilesystemFile, NonExistingFile
@pytest.fixture(autouse=True)
@@ -56,3 +57,12 @@ def tool_older_than(cmdline, min_ver, vcls=StrictVersion):
return True
actual_ver = subprocess.check_output(cmdline).decode("utf-8").strip()
return vcls(actual_ver) < vcls(min_ver)
+
+def assert_non_existing(monkeypatch, fixture, has_null_source=True, has_details=True):
+ monkeypatch.setattr(Config.general, 'new_file', True)
+
+ difference = fixture.compare(NonExistingFile('/nonexisting', fixture))
+
+ assert difference.source2 == '/nonexisting'
+ assert not has_details or len(difference.details) > 0
+ assert not has_null_source or difference.details[-1].source2 == '/dev/null'
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git
More information about the diffoscope
mailing list