Skip to content

Tests: self.assertRaises -> with self.assertRaises #1670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 18 additions & 44 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,38 +613,26 @@ def test_length_and_hash_validation(self):
# test exceptions
expected_length = snapshot_metafile.length
snapshot_metafile.length = 2345
self.assertRaises(
exceptions.LengthOrHashMismatchError,
snapshot_metafile.verify_length_and_hashes,
data,
)
with self.assertRaises(exceptions.LengthOrHashMismatchError):
snapshot_metafile.verify_length_and_hashes(data)

snapshot_metafile.length = expected_length
snapshot_metafile.hashes = {"sha256": "incorrecthash"}
self.assertRaises(
exceptions.LengthOrHashMismatchError,
snapshot_metafile.verify_length_and_hashes,
data,
)
with self.assertRaises(exceptions.LengthOrHashMismatchError):
snapshot_metafile.verify_length_and_hashes(data)

snapshot_metafile.hashes = {
"unsupported-alg": "8f88e2ba48b412c3843e9bb26e1b6f8fc9e98aceb0fbaa97ba37b4c98717d7ab"
}
self.assertRaises(
exceptions.LengthOrHashMismatchError,
snapshot_metafile.verify_length_and_hashes,
data,
)
with self.assertRaises(exceptions.LengthOrHashMismatchError):
snapshot_metafile.verify_length_and_hashes(data)

# Test wrong algorithm format (sslib.FormatError)
snapshot_metafile.hashes = {
256: "8f88e2ba48b412c3843e9bb26e1b6f8fc9e98aceb0fbaa97ba37b4c98717d7ab"
}
self.assertRaises(
exceptions.LengthOrHashMismatchError,
snapshot_metafile.verify_length_and_hashes,
data,
)
with self.assertRaises(exceptions.LengthOrHashMismatchError):
snapshot_metafile.verify_length_and_hashes(data)

# test optional length and hashes
snapshot_metafile.length = None
Expand All @@ -663,19 +651,13 @@ def test_length_and_hash_validation(self):
# test exceptions
expected_length = file1_targetfile.length
file1_targetfile.length = 2345
self.assertRaises(
exceptions.LengthOrHashMismatchError,
file1_targetfile.verify_length_and_hashes,
file1,
)
with self.assertRaises(exceptions.LengthOrHashMismatchError):
file1_targetfile.verify_length_and_hashes(file1)

file1_targetfile.length = expected_length
file1_targetfile.hashes = {"sha256": "incorrecthash"}
self.assertRaises(
exceptions.LengthOrHashMismatchError,
file1_targetfile.verify_length_and_hashes,
file1,
)
with self.assertRaises(exceptions.LengthOrHashMismatchError):
file1_targetfile.verify_length_and_hashes(file1)

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

# Test with a non-existing file
file_path = os.path.join(self.repo_dir, "targets", "file123.txt")
self.assertRaises(
FileNotFoundError,
TargetFile.from_file,
file_path,
file_path,
[sslib_hash.DEFAULT_HASH_ALGORITHM],
)
with self.assertRaises(FileNotFoundError):
TargetFile.from_file(
file_path, file_path, [sslib_hash.DEFAULT_HASH_ALGORITHM]
)

# Test with an unsupported algorithm
file_path = os.path.join(self.repo_dir, "targets", "file1.txt")
self.assertRaises(
exceptions.UnsupportedAlgorithmError,
TargetFile.from_file,
file_path,
file_path,
["123"],
)
with self.assertRaises(exceptions.UnsupportedAlgorithmError):
TargetFile.from_file(file_path, file_path, ["123"])

def test_targetfile_from_data(self):
data = b"Inline test content"
Expand Down
8 changes: 4 additions & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ def test_cleanup(self):


def test_server_exit_before_timeout(self):
self.assertRaises(utils.TestServerProcessError, utils.TestServerProcess,
logger, server='non_existing_server.py')
with self.assertRaises(utils.TestServerProcessError):
utils.TestServerProcess(logger, server='non_existing_server.py')

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


if __name__ == '__main__':
Expand Down