Skip to content

Commit ea5a851

Browse files
authored
Merge pull request SCons#4533 from mwichmann/maint/Variables-main
Variables cleanup: core
2 parents 04c5b6e + 9b4f2eb commit ea5a851

File tree

8 files changed

+241
-206
lines changed

8 files changed

+241
-206
lines changed

SCons/Variables/VariablesTests.py

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __contains__(self, key) -> bool:
4545

4646

4747
def check(key, value, env) -> None:
48-
assert int(value) == 6 * 9, "key %s = %s" % (key, repr(value))
48+
assert int(value) == 6 * 9, f"key {key!r} = {value!r}"
4949

5050
# Check saved option file by executing and comparing against
5151
# the expected dictionary
@@ -55,7 +55,7 @@ def checkSave(file, expected) -> None:
5555
with open(file, 'r') as f:
5656
exec(f.read(), gdict, ldict)
5757

58-
assert expected == ldict, "%s\n...not equal to...\n%s" % (expected, ldict)
58+
assert expected == ldict, f"{expected}\n...not equal to...\n{ldict}"
5959

6060
class VariablesTestCase(unittest.TestCase):
6161

@@ -97,12 +97,9 @@ def test_Add(self) -> None:
9797
o.validator(o.key, o.converter(o.default), {})
9898

9999
def test_it(var, opts=opts) -> None:
100-
exc_caught = None
101-
try:
100+
with self.assertRaises(SCons.Errors.UserError):
102101
opts.Add(var)
103-
except SCons.Errors.UserError:
104-
exc_caught = 1
105-
assert exc_caught, "did not catch UserError for '%s'" % var
102+
106103
test_it('foo/bar')
107104
test_it('foo-bar')
108105
test_it('foo.bar')
@@ -168,19 +165,12 @@ def test_Update(self) -> None:
168165

169166
env = Environment()
170167
exc_caught = None
171-
try:
168+
with self.assertRaises(AssertionError):
172169
opts.Update(env)
173-
except AssertionError:
174-
exc_caught = 1
175-
assert exc_caught, "did not catch expected assertion"
176170

177171
env = Environment()
178-
exc_caught = None
179-
try:
172+
with self.assertRaises(AssertionError):
180173
opts.Update(env, {})
181-
except AssertionError:
182-
exc_caught = 1
183-
assert exc_caught, "did not catch expected assertion"
184174

185175
# Test that a good value from the file is used and validated.
186176
test = TestSCons.TestSCons()
@@ -216,12 +206,8 @@ def test_Update(self) -> None:
216206
lambda x: int(x) + 12)
217207

218208
env = Environment()
219-
exc_caught = None
220-
try:
209+
with self.assertRaises(AssertionError):
221210
opts.Update(env, {'ANSWER':'54'})
222-
except AssertionError:
223-
exc_caught = 1
224-
assert exc_caught, "did not catch expected assertion"
225211

226212
# Test that a good value from an args dictionary
227213
# passed to Update() is used and validated.
@@ -303,12 +289,8 @@ def test_args(self) -> None:
303289
lambda x: int(x) + 12)
304290

305291
env = Environment()
306-
exc_caught = None
307-
try:
292+
with self.assertRaises(AssertionError):
308293
opts.Update(env)
309-
except AssertionError:
310-
exc_caught = 1
311-
assert exc_caught, "did not catch expected assertion"
312294

313295
# Test that a good (command-line) argument is used and validated.
314296
test = TestSCons.TestSCons()
@@ -466,7 +448,8 @@ def test_GenerateHelpText(self) -> None:
466448
"""
467449

468450
text = opts.GenerateHelpText(env)
469-
assert text == expect, text
451+
with self.subTest():
452+
self.assertEqual(expect, text)
470453

471454
expectAlpha = """
472455
A: a - alpha test
@@ -496,25 +479,26 @@ def test_GenerateHelpText(self) -> None:
496479
actual: 54
497480
"""
498481
text = opts.GenerateHelpText(env, sort=cmp)
499-
assert text == expectAlpha, text
482+
with self.subTest():
483+
self.assertEqual(expectAlpha, text)
500484

501485
textBool = opts.GenerateHelpText(env, sort=True)
502-
assert text == expectAlpha, text
486+
with self.subTest():
487+
self.assertEqual(expectAlpha, textBool)
503488

504489
textBackwards = opts.GenerateHelpText(env, sort=lambda x, y: cmp(y, x))
505-
assert textBackwards == expectBackwards, "Expected:\n%s\nGot:\n%s\n" % (
506-
textBackwards,
507-
expectBackwards,
508-
)
490+
with self.subTest():
491+
self.assertEqual(expectBackwards, textBackwards)
509492

510493
def test_FormatVariableHelpText(self) -> None:
511494
"""Test generating custom format help text"""
512495
opts = SCons.Variables.Variables()
513496

514497
def my_format(env, opt, help, default, actual, aliases) -> str:
515-
return '%s %s %s %s %s\n' % (opt, default, actual, help, aliases)
498+
return f'{opt} {default} {actual} {help} {aliases}\n'
516499

517-
opts.FormatVariableHelpText = my_format
500+
_save = opts.FormatVariableHelpText
501+
setattr(opts, 'FormatVariableHelpText', my_format)
518502

519503
opts.Add('ANSWER',
520504
'THE answer to THE question',
@@ -544,15 +528,19 @@ def my_format(env, opt, help, default, actual, aliases) -> str:
544528
"""
545529

