Skip to content

Commit

Permalink
test: Use temporary directories, instead of deleting directories. Use…
Browse files Browse the repository at this point in the history
… terminate() instead of kill(), so that pytest-cov can measure subprocesses.
  • Loading branch information
jpmckinney committed Jul 20, 2024
1 parent 9537716 commit c1116d8
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 38 deletions.
11 changes: 0 additions & 11 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import io
import os.path
import pkgutil
import shutil


def get_egg_data(basename):
Expand All @@ -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)
18 changes: 11 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
16 changes: 5 additions & 11 deletions tests/mockserver.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import os
import re
import shutil
import socket
import sys
from subprocess import PIPE, Popen
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_eggstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
4 changes: 2 additions & 2 deletions tests/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
Expand Down
9 changes: 3 additions & 6 deletions tests/test_webservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit c1116d8

Please sign in to comment.