Skip to content

Commit

Permalink
Merge pull request theupdateframework#1670 from MVrachev/assert-raise…
Browse files Browse the repository at this point in the history
…s-fix

Tests: self.assertRaises -> with self.assertRaises
  • Loading branch information
Jussi Kukkonen authored Nov 11, 2021
2 parents 45f69a2 + 3c80c5b commit fa7990c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 48 deletions.
62 changes: 18 additions & 44 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,38 +631,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 @@ -681,19 +669,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 @@ -707,23 +689,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

0 comments on commit fa7990c

Please sign in to comment.