Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Fix test-install by moving tests into module #1232

Merged
merged 96 commits into from
May 25, 2018
Merged
Show file tree
Hide file tree
Changes from 91 commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
cf8caa5
Prevent tests from being installed as module by setup.py
nelson-liu May 17, 2018
95784af
Bulk move tests
nelson-liu May 17, 2018
369b044
Add PROJECT_ROOT, TESTS_ROOT, FIXTURES_ROOT
nelson-liu May 17, 2018
c79d3c8
Fix fine_tune_test
nelson-liu May 17, 2018
26def09
Fix dry run tests
nelson-liu May 17, 2018
2d81aae
Fix broken import in elmo_test
nelson-liu May 17, 2018
380f947
Fix elmo tests
nelson-liu May 17, 2018
a7955ef
Fix evaluate test
nelson-liu May 17, 2018
ac949e7
Fix imports in adaptive/bucket/epoch_tracking_bucket iterator tests
nelson-liu May 17, 2018
7917d4a
Move ROOT variables out of setUp
nelson-liu May 17, 2018
fa1adf6
Make cached_path work with Path
nelson-liu May 17, 2018
96fbc0c
Fix server_flask_test
nelson-liu May 17, 2018
3f07136
Edit pytest.ini for new path
nelson-liu May 17, 2018
402ecc5
remove tests from manifest.in
nelson-liu May 17, 2018
c334b7f
fix lint in fine_tune_test
nelson-liu May 17, 2018
b07b216
Fix lint in server_flask_test
nelson-liu May 17, 2018
b1752dc
Add mypy.ini to ignore tests
nelson-liu May 17, 2018
7a5cf23
Fix pylint command in verify.py
nelson-liu May 17, 2018
bba7250
Use pathlib in server_flask_test
nelson-liu May 17, 2018
5257ddb
Fix make_vocab test
nelson-liu May 17, 2018
0b99d7e
Fix commands.main_test
nelson-liu May 17, 2018
dab4575
Fix predict test
nelson-liu May 17, 2018
80af620
Fix train_test
nelson-liu May 17, 2018
4e29992
Make file_utils use pathlib
nelson-liu May 18, 2018
5dc6a19
Fix file_utils test
nelson-liu May 18, 2018
2a88224
Fix params test
nelson-liu May 18, 2018
c731494
Fix test_util
nelson-liu May 18, 2018
36a506c
Fix conll2003_dataset_reader
nelson-liu May 18, 2018
b2d7411
Fix coref_reader_test
nelson-liu May 18, 2018
dde498d
Fix winobias reader test
nelson-liu May 18, 2018
84fc4e6
fix ontonotes_test
nelson-liu May 18, 2018
a75c674
Fix language_modeling_dataset
nelson-liu May 18, 2018
caac10c
Fix nlvr_test
nelson-liu May 18, 2018
aed512e
Fix penn_tree_bank_reader
nelson-liu May 18, 2018
54b42e2
Fix squad_test
nelson-liu May 18, 2018
dc56c10
Fix triviaqa_test
nelson-liu May 18, 2018
df33454
Fix seq2seq_test
nelson-liu May 18, 2018
b006aea
Fix sequence_tagging_test
nelson-liu May 18, 2018
c840400
Fix SNLI reader test
nelson-liu May 18, 2018
17a62b0
Fix srl_dataset_reader
nelson-liu May 18, 2018
26d1d81
Fix SST dataset reader tests
nelson-liu May 18, 2018
00f4a37
Fix wikitables_test
nelson-liu May 18, 2018
8079bd8
Change directory to module root when testing model so fixture paths work
nelson-liu May 18, 2018
b64f249
Fix archival test
nelson-liu May 18, 2018
1e58031
Fix BCN
nelson-liu May 18, 2018
e7a5e35
Fix constituency parser tests
nelson-liu May 18, 2018
e6a02af
Fix coref_test
nelson-liu May 18, 2018
12dd555
Fix crf_tagger_test
nelson-liu May 18, 2018
ac0ff22
Fix decomposable_attention_test
nelson-liu May 18, 2018
96f47e7
Fix simple_seq2seq_test
nelson-liu May 18, 2018
19e5af3
Fix bidaf_ensemble_test
nelson-liu May 18, 2018
de5dde1
Fix bidaf_test
nelson-liu May 18, 2018
b6c86e3
Fix nlvr_coverage_semantic_parser
nelson-liu May 18, 2018
d6ea1b7
Fix nlvr_direct_semantic_parser_test
nelson-liu May 18, 2018
32d6d5d
Fix wikitables_erm_semantic_parser_test
nelson-liu May 18, 2018
e276f86
Fix wikitables_mml_semantic_parser_test
nelson-liu May 18, 2018
7ca33b0
Fix semantic_role_labeler test
nelson-liu May 18, 2018
c28877e
Add simple_tagger_test
nelson-liu May 18, 2018
d401a1e
Fix fine_tune_test that i broke
nelson-liu May 18, 2018
35963d1
Fix vocabulary_test
nelson-liu May 18, 2018
4df7dc9
Fix elmo_token_embedder_test
nelson-liu May 18, 2018
36df899
Fix embedding_test
nelson-liu May 18, 2018
3c08b8a
Fix nlvr_world_test
nelson-liu May 18, 2018
a1439e1
Fix wikitables_world_test
nelson-liu May 18, 2018
dc4ec71
Fix world_test
nelson-liu May 18, 2018
ba98ac2
Fix server_simple_test
nelson-liu May 18, 2018
3ed0e7d
Fix predictors.bidaf_test
nelson-liu May 18, 2018
b564459
Fix predictors.constituency_parser
nelson-liu May 18, 2018
36d4df7
Fix predictors.coref_test
nelson-liu May 18, 2018
48265f2
Fix decomposable_attention test
nelson-liu May 18, 2018
1906755
Fix nlvr_parser_test
nelson-liu May 18, 2018
b11c4f6
Fix predictors.isimple_seq2seq_test
nelson-liu May 18, 2018
a50e9d2
Fix srl_test
nelson-liu May 18, 2018
38b1589
Fix wikitables_parser_test
nelson-liu May 18, 2018
a27847a
Fix optimizer_test
nelson-liu May 18, 2018
c873879
Fix trainer test
nelson-liu May 18, 2018
3b15536
Fix wikitables_accuracy_test
nelson-liu May 18, 2018
977d33b
Add srl-eval.pl to tools, use it in tests
nelson-liu May 18, 2018
8cd7e11
Make SEMPRE better clean up after itself
nelson-liu May 18, 2018
6347bff
Reword test_install command
nelson-liu May 18, 2018
24b19b7
Fix lint
nelson-liu May 18, 2018
8602e89
Add tests to ignored modules in check_docs
nelson-liu May 18, 2018
dd65b29
Remove copy tests from dockerfile
nelson-liu May 18, 2018
fae7143
Rename project_root to module_root in test_install
nelson-liu May 18, 2018
a963695
Fix lint
nelson-liu May 18, 2018
f123ec8
skip notebook tests in allennlp test-install --run-all, since we're r…
nelson-liu May 18, 2018
0d4d000
Further cleanup of files leftover by SEMPRE
nelson-liu May 18, 2018
309cf5f
Remove accidentally committed evalb binary
nelson-liu May 20, 2018
5cddce8
Wildcard exclude custom_extensions from check_docs.py
nelson-liu May 21, 2018
92f9d82
Merge branch 'master' into rework_test_install
nelson-liu May 25, 2018
00c166c
Merge branch 'master' into rework_test_install
nelson-liu May 25, 2018
e01ba27
Remove accidental print in wikitables_accuracy_test
nelson-liu May 25, 2018
6324d62
Remove srl-eval.pl from scripts
nelson-liu May 25, 2018
852afb1
Merge branch 'master' into rework_test_install
nelson-liu May 25, 2018
65f51a3
Merge branch 'master' into rework_test_install
nelson-liu May 25, 2018
d478ccd
Add comment explaining test_install_test
nelson-liu May 25, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ RUN ./scripts/build_demo.py

