def test_string_parser(self, allow_empty_string, user_input, expected):
     parser = utils.StringParser(allow_empty_string)
     self.assertEqual(parser.parse(user_input), expected)
     self.assertEqual(utils.StringParser(allow_empty_string), parser)
     self.assertEqual(
         '<StringParser(allow_empty_string={})>'.format(allow_empty_string),
         repr(parser))
     self.assertEqual('StringParser', str(parser))
Exemplo n.º 2
0
class AppConstantsTest(parameterized.TestCase, absltest.TestCase):
    def test_get_default_constants(self):
        constants = app_constants.get_default_constants()
        self.assertLen(constants, 7)

    @flagsaver.flagsaver(app_constants_test_flag='valid')
    def test_get_constants_from_flags(self):
        constants = app_constants.get_constants_from_flags(module=__name__)
        self.assertLen(constants, 1)
        self.assertEqual('valid', constants['app_constants_test_flag'].value)

    def test_get_constants_from_flags__not_parsed(self):
        FLAGS.__dict__['__flags_parsed'] = False
        constants = app_constants.get_constants_from_flags(module=__name__)
        FLAGS.__dict__['__flags_parsed'] = True
        self.assertLen(constants, 1)
        self.assertEqual('', constants['app_constants_test_flag'].value)

    @parameterized.parameters(
        ('DEFAULT', None, 'new', 'new'),
        ('', utils.StringParser(True), '', ''),
        ('yes', utils.YesNoParser(), 'no', False),
        ('', utils.ListParser(True), 'this,one', ['this', 'one']),
        ('', utils.StringParser(False), 'asdf', 'asdf'),
        ('1', flags.IntegerParser(), '10', 10),
        ('*****@*****.**', utils.EmailParser(), '*****@*****.**', '*****@*****.**'),
    )
    def test_constant_constructor(self, default, parser, new_value, expected):
        test_constant = app_constants.Constant('name', 'message', default,
                                               parser)
        self.assertEqual('name: {}'.format(default), str(test_constant))
        self.assertEqual(
            "<Constant('name', 'message', {!r}, {!r})>".format(
                default, parser), repr(test_constant))
        self.assertEqual(
            app_constants.Constant('name', 'message', default, parser),
            test_constant)
        self.assertNotEqual(
            app_constants.Constant('other', 'message', default, parser),
            test_constant)
        test_constant.value = new_value
        self.assertTrue(test_constant.valid)
        self.assertEqual(test_constant.value, expected)

    def test_constant_prompt(self):
        test_constant = app_constants.Constant('name', 'messsage', '',
                                               utils.StringParser(False))
        self.assertFalse(test_constant.valid)
        with mock.patch.object(utils, 'prompt', return_value='VALID!'):
            test_constant.prompt()
            self.assertTrue(test_constant.valid)
Exemplo n.º 3
0
 def test_constant_prompt(self):
     test_constant = app_constants.Constant('name', 'messsage', '',
                                            utils.StringParser(False))
     self.assertFalse(test_constant.valid)
     with mock.patch.object(utils, 'prompt', return_value='VALID!'):
         test_constant.prompt()
         self.assertTrue(test_constant.valid)
Exemplo n.º 4
0
    def setUp(self):
        super(ManagerTest, self).setUp()
        # Save the real modules for clean up.
        self.real_open = builtins.open
        # Create a fake file system and stub out builtin modules.
        self.fs = fake_filesystem.FakeFilesystem()
        self.os = fake_filesystem.FakeOsModule(self.fs)
        self.open = fake_filesystem.FakeFileOpen(self.fs)
        self.stdout = StringIO()
        self.stubs = mox3_stubout.StubOutForTesting()
        self.stubs.SmartSet(builtins, 'open', self.open)
        self.stubs.SmartSet(common, 'os', self.os)
        self.stubs.SmartSet(sys, 'stdout', self.stdout)

        # Setup Testdata.
        self._testdata_path = '/testdata'
        self._valid_config_path = self._testdata_path + '/valid_config.yaml'
        self._blank_config_path = self._testdata_path + '/blank_config.yaml'

        self.fs.CreateFile(self._valid_config_path, contents=_VALID_CONFIG)
        self.fs.CreateFile(self._blank_config_path, contents=_BLANK_CONFIG)

        # Load the default config.
        self._valid_default_config = common.ProjectConfig.from_yaml(
            common.DEFAULT, self._valid_config_path)

        # Create test constants.
        self._constants = {
            'test':
            app_constants.Constant(
                'test',
                'message',
                '',
                parser=utils.StringParser(allow_empty_string=False),
            ),
            'other':
            app_constants.Constant('other', 'other message', 'value'),
        }

        # Mock out the authentication credentials.
        self.auth_patcher = mock.patch.object(auth, 'CloudCredentials')
        self.mock_creds = self.auth_patcher.start()
        self.mock_creds.return_value.get_credentials.return_value = (
            credentials.AnonymousCredentials())
 def test_string_parser__invalid_input(self):
     parser = utils.StringParser(False)
     with self.assertRaises(ValueError):
         parser.parse('')
    'The G Suite customer ID.\nIf you are an administrator of the organization '
    'this application is running in leave the default. If you are a reseller '
    'you can get the customer ID by making a get user request: '
    'https://developers.google.com/admin-sdk/directory/v1/guides/manage-users'
    '.html#get_user')

# Dictionary where the flag name is the key and the value is a parser, an object
# that has `parse` as a public instance method. A parser is not required,
# without one any value will be accepted.
_PARSERS = {
    APP_DOMAINS: utils.ListParser(allow_empty_list=False),
    CHROME_CLIENT_ID: utils.ClientIDParser(),
    WEB_CLIENT_ID: utils.ClientIDParser(),
    ADMIN_EMAIL: utils.EmailParser(),
    SEND_EMAIL_AS: utils.EmailParser(),
    SUPERADMINS_GROUP: utils.StringParser(allow_empty_string=False),
    CUSTOMER_ID: utils.StringParser(allow_empty_string=False),
}


def get_constants_from_flags(module=__name__):
    """Returns a dictionary of all constants from flags.

  This should only be used when skipping user validation (e.g. scripting) since
  it does not validate the provided values with the custom parsers until the
  value is requested. If the flag provided does not meet the `Parser`
  requirements an error will be raised when attempting to retrieve the value.

  Args:
    module: str, the name of the module to get the constants from.