Example #1
0
def _DisplayError(component_trace):
    """Prints the Fire trace and the error to stdout."""
    result = component_trace.GetResult()

    output = []
    show_help = False
    for help_flag in ('-h', '--help'):
        if help_flag in component_trace.elements[-1].args:
            show_help = True

    if show_help:
        command = '{cmd} -- --help'.format(cmd=component_trace.GetCommand())
        print('INFO: Showing help with the command {cmd}.\n'.format(
            cmd=pipes.quote(command)),
              file=sys.stderr)
        help_text = helptext.HelpText(result,
                                      trace=component_trace,
                                      verbose=component_trace.verbose)
        output.append(help_text)
        Display(output, out=sys.stderr)
    else:
        print(formatting.Error('ERROR: ') +
              component_trace.elements[-1].ErrorAsStr(),
              file=sys.stderr)
        error_text = helptext.UsageText(result,
                                        trace=component_trace,
                                        verbose=component_trace.verbose)
        print(error_text, file=sys.stderr)
Example #2
0
    def testUsageOutputNone(self):
        component = None
        t = trace.FireTrace(component, name="None")
        usage_output = helptext.UsageText(component, trace=t, verbose=True)
        expected_output = """
    Usage: None

    For detailed information on this command, run:
      None --help"""
        self.assertEqual(textwrap.dedent(expected_output).lstrip("\n"), usage_output)
Example #3
0
    def testUsageOutputConstructorWithParameter(self):
        component = tc.InstanceVars
        t = trace.FireTrace(component, name="InstanceVars")
        usage_output = helptext.UsageText(component, trace=t, verbose=False)
        expected_output = """
    Usage: InstanceVars --arg1=ARG1 --arg2=ARG2

    For detailed information on this command, run:
      InstanceVars --help"""
        self.assertEqual(textwrap.dedent(expected_output).lstrip("\n"), usage_output)
Example #4
0
    def testUsageOutputConstructorWithParameter(self):
        component = tc.InstanceVars
        t = trace.FireTrace(component, name='InstanceVars')
        usage_output = helptext.UsageText(component, trace=t, verbose=True)
        expected_output = '''
    Usage: InstanceVars --arg1=ARG1 --arg2=ARG2

    For detailed information on this command, run:
      InstanceVars --help'''
        self.assertEqual(usage_output,
                         textwrap.dedent(expected_output).lstrip('\n'))
Example #5
0
    def testUsageOutputEmptyDict(self):
        component = {}
        t = trace.FireTrace(component, name='EmptyDict')
        usage_output = helptext.UsageText(component, trace=t, verbose=True)
        expected_output = """
    Usage: EmptyDict

    For detailed information on this command, run:
      EmptyDict --help"""
        self.assertEqual(
            textwrap.dedent(expected_output).lstrip('\n'), usage_output)
Example #6
0
    def testUsageOutputNone(self):
        component = None
        t = trace.FireTrace(component, name='None')
        usage_output = helptext.UsageText(component, trace=t, verbose=True)
        expected_output = '''
    Usage: None

    For detailed information on this command, run:
      None --help'''
        self.assertEqual(usage_output,
                         textwrap.dedent(expected_output).lstrip('\n'))
Example #7
0
    def testUsageOutputFunctionWithDocstring(self):
        component = tc.multiplier_with_docstring
        t = trace.FireTrace(component, name="multiplier_with_docstring")
        usage_output = helptext.UsageText(component, trace=t, verbose=False)
        expected_output = """
    Usage: multiplier_with_docstring NUM <flags>
      optional flags:        --rate

    For detailed information on this command, run:
      multiplier_with_docstring --help"""
        self.assertEqual(textwrap.dedent(expected_output).lstrip("\n"), usage_output)
Example #8
0
    def testUsageOutputFunctionWithHelp(self):
        component = tc.function_with_help
        t = trace.FireTrace(component, name="function_with_help")
        usage_output = helptext.UsageText(component, trace=t, verbose=False)
        expected_output = """
    Usage: function_with_help <flags>
      optional flags:        --help

    For detailed information on this command, run:
      function_with_help -- --help"""
        self.assertEqual(usage_output, textwrap.dedent(expected_output).lstrip("\n"))
Example #9
0
    def testUsageOutputMethod(self):
        component = tc.NoDefaults().double
        t = trace.FireTrace(component, name="NoDefaults")
        t.AddAccessedProperty(component, "double", ["double"], None, None)
        usage_output = helptext.UsageText(component, trace=t, verbose=False)
        expected_output = """
    Usage: NoDefaults double COUNT

    For detailed information on this command, run:
      NoDefaults double --help"""
        self.assertEqual(usage_output, textwrap.dedent(expected_output).lstrip("\n"))
Example #10
0
    def testUsageOutputVerbose(self):
        component = tc.NoDefaults()
        t = trace.FireTrace(component, name="NoDefaults")
        usage_output = helptext.UsageText(component, trace=t, verbose=True)
        expected_output = """
    Usage: NoDefaults <command>
      available commands:    double | triple

    For detailed information on this command, run:
      NoDefaults --help"""
        self.assertEqual(usage_output, textwrap.dedent(expected_output).lstrip("\n"))
