Skip to content

Commit 002c828

Browse files
authored
Merge pull request #1710 from MVrachev/start-linting
Start linting the test
2 parents d3b877b + 28602e4 commit 002c828

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+178
-102
lines changed

docs/CONTRIBUTORS.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ Individual tests can also be executed. Optional '-v' flags can be added to
139139
increase log level up to DEBUG ('-vvvv').
140140
::
141141

142-
$ python3 test_updater.py # run a specific test file
143-
$ python3 test_updater.py TestUpdater.test_4_refresh # run a specific test
144-
$ python3 test_updater.py -vvvv TestUpdater.test_4_refresh # run test with DEBUG log level
142+
$ python3 test_updater_ng.py # run a specific test file
143+
$ python3 test_updater_ng.py TestUpdater.test_refresh_and_download # run a specific test
144+
$ python3 test_updater_ng.py -vvvv TestUpdater.test_refresh_and_download # run test with DEBUG log level
145145

146146

147147
All of the log levels and the corresponding options that could be used for testing are:

pyproject.toml

+9
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ build-backend = "setuptools.build_meta"
77
# Read more here: https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#configuration-via-a-file
88
[tool.black]
99
line-length=80
10+
# TODO: remove "excludes" after deleting old test files
11+
exclude="tests/.*old.py"
1012

1113
# Isort section
1214
# Read more here: https://pycqa.github.io/isort/docs/configuration/config_files.html
1315
[tool.isort]
1416
profile="black"
1517
line_length=80
1618
known_first_party = ["tuf"]
19+
# TODO: remove "skip_glob" after deleting old test files
20+
skip_glob="*old.py"
1721

1822
# Pylint section
1923

@@ -55,6 +59,8 @@ module-rgx="^(_?[a-z][a-z0-9_]*|__init__)$"
5559
no-docstring-rgx="(__.*__|main|test.*|.*test|.*Test)$"
5660
variable-rgx="^[a-z][a-z0-9_]*$"
5761
docstring-min-length=10
62+
# TODO: remove "ignore-patterns" after deleting old test files
63+
ignore-patterns=".*_old.py"
5864

5965
[tool.pylint.logging]
6066
logging-format-style="old"
@@ -76,6 +82,9 @@ strict_equality = "True"
7682
disallow_untyped_defs = "True"
7783
disallow_untyped_calls = "True"
7884
show_error_codes = "True"
85+
disable_error_code = ["attr-defined"]
86+
# TODO: remove "exclude" after deleting old test files
87+
exclude=".*_old.py"
7988

