File size: 3,560 Bytes
3f15e9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""Portable driver checks: no raw RPLAN data or geometry dependencies needed."""
import os
from pathlib import Path
import subprocess
import tempfile
import unittest

TOOLS = Path(__file__).resolve().parent


class BuildScriptTests(unittest.TestCase):
    def test_missing_mat_has_actionable_error_before_running_python(self):
        for script in ('build_conventional.sh', 'build_full.sh'):
            with self.subTest(script=script), tempfile.TemporaryDirectory() as folder:
                env = dict(os.environ)
                env.pop('RPLAN_MAT', None)
                result = subprocess.run(['bash', str(TOOLS / script), folder, str(TOOLS.parents[1])],
                                        env=env, capture_output=True, text=True)
                self.assertNotEqual(result.returncode, 0)
                self.assertIn('RPLAN_MAT', result.stderr)
                self.assertIn('data.mat', result.stderr)

    def test_relative_paths_spaces_and_python3_from_path(self):
        for script, calls in (('build_conventional.sh', 9), ('build_full.sh', 4)):
            with self.subTest(script=script), tempfile.TemporaryDirectory() as folder:
                root = Path(folder)
                binary = root / 'bin'
                binary.mkdir()
                fake = binary / 'python3'
                fake.write_text('#!/bin/bash\n'
                                'for arg in "$@"; do printf "%s\\n" "$arg"; done >> "$CALL_LOG"\n'
                                'printf "END\\n" >> "$CALL_LOG"\n')
                fake.chmod(0o755)
                (root / 'data.mat').touch()
                (root / 'code tree/engine/supervisor_site').mkdir(parents=True)
                log = root / 'calls.txt'
                env = {**os.environ, 'PATH': str(binary) + os.pathsep + os.environ['PATH'],
                       'RPLAN_MAT': 'data.mat', 'CALL_LOG': str(log)}
                result = subprocess.run(['bash', str(TOOLS / script), 'out dir', 'code tree'],
                                        cwd=root, env=env, capture_output=True, text=True)
                self.assertEqual(result.returncode, 0, result.stderr)
                args = log.read_text().splitlines()
                self.assertEqual(args.count('END'), calls)
                self.assertIn(str((root / 'data.mat').resolve()), args)
                self.assertIn(str((root / 'code tree').resolve()), args)
                self.assertTrue((root / 'out dir').is_dir())

    def test_conversion_failure_stops_before_screening(self):
        with tempfile.TemporaryDirectory() as folder:
            root = Path(folder)
            (root / 'bin').mkdir()
            fake = root / 'bin/python3'
            fake.write_text('#!/bin/bash\nexit 7\n')
            fake.chmod(0o755)
            (root / 'data.mat').touch()
            (root / 'code/engine/supervisor_site').mkdir(parents=True)
            env = {**os.environ, 'PATH': str(fake.parent) + os.pathsep + os.environ['PATH'],
                   'RPLAN_MAT': str(root / 'data.mat')}
            for script in ('build_conventional.sh', 'build_full.sh'):
                with self.subTest(script=script):
                    out = root / script
                    result = subprocess.run(['bash', str(TOOLS / script), str(out), str(root / 'code')],
                                            env=env, capture_output=True, text=True)
                    self.assertEqual(result.returncode, 7, result.stderr)
                    self.assertFalse(list(out.glob('*.screened.log')))


if __name__ == '__main__':
    unittest.main()