commit f9f69d4ba2122c5244888db96eb6d5dc6284e3b5 Author: Lokendra Singh Date: Sat Aug 3 10:28:12 2019 +0530 Test installer (#57) * added: tests for apertium/installer.py * update: wrapper calls load dictionary during instatiation to reduce time * update: build-swig-wrapper apertium_core clone wrapper_optimise branch lttoolbox: build wrapper until package isnt updated * utils.py: moved dictionary_path to if block * tests: use inbuilt methods for assertion * update class name in apertium_core * clone apertium master, branch merged * test: check all the binaries used for subprocess call * test apertium binaries availability in single test * added: comment in install_apertium() test diff --git a/apertium/utils.py b/apertium/utils.py index cd71e80..30176d4 100644 --- a/apertium/utils.py +++ b/apertium/utils.py @@ -56,37 +56,36 @@ def execute_pipeline(inp: str, commands: List[List[str]]) -> str: input_file_name, output_file_name = input_file.name, output_file.name arg = command[1][1] if len(command) >= 3 else '' - path = command[-1] text = end.decode() input_file.write(text) input_file.close() if 'lt-proc' == command[0]: + dictionary_path = command[-1] lttoolbox.LtLocale.tryToSetLocale() - fst = lttoolbox.FST() + fst = lttoolbox.FST(dictionary_path) if not fst.valid(): raise ValueError('FST Invalid') - fst = lttoolbox.FST() - fst.lt_proc(arg, path, input_file_name, output_file_name) + fst.lt_proc(arg, input_file_name, output_file_name) elif 'lrx-proc' == command[0]: + dictionary_path = command[-1] lextools.LtLocale.tryToSetLocale() - lrx = lextools.LRX() - lrx.lrx_proc(arg, path, input_file.name, output_file.name) + lrx = lextools.LRXProc(dictionary_path) + lrx.lrx_proc(arg, input_file.name, output_file.name) elif 'apertium-transfer' == command[0]: - obj = apertium_core.apertium() - obj.transfer_text(arg, command[2], command[3], input_file.name, output_file.name) + obj = apertium_core.ApertiumTransfer(command[2], command[3]) + obj.transfer_text(arg, input_file.name, output_file.name) elif 'apertium-interchunk' == command[0]: - obj = apertium_core.apertium() - obj.interchunk_text(arg, command[1], command[2], input_file.name, output_file.name) + obj = apertium_core.ApertiumInterchunk(command[1], command[2]) + obj.interchunk_text(arg, input_file.name, output_file.name) elif 'apertium-postchunk' == command[0]: - obj = apertium_core.apertium() - obj.postchunk_text(arg, command[1], command[2], input_file.name, output_file.name) + obj = apertium_core.ApertiumPostchunk(command[1], command[2]) + obj.postchunk_text(arg, input_file.name, output_file.name) elif 'apertium-pretransfer' == command[0]: - obj = apertium_core.apertium() - obj.pretransfer(arg, input_file.name, output_file.name) + apertium_core.pretransfer(arg, input_file.name, output_file.name) elif 'apertium-tagger' == command[0]: command += [input_file.name, output_file.name] - apertium_core.tagger(len(command), command) + apertium_core.ApertiumTagger(len(command), command) else: used_wrapper = False diff --git a/build-swig-wrapper.sh b/build-swig-wrapper.sh index 8ad4e55..3da1ccf 100755 --- a/build-swig-wrapper.sh +++ b/build-swig-wrapper.sh @@ -18,3 +18,11 @@ cd python make -j2 python3 setup.py install popd + +git clone --depth 1 https://github.com/apertium/lttoolbox.git +pushd lttoolbox +./autogen.sh --enable-python-bindings +cd python +make -j2 +python3 setup.py install +popd diff --git a/tests/__init__.py b/tests/__init__.py index 2d5810f..01e06b7 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,4 +1,7 @@ +import importlib.util import os +import platform +import shutil import sys import unittest @@ -69,6 +72,32 @@ class TestGenerate(unittest.TestCase): apertium.generate('spa', 'cat') +class TestInstallation(unittest.TestCase): + def test_apertium_installer(self): + # This test doesn't remove existing apertium binaries. + # So it is possible that apertium.installer.install_apertium() isn't working + apertium.installer.install_apertium() + apertium_processes = ['apertium-destxt', 'apertium-interchunk', 'apertium-postchunk', + 'apertium-pretransfer', 'apertium-tagger', 'apertium-transfer', + 'lrx-proc', 'lt-proc' + ] + for process in apertium_processes: + self.assertIsNotNone(shutil.which(process), 'apertium installer not working. {} not available on system path'.format(process)) + break + + def test_install_module(self): + language = 'kir' + apertium.installer.install_module(language) + importlib.reload(apertium) + self.assertIn(language, apertium.analyzers, 'apetium.install_module not working') + + def test_install_wrapper(self): + apertium.installer.install_wrapper('python3-lttoolbox') + if platform.system() == 'Linux': + sys.path.append('/usr/lib/python3/dist-packages') + self.assertIsNotNone(importlib.util.find_spec('lttoolbox'), 'Wrapper not installed') + + class TestTranslate(unittest.TestCase): def test_translator_en_spa(self): translator = apertium.Translator('eng', 'spa')