diff --git a/tests/__init__.py b/tests/__init__.py index ce4eff79..30673c5f 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,7 +1,5 @@ import io -import os.path import pkgutil -import shutil def get_egg_data(basename): @@ -10,12 +8,3 @@ def get_egg_data(basename): def root_add_version(root, project, version, basename): root.eggstorage.put(io.BytesIO(get_egg_data(basename)), project, version) - - -def clean(config, setting): - directory = os.path.realpath(config.get(setting)) - basedir = os.path.realpath(os.path.dirname(os.path.dirname(__file__))) - # Avoid accidentally deleting directories outside the project. - assert os.path.commonprefix((directory, basedir)) == basedir - if os.path.exists(directory): - shutil.rmtree(directory) diff --git a/tests/conftest.py b/tests/conftest.py index a77dfe8d..2aae8dd2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,7 +6,7 @@ from scrapyd import Config from scrapyd.app import application from scrapyd.website import Root -from tests import clean, root_add_version +from tests import root_add_version @pytest.fixture() @@ -16,18 +16,22 @@ def txrequest(): return Request(http_channel) -@pytest.fixture(params=[None, ("scrapyd", "items_dir", "items")], ids=["default", "items_dir"]) -def root(request): +@pytest.fixture() +def chdir(monkeypatch, tmpdir): + monkeypatch.chdir(tmpdir) + + return tmpdir + + +@pytest.fixture(params=[None, (Config.SECTION, "items_dir", "items")], ids=["default", "items_dir"]) +def root(request, chdir): config = Config() if request.param: config.cp.set(*request.param) app = application(config) - yield Root(config, app) - - for setting in ("dbs_dir", "eggs_dir"): - clean(config, setting) + return Root(config, app) @pytest.fixture() diff --git a/tests/mockserver.py b/tests/mockserver.py index 6ba50c54..f7f92c76 100644 --- a/tests/mockserver.py +++ b/tests/mockserver.py @@ -1,6 +1,4 @@ -import os import re -import shutil import socket import sys from subprocess import PIPE, Popen @@ -26,21 +24,17 @@ def __enter__(self, authentication=None): if self.authentication is not None: command.append("--auth=" + self.authentication) - self.proc = Popen(command, stdout=PIPE) + self.process = Popen(command, stdout=PIPE) for _ in range(10): - msg = self.proc.stdout.readline().strip().decode("ascii") - addr_line = re.search("available at (.+/)", msg) - if addr_line: - self.url = addr_line.group(1) + if address := re.search("available at (.+/)", self.process.stdout.readline().strip().decode("ascii")): + self.url = address.group(1) break return self def __exit__(self, exc_type, exc_value, traceback): - self.proc.kill() - self.proc.communicate() - if os.path.isdir("eggs") and os.listdir("eggs") != []: - shutil.rmtree("eggs") + self.process.terminate() + self.process.communicate() def urljoin(self, path): return urljoin(self.url, path) diff --git a/tests/test_eggstorage.py b/tests/test_eggstorage.py index 31dbf176..86fa315c 100644 --- a/tests/test_eggstorage.py +++ b/tests/test_eggstorage.py @@ -51,7 +51,7 @@ def test_sorted_versions(versions, expected): assert sorted_versions(versions) == expected -def test_config(): +def test_config(chdir): config = Config() config.cp.set("scrapyd", "eggstorage", "tests.test_eggstorage.FakeEggStorage") diff --git a/tests/test_endpoints.py b/tests/test_endpoints.py index afe14436..a12a762f 100644 --- a/tests/test_endpoints.py +++ b/tests/test_endpoints.py @@ -84,7 +84,7 @@ def test_spider_list_project_no_egg(mock_scrapyd): assert data["status"] == "ok" -def test_addversion_and_delversion(mock_scrapyd, quotesbot_egg): +def test_addversion_and_delversion(chdir, mock_scrapyd, quotesbot_egg): resp = _deploy(mock_scrapyd, quotesbot_egg) data = resp.json() @@ -100,7 +100,7 @@ def test_addversion_and_delversion(mock_scrapyd, quotesbot_egg): assert res.json()["status"] == "ok" -def test_failed_settings(mock_scrapyd, quotesbot_egg_asyncio): +def test_failed_settings(chdir, mock_scrapyd, quotesbot_egg_asyncio): response = _deploy(mock_scrapyd, quotesbot_egg_asyncio) assert response.status_code == 200 diff --git a/tests/test_webservice.py b/tests/test_webservice.py index 22d38756..367427b3 100644 --- a/tests/test_webservice.py +++ b/tests/test_webservice.py @@ -5,20 +5,17 @@ import pytest from twisted.web import error -from scrapyd.config import Config from scrapyd.exceptions import DirectoryTraversalError, RunnerError from scrapyd.interfaces import IEggStorage, IJobStorage from scrapyd.jobstorage import Job from scrapyd.txapp import application from scrapyd.webservice import UtilsCache, get_spider_list -from tests import clean, get_egg_data, root_add_version +from tests import get_egg_data, root_add_version @pytest.fixture() -def app(): - yield application - - clean(Config(), "eggs_dir") +def app(chdir): + return application def add_test_version(app, project, version, basename):