-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make clean return non-zero exit status when a filter fails
Fixes #106.
- Loading branch information
Showing
6 changed files
with
115 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,28 @@ | ||
from typing import TypeVar, Optional | ||
|
||
from threading import Thread | ||
|
||
T = TypeVar("T") | ||
|
||
def none_throws(optional: Optional[T], message: str = "Unexpected `None`") -> T: | ||
if optional is None: | ||
raise AssertionError(message) | ||
return optional | ||
|
||
|
||
class RaisingThread(Thread): | ||
"""Thread that will raise any uncaught exceptions in the thread in the | ||
parent once it joins again.""" | ||
|
||
exception: Optional[Exception] | ||
|
||
def run(self): | ||
self.exception = None | ||
try: | ||
super().run() | ||
except Exception as exc: | ||
self.exception = exc | ||
|
||
def join(self, timeout:float=None): | ||
super().join(timeout=timeout) | ||
if self.exception is not None: | ||
raise self.exception |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"description": "Crashes", | ||
"type": "monolingual", | ||
"command": "python3 -c 'import sys\nsys.exit(int(sys.argv[1]))' $EXITCODE", | ||
"parameters": { | ||
"EXITCODE": { | ||
"type": "int", | ||
"default": 1 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import sys | ||
import os | ||
import unittest | ||
import subprocess | ||
import json | ||
from typing import List | ||
from pathlib import Path | ||
from tempfile import NamedTemporaryFile | ||
|
||
|
||
TEST_CWD = Path(os.path.join(os.path.dirname(__file__), 'deeper')) | ||
|
||
FILES = [ | ||
"bible-uedin-v1.de-en.de.gz", | ||
"bible-uedin-v1.de-en.en.gz" | ||
] | ||
|
||
|
||
class TestClean(unittest.TestCase): | ||
def _run(self, args:List[str]): | ||
proc = subprocess.Popen( | ||
args=[sys.executable, '-m', 'opuscleaner.clean'] + args, | ||
cwd=TEST_CWD, # so it can find filters | ||
env={ | ||
'PYTHONPATH': os.path.join(os.path.dirname(__file__), '..') # so it can find opuscleaner code | ||
}, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE) | ||
|
||
out, err = proc.communicate() | ||
proc.wait() | ||
return out, err, proc.returncode | ||
|
||
def test_simple(self): | ||
"""Test that clean runs""" | ||
config = { | ||
"version": 1, | ||
"files": FILES, | ||
"filters": [ | ||
{ | ||
"filter": "deescape_tsv", | ||
"parameters": {}, | ||
"language": None | ||
} | ||
] | ||
} | ||
with NamedTemporaryFile(mode='w', dir=TEST_CWD / 'data/train-parts') as fh: | ||
json.dump(config, fh) | ||
fh.flush() | ||
for mode in [[], ['--parallel', '1']]: | ||
with self.subTest(mode=mode): | ||
out, err, retval = self._run([*mode, fh.name]) | ||
self.assertEqual(out.count(b'\n'), 62195) | ||
self.assertEqual(retval, 0) | ||
|
||
def test_filter_fail(self): | ||
"""Test that clean returns a non-zero exit code if a filter fails""" | ||
config = { | ||
"version": 1, | ||
"files": FILES, | ||
"filters": [ | ||
{ | ||
"filter": "fail", | ||
"parameters": { | ||
"EXITCODE": "42" | ||
}, | ||
"language": "de" | ||
} | ||
] | ||
} | ||
with NamedTemporaryFile(mode='w', dir=TEST_CWD / 'data/train-parts') as fh: | ||
json.dump(config, fh) | ||
fh.flush() | ||
|
||
for mode in [[], ['--parallel', '1']]: | ||
with self.subTest(mode=mode): | ||
out, err, retval = self._run([*mode, fh.name]) | ||
self.assertEqual(out.count(b'\n'), 0) | ||
self.assertNotEqual(retval, 0) |