Skip to content

Commit

Permalink
Few more tests copied from spaCy (#11)
Browse files Browse the repository at this point in the history
* move remote_storage tests from spaCy

* add one more unit test from spaCy

* test: fix test_project_check_requirements

spacy was still lurking

* ci: uncomment test suite

---------

Co-authored-by: Basile Dura <[email protected]>
  • Loading branch information
svlandeg and bdura authored Mar 25, 2023
1 parent 6077ba9 commit 9a36328
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 3 deletions.
6 changes: 3 additions & 3 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,6 @@ jobs:
python -m pip install -r requirements.txt
displayName: 'Install test requirements'
# - script: |
# python -m pytest --pyargs weasel
# displayName: 'Run tests'
- script: |
python -m pytest --pyargs weasel
displayName: 'Run tests'
101 changes: 101 additions & 0 deletions weasel/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import os
import pytest
import srsly
import time

from weasel._util import is_subpath_of, load_project_config
from weasel._util import substitute_project_variables
from weasel._util import validate_project_commands, ConfigValidationError
from weasel.schemas import ProjectConfigSchema, validate

from weasel.cli.remote_storage import RemoteStorage
from weasel.cli.run import _check_requirements

from weasel.util import make_tempdir


Expand Down Expand Up @@ -173,3 +177,100 @@ def test_project_config_interpolation_env():
)
def test_is_subpath_of(parent, child, expected):
assert is_subpath_of(parent, child) == expected


def test_local_remote_storage():
with make_tempdir() as d:
filename = "a.txt"

content_hashes = ("aaaa", "cccc", "bbbb")
for i, content_hash in enumerate(content_hashes):
# make sure that each subsequent file has a later timestamp
if i > 0:
time.sleep(1)
content = f"{content_hash} content"
loc_file = d / "root" / filename
if not loc_file.parent.exists():
loc_file.parent.mkdir(parents=True)
with loc_file.open(mode="w") as file_:
file_.write(content)

# push first version to remote storage
remote = RemoteStorage(d / "root", str(d / "remote"))
remote.push(filename, "aaaa", content_hash)

# retrieve with full hashes
loc_file.unlink()
remote.pull(filename, command_hash="aaaa", content_hash=content_hash)
with loc_file.open(mode="r") as file_:
assert file_.read() == content

# retrieve with command hash
loc_file.unlink()
remote.pull(filename, command_hash="aaaa")
with loc_file.open(mode="r") as file_:
assert file_.read() == content

# retrieve with content hash
loc_file.unlink()
remote.pull(filename, content_hash=content_hash)
with loc_file.open(mode="r") as file_:
assert file_.read() == content

# retrieve with no hashes
loc_file.unlink()
remote.pull(filename)
with loc_file.open(mode="r") as file_:
assert file_.read() == content


def test_local_remote_storage_pull_missing():
# pulling from a non-existent remote pulls nothing gracefully
with make_tempdir() as d:
filename = "a.txt"
remote = RemoteStorage(d / "root", str(d / "remote"))
assert remote.pull(filename, command_hash="aaaa") is None
assert remote.pull(filename) is None


@pytest.mark.filterwarnings("ignore::DeprecationWarning")
@pytest.mark.parametrize(
"reqs,output",
[
[
"""
weasel
# comment
thinc""",
(False, False),
],
[
"""# comment
--some-flag
weasel""",
(False, False),
],
[
"""# comment
--some-flag
weasel; python_version >= '3.6'""",
(False, False),
],
[
"""# comment
spacyunknowndoesnotexist12345""",
(True, False),
],
],
)
def test_project_check_requirements(reqs, output):
import pkg_resources

# excessive guard against unlikely package name
try:
pkg_resources.require("spacyunknowndoesnotexist12345")
except pkg_resources.DistributionNotFound:
assert output == _check_requirements([req.strip() for req in reqs.split("\n")])

0 comments on commit 9a36328

Please sign in to comment.