Bug 1365089 - Pre: Support setting default keyfile locations in moz.configure. r=nalexander
authorNick Alexander <nalexander@mozilla.com>
Fri, 19 May 2017 11:30:26 -0700
changeset 361189 c6887edb612c509263be885af3303ff8ff599563
parent 361188 bc2d16e4e1ad02962a960abd1dd0534cff3a5b74
child 361190 48aff114c781d59154f8b50e341ae801e4ad37a7
push id90813
push usercbook@mozilla.com
push dateTue, 30 May 2017 09:53:44 +0000
treeherdermozilla-inbound@b5c3bb245c4e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnalexander
bugs1365089
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1365089 - Pre: Support setting default keyfile locations in moz.configure. r=nalexander 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()