aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCody Logan <cody@lokken.dev>2023-11-15 10:02:48 -0800
committerGitHub <noreply@github.com>2023-11-15 10:02:48 -0800
commit55a5311ee4da639a65bd60622315f3fff6936c9a (patch)
tree285f960d4a7afe9d27ecca51e86e7f00fb914d22
parent96316c8be7bc21617ec5333f87864a0f002ebaa4 (diff)
parentba782d54aa70a1faf6bf6cdcaeb446f7b4c503db (diff)
downloadwikiget-55a5311ee4da639a65bd60622315f3fff6936c9a.tar.gz
wikiget-55a5311ee4da639a65bd60622315f3fff6936c9a.zip
Merge pull request #13 from clpo13/testing-tmp-folder
Change to temp directory when running tests
-rw-r--r--src/wikiget/dl.py4
-rw-r--r--src/wikiget/file.py2
-rw-r--r--src/wikiget/logging.py7
-rw-r--r--src/wikiget/parse.py2
-rw-r--r--tests/conftest.py77
-rw-r--r--tests/test_dl.py12
-rw-r--r--tests/test_logging.py4
-rw-r--r--tests/test_parse.py32
-rw-r--r--tests/test_validations.py15
9 files changed, 108 insertions, 47 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/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 <https://www.gnu.org/licenses/>.
+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
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/conftest.py b/tests/conftest.py
index 5fccfc0..6088029 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -17,11 +17,80 @@
"""Define fixtures used across all tests in this folder."""
+from pathlib import Path
+
import pytest
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)
+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
+ """
+ monkeypatch.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(scope="session")
+def test_file(tmp_path_factory: pytest.TempPathFactory) -> Path:
+ """Create a fake downloaded file with known contents.
+
+ :param tmp_path_factory: temporary path generator
+ :type tmp_path_factory: pytest.TempPathFactory
+ :return: test file
+ :rtype: pathlib.Path
+ """
+ tmp_file = tmp_path_factory.getbasetemp() / "Testfile.jpg"
+ tmp_file.write_bytes(TEST_FILE_BYTES)
+ return tmp_file
+
@pytest.fixture()
def file_with_name() -> File:
@@ -31,7 +100,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")
@@ -41,7 +110,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")
@@ -51,9 +120,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..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)
@@ -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: pathlib.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: pathlib.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