Skip to content

Commit

Permalink
test: Update Environment tests for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Jul 25, 2024
1 parent d498ae3 commit 9915c73
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions tests/test_environ.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
import re
import time
from unittest.mock import patch

import pytest
Expand All @@ -23,48 +24,54 @@ def test_get_settings(environ):
assert re.search(r"^\S+j1\.log$", settings["LOG_FILE"])

if environ.items_dir:
feeds = json.loads(settings["FEEDS"])
feeds = json.loads(settings.pop("FEEDS"))
path, value = feeds.popitem()

assert list(settings) == ["LOG_FILE"]
assert feeds == {}
assert re.search(r"^file:///\S+j1\.jl$", path)
assert value == {"format": "jsonlines"}


@pytest.mark.parametrize(
("items_dir", "expected"),
("items_dir", "pattern"),
[
(
"https://host.example/path?query=value#fragment",
"https://host.example/path/p1/s1/j1.jl?query=value#fragment",
r"https://host\.example/path/p1/s1/j1\.jl\?query=value#fragment",
),
(
"https://host.example/path/",
"https://host.example/path/p1/s1/j1.jl", # no double slashes
),
(
"file:/root.dir/path?ignored#ignored",
"file:///root.dir/path/p1/s1/j1.jl",
r"file:///([A-Z]:/)?root\.dir/path/p1/s1/j1\.jl",
),
(
"file://hostname/root.dir/path?ignored#ignored",
"file:///root.dir/path/p1/s1/j1.jl",
r"file:///([A-Z]:/)?root.dir/path/p1/s1/j1.jl",
),
(
"file:///root.dir/path?ignored#ignored",
"file:///root.dir/path/p1/s1/j1.jl",
r"file:///([A-Z]:/)?root.dir/path/p1/s1/j1.jl",
),
],
)
@patch("os.listdir", lambda _: [])
@patch("os.makedirs", lambda _: _)
def test_get_settings_url(items_dir, expected):
def test_get_settings_url(items_dir, pattern):
config = Config(values={"logs_dir": "", "items_dir": items_dir})
environ = Environment(config, initenv={})

settings = environ.get_settings({"_project": "p1", "_spider": "s1", "_job": "j1"})
feeds = json.loads(settings.pop("FEEDS"))
path, value = feeds.popitem()

assert settings == {"FEEDS": f'{{"{expected}": {{"format": "jsonlines"}}}}'}
assert settings == {}
assert feeds == {}
assert re.search(pattern, path)
assert value == {"format": "jsonlines"}


@pytest.mark.parametrize("values", [{"items_dir": "../items"}, {"logs_dir": "../logs"}])
Expand Down Expand Up @@ -96,6 +103,7 @@ def test_jobs_to_keep(chdir):

(directory / "j1.a").touch()
(directory / "j2.b").touch()
time.sleep(0.000001)
(directory / "j3.c").touch()
(directory / "j4.d").touch()

Expand Down

0 comments on commit 9915c73

Please sign in to comment.