Bug 1445944 - [mozprofile] Add a 'create_profile' helper method for instanting an instance from an app r=rwood
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Thu, 12 Apr 2018 12:40:47 -0400
changeset 467471 96f6bb303871f2ffade8413902135803422a46eb
parent 467470 74448840c21fda21bee03f7b278294105b47a067
child 467472 62c954751e6535370566d267024579e88a01b1a6
push id9165
push userasasaki@mozilla.com
push dateThu, 26 Apr 2018 21:04:54 +0000
treeherdermozilla-beta@064c3804de2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrwood
bugs1445944
milestone61.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 1445944 - [mozprofile] Add a 'create_profile' helper method for instanting an instance from an app r=rwood This will make it a bit easier for consumers to create a profile instance. They can just call: profile = create_profile('firefox', prefs=...) Instead of needing to first find the class, then do the instantiation. MozReview-Commit-ID: 7FqAGsSyZVe
testing/mozbase/mozprofile/mozprofile/profile.py
testing/mozbase/mozprofile/tests/test_profile.py
--- a/testing/mozbase/mozprofile/mozprofile/profile.py
+++ b/testing/mozbase/mozprofile/mozprofile/profile.py
@@ -13,17 +13,18 @@ import uuid
 from .addons import AddonManager
 import mozfile
 from .permissions import Permissions
 from .prefs import Preferences
 from shutil import copytree
 
 __all__ = ['Profile',
            'FirefoxProfile',
-           'ThunderbirdProfile']
+           'ThunderbirdProfile',
+           'create_profile']
 
 
 class Profile(object):
     """Handles all operations regarding profile.
 
     Creating new profiles, installing add-ons, setting preferences and
     handling cleanup.
 
@@ -418,8 +419,30 @@ class ThunderbirdProfile(Profile):
                    'extensions.update.notifyUser': False,
                    'browser.shell.checkDefaultBrowser': False,
                    'browser.tabs.warnOnClose': False,
                    'browser.warnOnQuit': False,
                    'browser.sessionstore.resume_from_crash': False,
                    # prevents the 'new e-mail address' wizard on new profile
                    'mail.provider.enabled': False,
                    }
+
+
+profile_class = {
+    'firefox': FirefoxProfile,
+    'thunderbird': ThunderbirdProfile,
+}
+
+
+def create_profile(app, **kwargs):
+    """Create a profile given an application name.
+
+    :param app: String name of the application to create a profile for, e.g 'firefox'.
+    :param kwargs: Same as the arguments for the Profile class (optional).
+    :returns: An application specific Profile instance
+    :raises: NotImplementedError
+    """
+    cls = profile_class.get(app)
+
+    if not cls:
+        raise NotImplementedError("Profiles not supported for application '{}'".format(app))
+
+    return cls(**kwargs)
--- a/testing/mozbase/mozprofile/tests/test_profile.py
+++ b/testing/mozbase/mozprofile/tests/test_profile.py
@@ -6,17 +6,22 @@
 
 from __future__ import absolute_import
 
 import os
 
 import mozunit
 import pytest
 
-from mozprofile import Profile
+from mozprofile import (
+    Profile,
+    FirefoxProfile,
+    ThunderbirdProfile,
+    create_profile,
+)
 
 
 def test_with_profile_should_cleanup():
     with Profile() as profile:
         assert os.path.exists(profile.profile)
 
     # profile is cleaned
     assert not os.path.exists(profile.profile)
@@ -27,10 +32,29 @@ def test_with_profile_should_cleanup_eve
         with Profile() as profile:
             assert os.path.exists(profile.profile)
             1 / 0  # will raise ZeroDivisionError
 
     # profile is cleaned
     assert not os.path.exists(profile.profile)
 
 
+@pytest.mark.parametrize('app,cls', [
+    ('firefox', FirefoxProfile),
+    ('thunderbird', ThunderbirdProfile),
+    ('unknown', None)
+])
+def test_create_profile(tmpdir, app, cls):
+    path = tmpdir.strpath
+
+    if cls is None:
+        with pytest.raises(NotImplementedError):
+            create_profile(app)
+        return
+
+    profile = create_profile(app, profile=path)
+    assert isinstance(profile, Profile)
+    assert profile.__class__ == cls
+    assert profile.profile == path
+
+
 if __name__ == '__main__':
     mozunit.main()