Skip to content

Commit 12fa58b

Browse files
authored
Merge pull request #4205 from Flamefire/test-asserts
Replace more usages of assertTrue/False
2 parents 4cd7557 + 5285d9c commit 12fa58b

24 files changed

+384
-371
lines changed

easybuild/base/testing.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* Kenneth Hoste (Ghent University)
3535
"""
3636
import difflib
37+
import os
3738
import pprint
3839
import re
3940
import sys
@@ -117,6 +118,18 @@ def assertEqual(self, a, b, msg=None):
117118

118119
raise AssertionError("%s:\nDIFF%s:\n%s" % (msg, limit, ''.join(diff[:self.ASSERT_MAX_DIFF])))
119120

121+
def assertExists(self, path, msg=None):
122+
"""Assert the given path exists"""
123+
if msg is None:
124+
msg = "'%s' should exist" % path
125+
self.assertTrue(os.path.exists(path), msg)
126+
127+
def assertNotExists(self, path, msg=None):
128+
"""Assert the given path exists"""
129+
if msg is None:
130+
msg = "'%s' should not exist" % path
131+
self.assertFalse(os.path.exists(path), msg)
132+
120133
def setUp(self):
121134
"""Prepare test case."""
122135
super(TestCase, self).setUp()

test/framework/build_log.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -388,26 +388,26 @@ def test_init_logging(self):
388388
tmp_logfile = os.path.join(self.test_prefix, 'test.log')
389389
log, logfile = init_logging(tmp_logfile, silent=True)
390390
self.assertEqual(logfile, tmp_logfile)
391-
self.assertTrue(os.path.exists(logfile))
392-
self.assertTrue(isinstance(log, EasyBuildLog))
391+
self.assertExists(logfile)
392+
self.assertIsInstance(log, EasyBuildLog)
393393

394394
stop_logging(logfile)
395395

396396
# no log provided, so create one (should be file in $TMPDIR)
397397
log, logfile = init_logging(None, silent=True)
398-
self.assertTrue(os.path.exists(logfile))
398+
self.assertExists(logfile)
399399
self.assertEqual(os.path.dirname(logfile), tmpdir)
400-
self.assertTrue(isinstance(log, EasyBuildLog))
400+
self.assertIsInstance(log, EasyBuildLog)
401401

402402
stop_logging(logfile)
403403

404404
# no problem with specifying a different directory to put log file in (even if it doesn't exist yet)
405405
tmp_logdir = os.path.join(self.test_prefix, 'tmp_logs')
406-
self.assertFalse(os.path.exists(tmp_logdir))
406+
self.assertNotExists(tmp_logdir)
407407

408408
log, logfile = init_logging(None, silent=True, tmp_logdir=tmp_logdir)
409409
self.assertEqual(os.path.dirname(logfile), tmp_logdir)
410-
self.assertTrue(isinstance(log, EasyBuildLog))
410+
self.assertIsInstance(log, EasyBuildLog)
411411

412412
stop_logging(logfile)
413413

@@ -416,9 +416,9 @@ def test_init_logging(self):
416416
log, logfile = init_logging(None)
417417
stdout = self.get_stdout()
418418
self.mock_stdout(False)
419-
self.assertTrue(os.path.exists(logfile))
419+
self.assertExists(logfile)
420420
self.assertEqual(os.path.dirname(logfile), tmpdir)
421-
self.assertTrue(isinstance(log, EasyBuildLog))
421+
self.assertIsInstance(log, EasyBuildLog)
422422
self.assertTrue(stdout.startswith("== Temporary log file in case of crash"))
423423

424424
stop_logging(logfile)
@@ -428,7 +428,7 @@ def test_init_logging(self):
428428
log, logfile = init_logging(None, logtostdout=True)
429429
self.mock_stdout(False)
430430
self.assertEqual(logfile, None)
431-
self.assertTrue(isinstance(log, EasyBuildLog))
431+
self.assertIsInstance(log, EasyBuildLog)
432432

433433
stop_logging(logfile, logtostdout=True)
434434

test/framework/config.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ def test_configuration_variables(self):
341341
cv1 = ConfigurationVariables()
342342
cv2 = ConfigurationVariables()
343343
cv3 = ConfigurationVariables({'foo': 'bar'}) # note: argument is ignored, an instance is already available
344-
self.assertTrue(cv1 is cv2)
345-
self.assertTrue(cv1 is cv3)
344+
self.assertIs(cv1, cv2)
345+
self.assertIs(cv1, cv3)
346346

347347
def test_build_options(self):
348348
"""Test usage of BuildOptions."""
@@ -353,8 +353,8 @@ def test_build_options(self):
353353
bo1 = BuildOptions()
354354
bo2 = BuildOptions()
355355
bo3 = BuildOptions({'foo': 'bar'}) # note: argument is ignored, an instance is already available
356-
self.assertTrue(bo1 is bo2)
357-
self.assertTrue(bo1 is bo3)
356+
self.assertIs(bo1, bo2)
357+
self.assertIs(bo1, bo3)
358358

359359
# test basic functionality
360360
BuildOptions.__class__._instances.clear()
@@ -394,7 +394,7 @@ def test_build_options(self):
394394

395395
# there should be only one BuildOptions instance
396396
bo2 = BuildOptions()
397-
self.assertTrue(bo is bo2)
397+
self.assertIs(bo, bo2)
398398

399399
def test_XDG_CONFIG_env_vars(self):
400400
"""Test effect of XDG_CONFIG* environment variables on default configuration."""

test/framework/containers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ def test_end2end_singularity_image(self):
308308
]
309309
self.check_regexs(regexs, stdout)
310310

311-
self.assertTrue(os.path.exists(os.path.join(containerpath, 'toy-0.0.%s' % ext)))
311+
self.assertExists(os.path.join(containerpath, 'toy-0.0.%s' % ext))
312312

313313
remove_file(os.path.join(containerpath, 'Singularity.toy-0.0'))
314314

@@ -330,7 +330,7 @@ def test_end2end_singularity_image(self):
330330
self.check_regexs(regexs, stdout)
331331

332332
cont_img = os.path.join(containerpath, 'foo-bar.img')
333-
self.assertTrue(os.path.exists(cont_img))
333+
self.assertExists(cont_img)
334334

335335
remove_file(os.path.join(containerpath, 'Singularity.foo-bar'))
336336

@@ -348,7 +348,7 @@ def test_end2end_singularity_image(self):
348348
"WARNING: overwriting existing container image at %s due to --force" % cont_img,
349349
])
350350
self.check_regexs(regexs, stdout)
351-
self.assertTrue(os.path.exists(cont_img))
351+
self.assertExists(cont_img)
352352

353353
# also check behaviour under --extended-dry-run
354354
args.append('--extended-dry-run')

test/framework/easyblock.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ def check_extra_options_format(extra_options):
8686
"""Make sure extra_options value is of correct format."""
8787
# EasyBuild v2.0: dict with <string> keys and <list> values
8888
# (breaks backward compatibility compared to v1.x)
89-
self.assertTrue(isinstance(extra_options, dict)) # conversion to a dict works
89+
self.assertIsInstance(extra_options, dict) # conversion to a dict works
9090
extra_options.items()
9191
extra_options.keys()
9292
extra_options.values()
9393
for key in extra_options.keys():
94-
self.assertTrue(isinstance(extra_options[key], list))
94+
self.assertIsInstance(extra_options[key], list)
9595
self.assertEqual(len(extra_options[key]), 3)
9696

9797
name = "pi"
@@ -202,7 +202,7 @@ def test_load_module(self):
202202

203203
# we expect $TMPDIR to be tweaked by the prepare step (OpenMPI 2.x doesn't like long $TMPDIR values)
204204
tweaked_tmpdir = os.environ.get('TMPDIR')
205-
self.assertTrue(tweaked_tmpdir != orig_tmpdir)
205+
self.assertNotEqual(tweaked_tmpdir, orig_tmpdir)
206206

207207
eb.make_module_step()
208208
eb.load_module()
@@ -234,7 +234,7 @@ def test_fake_module_load(self):
234234
if get_module_syntax() == 'Lua':
235235
pi_modfile += '.lua'
236236

237-
self.assertTrue(os.path.exists(pi_modfile))
237+
self.assertExists(pi_modfile)
238238

239239
# check whether temporary module file is marked as default
240240
if get_module_syntax() == 'Lua':
@@ -668,7 +668,7 @@ def test_make_module_extra(self):
668668
if get_module_syntax() == 'Lua':
669669
modpath += '.lua'
670670

671-
self.assertTrue(os.path.exists(modpath), "%s exists" % modpath)
671+
self.assertExists(modpath)
672672
txt = read_file(modpath)
673673
patterns = [
674674
r"^prepend[-_]path.*TEST_PATH_VAR.*root.*foo",
@@ -1203,7 +1203,7 @@ def test_make_module_step(self):
12031203
modpath = os.path.join(eb.make_module_step(), name, version)
12041204
if get_module_syntax() == 'Lua':
12051205
modpath += '.lua'
1206-
self.assertTrue(os.path.exists(modpath), "%s exists" % modpath)
1206+
self.assertExists(modpath)
12071207

12081208
# verify contents of module
12091209
txt = read_file(modpath)
@@ -1374,13 +1374,13 @@ def test_make_builddir(self):
13741374
builddir = eb.builddir
13751375
testfile = os.path.join(builddir, 'test123', 'foobar.txt')
13761376
write_file(testfile, 'test123')
1377-
self.assertTrue(os.path.exists(testfile))
1377+
self.assertExists(testfile)
13781378

13791379
eb.make_builddir()
13801380
self.assertEqual(builddir, eb.builddir)
13811381
# file is gone because directory was removed and re-created
1382-
self.assertFalse(os.path.exists(testfile))
1383-
self.assertFalse(os.path.exists(os.path.dirname(testfile)))
1382+
self.assertNotExists(testfile)
1383+
self.assertNotExists(os.path.dirname(testfile))
13841384
self.assertEqual(os.listdir(eb.builddir), [])
13851385

13861386
# make sure that build directory does *not* get re-created when we're building in installation directory
@@ -1392,7 +1392,7 @@ def test_make_builddir(self):
13921392
builddir = eb.builddir
13931393
testfile = os.path.join(builddir, 'test123', 'foobar.txt')
13941394
write_file(testfile, 'test123')
1395-
self.assertTrue(os.path.exists(testfile))
1395+
self.assertExists(testfile)
13961396
self.assertEqual(os.listdir(eb.builddir), ['test123'])
13971397
self.assertEqual(os.listdir(os.path.join(eb.builddir, 'test123')), ['foobar.txt'])
13981398

@@ -1401,7 +1401,7 @@ def test_make_builddir(self):
14011401
eb.make_builddir()
14021402
eb.make_installdir()
14031403
self.assertEqual(builddir, eb.builddir)
1404-
self.assertTrue(os.path.exists(testfile))
1404+
self.assertExists(testfile)
14051405
self.assertEqual(os.listdir(eb.builddir), ['test123'])
14061406
self.assertEqual(os.listdir(os.path.join(eb.builddir, 'test123')), ['foobar.txt'])
14071407

@@ -1410,8 +1410,8 @@ def test_make_builddir(self):
14101410
eb.make_builddir()
14111411
eb.make_installdir()
14121412
self.assertEqual(builddir, eb.builddir)
1413-
self.assertFalse(os.path.exists(testfile))
1414-
self.assertFalse(os.path.exists(os.path.dirname(testfile)))
1413+
self.assertNotExists(testfile)
1414+
self.assertNotExists(os.path.dirname(testfile))
14151415
self.assertEqual(os.listdir(eb.builddir), [])
14161416

14171417
def test_get_easyblock_instance(self):
@@ -1421,7 +1421,7 @@ def test_get_easyblock_instance(self):
14211421

14221422
ec = process_easyconfig(os.path.join(testdir, 'easyconfigs', 'test_ecs', 't', 'toy', 'toy-0.0.eb'))[0]
14231423
eb = get_easyblock_instance(ec)
1424-
self.assertTrue(isinstance(eb, EB_toy))
1424+
self.assertIsInstance(eb, EB_toy)
14251425

14261426
# check whether 'This is easyblock' log message is there
14271427
tup = ('EB_toy', 'easybuild.easyblocks.toy', '.*test/framework/sandbox/easybuild/easyblocks/t/toy.pyc*')
@@ -1483,9 +1483,9 @@ def test_fetch_sources(self):
14831483
self.assertEqual(len(eb.src), 3)
14841484
for idx in range(3):
14851485
self.assertEqual(eb.src[idx]['name'], expected_sources[idx])
1486-
self.assertTrue(os.path.exists(eb.src[idx]['path']))
1486+
self.assertExists(eb.src[idx]['path'])
14871487
source_loc = os.path.join(toy_source_dir, expected_sources[idx])
1488-
self.assertTrue(os.path.exists(source_loc))
1488+
self.assertExists(source_loc)
14891489
self.assertTrue(os.path.samefile(eb.src[idx]['path'], source_loc))
14901490
self.assertEqual(eb.src[0]['cmd'], None)
14911491
self.assertEqual(eb.src[1]['cmd'], "gunzip %s")
@@ -1751,7 +1751,7 @@ def test_obtain_file(self):
17511751

17521752
# toy tarball was indeed re-downloaded to tmpdir
17531753
self.assertEqual(res, os.path.join(tmpdir, 't', 'toy', toy_tarball))
1754-
self.assertTrue(os.path.exists(os.path.join(tmpdir, 't', 'toy', toy_tarball)))
1754+
self.assertExists(os.path.join(tmpdir, 't', 'toy', toy_tarball))
17551755

17561756
# obtain_file yields error for non-existing files
17571757
fn = 'thisisclearlyanonexistingfile'
@@ -1784,7 +1784,7 @@ def test_obtain_file(self):
17841784
if res is not None:
17851785
loc = os.path.join(tmpdir, 't', 'toy', fn)
17861786
self.assertEqual(res, loc)
1787-
self.assertTrue(os.path.exists(loc), "%s file is found at %s" % (fn, loc))
1787+
self.assertExists(loc)
17881788
txt = read_file(loc)
17891789
eb_regex = re.compile("EasyBuild: building software with ease")
17901790
self.assertTrue(eb_regex.search(txt), "Pattern '%s' found in: %s" % (eb_regex.pattern, txt))
@@ -1834,7 +1834,7 @@ def test_collect_exts_file_info(self):
18341834

18351835
exts_file_info = toy_eb.collect_exts_file_info()
18361836

1837-
self.assertTrue(isinstance(exts_file_info, list))
1837+
self.assertIsInstance(exts_file_info, list)
18381838
self.assertEqual(len(exts_file_info), 4)
18391839

18401840
self.assertEqual(exts_file_info[0], {'name': 'ls'})
@@ -1859,7 +1859,7 @@ def test_collect_exts_file_info(self):
18591859
# location of files is missing when fetch_files is set to False
18601860
exts_file_info = toy_eb.collect_exts_file_info(fetch_files=False, verify_checksums=False)
18611861

1862-
self.assertTrue(isinstance(exts_file_info, list))
1862+
self.assertIsInstance(exts_file_info, list)
18631863
self.assertEqual(len(exts_file_info), 4)
18641864

18651865
self.assertEqual(exts_file_info[0], {'name': 'ls'})
@@ -1898,7 +1898,7 @@ def test_obtain_file_extension(self):
18981898
ext = ExtensionEasyBlock(toy_eb, test_ext)
18991899
ext_src_path = ext.obtain_file(test_ext_src_fn)
19001900
self.assertEqual(os.path.basename(ext_src_path), 'toy-0.0.tar.gz')
1901-
self.assertTrue(os.path.exists(ext_src_path))
1901+
self.assertExists(ext_src_path)
19021902

19031903
def test_check_readiness(self):
19041904
"""Test check_readiness method."""
@@ -2136,7 +2136,7 @@ def test_guess_start_dir(self):
21362136
ec = process_easyconfig(os.path.join(test_easyconfigs, 't', 'toy', 'toy-0.0.eb'))[0]
21372137

21382138
cwd = os.getcwd()
2139-
self.assertTrue(os.path.exists(cwd))
2139+
self.assertExists(cwd)
21402140

21412141
def check_start_dir(expected_start_dir):
21422142
"""Check start dir."""
@@ -2171,7 +2171,7 @@ def test_extension_set_start_dir(self):
21712171
ec = process_easyconfig(os.path.join(test_easyconfigs, 't', 'toy', 'toy-0.0.eb'))[0]
21722172

21732173
cwd = os.getcwd()
2174-
self.assertTrue(os.path.exists(cwd))
2174+
self.assertExists(cwd)
21752175

21762176
def check_ext_start_dir(expected_start_dir, unpack_src=True):
21772177
"""Check start dir."""
@@ -2659,8 +2659,8 @@ def run_checks():
26592659
def test_this_is_easybuild(self):
26602660
"""Test 'this_is_easybuild' function (and get_git_revision function used by it)."""
26612661
# make sure both return a non-Unicode string
2662-
self.assertTrue(isinstance(get_git_revision(), str))
2663-
self.assertTrue(isinstance(this_is_easybuild(), str))
2662+
self.assertIsInstance(get_git_revision(), str)
2663+
self.assertIsInstance(this_is_easybuild(), str)
26642664

26652665
def test_stale_module_caches(self):
26662666
"""Test whether module caches are reset between builds."""

0 commit comments

Comments
 (0)