From 7b8c47bee643cdde2ed2ef1684529df99780bffa Mon Sep 17 00:00:00 2001 From: Thomas Robitaille Date: Tue, 22 Nov 2016 19:58:36 +0000 Subject: [PATCH 1/5] Make it possible to specify default file format for reference arrays --- README.md | 4 ++++ pytest_arraydiff/plugin.py | 12 +++++++++--- tests/test_pytest_arraydiff.py | 27 +++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7647d74..627b571 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,10 @@ def test_image(): ... ``` +The default file format can also be specified using the +``--arraydiff-default-format=`` flag when running ``py.test``, and +```` should be either ``fits`` or ``text``. + The supported formats at this time are ``text`` and ``fits``, and contributions for other formats are welcome. The default format is ``text``. diff --git a/pytest_arraydiff/plugin.py b/pytest_arraydiff/plugin.py index 300964a..d21dc54 100755 --- a/pytest_arraydiff/plugin.py +++ b/pytest_arraydiff/plugin.py @@ -98,6 +98,8 @@ def pytest_addoption(parser): help="directory to generate reference files in, relative to location where py.test is run", action='store') group.addoption('--arraydiff-reference-path', help="directory containing reference files, relative to location where py.test is run", action='store') + group.addoption('--arraydiff-default-format', + help="Default format for the reference arrays (can be 'fits' or 'text' currently)") def pytest_configure(config): @@ -115,17 +117,21 @@ def pytest_configure(config): if generate_dir is not None: reference_dir = os.path.abspath(generate_dir) + default_format = config.getoption("--arraydiff-default-format") or 'text' + config.pluginmanager.register(ArrayComparison(config, reference_dir=reference_dir, - generate_dir=generate_dir)) + generate_dir=generate_dir, + default_format=default_format)) class ArrayComparison(object): - def __init__(self, config, reference_dir=None, generate_dir=None): + def __init__(self, config, reference_dir=None, generate_dir=None, default_format='text'): self.config = config self.reference_dir = reference_dir self.generate_dir = generate_dir + self.default_format = default_format def pytest_runtest_setup(self, item): @@ -134,7 +140,7 @@ def pytest_runtest_setup(self, item): if compare is None: return - file_format = compare.kwargs.get('file_format', 'text') + file_format = compare.kwargs.get('file_format', self.default_format) if file_format not in FORMATS: raise ValueError("Unknown format: {0}".format(file_format)) diff --git a/tests/test_pytest_arraydiff.py b/tests/test_pytest_arraydiff.py index 692ba33..da49daf 100644 --- a/tests/test_pytest_arraydiff.py +++ b/tests/test_pytest_arraydiff.py @@ -91,6 +91,33 @@ def test_generate(file_format): assert os.path.exists(os.path.join(gen_dir, 'test_gen.' + ('fits' if file_format == 'fits' else 'txt'))) +TEST_DEFAULT = """ +import pytest +import numpy as np +from astropy.io import fits +@pytest.mark.array_compare +def test_default(): + return np.arange(6 * 5).reshape((6, 5)) +""" + +@pytest.mark.parametrize('file_format', ('fits', 'text')) +def test_default_format(file_format): + + tmpdir = tempfile.mkdtemp() + + test_file = os.path.join(tmpdir, 'test.py') + with open(test_file, 'w') as f: + f.write(TEST_DEFAULT) + + gen_dir = os.path.join(tmpdir, 'spam', 'egg') + + # If we do generate, the test should succeed and a new file will appear + code = subprocess.call('py.test -s --arraydiff-default-format={0}' + ' --arraydiff-generate-path={1} {2}'.format(file_format, gen_dir, test_file), shell=True) + assert code == 0 + assert os.path.exists(os.path.join(gen_dir, 'test_default.' + ('fits' if file_format == 'fits' else 'txt'))) + + @pytest.mark.array_compare(reference_dir=reference_dir, rtol=0.5) def test_tolerance(): return np.ones((3,4)) * 1.6 From 29548fab45c392f43b0e26f985ba3590d3c9b545 Mon Sep 17 00:00:00 2001 From: Thomas Robitaille Date: Tue, 22 Nov 2016 20:08:42 +0000 Subject: [PATCH 2/5] Make sure the long_description is valid rst --- .travis.yml | 1 + README.md | 2 +- setup.py | 8 ++++++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9dc9bc7..1e41c80 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,3 +19,4 @@ install: script: - cd tests - py.test --fits + - python setup.py check --restructuredtext diff --git a/README.md b/README.md index 627b571..ff90497 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ Using To use, you simply need to mark the function where you want to compare images using ``@pytest.mark.array_compare``, and make sure that the function -returns a plain Numpy array:: +returns a plain Numpy array: python import pytest diff --git a/setup.py b/setup.py index c0c8aa2..6eeb1b9 100755 --- a/setup.py +++ b/setup.py @@ -1,3 +1,4 @@ +import sys from setuptools import setup from pytest_arraydiff import __version__ @@ -6,8 +7,11 @@ import pypandoc long_description = pypandoc.convert('README.md', 'rst') except (IOError, ImportError): - with open('README.md') as infile: - long_description = infile.read() + if 'register' in sys.argv: + raise + else: + with open('README.md') as infile: + long_description = infile.read() setup( version=__version__, From 4ca00c8f9575a7f038b73df8af8edb18fe7fb471 Mon Sep 17 00:00:00 2001 From: Thomas Robitaille Date: Tue, 22 Nov 2016 20:15:28 +0000 Subject: [PATCH 3/5] Fix test command --- .travis.yml | 2 +- appveyor.yml | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1e41c80..1958d2f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,5 +18,5 @@ install: script: - cd tests - - py.test --fits + - py.test --arraydiff - python setup.py check --restructuredtext diff --git a/appveyor.yml b/appveyor.yml index ab7611c..a689789 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -29,5 +29,4 @@ build: false test_script: - "%CMD_IN_ENV% cd tests" - - "%CMD_IN_ENV% py.test --fits" - + - "%CMD_IN_ENV% py.test --arraydiff" From 625468395423b73245bc5ad9bd4667e7c18eeaa0 Mon Sep 17 00:00:00 2001 From: Thomas Robitaille Date: Tue, 22 Nov 2016 20:35:11 +0000 Subject: [PATCH 4/5] Fix tests following refactoring --- tests/baseline/test_succeeds_class.fits | Bin 5760 -> 5760 bytes tests/baseline/test_succeeds_func.fits | Bin 5760 -> 0 bytes tests/baseline/test_succeeds_func_default.txt | 3 +++ ...rray.fits => test_succeeds_func_fits.fits} | Bin 5760 -> 5760 bytes tests/baseline/test_succeeds_func_text.txt | 3 +++ tests/baseline/test_tolerance.fits | Bin 5760 -> 5760 bytes tests/test_pytest_arraydiff.py | 10 +++++----- 7 files changed, 11 insertions(+), 5 deletions(-) delete mode 100644 tests/baseline/test_succeeds_func.fits create mode 100644 tests/baseline/test_succeeds_func_default.txt rename tests/baseline/{test_succeeds_func_array.fits => test_succeeds_func_fits.fits} (85%) create mode 100644 tests/baseline/test_succeeds_func_text.txt diff --git a/tests/baseline/test_succeeds_class.fits b/tests/baseline/test_succeeds_class.fits index 2e6c1b14472ce3eba7377f6aca3b4aac1eda9dd2..6b5ccefab1a9297df4c6d1a30881dd90726485c5 100644 GIT binary patch delta 40 wcmZqBZP4BDfRWQRBE;3tMM1%KVxtIS$mEGkMw=U$4sc9fz!b3Af!l!_01rnD*Z=?k delta 77 zcmZqBZP4BDfKepGH8?~82y7M9lXDV_i-An_$&O4ma$$}>p{`&FkTeo7)H60Q0V*-t OY`}DYW3vOd12+KoJrW}T diff --git a/tests/baseline/test_succeeds_func.fits b/tests/baseline/test_succeeds_func.fits deleted file mode 100644 index eee021edc6faf6d115622e4aa494523dbce89595..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 5760 zcmeIw$xp&S7{+1uecyM-6<3heEgUdn(gX4DpP$@}eE`-1}aap3du^&m>i#(lCpTTTGzW4DbAFD)Zw0E|-&a4!0UX339L5nG#W5Vm37o_!oW>cP z#W|eE1zf}>T*eh##Wh^V4cx>n+{PW;MTL8~j|X^&M|g|@Ji${u!*jgAOAKNNuP}@e jjA9Jqn80hi!CSn;dlaMrX+Ro~2BZOLKpKz+{-}WuF;|Q{ diff --git a/tests/baseline/test_succeeds_func_default.txt b/tests/baseline/test_succeeds_func_default.txt new file mode 100644 index 0000000..1e60a17 --- /dev/null +++ b/tests/baseline/test_succeeds_func_default.txt @@ -0,0 +1,3 @@ +0 1 2 3 4 +5 6 7 8 9 +10 11 12 13 14 diff --git a/tests/baseline/test_succeeds_func_array.fits b/tests/baseline/test_succeeds_func_fits.fits similarity index 85% rename from tests/baseline/test_succeeds_func_array.fits rename to tests/baseline/test_succeeds_func_fits.fits index 2950d77cffd89b3c8c75f7d741bec7496a60f0a4..2e452d9818fac75f748a22a4146e8e2f3cb8de85 100644 GIT binary patch delta 42 ycmZqBZP1;tm(ghAKL5$Fj24q!86`HaWt_}8Ie;l(vIBShW)0p5&dmnAnT!BI?G42M literal 5760 zcmeIu$4kR-7>9B9-h2DHx3$xS2gM2jErsULYqTx&&_t4W=znhrkF-JpEqIdmcljhC z`4XOz@m74zi#$lgF(n}%j$L!yh9|xwFNRqX&$sM~L8#t~j-y<$9xPdv(Y5S|e>&;a)OkFVps7 zhnL}ADrPU+;YizS;a)msFL>VV$(PN1;W=i{>~>8g9P9ow{Xz?90WF{fw15`S0{^B! z&+pUAZ6EgI01o014&w-p;uwzO1Ww` Date: Tue, 22 Nov 2016 20:39:35 +0000 Subject: [PATCH 5/5] Move check test earlier --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 1958d2f..17db93e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,6 +17,6 @@ install: - python setup.py install script: + - python setup.py check --restructuredtext - cd tests - py.test --arraydiff - - python setup.py check --restructuredtext