COPY scripts/ scripts/
COPY allennlp/ allennlp/
COPY tests/ tests/
COPY pytest.ini pytest.ini
COPY .pylintrc .pylintrc
COPY tutorials/ tutorials/
Expand Down
1 change: 0 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ include requirements.txt
include bin/allennlp
recursive-include allennlp *
recursive-include scripts *
recursive-include tests *
recursive-include training_config *.json
recursive-include tutorials *
recursive-exclude * __pycache__
20 changes: 10 additions & 10 deletions allennlp/commands/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import argparse
import logging
import os
import pathlib

import pytest

import allennlp
from allennlp.commands.subcommand import Subcommand

logger = logging.getLogger(__name__) # pylint: disable=invalid-name
Expand All @@ -44,22 +46,20 @@ def add_subparser(self, name: str, parser: argparse._SubParsersAction) -> argpar
return subparser


def _get_project_root():
return os.path.abspath(
os.path.join(
os.path.dirname(os.path.realpath(__file__)),
os.pardir, os.pardir))
def _get_module_root():
return pathlib.Path(allennlp.__file__).parent


def _run_test(args: argparse.Namespace):
initial_working_dir = os.getcwd()
project_root = _get_project_root()
logger.info("Changing directory to %s", project_root)
os.chdir(project_root)
test_dir = os.path.join(project_root, "tests")
module_root = _get_module_root()
logger.info("Changing directory to %s", module_root)
os.chdir(module_root)
test_dir = os.path.join(module_root, "tests")
logger.info("Running tests at %s", test_dir)
if args.run_all:
pytest.main([test_dir])
# TODO(nfliu): remove this when notebooks have been rewritten as markdown.
pytest.main([test_dir, '-k', 'not notebooks_test'])
else:
pytest.main([test_dir, '-k', 'not sniff_test and not notebooks_test'])
# Change back to original working directory after running tests
Expand Down
11 changes: 7 additions & 4 deletions allennlp/common/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
Utilities for working with the local dataset cache.
"""

from typing import Tuple
from typing import Tuple, Union
import os
from hashlib import sha256
import logging
from pathlib import Path
import shutil
import tempfile
from urllib.parse import urlparse
Expand All @@ -17,8 +18,8 @@

logger = logging.getLogger(__name__) # pylint: disable=invalid-name

CACHE_ROOT = os.getenv('ALLENNLP_CACHE_ROOT', os.path.expanduser(os.path.join('~', '.allennlp')))
DATASET_CACHE = os.path.join(CACHE_ROOT, "datasets")
CACHE_ROOT = Path(os.getenv('ALLENNLP_CACHE_ROOT', Path.home() / '.allennlp'))
DATASET_CACHE = str(CACHE_ROOT / "datasets")

def url_to_filename(url: str, etag: str = None) -> str:
"""
Expand Down Expand Up @@ -60,7 +61,7 @@ def filename_to_url(filename: str, cache_dir: str = None) -> Tuple[str, str]:

