|
| 1 | +""" |
| 2 | +Test `alr run -a` argument splitting |
| 3 | +""" |
| 4 | + |
| 5 | +from drivers.alr import run_alr, CalledProcessError |
| 6 | +from drivers.asserts import assert_match |
| 7 | +from subprocess import run |
| 8 | + |
| 9 | +import os, re, shutil |
| 10 | + |
| 11 | +target = 'echo_arguments_1.0.0_filesystem' |
| 12 | +alr_path = os.environ['ALR_PATH'] |
| 13 | + |
| 14 | +def check_run(arguments, match="", should_fail=False): |
| 15 | + if not should_fail: |
| 16 | + p = run_alr('run', '-a', arguments, quiet=not match, complain_on_error=not should_fail) |
| 17 | + if match: |
| 18 | + assert_match(match, p.out, flags=re.S) |
| 19 | + else: |
| 20 | + # Need to check stderr on failure, so using subprocess.run |
| 21 | + p = run([alr_path, "run", "-a", arguments], capture_output=True) |
| 22 | + if 0 == p.returncode: |
| 23 | + raise CalledProcessError |
| 24 | + if match: |
| 25 | + assert_match(match, p.stderr.decode(), flags=re.S) |
| 26 | + |
| 27 | +p = run_alr('get', 'echo_arguments', quiet=True) |
| 28 | +os.chdir(target) |
| 29 | + |
| 30 | +# Split on spaces |
| 31 | +check_run('code example with spaces', match=".*'code'\n'example'\n'with'\n'spaces'") |
| 32 | + |
| 33 | +# Escaped spaces |
| 34 | +check_run('code\ example\ with\ spaces', match=".*'code example with spaces'") |
| 35 | + |
| 36 | +# Unnecessary escapes |
| 37 | +check_run('\c\o\d\e \example \with \s\p\\a\c\e\s', match=".*'code'\n'example'\n'with'\n'spaces'") |
| 38 | + |
| 39 | +# Double quotes |
| 40 | +check_run('code "example with" spaces', match=".*'code'\n'example with'\n'spaces'") |
| 41 | + |
| 42 | +# Single quotes |
| 43 | +check_run('code \'example with\' spaces', match=".*'code'\n'example with'\n'spaces'") |
| 44 | + |
| 45 | +# Escaped double quotes |
| 46 | +check_run('code \\"example with\\" spaces', match=".*'code'\n'\"example'\n'with\"'\n'spaces'") |
| 47 | + |
| 48 | +# Escaped single quotes |
| 49 | +check_run('code \\\'example with\\\' spaces', match=".*'code'\n'\\\\'example'\n'with\\\\''\n'spaces'") |
| 50 | + |
| 51 | +# Nested escaped double quotes |
| 52 | +check_run('code \"example \\\" with\" spaces', match=".*'code'\n'example \" with'\n'spaces'") |
| 53 | + |
| 54 | +# Escaped closing single quote (closing quote cannot be escaped) |
| 55 | +check_run('code \'example \\\' with spaces', match=".*'code'\n'example \\\\\\\\'\n'with'\n'spaces'") |
| 56 | + |
| 57 | +# Nested double & single quotes |
| 58 | +check_run('code \"example \'with spaces\'\"', match=".*'code'\n'example \\\\'with spaces\\\\''") |
| 59 | + |
| 60 | +# Unterminated escape should fail |
| 61 | +check_run('code example with spaces\\', match=".*Unterminated escape sequence in command:.*", should_fail=True) |
| 62 | + |
| 63 | +# Unterminated single quote should fail |
| 64 | +check_run('code example with \'spaces', match=".*Unterminated single quote sequence in command:.*", should_fail=True) |
| 65 | + |
| 66 | +# Unterminated double quote should fail |
| 67 | +check_run('code example with \"spaces', match=".*Unterminated double quote sequence in command:.*", should_fail=True) |
| 68 | + |
| 69 | +os.chdir('..') |
| 70 | +shutil.rmtree(target) |
| 71 | + |
| 72 | +print('SUCCESS') |
0 commit comments