Skip to content

Commit

Permalink
test: Use temporary directories, instead of deleting directories
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Jul 20, 2024
1 parent 9537716 commit 9c6f39f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 23 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)
13 changes: 6 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,17 @@ def txrequest():
return Request(http_channel)


@pytest.fixture(params=[None, ("scrapyd", "items_dir", "items")], ids=["default", "items_dir"])
def root(request):
@pytest.fixture(params=[None, (Config.SECTION, "items_dir", "items")], ids=["default", "items_dir"])
def root(request, monkeypatch, tmpdir):
monkeypatch.chdir(tmpdir)

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
7 changes: 2 additions & 5 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")
return application


def add_test_version(app, project, version, basename):
Expand Down

0 comments on commit 9c6f39f

Please sign in to comment.