Example #11
0
    def testUsageOutputMethod(self):
        component = tc.NoDefaults().double
        t = trace.FireTrace(component, name='NoDefaults')
        t.AddAccessedProperty(component, 'double', ['double'], None, None)
        usage_output = helptext.UsageText(component, trace=t, verbose=True)
        expected_output = '''
    Usage: NoDefaults double COUNT

    For detailed information on this command, run:
      NoDefaults double --help'''
        self.assertEqual(usage_output,
                         textwrap.dedent(expected_output).lstrip('\n'))
Example #12
0
    def testUsageOutputFunctionWithDocstring(self):
        component = tc.multiplier_with_docstring
        t = trace.FireTrace(component, name='multiplier_with_docstring')
        usage_output = helptext.UsageText(component, trace=t, verbose=True)
        expected_output = '''
    Usage: multiplier_with_docstring NUM <flags>

    Available flags: --rate

    For detailed information on this command, run:
      multiplier_with_docstring --help'''
        self.assertEqual(usage_output,
                         textwrap.dedent(expected_output).lstrip('\n'))
Example #13
0
    def testUsageOutputFunctionWithHelp(self):
        component = tc.function_with_help
        t = trace.FireTrace(component, name='function_with_help')
        usage_output = helptext.UsageText(component, trace=t, verbose=True)
        expected_output = '''
    Usage: function_with_help <flags>

    Available flags: --help

    For detailed information on this command, run:
      function_with_help -- --help'''
        self.assertEqual(usage_output,
                         textwrap.dedent(expected_output).lstrip('\n'))
Example #14
0
    def testUsageOutput(self):
        component = tc.NoDefaults()
        t = trace.FireTrace(component, name='NoDefaults')
        usage_output = helptext.UsageText(component, trace=t, verbose=False)
        expected_output = '''
    Usage: NoDefaults <command>
      available commands:    double | triple

    For detailed information on this command, run:
      NoDefaults --help'''

        self.assertEqual(usage_output,
                         textwrap.dedent(expected_output).lstrip('\n'))
Example #15
0
    def testUsageOutputFunctionMixedDefaults(self):
        component = tc.py3.HelpTextComponent().identity
        t = trace.FireTrace(component, name='FunctionMixedDefaults')
        usage_output = helptext.UsageText(component, trace=t, verbose=False)
        expected_output = """
    Usage: FunctionMixedDefaults <flags>
      optional flags:        --beta
      required flags:        --alpha

    For detailed information on this command, run:
      FunctionMixedDefaults --help"""
        expected_output = textwrap.dedent(expected_output).lstrip('\n')
        self.assertEqual(expected_output, usage_output)
Example #16
0
    def testUsageOutputEmptyDict(self):
        component = {}
        t = trace.FireTrace(component, name='EmptyDict')
        info = inspectutils.Info(component)
        usage_output = helptext.UsageText(component,
                                          info,
                                          trace=t,
                                          verbose=True)
        expected_output = '''
    Usage: EmptyDict

    For detailed information on this command, run:
      EmptyDict --help'''
        self.assertEqual(usage_output,
                         textwrap.dedent(expected_output).lstrip('\n'))
Example #17
0
    def testUsageOutputCallable(self):
        # This is both a group and a command.
        component = tc.CallableWithKeywordArgument()
        t = trace.FireTrace(
            component, name="CallableWithKeywordArgument", separator="@"
        )
        usage_output = helptext.UsageText(component, trace=t, verbose=False)
        expected_output = """
    Usage: CallableWithKeywordArgument <command> | <flags>
      available commands:    print_msg
      flags are accepted

    For detailed information on this command, run:
      CallableWithKeywordArgument -- --help"""
        self.assertEqual(textwrap.dedent(expected_output).lstrip("\n"), usage_output)
Example #18
0
  def testUsageOutputVerbose(self):
    component = tc.NoDefaults()
    t = trace.FireTrace(component, name='NoDefaults')
    info = inspectutils.Info(component)
    usage_output = helptext.UsageText(component, info, trace=t, verbose=True)
    expected_output = '''
    Usage: NoDefaults <commands>
    available commands: double | triple

    For detailed information on this command and its flags, run:
    NoDefaults --help
    '''
    self.assertEqual(
        usage_output,
        textwrap.dedent(expected_output).lstrip('\n'))
Example #19
0
    def testUsageOutputCallable(self):
        # This is both a group and a command!
        component = tc.CallableWithKeywordArgument
        t = trace.FireTrace(component, name='CallableWithKeywordArgument')
        usage_output = helptext.UsageText(component, trace=t, verbose=True)
        # TODO(joejoevictor): We need to handle the case for keyword args as well
        # i.e. __call__ method of CallableWithKeywordArgument
        expected_output = '''
    Usage: CallableWithKeywordArgument <command>

      Available commands:    print_msg

    For detailed information on this command, run:
      CallableWithKeywordArgument -- --help'''
        self.assertEqual(usage_output,
                         textwrap.dedent(expected_output).lstrip('\n'))
Example #20
0
 def testInitRequiresFlagSyntaxSubclassNamedTuple(self):
     component = tc.SubPoint
     t = trace.FireTrace(component, name='SubPoint')
     usage_output = helptext.UsageText(component, trace=t, verbose=False)
     expected_output = 'Usage: SubPoint --x=X --y=Y'
     self.assertIn(expected_output, usage_output)