From 733eeb27581ee6fc2a9c2d79d7b002127bde85f1 Mon Sep 17 00:00:00 2001 From: Cody Logan Date: Wed, 8 Nov 2023 17:06:33 -0800 Subject: Change to a temporary directory before running tests Some tests will throw errors if files or directories with certain names exist in the current working directory when pytest is run, such as Example.jpg. --- tests/conftest.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index 5fccfc0..8b1fea9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -17,12 +17,25 @@ """Define fixtures used across all tests in this folder.""" +from os import chdir +from pathlib import Path + import pytest import requests_mock as rm from wikiget.file import File +@pytest.fixture(autouse=True) +def _chdir_to_tmp_dir(tmp_path: Path) -> None: + """Change to a temporary directory before running tests. + + :param tmp_path: temporary path object + :type tmp_path: Path + """ + chdir(tmp_path) + + @pytest.fixture() def file_with_name() -> File: """Create a test File with only a filename. -- cgit v1.2.3 From 605085c95fe5ab6af337aefc723794f78cf3dfd7 Mon Sep 17 00:00:00 2001 From: Cody Logan Date: Tue, 14 Nov 2023 16:44:34 -0800 Subject: Use fixtures to create test files This reduces the number of temporary folders and files created during testing. Additionally, an actual JPEG is created for a couple tests instead of using random text for the contents. --- tests/conftest.py | 73 ++++++++++++++++++++++++++++++++++++++++------- tests/test_dl.py | 12 +++----- tests/test_logging.py | 4 +-- tests/test_parse.py | 28 +++++++----------- tests/test_validations.py | 15 ++++------ 5 files changed, 86 insertions(+), 46 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 8b1fea9..3eb99cd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -25,15 +25,68 @@ import requests_mock as rm from wikiget.file import File +# 2x2 JPEG +TEST_FILE_BYTES = ( + b"\xff\xd8\xff\xdb\x00C\x00\x03\x02\x02\x02\x02\x02\x03\x02\x02\x02\x03\x03\x03\x03" + b"\x04\x06\x04\x04\x04\x04\x04\x08\x06\x06\x05\x06\t\x08\n\n\t\x08\t\t\n\x0c\x0f" + b"\x0c\n\x0b\x0e\x0b\t\t\r\x11\r\x0e\x0f\x10\x10\x11\x10\n\x0c\x12\x13\x12\x10\x13" + b"\x0f\x10\x10\x10\xff\xc0\x00\x0b\x08\x00\x02\x00\x02\x01\x01\x11\x00\xff\xc4\x00" + b"\x14\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\t\xff" + b"\xc4\x00\x14\x10\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + b"\x00\xff\xda\x00\x08\x01\x01\x00\x00?\x00T\xdf\xff\xd9" +) + + +@pytest.fixture(autouse=True, scope="session") +def _chdir_to_tmp_dir(tmp_path_factory: pytest.TempPathFactory) -> None: + """Change to the base temporary directory before running tests. + + :param tmp_path_factory: temporary path generator + :type tmp_path_factory: pytest.TempPathFactory + """ + chdir(tmp_path_factory.getbasetemp()) + + +@pytest.fixture(scope="session") +def batch_file(tmp_path_factory: pytest.TempPathFactory) -> Path: + """Create a temporary batch file for testing. + + :param tmp_path_factory: temporary path generator + :type tmp_path_factory: pytest.TempPathFactory + :return: test batch file + :rtype: pathlib.Path + """ + tmp_file = tmp_path_factory.getbasetemp() / "batch.txt" + tmp_file.write_text("File:Foo.jpg\nFile:Bar.jpg\nFile:Baz.jpg\n") + return tmp_file + + +@pytest.fixture(scope="session") +def batch_file_with_comment(tmp_path_factory: pytest.TempPathFactory) -> Path: + """Create a temporary batch file with comments for testing. + + :param tmp_path_factory: temporary path generator + :type tmp_path_factory: pytest.TempPathFactory + :return: test batch file + :rtype: pathlib.Path + """ + tmp_file = tmp_path_factory.getbasetemp() / "batch_with_comment.txt" + tmp_file.write_text("File:Foo.jpg\n\n#File:Bar.jpg\nFile:Baz.jpg\n") + return tmp_file + -@pytest.fixture(autouse=True) -def _chdir_to_tmp_dir(tmp_path: Path) -> None: - """Change to a temporary directory before running tests. +@pytest.fixture(scope="session") +def test_file(tmp_path_factory: pytest.TempPathFactory) -> Path: + """Create a fake downloaded file with known contents. - :param tmp_path: temporary path object - :type tmp_path: Path + :param tmp_path_factory: temporary path generator + :type tmp_path_factory: pytest.TempPathFactory + :return: test file + :rtype: pathlib.Path """ - chdir(tmp_path) + tmp_file = tmp_path_factory.getbasetemp() / "Testfile.jpg" + tmp_file.write_bytes(TEST_FILE_BYTES) + return tmp_file @pytest.fixture() @@ -44,7 +97,7 @@ def file_with_name() -> File: the same value and its site property to the program's default site. :return: File object created using a filename - :rtype: File + :rtype: wikiget.file.File """ return File("foobar.jpg") @@ -54,7 +107,7 @@ def file_with_name_and_dest() -> File: """Create a test File with a name and destination. :return: File object created with name and dest - :rtype: File + :rtype: wikiget.file.File """ return File(name="foobar.jpg", dest="bazqux.jpg") @@ -64,9 +117,9 @@ def _mock_get(requests_mock: rm.Mocker) -> None: """Fake the download request for the true URL of File:Example.jpg. :param requests_mock: a requests_mock Mocker object - :type requests_mock: rm.Mocker + :type requests_mock: requests_mock.Mocker """ requests_mock.get( "https://upload.wikimedia.org/wikipedia/commons/a/a9/Example.jpg", - text="data", + content=TEST_FILE_BYTES, ) diff --git a/tests/test_dl.py b/tests/test_dl.py index d7d5d77..cb5f0b6 100644 --- a/tests/test_dl.py +++ b/tests/test_dl.py @@ -54,15 +54,13 @@ class TestPrepDownload: assert file == expected_file - def test_prep_download_with_existing_file(self, tmp_path: Path) -> None: + def test_prep_download_with_existing_file(self, test_file: Path) -> None: """Test that an exception is raised when the download file already exists. Attempting to download a file with the same destination name as an existing file should raise a FileExistsError. """ - tmp_file = tmp_path / "File:Example.jpg" - tmp_file.write_text("nothing") - args = parse_args(["File:Example.jpg", "-o", str(tmp_file)]) + args = parse_args(["File:Example.jpg", "-o", str(test_file)]) with pytest.raises(FileExistsError): _ = prep_download(args.FILE, args) @@ -301,15 +299,13 @@ class TestDownload: """Define tests related to wikiget.dl.download.""" @pytest.fixture() - def mock_file(self, tmp_path: Path) -> File: + def mock_file(self) -> File: """Create a mock File object to test against. - :param tmp_path: temporary path object - :type tmp_path: Path :return: mock File object :rtype: File """ - file = File(name="Example.jpg", dest=str(tmp_path / "Example.jpg")) + file = File(name="Example.jpg") file.image = Mock() file.image.exists = True file.image.imageinfo = { diff --git a/tests/test_logging.py b/tests/test_logging.py index 2fd95cd..8d58cdf 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -39,9 +39,9 @@ class TestLogging: adapter.warning("test log") assert "[Example.jpg] test log" in caplog.text - def test_file_logging(self, tmp_path: Path) -> None: + def test_file_logging(self) -> None: """Logging to a file should create the file in the specified location.""" - logfile_location = tmp_path / "test.log" + logfile_location = Path("test.log") args = parse_args(["File:Example.jpg", "-l", str(logfile_location)]) configure_logging(args.verbose, args.logfile, quiet=args.quiet) assert logfile_location.is_file() diff --git a/tests/test_parse.py b/tests/test_parse.py index fbbd1b7..4f74ac9 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -126,20 +126,18 @@ class TestReadBatchFile: """Define tests related to wikiget.parse.read_batch_file.""" @pytest.fixture() - def dl_dict(self, tmp_path: Path) -> dict[int, str]: + def dl_dict(self, batch_file: Path) -> dict[int, str]: """Create and process a test batch file with three lines. - :param tmp_path: temporary path object - :type tmp_path: Path + :param batch_file: test batch file + :type batch_file: Path :return: dictionary representation of the input file :rtype: dict[int, str] """ - tmp_file = tmp_path / "batch.txt" - tmp_file.write_text("File:Foo.jpg\nFile:Bar.jpg\nFile:Baz.jpg\n") - return read_batch_file(str(tmp_file)) + return read_batch_file(str(batch_file)) def test_batch_file_log( - self, caplog: pytest.LogCaptureFixture, tmp_path: Path + self, caplog: pytest.LogCaptureFixture, batch_file: Path ) -> None: """Test that reading a batch file creates an info log message. @@ -147,10 +145,8 @@ class TestReadBatchFile: of the batch file. """ caplog.set_level(logging.INFO) - tmp_file = tmp_path / "batch.txt" - tmp_file.write_text("File:Foo.jpg\n") - _ = read_batch_file(str(tmp_file)) - assert f"Using file '{tmp_file}' for batch download" in caplog.text + _ = read_batch_file(str(batch_file)) + assert f"Using file '{batch_file}' for batch download" in caplog.text def test_batch_file_length(self, dl_dict: dict[int, str]) -> None: """Test that the batch dict has the same number of lines as the batch file.""" @@ -205,19 +201,17 @@ class TestReadBatchFile: assert dl_dict_stdin == expected_list @pytest.fixture() - def dl_dict_with_comment(self, tmp_path: Path) -> dict[int, str]: + def dl_dict_with_comment(self, batch_file_with_comment: Path) -> dict[int, str]: """Create and process a test batch file with four lines. In addition to filenames, one line is commented out and another line is blank. - :param tmp_path: temporary path object - :type tmp_path: Path + :param batch_file_with_comment: test batch file + :type batch_file_with_comment: Path :return: dictionary representation of the input file :rtype: dict[int, str] """ - tmp_file = tmp_path / "batch.txt" - tmp_file.write_text("File:Foo.jpg\n\n#File:Bar.jpg\nFile:Baz.jpg\n") - return read_batch_file(str(tmp_file)) + return read_batch_file(str(batch_file_with_comment)) def test_batch_file_with_comment_length( self, dl_dict_with_comment: dict[int, str] diff --git a/tests/test_validations.py b/tests/test_validations.py index 161d102..aa4fdd0 100644 --- a/tests/test_validations.py +++ b/tests/test_validations.py @@ -163,14 +163,11 @@ class TestFileInput: class TestVerifyHash: """Define tests related to wikiget.validations.verify_hash.""" - def test_verify_hash(self, tmp_path: Path) -> None: - """Confirm that verify_hash returns the proper SHA1 hash.""" - file_name = "testfile" - file_contents = "foobar" - file_sha1 = "8843d7f92416211de9ebb963ff4ce28125932878" + def test_verify_hash(self, test_file: Path) -> None: + """Confirm that verify_hash returns the proper SHA1 hash. - # create a temporary file with known contents - tmp_file = tmp_path / file_name - tmp_file.write_text(file_contents) + The test file used here is generated by a fixture in conftest.py. + """ + expected_sha1 = "cd19c009a30ca9b68045415a3a0838e64f3c2443" - assert verify_hash(str(tmp_file)) == file_sha1 + assert verify_hash(str(test_file)) == expected_sha1 -- cgit v1.2.3 From c459e3bb9553e31224348b055aaaec3a567613bb Mon Sep 17 00:00:00 2001 From: Cody Logan Date: Tue, 14 Nov 2023 16:56:15 -0800 Subject: Docstring type fixes --- src/wikiget/dl.py | 4 ++-- src/wikiget/file.py | 2 +- src/wikiget/parse.py | 2 +- tests/test_parse.py | 8 ++++---- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/wikiget/dl.py b/src/wikiget/dl.py index abd0533..9b2777f 100644 --- a/src/wikiget/dl.py +++ b/src/wikiget/dl.py @@ -49,7 +49,7 @@ def prep_download(dl: str, args: Namespace) -> File: :type args: argparse.Namespace :raises FileExistsError: the destination file already exists on disk :return: a File object representing the file to download - :rtype: File + :rtype: wikiget.file.File """ file = get_dest(dl, args) @@ -169,7 +169,7 @@ def download(f: File, args: Namespace) -> int: """Fetch file information and contents if the file exists and save it to disk. :param f: a File object representing the file to be downloaded - :type f: File + :type f: wikiget.file.File :param args: command-line arguments and their values :type args: argparse.Namespace :return: number of errors encountered during processing diff --git a/src/wikiget/file.py b/src/wikiget/file.py index f2320b2..cbd738a 100644 --- a/src/wikiget/file.py +++ b/src/wikiget/file.py @@ -54,7 +54,7 @@ class File: """Compare this File object with another for equality. :param other: another File to compare - :type other: File + :type other: wikiget.file.File :return: True if the Files are equal and False otherwise :rtype: bool """ diff --git a/src/wikiget/parse.py b/src/wikiget/parse.py index 92726f0..1deaacf 100644 --- a/src/wikiget/parse.py +++ b/src/wikiget/parse.py @@ -42,7 +42,7 @@ def get_dest(dl: str, args: Namespace) -> File: :type args: argparse.Namespace :raises ParseError: the target was unable to be parsed as a valid file :return: a File object representing the target, destination, and site - :rtype: File + :rtype: wikiget.file.File """ url = urlparse(dl) diff --git a/tests/test_parse.py b/tests/test_parse.py index 4f74ac9..fb824d4 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -46,7 +46,7 @@ class TestGetDest: to the same value as the filename and the default site will be used. :return: a File object created using a filename - :rtype: File + :rtype: wikiget.file.File """ args = parse_args(["File:Example.jpg"]) return get_dest(args.FILE, args) @@ -77,7 +77,7 @@ class TestGetDest: filename and site parsed from the URL. :return: a File object created using a URL - :rtype: File + :rtype: wikiget.file.File """ args = parse_args(["https://en.wikipedia.org/wiki/File:Example.jpg"]) return get_dest(args.FILE, args) @@ -130,7 +130,7 @@ class TestReadBatchFile: """Create and process a test batch file with three lines. :param batch_file: test batch file - :type batch_file: Path + :type batch_file: pathlib.Path :return: dictionary representation of the input file :rtype: dict[int, str] """ @@ -207,7 +207,7 @@ class TestReadBatchFile: In addition to filenames, one line is commented out and another line is blank. :param batch_file_with_comment: test batch file - :type batch_file_with_comment: Path + :type batch_file_with_comment: pathlib.Path :return: dictionary representation of the input file :rtype: dict[int, str] """ -- cgit v1.2.3 From 5ff82fcacf80f38395edc4f83fdc42b9670d87fd Mon Sep 17 00:00:00 2001 From: Cody Logan Date: Wed, 15 Nov 2023 09:41:06 -0800 Subject: Use monkeypatch.chdir instead of os.chdir --- tests/conftest.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 3eb99cd..6088029 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -17,7 +17,6 @@ """Define fixtures used across all tests in this folder.""" -from os import chdir from pathlib import Path import pytest @@ -37,14 +36,18 @@ TEST_FILE_BYTES = ( ) -@pytest.fixture(autouse=True, scope="session") -def _chdir_to_tmp_dir(tmp_path_factory: pytest.TempPathFactory) -> None: +@pytest.fixture(autouse=True) +def _chdir_to_tmp_dir( + tmp_path_factory: pytest.TempPathFactory, monkeypatch: pytest.MonkeyPatch +) -> None: """Change to the base temporary directory before running tests. :param tmp_path_factory: temporary path generator :type tmp_path_factory: pytest.TempPathFactory + :param tmp_path_factory: Pytest monkeypatch helper + :type tmp_path_factory: pytest.MonkeyPatch """ - chdir(tmp_path_factory.getbasetemp()) + monkeypatch.chdir(tmp_path_factory.getbasetemp()) @pytest.fixture(scope="session") -- cgit v1.2.3 From ba782d54aa70a1faf6bf6cdcaeb446f7b4c503db Mon Sep 17 00:00:00 2001 From: Cody Logan Date: Wed, 15 Nov 2023 09:52:00 -0800 Subject: Add missing type annotations --- src/wikiget/logging.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/wikiget/logging.py b/src/wikiget/logging.py index 6673877..6614a15 100644 --- a/src/wikiget/logging.py +++ b/src/wikiget/logging.py @@ -15,13 +15,18 @@ # You should have received a copy of the GNU General Public License # along with Wikiget. If not, see . +from __future__ import annotations + import logging +from typing import Any, MutableMapping import wikiget class FileLogAdapter(logging.LoggerAdapter): - def process(self, msg, kwargs): + def process( + self, msg: Any, kwargs: MutableMapping[str, Any] + ) -> tuple[str, MutableMapping[str, Any]]: return f"[{self.extra['filename']}] {msg}", kwargs -- cgit v1.2.3