improve make parse_json_string_or_file test
@@ -179,13 +179,16 @@ class TestUtils(TestCase):
self.assertEqual(parse_json_string_or_file('{"foo": 32}'), {'foo': 32})
def test_parameter_file(self):
+ """
+ Verify that existing filenames get priority over valid JSON string interpretation
+ """
with TemporaryDirectory() as tempdir:
- paramfile = str(Path(tempdir, '{}')) # XXX yes, the file is called '{}'
- with open(paramfile, 'w') as f:
- f.write('{"bar": 42}')
- self.assertEqual(parse_json_string_or_file(paramfile), {'bar': 42})
- with pushd_popd(tempdir):
- self.assertEqual(parse_json_string_or_file(), {'bar': 42})
+ paramfile = Path(tempdir, '{"foo":23}') # XXX yes, the file is called '{"foo":23}'
+ paramfile.write_text('{"bar": 42}')
+ # /tmp/<var>/{"foo":23} -- exists, read file and parse as JSON
+ self.assertEqual(parse_json_string_or_file(str(paramfile)), {'bar': 42})
+ # $PWD/{"foo":23} -- does not exist, parse as json
+ self.assertEqual(parse_json_string_or_file(paramfile.name), {'foo': 23})
def test_parameters_invalid(self):
with self.assertRaisesRegex(ValueError, 'Not a valid JSON object'):
