MCPcopy Index your code
hub / github.com/aws/aws-cli / CLIRunner

Class CLIRunner

tests/__init__.py:112–161  ·  view source on GitHub ↗

Runs CLI commands in a stubbed environment

Source from the content-addressed store, hash-verified

110
111
112class CLIRunner:
113 """Runs CLI commands in a stubbed environment"""
114
115 def __init__(self, env=None, session_stubber=None):
116 if env is None:
117 env = self._get_default_env()
118 self.env = env
119 if session_stubber is None:
120 session_stubber = SessionStubber()
121 self._session_stubber = session_stubber
122
123 def run(self, cmdline):
124 with mock.patch('os.environ', self.env):
125 with capture_output() as output:
126 runner_result = self._do_run(cmdline)
127 runner_result.stdout = output.stdout.getvalue()
128 runner_result.stderr = output.stderr.getvalue()
129 return runner_result
130
131 def add_response(self, response):
132 self._session_stubber.add_response(response)
133
134 def _get_default_env(self):
135 # awscli/__init__.py injects AWS_DATA_PATH at import time
136 # so that we can find cli.json. This might be fixed in the
137 # future, but for now we are just replicating the logic in
138 # this abstraction.
139 cli_data_dir = os.path.join(
140 os.path.dirname(os.path.abspath(awscli.__file__)), 'data'
141 )
142 return {
143 'AWS_DATA_PATH': cli_data_dir,
144 'AWS_DEFAULT_REGION': 'us-west-2',
145 'AWS_ACCESS_KEY_ID': 'access_key',
146 'AWS_SECRET_ACCESS_KEY': 'secret_key',
147 'AWS_CONFIG_FILE': '',
148 'AWS_SHARED_CREDENTIALS_FILE': '',
149 }
150
151 def _do_run(self, cmdline):
152 driver = create_clidriver()
153 entry_point = AWSCLIEntryPoint(driver)
154 self._session_stubber.register(driver.session)
155 rc = entry_point.main(cmdline)
156 self._session_stubber.assert_no_remaining_responses()
157 runner_result = CLIRunnerResult(rc)
158 runner_result.aws_requests = copy.copy(
159 self._session_stubber.received_aws_requests
160 )
161 return runner_result
162
163
164class SessionStubber:

Callers 12

test_basic_user_agentFunction · 0.90
cli_runnerFunction · 0.90
setUpMethod · 0.90
cli_runnerFunction · 0.90
cli_runnerFunction · 0.90
setUpMethod · 0.90
runnerFunction · 0.90

Calls

no outgoing calls

Tested by 11

test_basic_user_agentFunction · 0.72
cli_runnerFunction · 0.72
setUpMethod · 0.72
cli_runnerFunction · 0.72
cli_runnerFunction · 0.72
runnerFunction · 0.72