def testGetValueWithPromptingFallthrough(self, current_value):
        properties.VALUES.core.disable_prompts.Set(current_value)
        spec = copy.deepcopy(self.resource_spec)

        def GetValue():
            return console_io.PromptResponse('value? >')

        spec.attributes[0].fallthroughs = [deps.Fallthrough(GetValue, 'hint')]
        presentation_spec = presentation_specs.ResourcePresentationSpec(
            '--book',
            spec,
            'a resource',
            flag_name_overrides={'project': '--book-project'},
            prefixes=False)
        resource_info = concept_parsers.ConceptParser(
            [presentation_spec]).GetInfo(presentation_spec.name)
        ns = core_completer_test_base.MockNamespace(args={},
                                                    handler_info=resource_info)
        argument = mock.MagicMock(dest='book')

        parameter_info = resource_parameter_info.ResourceParameterInfo(
            resource_info, ns, argument)

        self.assertEqual(None, parameter_info.GetValue('projectsId'))
        # Ensure that the property is restored after GetValue.
        self.assertEqual(current_value,
                         properties.VALUES.core.disable_prompts.GetBool())
        properties.VALUES.core.disable_prompts.Set(True)
Example #2
0
 def SetUp(self):
     self.calls = None
     self.completer = None
     self.parsed_args = core_completer_test_base.MockNamespace({
         '--project':
         'default-x-project',
     })
 def Run(self, completer, expected_command=None, expected_completions=None,
         args=None):
   completer = parser_completer.ArgumentCompleter(
       completer, core_completer_test_base.MockArgument('id'))
   completions = completer(
       '', core_completer_test_base.MockNamespace(args=args))
   self.assertEqual(expected_completions or [], completions)
   self.AssertListCommand(expected_command)
  def testParse(self):
    dependency_manager = dependency_managers.DependencyManager(
        dependency_managers.DependencyNode.FromAttribute(
            self.string_concept.Attribute()))
    mock_namespace = core_completer_test_base.MockNamespace(
        args={'c': 'value'})

    result = dependency_manager.ParseConcept(mock_namespace)

    self.assertEqual('value', result)
    def testFinalParse(self):
        dependencies = dependency_managers.DependencyNode.FromAttribute(
            self.string_concept.Attribute())
        mock_namespace = core_completer_test_base.MockNamespace(
            args={'c': 'value'})
        arg_getter = lambda: mock_namespace

        result = concept_managers.FinalParse(dependencies, arg_getter)

        self.assertEqual('value', result)
Example #6
0
 def SetUp(self):
     self.StartPropertyPatch(config.Paths,
                             'cache_dir',
                             return_value=os.path.join(
                                 self.temp_path, 'cache'))
     self.StartObjectPatch(persistent_cache_base,
                           'Now',
                           side_effect=self.Now)
     self.now = updaters.NOW_START_TIME
     self.cache = resource_cache.ResourceCache()
     dest = 'instance'
     self.parsed_args = core_completer_test_base.MockNamespace(
         args={dest: None})
     self.argument = self.parsed_args.GetPositionalArgument(dest)
    def SetUpBookParameterInfo(self, args):
        """Creates ResourceParameterInfo for book resource.

    Args:
      args: {str: str}, dict of flag names to values for the mock namespace.

    Returns:
      resource_parameter_info.ResourceParameterInfo, the parameter info object.
    """
        ns = core_completer_test_base.MockNamespace(
            args=args, handler_info=self.resource_info)
        argument = mock.MagicMock(dest='book')
        parameter_info = resource_parameter_info.ResourceParameterInfo(
            self.resource_info, ns, argument)
        return parameter_info
  def testRecursiveParsingWithFallthroughs(self):
    properties.VALUES.core.project.Set('ft')
    dependency_manager = dependency_managers.DependencyManager(
        dependency_managers.DependencyNode.FromAttribute(
            self.group_arg_concept.Attribute()))
    mock_namespace = core_completer_test_base.MockNamespace(
        args={'first_bar': 'y',
              'second_bar': 'b'})

    result = dependency_manager.ParseConcept(mock_namespace)

    self.assertEqual('ft', result.first.foo)
    self.assertEqual('y', result.first.bar)
    self.assertEqual('ft', result.second.foo)
    self.assertEqual('b', result.second.bar)
  def testRecursiveParsing(self):
    dependency_manager = dependency_managers.DependencyManager(
        dependency_managers.DependencyNode.FromAttribute(
            self.group_arg_concept.Attribute()))
    mock_namespace = core_completer_test_base.MockNamespace(
        args={'first_foo': 'x',
              'first_bar': 'y',
              'second_foo': 'a',
              'second_bar': 'b'})

    result = dependency_manager.ParseConcept(mock_namespace)
    self.assertEqual('x', result.first.foo)
    self.assertEqual('y', result.first.bar)
    self.assertEqual('a', result.second.foo)
    self.assertEqual('b', result.second.bar)