546530
text = opts.GenerateHelpText(env)
547-
assert text == expect, text
531+
with self.subTest():
532+
self.assertEqual(expect, text)
548533

549534
expectAlpha = """\
550535
A 42 54 a - alpha test ['A']
551536
ANSWER 42 54 THE answer to THE question ['ANSWER']
552537
B 42 54 b - alpha test ['B']
553538
"""
554539
text = opts.GenerateHelpText(env, sort=cmp)
555-
assert text == expectAlpha, text
540+
with self.subTest():
541+
self.assertEqual(expectAlpha, text)
542+
543+
setattr(opts, 'FormatVariableHelpText', _save)
556544

557545
def test_Aliases(self) -> None:
558546
"""Test option aliases"""
@@ -567,12 +555,15 @@ def test_Aliases(self) -> None:
567555
env = Environment()
568556
opts.Update(env, {'ANSWER' : 'answer'})
569557

570-
assert 'ANSWER' in env
558+
with self.subTest():
559+
self.assertIn('ANSWER', env)
571560

572561
env = Environment()
573562
opts.Update(env, {'ANSWERALIAS' : 'answer'})
574563

575-
assert 'ANSWER' in env and 'ANSWERALIAS' not in env
564+
with self.subTest():
565+
self.assertIn('ANSWER', env)
566+
self.assertNotIn('ANSWERALIAS', env)
576567

577568
# test alias as a list
578569
opts = SCons.Variables.Variables()
@@ -585,12 +576,15 @@ def test_Aliases(self) -> None:
585576
env = Environment()
586577
opts.Update(env, {'ANSWER' : 'answer'})
587578

588-
assert 'ANSWER' in env
579+
with self.subTest():
580+
self.assertIn('ANSWER', env)
589581

590582
env = Environment()
591583
opts.Update(env, {'ANSWERALIAS' : 'answer'})
592584

593-
assert 'ANSWER' in env and 'ANSWERALIAS' not in env
585+
with self.subTest():
586+
self.assertIn('ANSWER', env)
587+
self.assertNotIn('ANSWERALIAS', env)
594588

595589

596590
class UnknownVariablesTestCase(unittest.TestCase):
@@ -612,8 +606,8 @@ def test_unknown(self) -> None:
612606
opts.Update(env, args)
613607

614608
r = opts.UnknownVariables()
615-
assert r == {'UNKNOWN' : 'unknown'}, r
616-
assert env['ANSWER'] == 'answer', env['ANSWER']
609+
self.assertEqual({'UNKNOWN': 'unknown'}, r)
610+
self.assertEqual('answer', env['ANSWER'])
617611

618612
def test_AddOptionUpdatesUnknown(self) -> None:
619613
"""Test updating of the 'unknown' dict"""
@@ -632,8 +626,9 @@ def test_AddOptionUpdatesUnknown(self) -> None:
632626
opts.Update(env,args)
633627

634628
r = opts.UnknownVariables()
635-
assert r == {'ADDEDLATER' : 'notaddedyet'}, r
636-
assert env['A'] == 'a', env['A']
629+
with self.subTest():
630+
self.assertEqual({'ADDEDLATER': 'notaddedyet'}, r)
631+
self.assertEqual('a', env['A'])
637632

638633
opts.Add('ADDEDLATER',
639634
'An option not present initially',
@@ -647,8 +642,9 @@ def test_AddOptionUpdatesUnknown(self) -> None:
647642
opts.Update(env, args)
648643

649644
r = opts.UnknownVariables()
650-
assert len(r) == 0, r
651-
assert env['ADDEDLATER'] == 'added', env['ADDEDLATER']
645+
with self.subTest():
646+
self.assertEqual(0, len(r))
647+
self.assertEqual('added', env['ADDEDLATER'])
652648

653649
def test_AddOptionWithAliasUpdatesUnknown(self) -> None:
654650
"""Test updating of the 'unknown' dict (with aliases)"""

0 commit comments

Comments
 (0)