From 3be940d145c009a33ae763fbed4e61612da3fd15 Mon Sep 17 00:00:00 2001 From: openSourcerer Date: Fri, 10 Sep 2021 17:10:48 -0500 Subject: [PATCH 1/3] strip unittest and STATIC_FILES dict from test_cli.py --- compliance_checker/tests/conftest.py | 16 ++++- compliance_checker/tests/test_cli.py | 95 ++++++++++------------------ 2 files changed, 49 insertions(+), 62 deletions(-) diff --git a/compliance_checker/tests/conftest.py b/compliance_checker/tests/conftest.py index 5061fcc9..94734808 100644 --- a/compliance_checker/tests/conftest.py +++ b/compliance_checker/tests/conftest.py @@ -47,7 +47,7 @@ def static_files(cdl_stem): assert ( nc_path.exists() ), f"ncgen CLI utility failed to produce {nc_path} from {cdl_path}" - return nc_path + return str(nc_path) # ---------Fixtures----------- @@ -98,3 +98,17 @@ def new_nc_file(tmpdir): nc = Dataset(nc_file_path, "w") # no need for cleanup, built-in tmpdir fixture will handle it return nc + +@pytest.fixture +def tmp_txt_file(tmpdir): + file_path = os.path.join(tmpdir, "output.txt") + if os.path.exists(file_path): + raise IOError("File Exists: %s" % file_path) + + return file_path + +@pytest.fixture +def checksuite_setup(): + '''For test_cli''' + CheckSuite.checkers.clear() + CheckSuite.load_all_available_checkers() \ No newline at end of file diff --git a/compliance_checker/tests/test_cli.py b/compliance_checker/tests/test_cli.py index 73fe2b1f..6f0ec839 100644 --- a/compliance_checker/tests/test_cli.py +++ b/compliance_checker/tests/test_cli.py @@ -11,93 +11,66 @@ import tempfile from argparse import Namespace -from unittest import TestCase import pytest from compliance_checker.runner import CheckSuite, ComplianceChecker -from compliance_checker.tests.resources import STATIC_FILES +from .conftest import static_files - -class TestCLI(TestCase): +@pytest.mark.usefixtures("checksuite_setup") +class TestCLI: """ Tests various functions and aspects of the command line tool and runner """ - def setUp(self): - self.fid, self.path = tempfile.mkstemp() - # why is the class being written to - CheckSuite.checkers.clear() - CheckSuite.load_all_available_checkers() - - def tearDown(self): - if os.path.isfile(self.path): - os.close(self.fid) - os.remove(self.path) - def shortDescription(self): return None - # override __str__ and __repr__ behavior to show a copy-pastable nosetest name for ion tests - # ion.module:TestClassName.test_function_name - def __repr__(self): - name = self.id() - name = name.split(".") - if name[0] not in ["ion", "pyon"]: - return "%s (%s)" % (name[-1], ".".join(name[:-1])) - else: - return "%s ( %s )" % ( - name[-1], - ".".join(name[:-2]) + ":" + ".".join(name[-2:]), - ) - - __str__ = __repr__ - - def test_unicode_acdd_html(self): + def test_unicode_acdd_html(self,tmp_txt_file): """ Tests that the checker is capable of producing HTML with unicode characters """ return_value, errors = ComplianceChecker.run_checker( - ds_loc=STATIC_FILES["2dim"], + ds_loc=static_files("2dim"), verbose=0, criteria="strict", checker_names=["acdd"], - output_filename=self.path, + output_filename=tmp_txt_file, output_format="html", ) - assert os.stat(self.path).st_size > 0 + assert os.stat(tmp_txt_file).st_size > 0 - def test_unicode_cf_html(self): + def test_unicode_cf_html(self,tmp_txt_file): """ Tests that the CF checker can produce HTML output with unicode characters """ return_value, errors = ComplianceChecker.run_checker( - ds_loc=STATIC_FILES["2dim"], + ds_loc=static_files("2dim"), verbose=0, criteria="strict", checker_names=["cf"], - output_filename=self.path, + output_filename=tmp_txt_file, output_format="html", ) - assert os.stat(self.path).st_size > 0 + assert os.stat(tmp_txt_file).st_size > 0 - def test_single_json_output(self): + def test_single_json_output(self,tmp_txt_file): """ Tests that a suite can produce JSON output to a file """ return_value, errors = ComplianceChecker.run_checker( - ds_loc=STATIC_FILES["conv_bad"], + ds_loc=static_files("conv_bad"), verbose=0, criteria="strict", checker_names=["cf"], - output_filename=self.path, + output_filename=tmp_txt_file, output_format="json", ) - assert os.stat(self.path).st_size > 0 - with open(self.path) as f: + assert os.stat(tmp_txt_file).st_size > 0 + with open(tmp_txt_file) as f: r = json.load(f) assert "cf" in r @@ -134,21 +107,21 @@ def checker_2(): sys.stdout = saved fake_stdout.close() - def test_multiple_json_output(self): + def test_multiple_json_output(self,tmp_txt_file): """ Tests that a suite can produce JSON output to a file """ return_value, errors = ComplianceChecker.run_checker( - ds_loc=STATIC_FILES["conv_bad"], + ds_loc=static_files("conv_bad"), verbose=0, criteria="strict", checker_names=["acdd", "cf"], - output_filename=self.path, + output_filename=tmp_txt_file, output_format="json", ) - assert os.stat(self.path).st_size > 0 - with open(self.path) as f: + assert os.stat(tmp_txt_file).st_size > 0 + with open(tmp_txt_file) as f: r = json.load(f) assert "cf" in r assert "acdd" in r @@ -162,7 +135,7 @@ def test_multiple_json_output_stdout(self): fake_stdout = io.StringIO() sys.stdout = fake_stdout return_value, errors = ComplianceChecker.run_checker( - ds_loc=STATIC_FILES["conv_bad"], + ds_loc=static_files("conv_bad"), verbose=0, criteria="strict", checker_names=["acdd", "cf"], @@ -185,7 +158,7 @@ def test_single_json_output_stdout(self): fake_stdout = io.StringIO() sys.stdout = fake_stdout return_value, errors = ComplianceChecker.run_checker( - ds_loc=STATIC_FILES["conv_bad"], + ds_loc=static_files("conv_bad"), verbose=0, criteria="strict", checker_names=["cf"], @@ -198,46 +171,46 @@ def test_single_json_output_stdout(self): sys.stdout = saved fake_stdout.close() - def test_text_output(self): + def test_text_output(self,tmp_txt_file): """ Tests that the 'text' output can be redirected to file with arguments to the command line """ return_value, errors = ComplianceChecker.run_checker( - ds_loc=STATIC_FILES["conv_bad"], + ds_loc=static_files("conv_bad"), verbose=0, criteria="strict", checker_names=["acdd", "cf"], - output_filename=self.path, + output_filename=tmp_txt_file, output_format="text", ) - assert os.stat(self.path).st_size > 0 + assert os.stat(tmp_txt_file).st_size > 0 - def test_multi_checker_return_value(self): + def test_multi_checker_return_value(self,tmp_txt_file): """ Tests that any failure for multiple checkers results in a failure return status """ # CF should pass here return_value, errors = ComplianceChecker.run_checker( - ds_loc=STATIC_FILES["ncei_gold_point_1"], + ds_loc=static_files("ncei_gold_point_1"), verbose=0, criteria="strict", checker_names=["cf:1.6"], - output_filename=self.path, + output_filename=tmp_txt_file, output_format="text", ) - self.assertTrue(return_value) + assert return_value # CF should pass, but ACDD will have some errors. Overall return # status should be a failure return_value, errors = ComplianceChecker.run_checker( - ds_loc=STATIC_FILES["ncei_gold_point_1"], + ds_loc=static_files("ncei_gold_point_1"), verbose=0, criteria="strict", checker_names=["acdd", "cf"], - output_filename=self.path, + output_filename=tmp_txt_file, output_format="text", ) - self.assertFalse(return_value) + assert not return_value From 40a3e186a2ea205dd19fa45342577abadfff3319 Mon Sep 17 00:00:00 2001 From: openSourcerer Date: Fri, 10 Sep 2021 17:11:13 -0500 Subject: [PATCH 2/3] remove unused import --- compliance_checker/tests/test_cli.py | 1 - 1 file changed, 1 deletion(-) diff --git a/compliance_checker/tests/test_cli.py b/compliance_checker/tests/test_cli.py index 6f0ec839..4d851a2c 100644 --- a/compliance_checker/tests/test_cli.py +++ b/compliance_checker/tests/test_cli.py @@ -8,7 +8,6 @@ import json import os import sys -import tempfile from argparse import Namespace From 7fa549774b0e01d45963ae76f4dbcc7d94dbb176 Mon Sep 17 00:00:00 2001 From: openSourcerer Date: Fri, 10 Sep 2021 17:23:46 -0500 Subject: [PATCH 3/3] run precommit --- compliance_checker/tests/conftest.py | 8 +++++--- compliance_checker/tests/test_cli.py | 14 ++++++++------ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/compliance_checker/tests/conftest.py b/compliance_checker/tests/conftest.py index 94734808..d51d3898 100644 --- a/compliance_checker/tests/conftest.py +++ b/compliance_checker/tests/conftest.py @@ -99,16 +99,18 @@ def new_nc_file(tmpdir): # no need for cleanup, built-in tmpdir fixture will handle it return nc + @pytest.fixture def tmp_txt_file(tmpdir): file_path = os.path.join(tmpdir, "output.txt") if os.path.exists(file_path): raise IOError("File Exists: %s" % file_path) - + return file_path + @pytest.fixture def checksuite_setup(): - '''For test_cli''' + """For test_cli""" CheckSuite.checkers.clear() - CheckSuite.load_all_available_checkers() \ No newline at end of file + CheckSuite.load_all_available_checkers() diff --git a/compliance_checker/tests/test_cli.py b/compliance_checker/tests/test_cli.py index 4d851a2c..7b572162 100644 --- a/compliance_checker/tests/test_cli.py +++ b/compliance_checker/tests/test_cli.py @@ -14,8 +14,10 @@ import pytest from compliance_checker.runner import CheckSuite, ComplianceChecker + from .conftest import static_files + @pytest.mark.usefixtures("checksuite_setup") class TestCLI: """ @@ -25,7 +27,7 @@ class TestCLI: def shortDescription(self): return None - def test_unicode_acdd_html(self,tmp_txt_file): + def test_unicode_acdd_html(self, tmp_txt_file): """ Tests that the checker is capable of producing HTML with unicode characters """ @@ -40,7 +42,7 @@ def test_unicode_acdd_html(self,tmp_txt_file): assert os.stat(tmp_txt_file).st_size > 0 - def test_unicode_cf_html(self,tmp_txt_file): + def test_unicode_cf_html(self, tmp_txt_file): """ Tests that the CF checker can produce HTML output with unicode characters """ @@ -55,7 +57,7 @@ def test_unicode_cf_html(self,tmp_txt_file): assert os.stat(tmp_txt_file).st_size > 0 - def test_single_json_output(self,tmp_txt_file): + def test_single_json_output(self, tmp_txt_file): """ Tests that a suite can produce JSON output to a file """ @@ -106,7 +108,7 @@ def checker_2(): sys.stdout = saved fake_stdout.close() - def test_multiple_json_output(self,tmp_txt_file): + def test_multiple_json_output(self, tmp_txt_file): """ Tests that a suite can produce JSON output to a file """ @@ -170,7 +172,7 @@ def test_single_json_output_stdout(self): sys.stdout = saved fake_stdout.close() - def test_text_output(self,tmp_txt_file): + def test_text_output(self, tmp_txt_file): """ Tests that the 'text' output can be redirected to file with arguments to the command line @@ -186,7 +188,7 @@ def test_text_output(self,tmp_txt_file): assert os.stat(tmp_txt_file).st_size > 0 - def test_multi_checker_return_value(self,tmp_txt_file): + def test_multi_checker_return_value(self, tmp_txt_file): """ Tests that any failure for multiple checkers results in a failure return status