return url, etag

def cached_path(url_or_filename: str, cache_dir: str = None) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joelgrus does this Union, and all the str(path) calls in this PR change how you feel about Path?

def cached_path(url_or_filename: Union[str, Path], cache_dir: str = None) -> str:
"""
Given something that might be a URL (or might be a local path),
determine which. If it's a URL, download the file and cache it, and
Expand All @@ -69,6 +70,8 @@ def cached_path(url_or_filename: str, cache_dir: str = None) -> str:
"""
if cache_dir is None:
cache_dir = DATASET_CACHE
if isinstance(url_or_filename, Path):
url_or_filename = str(url_or_filename)

parsed = urlparse(url_or_filename)

Expand Down
18 changes: 16 additions & 2 deletions allennlp/common/testing/model_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class ModelTestCase(AllenNlpTestCase):
"""
def set_up_model(self, param_file, dataset_file):
# pylint: disable=attribute-defined-outside-init
initial_working_dir = os.getcwd()
# Change directory to module root.
os.chdir(self.MODULE_ROOT)

self.param_file = param_file
params = Params.from_file(self.param_file)

Expand All @@ -40,12 +44,19 @@ def set_up_model(self, param_file, dataset_file):
self.dataset = Batch(self.instances)
self.dataset.index_instances(self.vocab)

# Change directory back to what it was initially
os.chdir(initial_working_dir)

def ensure_model_can_train_save_and_load(self,
param_file: str,
tolerance: float = 1e-4,
cuda_device: int = -1):
save_dir = os.path.join(self.TEST_DIR, "save_and_load_test")
archive_file = os.path.join(save_dir, "model.tar.gz")
initial_working_dir = os.getcwd()
# Change directory to module root.
os.chdir(self.MODULE_ROOT)

save_dir = self.TEST_DIR / "save_and_load_test"
archive_file = save_dir / "model.tar.gz"
model = train_model_from_file(param_file, save_dir)
loaded_model = load_archive(archive_file, cuda_device=cuda_device).model
state_keys = model.state_dict().keys()
Expand Down Expand Up @@ -109,6 +120,9 @@ def ensure_model_can_train_save_and_load(self,
name=key,
tolerance=tolerance)

# Change directory back to what it was initially
os.chdir(initial_working_dir)

return model, loaded_model

def assert_fields_equal(self, field1, field2, name: str, tolerance: float = 1e-6) -> None:
Expand Down
10 changes: 9 additions & 1 deletion allennlp/common/testing/test_case.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pylint: disable=invalid-name,protected-access
import logging
import os
import pathlib
import shutil
from unittest import TestCase

Expand All @@ -13,6 +14,12 @@ class AllenNlpTestCase(TestCase): # pylint: disable=too-many-public-methods
more verbose AllenNLP logging and that creates and destroys a temp directory
as a test fixture.
"""
PROJECT_ROOT = (pathlib.Path(__file__).parent / ".." / ".." / "..").resolve() # pylint: disable=no-member
MODULE_ROOT = PROJECT_ROOT / "allennlp"
TOOLS_ROOT = MODULE_ROOT / "tools"
TESTS_ROOT = MODULE_ROOT / "tests"
FIXTURES_ROOT = TESTS_ROOT / "fixtures"

