| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
# License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 5 |
# You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
import os |
| 8 |
|
| 9 |
import mozunit |
| 10 |
import pytest |
| 11 |
from mozprofile import ( |
| 12 |
BaseProfile, |
| 13 |
ChromeProfile, |
| 14 |
ChromiumProfile, |
| 15 |
FirefoxProfile, |
| 16 |
Profile, |
| 17 |
ThunderbirdProfile, |
| 18 |
create_profile, |
| 19 |
) |
| 20 |
from mozprofile.prefs import Preferences |
| 21 |
|
| 22 |
here = os.path.abspath(os.path.dirname(__file__)) |
| 23 |
|
| 24 |
|
| 25 |
def test_with_profile_should_cleanup(): |
| 26 |
with Profile() as profile: |
| 27 |
assert os.path.exists(profile.profile) |
| 28 |
|
| 29 |
# profile is cleaned |
| 30 |
assert not os.path.exists(profile.profile) |
| 31 |
|
| 32 |
|
| 33 |
def test_with_profile_should_cleanup_even_on_exception(): |
| 34 |
with pytest.raises(ZeroDivisionError): |
| 35 |
# pylint --py3k W1619 |
| 36 |
with Profile() as profile: |
| 37 |
assert os.path.exists(profile.profile) |
| 38 |
1 / 0 # will raise ZeroDivisionError |
| 39 |
|
| 40 |
# profile is cleaned |
| 41 |
assert not os.path.exists(profile.profile) |
| 42 |
|
| 43 |
|
| 44 |
@pytest.mark.parametrize( |
| 45 |
"app,cls", |
| 46 |
[ |
| 47 |
("chrome", ChromeProfile), |
| 48 |
("chromium", ChromiumProfile), |
| 49 |
("firefox", FirefoxProfile), |
| 50 |
("thunderbird", ThunderbirdProfile), |
| 51 |
("unknown", None), |
| 52 |
], |
| 53 |
) |
| 54 |
def test_create_profile(tmpdir, app, cls): |
| 55 |
path = tmpdir.strpath |
| 56 |
|
| 57 |
if cls is None: |
| 58 |
with pytest.raises(NotImplementedError): |
| 59 |
create_profile(app) |
| 60 |
return |
| 61 |
|
| 62 |
profile = create_profile(app, profile=path) |
| 63 |
assert isinstance(profile, BaseProfile) |
| 64 |
assert profile.__class__ == cls |
| 65 |
assert profile.profile == path |
| 66 |
|
| 67 |
|
| 68 |
@pytest.mark.parametrize( |
| 69 |
"cls", |
| 70 |
[ |
| 71 |
Profile, |
| 72 |
ChromeProfile, |
| 73 |
ChromiumProfile, |
| 74 |
], |
| 75 |
) |
| 76 |
def test_merge_profile(cls): |
| 77 |
profile = cls(preferences={"foo": "bar"}) |
| 78 |
assert profile._addons == [] |
| 79 |
assert os.path.isfile( |
| 80 |
os.path.join(profile.profile, profile.preference_file_names[0]) |
| 81 |
) |
| 82 |
|
| 83 |
other_profile = os.path.join(here, "files", "dummy-profile") |
| 84 |
profile.merge(other_profile) |
| 85 |
|
| 86 |
# make sure to add a pref file for each preference_file_names in the dummy-profile |
| 87 |
prefs = {} |
| 88 |
for name in profile.preference_file_names: |
| 89 |
path = os.path.join(profile.profile, name) |
| 90 |
assert os.path.isfile(path) |
| 91 |
|
| 92 |
try: |
| 93 |
prefs.update(Preferences.read_json(path)) |
| 94 |
except ValueError: |
| 95 |
prefs.update(Preferences.read_prefs(path)) |
| 96 |
|
| 97 |
assert "foo" in prefs |
| 98 |
assert len(prefs) == len(profile.preference_file_names) + 1 |
| 99 |
assert all(name in prefs for name in profile.preference_file_names) |
| 100 |
|
| 101 |
# for Google Chrome currently we ignore webext in profile prefs |
| 102 |
if cls == Profile: |
| 103 |
assert len(profile._addons) == 1 |
| 104 |
assert profile._addons[0].endswith("empty.xpi") |
| 105 |
assert os.path.exists(profile._addons[0]) |
| 106 |
else: |
| 107 |
assert len(profile._addons) == 0 |
| 108 |
|
| 109 |
|
| 110 |
if __name__ == "__main__": |
| 111 |
mozunit.main() |