MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / run_scan_click

Function run_scan_click

src/scancode/cli_test_utils.py:83–140  ·  view source on GitHub ↗

Run a scan as a Click-controlled subprocess If monkeypatch is provided, a tty with a size (80, 43) is mocked. Return a click.testing.Result object. If retry is True, wait 10 seconds after a failure and retry once

(
    options,
    monkeypatch=None,
    test_mode=True,
    expected_rc=0,
    env=None,
    retry=True,
    processes='-1',
)

Source from the content-addressed store, hash-verified

81
82
83def run_scan_click(
84 options,
85 monkeypatch=None,
86 test_mode=True,
87 expected_rc=0,
88 env=None,
89 retry=True,
90 processes='-1',
91):
92 """
93 Run a scan as a Click-controlled subprocess
94 If monkeypatch is provided, a tty with a size (80, 43) is mocked.
95 Return a click.testing.Result object.
96 If retry is True, wait 10 seconds after a failure and retry once
97 """
98 import click
99 import shutil
100 from click.testing import CliRunner
101 from scancode import cli
102
103 options = add_windows_extra_timeout(options)
104
105 if test_mode and '--test-mode' not in options:
106 options.append('--test-mode')
107
108 if processes and '-n' not in options and '--processes' not in options:
109 options.extend(['--processes', processes])
110
111 if monkeypatch:
112 monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)
113 monkeypatch.setattr(shutil , 'get_terminal_size', lambda: (80, 43,))
114
115 if not env:
116 env = dict(os.environ)
117
118 runner = CliRunner()
119
120 result = runner.invoke(cli.scancode, options, catch_exceptions=False, env=env)
121 if retry and result.exit_code != expected_rc:
122 if on_windows:
123 # wait and rerun in verbose mode to get more in the output
124 time.sleep(1)
125 if '--verbose' not in options:
126 options.append('--verbose')
127 result = runner.invoke(cli.scancode, options, catch_exceptions=False, env=env)
128
129 if result.exit_code != expected_rc:
130 output = result.output
131 opts = get_opts(options)
132 error = f'''
133Failure to run:
134rc: {result.exit_code}
135scancode {opts}
136output:
137{output}
138'''
139 assert result.exit_code == expected_rc, error
140 return result

Calls 3

get_optsFunction · 0.85
appendMethod · 0.45

Tested by

no test coverage detected