8089
[[tool.mypy.overrides]]
8190
module = [

requirements-test.txt

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ pylint
1414
mypy
1515
bandit
1616
types-requests
17+
types-python-dateutil
File renamed without changes.

tests/repository_data/generate_project_data.py renamed to tests/repository_data/generate_project_data_old.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
See LICENSE-MIT.txt OR LICENSE-APACHE.txt for licensing information.
1818
1919
<Purpose>
20-
Generate a pre-fabricated set of metadata files for 'test_developer_tool.py'
21-
test cases.
20+
Generate a pre-fabricated set of metadata files for
21+
'test_developer_tool_old.py' test cases.
2222
"""
2323

2424
import shutil

tests/repository_simulator.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def fetch(self, url: str) -> Iterator[bytes]:
216216
role = ver_and_name
217217
version = None
218218

219-
yield self._fetch_metadata(role, version)
219+
yield self.fetch_metadata(role, version)
220220
elif path.startswith("/targets/"):
221221
# figure out target path and hash prefix
222222
target_path = path[len("/targets/") :]
@@ -228,11 +228,11 @@ def fetch(self, url: str) -> Iterator[bytes]:
228228
prefix, _, filename = prefixed_filename.partition(".")
229229
target_path = f"{dir_parts}{sep}{filename}"
230230

231-
yield self._fetch_target(target_path, prefix)
231+
yield self.fetch_target(target_path, prefix)
232232
else:
233233
raise FetcherHTTPError(f"Unknown path '{path}'", 404)
234234

235-
def _fetch_target(
235+
def fetch_target(
236236
self, target_path: str, target_hash: Optional[str]
237237
) -> bytes:
238238
"""Return data for 'target_path', checking 'target_hash' if it is given.
@@ -253,9 +253,7 @@ def _fetch_target(
253253
logger.debug("fetched target %s", target_path)
254254
return repo_target.data
255255

256-
def _fetch_metadata(
257-
self, role: str, version: Optional[int] = None
258-
) -> bytes:
256+
def fetch_metadata(self, role: str, version: Optional[int] = None) -> bytes:
259257
"""Return signed metadata for 'role', using 'version' if it is given.
260258
261259
If version is None, non-versioned metadata is being requested.
@@ -298,7 +296,7 @@ def _fetch_metadata(
298296
def _compute_hashes_and_length(
299297
self, role: str
300298
) -> Tuple[Dict[str, str], int]:
301-
data = self._fetch_metadata(role)
299+
data = self.fetch_metadata(role)
302300
digest_object = sslib_hash.digest(sslib_hash.DEFAULT_HASH_ALGORITHM)
303301
digest_object.update(data)
304302
hashes = {sslib_hash.DEFAULT_HASH_ALGORITHM: digest_object.hexdigest()}
@@ -392,12 +390,12 @@ def write(self) -> None:
392390

393391
for ver in range(1, len(self.signed_roots) + 1):
394392
with open(os.path.join(dest_dir, f"{ver}.root.json"), "wb") as f:
395-
f.write(self._fetch_metadata(Root.type, ver))
393+
f.write(self.fetch_metadata(Root.type, ver))
396394

397395
for role in [Timestamp.type, Snapshot.type, Targets.type]:
398396
with open(os.path.join(dest_dir, f"{role}.json"), "wb") as f:
399-
f.write(self._fetch_metadata(role))
397+
f.write(self.fetch_metadata(role))
400398

401399
for role in self.md_delegates:
402400
with open(os.path.join(dest_dir, f"{role}.json"), "wb") as f:
403-
f.write(self._fetch_metadata(role))
401+
f.write(self.fetch_metadata(role))

tests/simple_https_server.py renamed to tests/simple_https_server_old.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
"""
77
<Program>
8-
simple_https_server.py
8+
simple_https_server_old.py
99
1010
<Author>
1111
Vladimir Diaz.
@@ -53,7 +53,7 @@
5353
print(port_message)
5454

5555
if len(sys.argv) > 1 and certfile != sys.argv[1]:
56-
print('simple_https_server: cert file was not found: ' + sys.argv[1] +
56+
print('simple_https_server_old: cert file was not found: ' + sys.argv[1] +
5757
'; using default: ' + certfile + " certfile")
5858

5959
httpd.serve_forever()

tests/slow_retrieval_server.py renamed to tests/slow_retrieval_server_old.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
"""
77
<Program Name>
8-
slow_retrieval_server.py
8+
slow_retrieval_server_old.py
99
1010
<Author>
1111
Konstantin Andrianov.
@@ -18,7 +18,7 @@
1818
1919
<Purpose>
2020
Server that throttles data by sending one byte at a time (specified time
21-
interval 'DELAY'). The server is used in 'test_slow_retrieval_attack.py'.
21+
interval 'DELAY'). The server is used in 'test_slow_retrieval_attack_old.py'.
2222
"""
2323

2424
import os

tests/test_api.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from tests import utils
2929
from tuf import exceptions
3030
from tuf.api.metadata import (
31+
TOP_LEVEL_ROLE_NAMES,
3132
DelegatedRole,
3233
Delegations,
3334
Key,
@@ -136,7 +137,7 @@ def test_compact_json(self) -> None:
136137
)
137138

138139
def test_read_write_read_compare(self) -> None:
139-
for metadata in [Root.type, Snapshot.type, Timestamp.type, Targets.type]:
140+
for metadata in TOP_LEVEL_ROLE_NAMES:
140141
path = os.path.join(self.repo_dir, "metadata", metadata + ".json")
141142
md_obj = Metadata.from_file(path)
142143

@@ -148,7 +149,7 @@ def test_read_write_read_compare(self) -> None:
148149
os.remove(path_2)
149150

150151
def test_to_from_bytes(self) -> None:
151-
for metadata in [Root.type, Snapshot.type, Timestamp.type, Targets.type]:
152+
for metadata in TOP_LEVEL_ROLE_NAMES:
152153
path = os.path.join(self.repo_dir, "metadata", metadata + ".json")
153154
with open(path, "rb") as f:
154155
metadata_bytes = f.read()
@@ -710,7 +711,9 @@ def test_targetfile_from_file(self) -> None:
710711

711712
def test_targetfile_from_data(self) -> None:
712713
data = b"Inline test content"
713-
target_file_path = os.path.join(self.repo_dir, Targets.type, "file1.txt")
714+
target_file_path = os.path.join(
715+
self.repo_dir, Targets.type, "file1.txt"
716+
)
714717

715718
# Test with a valid hash algorithm
716719
targetfile_from_data = TargetFile.from_data(

tests/test_arbitrary_package_attack.py renamed to tests/test_arbitrary_package_attack_old.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
"""
77
<Program Name>
8-
test_arbitrary_package_attack.py
8+
test_arbitrary_package_attack_old.py
99
1010
<Author>
1111
Konstantin Andrianov.

tests/test_developer_tool.py renamed to tests/test_developer_tool_old.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
"""
77
<Program Name>
8-
test_developer_tool.py.
8+
test_developer_tool_old.py.
99
1010
<Authors>
1111
Santiago Torres Arias <torresariass@gmail.com>

tests/test_download.py renamed to tests/test_download_old.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
"""
77
<Program>
8-
test_download.py
8+
test_download_old.py
99
1010
<Author>
1111
Konstantin Andrianov.
@@ -19,7 +19,7 @@
1919
<Purpose>
2020
Unit test for 'download.py'.
2121
22-
NOTE: Make sure test_download.py is ran in 'tuf/tests/' directory.
22+
NOTE: Make sure test_download_old.py is ran in 'tuf/tests/' directory.
2323
Otherwise, module that launches simple server would not be found.
2424
2525
TODO: Adopt the environment variable management from test_proxy_use.py here.
@@ -274,16 +274,16 @@ def test_https_connection(self):
274274

275275

276276
good_https_server_handler = utils.TestServerProcess(log=logger,
277-
server='simple_https_server.py',
277+
server='simple_https_server_old.py',
278278
extra_cmd_args=[good_cert_fname])
279279
good2_https_server_handler = utils.TestServerProcess(log=logger,
280-
server='simple_https_server.py',
280+
server='simple_https_server_old.py',
281281
extra_cmd_args=[good2_cert_fname])
282282
bad_https_server_handler = utils.TestServerProcess(log=logger,
283-
server='simple_https_server.py',
283+
server='simple_https_server_old.py',
284284
extra_cmd_args=[bad_cert_fname])
285285
expd_https_server_handler = utils.TestServerProcess(log=logger,
286-
server='simple_https_server.py',
286+
server='simple_https_server_old.py',
287287
extra_cmd_args=[expired_cert_fname])
288288

289289
suffix = '/' + os.path.basename(target_filepath)

tests/test_endless_data_attack.py renamed to tests/test_endless_data_attack_old.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
"""
77
<Program Name>
8-
test_endless_data_attack.py
8+
test_endless_data_attack_old.py
99
1010
<Author>
1111
Konstantin Andrianov.

tests/test_examples.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
import glob
88
import os
99
import shutil
10+
import sys
1011
import tempfile
1112
import unittest
1213
from pathlib import Path
14+
from typing import ClassVar, List
15+
16+
from tests import utils
1317

1418

1519
class TestRepoExamples(unittest.TestCase):
@@ -20,26 +24,30 @@ class TestRepoExamples(unittest.TestCase):
2024
2125
"""
2226

27+
repo_examples_dir: ClassVar[Path]
28+
2329
@classmethod
24-
def setUpClass(cls):
30+
def setUpClass(cls) -> None:
2531
"""Locate and cache 'repo_example' dir."""
2632
base = Path(__file__).resolve().parents[1]
2733
cls.repo_examples_dir = base / "examples" / "repo_example"
2834

29-
def setUp(self):
35+
def setUp(self) -> None:
3036
"""Create and change into test dir.
3137
NOTE: Test scripts are expected to create dirs/files in new CWD."""
3238
self.original_cwd = os.getcwd()
3339
self.base_test_dir = os.path.realpath(tempfile.mkdtemp())
3440
os.chdir(self.base_test_dir)
3541

36-
def tearDown(self):
42+
def tearDown(self) -> None:
3743
"""Change back to original dir and remove test dir, which may contain
3844
dirs/files the test created at test-time CWD."""
3945
os.chdir(self.original_cwd)
4046
shutil.rmtree(self.base_test_dir)
4147

42-
def _run_script_and_assert_files(self, script_name, filenames_created):
48+
def _run_script_and_assert_files(
49+
self, script_name: str, filenames_created: List[str]
50+
) -> None:
4351
"""Run script in 'repo_example' dir and assert that it created the
4452
files corresponding to the passed filenames inside a 'tmp*' test dir at
4553
CWD."""
@@ -63,7 +71,7 @@ def _run_script_and_assert_files(self, script_name, filenames_created):
6371
metadata_path.exists(), f"missing '{metadata_path}' file"
6472
)
6573

66-
def test_basic_repo(self):
74+
def test_basic_repo(self) -> None:
6775
"""Run 'basic_repo.py' and assert creation of metadata files."""
6876
self._run_script_and_assert_files(
6977
"basic_repo.py",
@@ -81,4 +89,5 @@ def test_basic_repo(self):
8189

8290

8391
if __name__ == "__main__":
92+
utils.configure_test_logging(sys.argv)
8493
unittest.main()

tests/test_extraneous_dependencies_attack.py renamed to tests/test_extraneous_dependencies_attack_old.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
"""
77
<Program Name>
8-
test_extraneous_dependencies_attack.py
8+
test_extraneous_dependencies_attack_old.py
99
1010
<Author>
1111
Zane Fisher.
File renamed without changes.

tests/test_formats.py renamed to tests/test_formats_old.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
"""
77
<Program Name>
8-
test_formats.py
8+
test_formats_old.py
99
1010
<Author>
1111
Vladimir Diaz <vladimir.v.diaz@gmail.com>

tests/test_indefinite_freeze_attack.py renamed to tests/test_indefinite_freeze_attack_old.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
"""
77
<Program Name>
8-
test_indefinite_freeze_attack.py
8+
test_indefinite_freeze_attack_old.py
99
1010
<Author>
1111
Konstantin Andrianov.
@@ -36,7 +36,6 @@
3636
metadata without the client being aware.
3737
"""
3838

39-
import datetime
4039
import os
4140
import time
4241
import tempfile

tests/test_key_revocation_integration.py renamed to tests/test_key_revocation_integration_old.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
"""
77
<Program Name>
8-
test_key_revocation_integration.py
8+
test_key_revocation_integration_old.py
99
1010
<Author>
1111
Vladimir Diaz.
@@ -18,7 +18,7 @@
1818
1919
<Purpose>
2020
Integration test that verifies top-level roles are updated after all of their
21-
keys have been revoked. There are unit tests in 'test_repository_tool.py'
21+
keys have been revoked. There are unit tests in 'test_repository_tool_old.py'
2222
that verify key and role revocation of specific roles, but these should be
2323
expanded to verify key revocations over the span of multiple snapshots of the
2424
repository.

tests/test_keydb.py renamed to tests/test_keydb_old.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
"""
77
<Program Name>
8-
test_keydb.py
8+
test_keydb_old.py
99
1010
<Author>
1111
Vladimir Diaz <vladimir.v.diaz@gmail.com>

tests/test_log.py renamed to tests/test_log_old.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
"""
77
<Program Name>
8-
test_log.py
8+
test_log_old.py
99
1010
<Authors>
1111
Vladimir Diaz <vladimir.v.diaz@gmail.com>

0 commit comments

Comments
 (0)