Skip to content

Commit 3c80c5b

Browse files
committed
Tests: self.assertRaises -> with self.assertRaises
Change the instances of "self.assertRaises" to "with self.assertRaises" as using "self.assertRaises" can lead to long statements separated to multiline expressions as pointed out by Jussi here: #1658 (comment) On another hand "with self.assertRaises()" looks a lot better: https://github.com/theupdateframework/python-tuf/blob/589ed9e0d48aad9acaea912f409e1445d5913416/tests/test_api.py#L131 Signed-off-by: Martin Vrachev <[email protected]>
1 parent 0088ebd commit 3c80c5b

File tree

2 files changed

+22
-48
lines changed

2 files changed

+22
-48
lines changed

tests/test_api.py

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -613,38 +613,26 @@ def test_length_and_hash_validation(self):
613613
# test exceptions
614614
expected_length = snapshot_metafile.length
615615
snapshot_metafile.length = 2345
616-
self.assertRaises(
617-
exceptions.LengthOrHashMismatchError,
618-
snapshot_metafile.verify_length_and_hashes,
619-
data,
620-
)
616+
with self.assertRaises(exceptions.LengthOrHashMismatchError):
617+
snapshot_metafile.verify_length_and_hashes(data)
621618

622619
snapshot_metafile.length = expected_length
623620
snapshot_metafile.hashes = {"sha256": "incorrecthash"}
624-
self.assertRaises(
625-
exceptions.LengthOrHashMismatchError,
626-
snapshot_metafile.verify_length_and_hashes,
627-
data,
628-
)
621+
with self.assertRaises(exceptions.LengthOrHashMismatchError):
622+
snapshot_metafile.verify_length_and_hashes(data)
629623

630624
snapshot_metafile.hashes = {
631625
"unsupported-alg": "8f88e2ba48b412c3843e9bb26e1b6f8fc9e98aceb0fbaa97ba37b4c98717d7ab"
632626
}
633-
self.assertRaises(
634-
exceptions.LengthOrHashMismatchError,
635-
snapshot_metafile.verify_length_and_hashes,
636-
data,
637-
)
627+
with self.assertRaises(exceptions.LengthOrHashMismatchError):
628+
snapshot_metafile.verify_length_and_hashes(data)
638629

639630
# Test wrong algorithm format (sslib.FormatError)
640631
snapshot_metafile.hashes = {
641632
256: "8f88e2ba48b412c3843e9bb26e1b6f8fc9e98aceb0fbaa97ba37b4c98717d7ab"
642633
}
643-
self.assertRaises(
644-
exceptions.LengthOrHashMismatchError,
645-
snapshot_metafile.verify_length_and_hashes,
646-
data,
647-
)
634+
with self.assertRaises(exceptions.LengthOrHashMismatchError):
635+
snapshot_metafile.verify_length_and_hashes(data)
648636

649637
# test optional length and hashes
650638
snapshot_metafile.length = None
@@ -663,19 +651,13 @@ def test_length_and_hash_validation(self):
663651
# test exceptions
664652
expected_length = file1_targetfile.length
665653
file1_targetfile.length = 2345
666-
self.assertRaises(
667-
exceptions.LengthOrHashMismatchError,
668-
file1_targetfile.verify_length_and_hashes,
669-
file1,
670-
)
654+
with self.assertRaises(exceptions.LengthOrHashMismatchError):
655+
file1_targetfile.verify_length_and_hashes(file1)
671656

672657
file1_targetfile.length = expected_length
673658
file1_targetfile.hashes = {"sha256": "incorrecthash"}
674-
self.assertRaises(
675-
exceptions.LengthOrHashMismatchError,
676-
file1_targetfile.verify_length_and_hashes,
677-
file1,
678-
)
659+
with self.assertRaises(exceptions.LengthOrHashMismatchError):
660+
file1_targetfile.verify_length_and_hashes(file1)
679661

680662
def test_targetfile_from_file(self):
681663
# Test with an existing file and valid hash algorithm
@@ -689,23 +671,15 @@ def test_targetfile_from_file(self):
689671

690672
# Test with a non-existing file
691673
file_path = os.path.join(self.repo_dir, "targets", "file123.txt")
692-
self.assertRaises(
693-
FileNotFoundError,
694-
TargetFile.from_file,
695-
file_path,
696-
file_path,
697-
[sslib_hash.DEFAULT_HASH_ALGORITHM],
698-
)
674+
with self.assertRaises(FileNotFoundError):
675+
TargetFile.from_file(
676+
file_path, file_path, [sslib_hash.DEFAULT_HASH_ALGORITHM]
677+
)
699678

700679
# Test with an unsupported algorithm
701680
file_path = os.path.join(self.repo_dir, "targets", "file1.txt")
702-
self.assertRaises(
703-
exceptions.UnsupportedAlgorithmError,
704-
TargetFile.from_file,
705-
file_path,
706-
file_path,
707-
["123"],
708-
)
681+
with self.assertRaises(exceptions.UnsupportedAlgorithmError):
682+
TargetFile.from_file(file_path, file_path, ["123"])
709683

710684
def test_targetfile_from_data(self):
711685
data = b"Inline test content"

tests/test_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ def test_cleanup(self):
110110

111111

112112
def test_server_exit_before_timeout(self):
113-
self.assertRaises(utils.TestServerProcessError, utils.TestServerProcess,
114-
logger, server='non_existing_server.py')
113+
with self.assertRaises(utils.TestServerProcessError):
114+
utils.TestServerProcess(logger, server='non_existing_server.py')
115115

116116
# Test starting a server which immediately exits."
117-
self.assertRaises(utils.TestServerProcessError, utils.TestServerProcess,
118-
logger, server='fast_server_exit.py')
117+
with self.assertRaises(utils.TestServerProcessError):
118+
utils.TestServerProcess(logger, server='fast_server_exit.py')
119119

120120

121121
if __name__ == '__main__':

0 commit comments

Comments
 (0)