Example #10
0
 def SetUp(self):
     properties.VALUES.core.project.Set('myxproject')
     self.parsed_args = core_completer_test_base.MockNamespace(
         args={
             'instance':
             'my_z_instance',  # positional, no flag representation
             '--project': None,  # properties fallback
             '--undefined': None,  # flag not specified
             '--zone': 'zone-1',  # flag specified
         }, )
     self.parameter_info = parameter_info_lib.ParameterInfoByConvention(
         self.parsed_args,
         MockArgument('instance'),
         collection='test.resource',
     )
 def testGetFlagNoProject(self):
     self.UnsetProject()
     presentation_spec = presentation_specs.ResourcePresentationSpec(
         '--book',
         self.resource_spec,
         'a resource',
         flag_name_overrides={},
         prefixes=False)
     resource_info = concept_parsers.ConceptParser(
         [presentation_spec]).GetInfo(presentation_spec.name)
     ns = core_completer_test_base.MockNamespace(args={},
                                                 handler_info=resource_info)
     argument = mock.MagicMock(dest='book')
     parameter_info = resource_parameter_info.ResourceParameterInfo(
         resource_info, ns, argument)
     flag = parameter_info.GetFlag('projectsId')
     self.assertEqual(None, flag)
Example #12
0
    def Resources(self,
                  args=None,
                  cli=None,
                  command_resources=None,
                  search_resources=None,
                  command_only=False,
                  handler_info=None):
        """Sets up resources and mocks for completer tests."""
        self.parsed_args = core_completer_test_base.MockNamespace(
            args=args,
            cli=cli,
            command_resources=command_resources,
            command_only=command_only,
            handler_info=handler_info)
        self.calls = self.parsed_args._calls  # pylint: disable=protected-access

        if search_resources:

            def _ResourceSearchList(limit=None,
                                    page_size=None,
                                    query=None,
                                    sort_by=None,
                                    uri=False):
                """resource_search.list mock."""

                del limit, page_size, sort_by, uri
                results = None
                select_resource = resource_filter.Compile(query).Evaluate
                for name, value in six.iteritems(search_resources):
                    if select_resource({'@type': name}):
                        if results is None:
                            results = []
                        results += value
                if results is None:
                    raise resource_search.CollectionNotIndexed(
                        'Collection [{}] not indexed for search.'.format(
                            query))
                return results

            self.StartObjectPatch(resource_search,
                                  'List',
                                  side_effect=_ResourceSearchList)
    def testGetValueWithFallthrough(self):
        spec = copy.deepcopy(self.resource_spec)
        spec.attributes[0].fallthroughs = [
            deps.PropertyFallthrough(properties.VALUES.core.project)
        ]
        presentation_spec = presentation_specs.ResourcePresentationSpec(
            '--book',
            spec,
            'a resource',
            flag_name_overrides={'project': '--book-project'},
            prefixes=False)
        resource_info = concept_parsers.ConceptParser(
            [presentation_spec]).GetInfo(presentation_spec.name)
        ns = core_completer_test_base.MockNamespace(args={},
                                                    handler_info=resource_info)
        argument = mock.MagicMock(dest='book')

        parameter_info = resource_parameter_info.ResourceParameterInfo(
            resource_info, ns, argument)

        self.assertEqual(self.Project(), parameter_info.GetValue('projectsId'))