def setUp(self):
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
level=logging.DEBUG)
Expand All @@ -23,7 +30,8 @@ def setUp(self):
logging.getLogger('allennlp.modules.token_embedders.embedding').setLevel(logging.INFO)
log_pytorch_version_info()

self.TEST_DIR = "/tmp/allennlp_tests/"
self.TEST_DIR = pathlib.Path("/tmp/allennlp_tests/")

os.makedirs(self.TEST_DIR, exist_ok=True)

def tearDown(self):
Expand Down
15 changes: 13 additions & 2 deletions allennlp/service/predictors/wikitables_parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import pathlib
from subprocess import run
from typing import Tuple
import shutil

from overrides import overrides

Expand All @@ -18,7 +20,9 @@
DEFAULT_EXECUTOR_JAR = "https://s3-us-west-2.amazonaws.com/allennlp/misc/wikitables-executor-0.1.0.jar"
ABBREVIATIONS_FILE = "https://s3-us-west-2.amazonaws.com/allennlp/misc/wikitables-abbreviations.tsv"
GROW_FILE = "https://s3-us-west-2.amazonaws.com/allennlp/misc/wikitables-grow.grammar"
SEMPRE_DIR = 'data/'
SEMPRE_DIR = str(pathlib.Path('data/'))
SEMPRE_ABBREVIATIONS_PATH = os.path.join(SEMPRE_DIR, "abbreviations.tsv")
SEMPRE_GRAMMAR_PATH = os.path.join(SEMPRE_DIR, "grow.grammar")

@Predictor.register('wikitables-parser')
class WikiTablesParserPredictor(Predictor):
Expand Down Expand Up @@ -117,4 +121,11 @@ def _execute_logical_form_on_table(logical_form, table):
denotations_file = os.path.join(SEMPRE_DIR, 'logical_forms_denotations.tsv')
with open(denotations_file) as temp_file:
line = temp_file.readline().split('\t')
return line[1] if len(line) > 1 else line[0]

# Clean up all the temp files generated from this function.
# Take care to not remove the auxiliary sempre files
os.remove(logical_form_filename)
shutil.rmtree(table_dir)
os.remove(denotations_file)
os.remove(test_data_filename)
return line[1] if len(line) > 1 else line[0]
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def setUp(self):
}
},
"dataset_reader": {"type": "sequence_tagging"},
"train_data_path": 'tests/fixtures/data/sequence_tagging.tsv',
"validation_data_path": 'tests/fixtures/data/sequence_tagging.tsv',
"train_data_path": self.FIXTURES_ROOT / 'data' / 'sequence_tagging.tsv',
"validation_data_path": self.FIXTURES_ROOT / 'data' / 'sequence_tagging.tsv',
"iterator": {"type": "basic", "batch_size": 2},
"trainer": {
"num_epochs": 2,
Expand All @@ -36,10 +36,10 @@ def setUp(self):
})

def test_dry_run_doesnt_overwrite_vocab(self):
vocab_path = os.path.join(self.TEST_DIR, 'pre-defined-vocab')
vocab_path = self.TEST_DIR / 'pre-defined-vocab'
os.mkdir(vocab_path)
# Put something in the vocab directory
with open(os.path.join(vocab_path, "test.txt"), "a+") as open_file:
with open(vocab_path / "test.txt", "a+") as open_file:
open_file.write("test")

self.params['vocabulary'] = {}
Expand All @@ -51,5 +51,5 @@ def test_dry_run_doesnt_overwrite_vocab(self):
predefined_vocab_files = os.listdir(vocab_path)
assert set(predefined_vocab_files) == {'test.txt'}
# But we should have written the created vocab to serialisation_dir/vocab:
new_vocab_files = os.listdir(os.path.join(self.TEST_DIR, 'vocabulary'))
new_vocab_files = os.listdir(self.TEST_DIR / 'vocabulary')
assert set(new_vocab_files) == {'tokens.txt', 'non_padded_namespaces.txt', 'labels.txt'}
Loading