uclahs-cds / tool-NFTest

CLI to automate Nextflow pipeline testing
GNU General Public License v2.0
11 stars 1 forks source link

Encapsulate NFTestAssert class, rewrite tests #67

Closed nwiltsie closed 5 months ago

nwiltsie commented 5 months ago

Description

This PR rewrites most of the internals of NFTestAssert in a way that is (mostly) invisible to the outside world. This is the key interface change:

@@ -109,12 +108,9 @@ class NFTestCase:
             self._logger.error(" [ failed ]")
             return False

-        for ass in self.asserts:
+        for assertion in self.asserts:
             try:
-                ass.identify_assertion_files()
-                ass.assert_exists()
-                ass.assert_updated()
-                ass.assert_expected()
+                assertion.perform_assertions()
             except Exception as error:
                 self._logger.error(error.args)
                 self._logger.error(" [ failed ]")

The new perform_assertions() method hides all of the details of identify_assertion_files(), assert_exists(), assert_updated(), and assert_expected(). Decoupling the interface from the implementation details like that makes it easier to test and maintain this code.

In that spirit, I've also re-written all of the tests to test the functionality of the class rather than the implementation details. There are a variety of test fixtures that create an NFTestAssert instance pointing to reference and updated files in a temporary directory. The fixtures are parameterized to test the following situations:

Those parameters are matched with appropriate xfail marks to confirm that the tests do/don't pass in the expected situations. (Aside: by default xfail has strict=False, meaning that tests that unexpectedly pass do not raise errors. That is bonkers.)

Checklist

[^1]: UCLA Health reaches $7.5m settlement over 2015 breach of 4.5m patient records [^2]: The average healthcare data breach costs $2.2 million, despite the majority of breaches releasing fewer than 500 records. [^3]: Genetic information is considered PHI. Forensic assays can identify patients with as few as 21 SNPs [^4]: RNA-Seq, DNA methylation, microbiome, or other molecular data can be used to predict genotypes (PHI) and reveal a patient's identity.

  To automatically exclude such files using a .gitignore file, see here for example.

nwiltsie commented 5 months ago

Argh, that's what I get for only running my new tests.

nwiltsie commented 5 months ago

Hmm, I'm not sure why this isn't working in the cloud - it works perfectly for me locally:

$ pytest
======================================== test session starts ========================================
platform darwin -- Python 3.11.4, pytest-7.4.0, pluggy-1.0.0
rootdir: /Users/nwiltsie/src/tool-NFTest
collected 162 items

test/unit/test_NFTestAssert.py xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx..xxxxxxxxxxxxxxxxxxxxxxxxxxxx [ 38%]
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx            [ 88%]
test/unit/test_NFTestCase.py ..                                                               [ 90%]
test/unit/test_NFTestEnv.py ..                                                                [ 91%]
test/unit/test_NFTestRunner.py .                                                              [ 91%]
test/unit/test_common.py .........                                                            [ 97%]
test/unit/test_syslog.py ....                                                                 [100%]

================================== 22 passed, 140 xfailed in 1.96s ==================================
nwiltsie commented 5 months ago

Well, that's irritating. These log lines from this test from 4e71965d0cdb45255c872c1c7c10c57ac75b62a2 show that the filetimes are all wonky in GitHub Actions:

DEBUG    NFTest:NFTestAssert.py:44 Actual path `/tmp/pytest-of-runner/pytest-0/test_nftest_assert_True_md5_1_11/*.actual` resolved to `/tmp/pytest-of-runner/pytest-0/test_nftest_assert_True_md5_1_11/0.actual`
DEBUG    NFTest:NFTestAssert.py:48 Expected path `/tmp/pytest-of-runner/pytest-0/test_nftest_assert_True_md5_1_11/*.expect` resolved to `/tmp/pytest-of-runner/pytest-0/test_nftest_assert_True_md5_1_11/0.expect`
DEBUG    NFTest:NFTestAssert.py:57 Test creation time: 2024-06-24 23:30:14.720138+00:00
DEBUG    NFTest:NFTestAssert.py:58 Actual mod time:    2024-06-24 23:30:14.716686+00:00

The only fix I can find is to insert time.sleep(0.01) calls in the cases the file needs to be updated, even in the case where the file is explicitly created after the NFTestAssert.

nwiltsie commented 5 months ago

Clearly there's something else going on under the hood. I'll dig in and try again later.