def test_usage_expands_docstring(self): with patch_main_module_docstring('Name: %s, %%s'): with mock.patch.object(sys, 'stderr', new=mock_stdio_type()) as mock_stderr: app.usage() self.assertIn('Name: {}, %s'.format(sys.argv[0]), mock_stderr.getvalue())
def test_usage(self): with mock.patch.object(sys, 'stderr', new=mock_stdio_type()) as mock_stderr: app.usage() self.assertIn(__doc__, mock_stderr.getvalue()) # Assert that flags are written to stderr. self.assertIn('\n --[no]helpfull:', mock_stderr.getvalue())
def main(args): cmd = SUBCOMMANDS.get(args.subparser, None) if cmd is None: app.usage(shorthelp=True, exitcode=-1) return # raise ValueError('invalid command %s', args.subparser) return cmd.main(args)
def CommandRun(self, argv): """Execute the command with given arguments. First register and parse additional flags. Then run the command. Returns: Command return value. Args: argv: Remaining command line arguments after parsing command and flags (that is a copy of sys.argv at the time of the function call with all parsed flags removed). """ # Register flags global when run normally FLAGS.append_flag_values(self._command_flags) # Prepare flags parsing, to redirect help, to show help for command orig_app_usage = app.usage def ReplacementAppUsage(shorthelp=0, writeto_stdout=1, detailed_error=None, exitcode=None): AppcommandsUsage(shorthelp, writeto_stdout, detailed_error, exitcode=exitcode, show_cmd=self._command_name, show_global_flags=True) app.usage = ReplacementAppUsage # Parse flags and restore app.usage afterwards try: try: argv = ParseFlagsWithUsage(argv) self._command_flags.mark_as_parsed() # Run command ret = self.Run(argv) if ret is None: ret = 0 else: assert isinstance(ret, int) return ret except app.UsageError as error: app.usage(shorthelp=1, detailed_error=error, exitcode=error.exitcode) except: if FLAGS.pdb_post_mortem: traceback.print_exc() pdb.post_mortem() raise finally: # Restore app.usage and remove this command's flags from the global flags. app.usage = orig_app_usage FLAGS.remove_flag_values(self._command_flags)
def Convert(self): """Executes the conversion process.""" if not self._paths: app.usage(shorthelp=True) return False if not self._Check(): return False success = True for path in self._paths: success &= self._ConvertFile(path) return success
def test_usage_exitcode(self): # The test environment may not have the correct output encoding, # and we can't really change it once we've started the test, # so we have to replace it with one that understands unicode. if six.PY2: stderr = codecs.getwriter('utf8')(sys.stderr) else: stderr = sys.stderr with mock.patch.object(sys, 'stderr', new=stderr): try: app.usage(exitcode=2) self.fail('app.usage(exitcode=1) should raise SystemExit') except SystemExit as e: self.assertEqual(2, e.code)
def main(argv): argv = argv[1:] # Discard the script name. # No arguments passed, show usage statement. if len(argv) < 1: app.usage(shorthelp=True, exitcode=1) # Application to deploy: web or chrome. application = argv[0] # Server to deploy to: local, dev, or prod. try: deployment_type = argv[1] except IndexError: logging.info('No deployment type was provided, defaulting to local.') deployment_type = _LOCAL if application == 'web': # If application is web, begin build and deploy for the web app. app_engine_server_config = AppEngineServerConfig( app_servers=FLAGS.app_servers, build_target=FLAGS.build_target, deployment_type=deployment_type, loaner_path=FLAGS.loaner_path, web_app_dir=FLAGS.web_app_dir, yaml_files=FLAGS.yaml_files, version=FLAGS.version) app_engine_server_config.DeployWebApp() elif application == 'chrome': # If application is chrome, begin build and deploy for the chrome app. chrome_app_config = ChromeAppConfig( chrome_app_dir=FLAGS.chrome_app_dir, deployment_type=deployment_type, loaner_path=FLAGS.loaner_path) try: chrome_app_config.DeployChromeApp() except ManifestError as err: sys.exit(err.errno) else: app.usage(shorthelp=True, exitcode=1)
def test_usage_detailed_error(self): with mock.patch.object(sys, 'stderr', new=mock_stdio_type()) as mock_stderr: app.usage(detailed_error='BAZBAZ') self.assertIn('BAZBAZ', mock_stderr.getvalue())
def test_usage_writeto_stderr(self): with mock.patch.object(sys, 'stdout', new=mock_stdio_type()) as mock_stdout: app.usage(writeto_stdout=True) self.assertIn(__doc__, mock_stdout.getvalue())
def test_usage_shorthelp(self): with mock.patch.object(sys, 'stderr', new=mock_stdio_type()) as mock_stderr: app.usage(shorthelp=True) # Assert that flags are NOT written to stderr. self.assertNotIn(' --', mock_stderr.getvalue())
def test_usage_does_not_expand_bad_docstring(self): with patch_main_module_docstring('Name: %s, %%s, %@'): with mock.patch.object(sys, 'stderr', new=mock_stdio_type()) as mock_stderr: app.usage() self.assertIn('Name: %s, %%s, %@', mock_stderr.getvalue())
def test_usage_exitcode(self): try: app.usage(exitcode=2) self.fail('app.usage(exitcode=1) should raise SystemExit') except SystemExit as e: self.assertEqual(2, e.code)