Bug 1445944 - [mozprofile] Add a 'create_profile' helper method for instanting an instance from an app draft
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Thu, 12 Apr 2018 12:40:47 -0400
changeset 783205 1cca70abe57ea916b86623d4ef1f5d96772a0d96
parent 783204 0cfcb74288d9d6a74743206e1b6a47614372489b
child 783206 e9333107b5bdbe49145de98ee8c77c9a606f209a
push id106641
push userahalberstadt@mozilla.com
push dateMon, 16 Apr 2018 19:49:26 +0000
bugs1445944
milestone61.0a1
Bug 1445944 - [mozprofile] Add a 'create_profile' helper method for instanting an instance from an app 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()