[2/8] Use assert(Not)Regex instead of assertTrue or assertFalse in tests - #5241
Conversation
… in tests This makes the tests shorter and hence easier to read, avoids `re.compile` calls and manual failure message composing. In some cases `assert_multi_regex` could be used to avoid the explicit loops.
| res = get_log_filename('foo', '1.2.3', date='19700101', timestamp='094651', add_salt=True) | ||
| regex = re.compile(os.path.join(tmpdir, r'easybuild-foo-1\.2\.3-19700101\.094651\.[a-zA-Z]{5}\.log$')) | ||
| self.assertTrue(regex.match(res), "Pattern '%s' matches '%s'" % (regex.pattern, res)) | ||
| self.assertRegex(res, os.path.join(tmpdir, r'easybuild-foo-1\.2\.3-19700101\.094651\.[a-zA-Z]{5}\.log$')) |
There was a problem hiding this comment.
Why the lack of re.compile here compared to the others above?
There was a problem hiding this comment.
Or perhaps the question should be why keep the re.compile above?
There was a problem hiding this comment.
re.compile was required when flags were used. E.g. when ^$ should match line begin/end. I removed the remaining ones that were no longer required now
| for pattern in patterns: | ||
| regex = re.compile(pattern, re.M) | ||
| self.assertFalse(regex.search(desc), "Pattern '%s' not found in: %s" % (regex.pattern, desc)) | ||
| self.assert_multi_regex(patterns, desc, assert_true=False) |
There was a problem hiding this comment.
Hmmm didn't think of this in the 1/8 PR... the assert_true is a weird name, just "assert" would make more sense, at least to me. (or at least something else than assert_true...)
There was a problem hiding this comment.
I think this existed before. How about assert_match?
There was a problem hiding this comment.
that feels better at least...
There was a problem hiding this comment.
Done. Can you merge this soon so I can update the others before they fail post-merge?
| @@ -255,7 +255,7 @@ def test_package(self): | |||
| self.assertTrue(os.path.isfile(pkgfile)) | |||
| pkgtxt = read_file(pkgfile) | |||
| self.assertRegex(pkgtxt, r"""DESCRIPTION:.*`backticks'.*""") | |||
| self.assertRegex(pkgtxt, re.compile(r"""DESCRIPTION:.*\nand newlines""", re.MULTILINE)) | |||
| self.assertRegex(pkgtxt, r"""DESCRIPTION:.*\nand newlines""") | |||
There was a problem hiding this comment.
Don't these two actually need MULTILINE? the first one does have a "$" in the middle of the regex at least and the second one includes newline in the pattern
There was a problem hiding this comment.
The first one? r"""DESCRIPTION:.*backticks'.*"""has no$`
MULTILINE only affects ^ and $ to match line start/end, so a \n always matches a newline
| no_logfiles_regex = re.compile(r'STARTCONTENTS.*\.(log|md)$.*ENDCONTENTS', re.DOTALL | re.MULTILINE) | ||
| res = no_logfiles_regex.search(pkgtxt) | ||
| self.assertFalse(res, "Pattern not '%s' found in: %s" % (no_logfiles_regex.pattern, pkgtxt)) | ||
| self.assertNotRegex(pkgtxt, re.compile(r'STARTCONTENTS.*\.(log|md)$.*ENDCONTENTS', re.DOTALL | re.M)) |
There was a problem hiding this comment.
Sorry, this is the one I meant that has $ in the middle
There was a problem hiding this comment.
Ok, I had kept the re.M there.
|
I pulled in another change to However IMO this is not a safe default: With |
There is at least 1 check that is supposed to match at the beginning/end of the string, not each line.
7097c20 to
1a0dddd
Compare
| pattern = r"\s*extensions\(" | ||
|
|
||
| self.assertFalse(re.search(pattern, desc), "No extensions found in: %s" % desc) | ||
| self.assertNotRegex(pattern, desc, re) |
There was a problem hiding this comment.
Still don't understand the "re" as third argument here. The third argument is the message string to be printed if you don't want the default.
There was a problem hiding this comment.
Just a leftover. I thought I had fixed that, done now
akesandgren
left a comment
There was a problem hiding this comment.
Looks even better now :-)
|
Going in, thanks @Flamefire! |
|
@akesandgren Followup: #5285 as per my last comment. |
This makes the tests shorter and hence easier to read, avoids
re.compilecalls and manual failure message composing. In some casesassert_multi_regexcould be used to avoid the explicit loops.