|
| 1 | +import glob |
| 2 | +import io |
| 3 | +import jgo |
| 4 | +import os |
| 5 | +import pathlib |
| 6 | +import unittest |
| 7 | +import shutil |
| 8 | +import subprocess |
| 9 | +import tempfile |
| 10 | + |
| 11 | +import logging |
| 12 | +_logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +IGNORE_JGORC = '--ignore-jgorc' |
| 16 | +LINK_TYPE = '--link-type' |
| 17 | +PARSINGTON_VERSION = '1.0.4' |
| 18 | +PARSINGTON_ENDPOINT = 'org.scijava:parsington:{}'.format(PARSINGTON_VERSION) |
| 19 | + |
| 20 | +def run_parsington(cache_dir, link_type, parsington_args): |
| 21 | + argv = (IGNORE_JGORC, LINK_TYPE, link_type, PARSINGTON_ENDPOINT) + parsington_args |
| 22 | + os.environ[jgo.jgo.jgo_cache_dir_environment_variable()] = cache_dir |
| 23 | + return jgo.jgo.jgo_main(argv=argv, stdout=subprocess.PIPE) |
| 24 | + |
| 25 | +def resolve_parsington(cache_dir, link_type, m2_repo): |
| 26 | + return jgo.resolve_dependencies( |
| 27 | + PARSINGTON_ENDPOINT, |
| 28 | + m2_repo=m2_repo, |
| 29 | + cache_dir=cache_dir, |
| 30 | + link_type=link_type) |
| 31 | + |
| 32 | +class ParsingtonTest(unittest.TestCase): |
| 33 | + |
| 34 | + def test_resolve_hard(self): |
| 35 | + tmp_dir = tempfile.mkdtemp(prefix='jgo-test-cache-dir') |
| 36 | + m2_repo = os.path.join(str(pathlib.Path.home()), '.m2', 'repository') |
| 37 | + try: |
| 38 | + _, workspace = resolve_parsington(cache_dir=tmp_dir, link_type='hard', m2_repo=m2_repo) |
| 39 | + jars = glob.glob(os.path.join(workspace, '*jar')) |
| 40 | + self.assertEqual(len(jars), 1, 'Expected exactly one jar in workspace') |
| 41 | + self.assertEqual(jars[0], os.path.join(workspace, 'parsington-%s.jar' % PARSINGTON_VERSION), 'Expected parsington jar') |
| 42 | + self.assertFalse(os.path.islink(jars[0]), 'Expected hard link but found symbolic link.') |
| 43 | + self.assertTrue(os.path.isfile(jars[0])) |
| 44 | + self.assertGreaterEqual(os.stat(jars[0]).st_nlink, 2, 'Expected ref count of at least 2 for hard link.') |
| 45 | + except OSError as e: |
| 46 | + if e.errno == 18: |
| 47 | + _logger.warning('Unable to cross-device hard link, skipping hard link test: %s', str(e)) |
| 48 | + else: |
| 49 | + raise e |
| 50 | + finally: |
| 51 | + shutil.rmtree(tmp_dir) |
| 52 | + |
| 53 | + def test_resolve_soft(self): |
| 54 | + tmp_dir = tempfile.mkdtemp(prefix='jgo-test-cache-dir') |
| 55 | + m2_repo = os.path.join(str(pathlib.Path.home()), '.m2', 'repository') |
| 56 | + try: |
| 57 | + _, workspace = resolve_parsington(cache_dir=tmp_dir, link_type='soft', m2_repo=m2_repo) |
| 58 | + jars = glob.glob(os.path.join(workspace, '*jar')) |
| 59 | + self.assertEqual(len(jars), 1, 'Expected exactly one jar in workspace') |
| 60 | + self.assertEqual(jars[0], os.path.join(workspace, 'parsington-%s.jar' % PARSINGTON_VERSION), 'Expected parsington jar') |
| 61 | + self.assertTrue(os.path.islink(jars[0]), 'Expected soft link.') |
| 62 | + finally: |
| 63 | + shutil.rmtree(tmp_dir) |
| 64 | + |
| 65 | + def test_resolve_copy(self): |
| 66 | + tmp_dir = tempfile.mkdtemp(prefix='jgo-test-cache-dir') |
| 67 | + m2_repo = os.path.join(str(pathlib.Path.home()), '.m2', 'repository') |
| 68 | + try: |
| 69 | + _, workspace = resolve_parsington(cache_dir=tmp_dir, link_type='copy', m2_repo=m2_repo) |
| 70 | + jars = glob.glob(os.path.join(workspace, '*jar')) |
| 71 | + self.assertEqual(len(jars), 1, 'Expected exactly one jar in workspace') |
| 72 | + self.assertEqual(jars[0], os.path.join(workspace, 'parsington-%s.jar' % PARSINGTON_VERSION), 'Expected parsington jar') |
| 73 | + self.assertFalse(os.path.islink(jars[0]), 'Expected copied file but found symbolic link.') |
| 74 | + self.assertTrue(os.path.isfile(jars[0])) |
| 75 | + self.assertEqual(os.stat(jars[0]).st_nlink, 1, 'Expected ref count of exactly 1 for copied file.') |
| 76 | + finally: |
| 77 | + shutil.rmtree(tmp_dir) |
| 78 | + |
| 79 | + def test_resolve_auto(self): |
| 80 | + tmp_dir = tempfile.mkdtemp(prefix='jgo-test-cache-dir') |
| 81 | + m2_repo = os.path.join(str(pathlib.Path.home()), '.m2', 'repository') |
| 82 | + try: |
| 83 | + _, workspace = resolve_parsington(cache_dir=tmp_dir, link_type='auto', m2_repo=m2_repo) |
| 84 | + jars = glob.glob(os.path.join(workspace, '*jar')) |
| 85 | + self.assertEqual(len(jars), 1, 'Expected exactly one jar in workspace') |
| 86 | + self.assertEqual(jars[0], os.path.join(workspace, 'parsington-%s.jar' % PARSINGTON_VERSION), 'Expected parsington jar') |
| 87 | + finally: |
| 88 | + shutil.rmtree(tmp_dir) |
| 89 | + |
| 90 | + def test_run_jgo(self): |
| 91 | + tmp_dir = tempfile.mkdtemp(prefix='jgo-test-cache-dir') |
| 92 | + |
| 93 | + try: |
| 94 | + completed_process = run_parsington(cache_dir=tmp_dir, link_type='auto', parsington_args=('1+3',)) |
| 95 | + self.assertEqual(completed_process.returncode, 0, 'Expected return code zero.') |
| 96 | + self.assertEqual(completed_process.stdout.decode('ascii').strip(), str(1+3)) |
| 97 | + finally: |
| 98 | + shutil.rmtree(tmp_dir) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + unittest.main() |
| 103 | + |
| 104 | + |
| 105 | + |
0 commit comments