1
|
# SPDX-License-Identifier: CC0-1.0
|
2
|
|
3
|
# Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org>
|
4
|
#
|
5
|
# Available under the terms of Creative Commons Zero v1.0 Universal.
|
6
|
|
7
|
import re
|
8
|
|
9
|
variable_word_re = re.compile(r'^<(.+)>$')
|
10
|
|
11
|
def process_command(command, expected_command):
|
12
|
"""Validate the command line and extract its variable parts (if any)."""
|
13
|
assert len(command) == len(expected_command)
|
14
|
|
15
|
extracted = {}
|
16
|
for word, expected_word in zip(command, expected_command):
|
17
|
match = variable_word_re.match(expected_word)
|
18
|
if match:
|
19
|
extracted[match.group(1)] = word
|
20
|
else:
|
21
|
assert word == expected_word
|
22
|
|
23
|
return extracted
|
24
|
|
25
|
def run_missing_executable(command, **kwargs):
|
26
|
"""
|
27
|
Instead of running a command, raise FileNotFoundError as if its executable
|
28
|
was missing.
|
29
|
"""
|
30
|
raise FileNotFoundError('dummy')
|
31
|
|
32
|
class MockedCompletedProcess:
|
33
|
"""
|
34
|
Object with some fields similar to those of subprocess.CompletedProcess.
|
35
|
"""
|
36
|
def __init__(self, args, returncode=0,
|
37
|
stdout='some output', stderr='some error output',
|
38
|
text_output=True):
|
39
|
"""
|
40
|
Initialize MockedCompletedProcess. Convert strings to bytes if needed.
|
41
|
"""
|
42
|
self.args = args
|
43
|
self.returncode = returncode
|
44
|
|
45
|
if type(stdout) is str and not text_output:
|
46
|
stdout = stdout.encode()
|
47
|
if type(stderr) is str and not text_output:
|
48
|
stderr = stderr.encode()
|
49
|
|
50
|
self.stdout = stdout
|
51
|
self.stderr = stderr
|