Bug 1365089 - Pre: Support setting default keyfile locations in moz.configure. r=chmanchester draft
authorNick Alexander <nalexander@mozilla.com>
Fri, 19 May 2017 11:30:26 -0700
changeset 586215 6064f2cd8217dfe4054567ba9c20f5bac694225a
parent 586049 34ac1a5d6576d6775491c8a882710a1520551da6
child 586216 15aab099b5c303b7027942720e5326295d717b72
push id61332
push userbmo:cnevinchen@gmail.com
push dateTue, 30 May 2017 00:29:11 +0000
reviewerschmanchester
bugs1365089
milestone55.0a1
Bug 1365089 - Pre: Support setting default keyfile locations in moz.configure. r=chmanchester MozReview-Commit-ID: 207kgPU2TPM
build/moz.configure/keyfiles.configure
python/mozbuild/mozbuild/test/configure/test_checks_configure.py
--- a/build/moz.configure/keyfiles.configure
+++ b/build/moz.configure/keyfiles.configure
@@ -1,23 +1,23 @@
 # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 
 @template
-def keyfile(desc, help=None, callback=lambda x: x):
+def keyfile(desc, default=None, help=None, callback=lambda x: x):
     help = help or ('Use the secret key contained in the given keyfile '
                     'for %s requests' % desc)
     name = desc.lower().replace(' ', '-')
     no_key = callback('no-%s-key' % name)
 
-    option('--with-%s-keyfile' % name, nargs=1, help=help)
+    option('--with-%s-keyfile' % name, nargs=1, default=default, help=help)
 
     @depends('--with-%s-keyfile' % name)
     @checking('for the %s key' % desc, lambda x: x and x is not no_key)
     @imports(_from='__builtin__', _import='open')
     @imports(_from='__builtin__', _import='IOError')
     def keyfile(value):
         if value:
             try:
@@ -29,36 +29,37 @@ def keyfile(desc, help=None, callback=la
             except IOError as e:
                 raise FatalCheckError("'%s': %s." % (value[0], e.strerror))
         return no_key
 
     return keyfile
 
 
 @template
-def simple_keyfile(desc):
-    value = keyfile(desc)
+def simple_keyfile(desc, default=None):
+    value = keyfile(desc, default=default)
     set_config('MOZ_%s_KEY' % desc.upper().replace(' ', '_'), value)
 
 
 @template
-def id_and_secret_keyfile(desc):
+def id_and_secret_keyfile(desc, default=None):
     def id_and_secret(value):
         if value.startswith('no-') and value.endswith('-key'):
             id = value[:-3] + 'clientid'
             secret = value
         elif ' ' in value:
             id, secret = value.split(' ', 1)
         else:
             raise FatalCheckError('%s key file has an invalid format.' % desc)
         return namespace(
             id=id,
             secret=secret,
         )
 
     content = keyfile(desc, help='Use the client id and secret key contained '
                                  'in the given keyfile for %s requests' % desc,
+                      default=default,
                       callback=id_and_secret)
 
 
     name = desc.upper().replace(' ', '_')
     set_config('MOZ_%s_CLIENTID' % name, content.id)
     set_config('MOZ_%s_KEY' % name, content.secret)
--- a/python/mozbuild/mozbuild/test/configure/test_checks_configure.py
+++ b/python/mozbuild/mozbuild/test/configure/test_checks_configure.py
@@ -867,16 +867,41 @@ class TestChecksConfigure(unittest.TestC
             self.assertEqual(status, 0)
             self.assertEqual(output, textwrap.dedent('''\
                 checking for the Mozilla API key... yes
             '''))
             self.assertEqual(config, {
                 'MOZ_MOZILLA_API_KEY': 'fake-key',
             })
 
+        with MockedOpen({'default': 'default-key\n'}):
+            config, output, status = self.get_result(
+                "simple_keyfile('Mozilla API', default='default')",
+                includes=includes)
+            self.assertEqual(status, 0)
+            self.assertEqual(output, textwrap.dedent('''\
+                checking for the Mozilla API key... yes
+            '''))
+            self.assertEqual(config, {
+                'MOZ_MOZILLA_API_KEY': 'default-key',
+            })
+
+        with MockedOpen({'default': 'default-key\n',
+                         'key': 'fake-key\n'}):
+            config, output, status = self.get_result(
+                "simple_keyfile('Mozilla API', default='key')",
+                includes=includes)
+            self.assertEqual(status, 0)
+            self.assertEqual(output, textwrap.dedent('''\
+                checking for the Mozilla API key... yes
+            '''))
+            self.assertEqual(config, {
+                'MOZ_MOZILLA_API_KEY': 'fake-key',
+            })
+
     def test_id_and_secret_keyfile(self):
         includes = ('util.configure', 'checks.configure', 'keyfiles.configure')
 
         config, output, status = self.get_result(
             "id_and_secret_keyfile('Bing API')", includes=includes)
         self.assertEqual(status, 0)
         self.assertEqual(output, textwrap.dedent('''\
             checking for the Bing API key... no
@@ -930,11 +955,39 @@ class TestChecksConfigure(unittest.TestC
                 includes=includes)
             self.assertEqual(status, 1)
             self.assertEqual(output, textwrap.dedent('''\
                 checking for the Bing API key... no
                 ERROR: Bing API key file has an invalid format.
             '''))
             self.assertEqual(config, {})
 
+        with MockedOpen({'default-key': 'default-id default-key\n'}):
+            config, output, status = self.get_result(
+                "id_and_secret_keyfile('Bing API', default='default-key')",
+                includes=includes)
+            self.assertEqual(status, 0)
+            self.assertEqual(output, textwrap.dedent('''\
+                checking for the Bing API key... yes
+            '''))
+            self.assertEqual(config, {
+                'MOZ_BING_API_CLIENTID': 'default-id',
+                'MOZ_BING_API_KEY': 'default-key',
+            })
+
+        with MockedOpen({'default-key': 'default-id default-key\n',
+                         'key': 'fake-id fake-key\n'}):
+            config, output, status = self.get_result(
+                "id_and_secret_keyfile('Bing API', default='default-key')",
+                args=['--with-bing-api-keyfile=key'],
+                includes=includes)
+            self.assertEqual(status, 0)
+            self.assertEqual(output, textwrap.dedent('''\
+                checking for the Bing API key... yes
+            '''))
+            self.assertEqual(config, {
+                'MOZ_BING_API_CLIENTID': 'fake-id',
+                'MOZ_BING_API_KEY': 'fake-key',
+            })
+
 
 if __name__ == '__main__':
     main()