def check_output_with_error(*pargs, **args): if 'stdout' in args or 'stderr' in args: raise ValueError( 'stdout and stderr not allowed, as they are overriden') proc = subprocess.Popen(stdout=subprocess.PIPE, stderr=subprocess.PIPE, *pargs, **args) stdout, stderr = proc.communicate() exit_code = proc.poll() if exit_code: cmd = args.get('args') if 'args' in args else pargs[0] error = CalledProcessError(exit_code, cmd, output=stdout) # Python 3 if 'stderr' in error.__dict__: error.stderr = stderr else: # Python 2.7, make it behave like Python 3 error.__dict__['stdout'] = error.output error.__dict__['stderr'] = stderr raise error return stdout
def test_fails_on_non_expected_exception(self): mock_client = _get_time_noop_mock_client() exp = CalledProcessError(-1, 'blah') exp.stderr = '"" is not a valid tag' controller_client = Mock() controller_client.get_models.side_effect = [exp] mock_client.get_controller_client.return_value = controller_client with self.assertRaises(CalledProcessError): amm.wait_until_model_disappears( mock_client, 'test_model', timeout=60)
def check_returncode(self): if self.returncode: # The stdout and stderr attributes weren't added until # Python 3.5; we manually include them here for code # targeting more recent versions which expects them. ret = CalledProcessError(self.returncode, self.args, output=self.stdout) ret.stdout = self.stdout ret.stderr = None raise ret
def test_fails_on_non_expected_exception(self): mock_client = _get_time_noop_mock_client() exp = CalledProcessError(-1, 'blah') exp.stderr = '"" is not a valid tag' controller_client = Mock() controller_client.get_models.side_effect = [exp] mock_client.get_controller_client.return_value = controller_client with self.assertRaises(CalledProcessError): amm.wait_until_model_disappears(mock_client, 'test_model', timeout=60)
def check_output(run_args, *args, **kwargs): kwargs['stdout'] = PIPE kwargs['stderr'] = PIPE process = Popen(run_args, *args, **kwargs) stdout, stderr = process.communicate() retcode = process.poll() if retcode is not 0: exception = CalledProcessError(retcode, run_args[0]) exception.stdout = stdout exception.stderr = stderr raise exception return stdout, stderr
def test_ignores_model_detail_exceptions(self): """ignore errors for model details as this might happen many times.""" mock_client = _get_time_noop_mock_client() model_data = {'models': [{'name': ''}]} exp = CalledProcessError(-1, 'blah') exp.stderr = 'cannot get model details' controller_client = Mock() controller_client.get_models.side_effect = [exp, model_data] mock_client.get_controller_client.return_value = controller_client with patch.object(amm, 'sleep') as mock_sleep: amm.wait_until_model_disappears(mock_client, 'test_model', timeout=60) mock_sleep.assert_called_once_with(1)
def check_call_capturing(arguments, input = None, preexec_fn = None): """Spawn a process and return its output.""" (stdout, stderr, code) = call_capturing(arguments, input, preexec_fn) if code == 0: return (stdout, stderr) else: from subprocess import CalledProcessError error = CalledProcessError(code, arguments) error.stdout = stdout error.stderr = stderr raise error
def test_ignores_model_detail_exceptions(self): """ignore errors for model details as this might happen many times.""" mock_client = _get_time_noop_mock_client() model_data = {'models': [{'name': ''}]} exp = CalledProcessError(-1, 'blah') exp.stderr = 'cannot get model details' controller_client = Mock() controller_client.get_models.side_effect = [ exp, model_data] mock_client.get_controller_client.return_value = controller_client with patch.object(amm, 'sleep') as mock_sleep: amm.wait_until_model_disappears( mock_client, 'test_model', timeout=60) mock_sleep.assert_called_once_with(1)
def func(*args): """Wrapper function used to append arguments to command.""" cmd = [command_line] cmd += [str(arg) for arg in args] proc = subprocess.Popen(cmd, stderr=PIPE, stdout=PIPE) stdout, stderr = proc.communicate() proc.wait() if proc.returncode: err = CalledProcessError( returncode=proc.returncode, cmd=" ".join(cmd), output=stdout, ) err.stderr = stderr raise err return stdout, stderr
def __submit(self, call, env): output_chunks = [] process = Popen(call, env=env, stderr=STDOUT, stdout=PIPE) while process.returncode is None: output_chunks.append(process.communicate()) sleep(0.1) stdout = "".join([c[0].decode('utf-8') for c in output_chunks if c[0] is not None]) stderr = "".join([c[1].decode('utf-8') for c in output_chunks if c[1] is not None]) if process.returncode == 0: return stdout, stderr exc = CalledProcessError(process.returncode, call) exc.stdout = stdout exc.stderr = stderr print(stdout) print(stderr) raise exc
def check_output(command, cwd=None, shell=False, env=None, stdin=__sentinel__, stderr=__sentinel__, preexec_fn=None, use_texpath=True, show_window=False): ''' Takes a command to be passed to subprocess.Popen. Returns the output if the command was successful. By default stderr is redirected to stdout, so this will return any output to either stream. This can be changed by calling execute_command with stderr set to subprocess.PIPE or any other valid value. Raises CalledProcessError if the command returned a non-zero value Raises OSError if the executable is not found This is pretty much identical to subprocess.check_output(), but implemented here since it is unavailable in Python 2.6's library. ''' returncode, stdout, stderr = execute_command(command, cwd=cwd, shell=shell, env=env, stdin=stdin, stderr=stderr, preexec_fn=preexec_fn, use_texpath=use_texpath, show_window=show_window) if returncode: e = CalledProcessError(returncode, command) e.output = stdout e.stderr = stderr raise e return stdout
def check_output(command, cwd=None, shell=False, env=None, stdin=__sentinel__, stderr=__sentinel__, preexec_fn=None, use_texpath=True, show_window=False): ''' Takes a command to be passed to subprocess.Popen. Returns the output if the command was successful. By default stderr is redirected to stdout, so this will return any output to either stream. This can be changed by calling execute_command with stderr set to subprocess.PIPE or any other valid value. Raises CalledProcessError if the command returned a non-zero value Raises OSError if the executable is not found This is pretty much identical to subprocess.check_output(), but implemented here since it is unavailable in Python 2.6's library. ''' returncode, stdout, stderr = execute_command( command, cwd=cwd, shell=shell, env=env, stdin=stdin, stderr=stderr, preexec_fn=preexec_fn, use_texpath=use_texpath, show_window=show_window ) if returncode: e = CalledProcessError( returncode, command ) e.output = stdout e.stderr = stderr raise e return stdout
@pytest.mark.slow def test_install_namespace_package(): client.install("@material-ui/core") assert client.web_module_exists("@material-ui/core") expected = "../web_modules/@material-ui/core.js" assert client.web_module_url("@material-ui/core") == expected def test_raise_on_missing_import_path(): with pytest.raises(ValueError, match="does not exist"): client.web_module_url("module/that/does/not/exist") called_process_error = CalledProcessError(1, "failing-cmd") called_process_error.stderr = b"an error occured" @mock.patch("subprocess.run", side_effect=called_process_error) def test_bad_subprocess_call(subprocess_run, caplog): with pytest.raises(CalledProcessError): client.install(["victory"]) assert "an error occured" in caplog.text def test_cannot_register_module_from_non_existant_source(): with pytest.raises(ValueError, match="does not exist"): client.register_web_module("my-module", Path() / "a-non-existant-file.js") with pytest.raises(ValueError, match="is not a file"):