Skip to content

Commit 2009579

Browse files
committed
Add check_jar function, and use it in the CoreNLP tests
1 parent b4e0000 commit 2009579

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

nltk/parse/corenlp.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ def tokenize(self, text, properties=None):
311311
"""Tokenize a string of text.
312312
313313
Skip these tests if CoreNLP is likely not ready.
314-
>>> if "CLASSPATH" not in os.environ: import pytest; pytest.skip("CoreNLP jars unavailable")
314+
>>> from nltk.test.setup_fixt import check_jar
315+
>>> check_jar(CoreNLPServer._JAR, env_vars=("CORENLP",), is_regex=True)
315316
316317
The CoreNLP server can be started using the following notation, although
317318
we recommend the `with CoreNLPServer() as server:` context manager notation
@@ -367,7 +368,8 @@ def tag(self, sentence: str) -> List[Tuple[str, str]]:
367368
:rtype: list(tuple(str, str))
368369
369370
Skip these tests if CoreNLP is likely not ready.
370-
>>> if "CLASSPATH" not in os.environ: import pytest; pytest.skip("CoreNLP jars unavailable")
371+
>>> from nltk.test.setup_fixt import check_jar
372+
>>> check_jar(CoreNLPServer._JAR, env_vars=("CORENLP",), is_regex=True)
371373
372374
The CoreNLP server can be started using the following notation, although
373375
we recommend the `with CoreNLPServer() as server:` context manager notation
@@ -422,7 +424,8 @@ def raw_tag_sents(self, sentences):
422424
class CoreNLPParser(GenericCoreNLPParser):
423425
"""
424426
Skip these tests if CoreNLP is likely not ready.
425-
>>> if "CLASSPATH" not in os.environ: import pytest; pytest.skip("CoreNLP jars unavailable")
427+
>>> from nltk.test.setup_fixt import check_jar
428+
>>> check_jar(CoreNLPServer._JAR, env_vars=("CORENLP",), is_regex=True)
426429
427430
The recommended usage of `CoreNLPParser` is using the context manager notation:
428431
>>> with CoreNLPServer() as server:
@@ -586,7 +589,8 @@ class CoreNLPDependencyParser(GenericCoreNLPParser):
586589
"""Dependency parser.
587590
588591
Skip these tests if CoreNLP is likely not ready.
589-
>>> if "CLASSPATH" not in os.environ: import pytest; pytest.skip("CoreNLP jars unavailable")
592+
>>> from nltk.test.setup_fixt import check_jar
593+
>>> check_jar(CoreNLPServer._JAR, env_vars=("CORENLP",), is_regex=True)
590594
591595
The recommended usage of `CoreNLPParser` is using the context manager notation:
592596
>>> with CoreNLPServer() as server:

nltk/test/setup_fixt.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
# Skip doctest when a required binary is not available
2-
from nltk.internals import find_binary
1+
from nltk.internals import find_binary, find_jar
32

43

5-
def check_binary(binary):
4+
def check_binary(binary: str, **args):
5+
"""Skip a test via `pytest.skip` if the `binary` executable is not found.
6+
Keyword arguments are passed to `nltk.internals.find_binary`."""
67
import pytest
78

89
try:
9-
find_binary(binary)
10+
find_binary(binary, **args)
1011
except LookupError:
1112
pytest.skip(f"Skipping test because the {binary} binary was not found")
13+
14+
15+
def check_jar(name_pattern: str, **args):
16+
"""Skip a test via `pytest.skip` if the `name_pattern` jar is not found.
17+
Keyword arguments are passed to `nltk.internals.find_jar`."""
18+
import pytest
19+
20+
try:
21+
find_jar(name_pattern, **args)
22+
except LookupError:
23+
pytest.skip(f"Skipping test because the {name_pattern!r} jar was not found")

0 commit comments

Comments
 (0)