diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /testing/mozbase/mozprofile/tests/test_profile.py | |
parent | Initial commit. (diff) | |
download | thunderbird-upstream.tar.xz thunderbird-upstream.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | testing/mozbase/mozprofile/tests/test_profile.py | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/testing/mozbase/mozprofile/tests/test_profile.py b/testing/mozbase/mozprofile/tests/test_profile.py new file mode 100644 index 0000000000..afbd4c365b --- /dev/null +++ b/testing/mozbase/mozprofile/tests/test_profile.py @@ -0,0 +1,111 @@ +#!/usr/bin/env 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/. + +import os + +import mozunit +import pytest +from mozprofile import ( + BaseProfile, + ChromeProfile, + ChromiumProfile, + FirefoxProfile, + Profile, + ThunderbirdProfile, + create_profile, +) +from mozprofile.prefs import Preferences + +here = os.path.abspath(os.path.dirname(__file__)) + + +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) + + +def test_with_profile_should_cleanup_even_on_exception(): + with pytest.raises(ZeroDivisionError): + # pylint --py3k W1619 + 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", + [ + ("chrome", ChromeProfile), + ("chromium", ChromiumProfile), + ("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, BaseProfile) + assert profile.__class__ == cls + assert profile.profile == path + + +@pytest.mark.parametrize( + "cls", + [ + Profile, + ChromeProfile, + ChromiumProfile, + ], +) +def test_merge_profile(cls): + profile = cls(preferences={"foo": "bar"}) + assert profile._addons == [] + assert os.path.isfile( + os.path.join(profile.profile, profile.preference_file_names[0]) + ) + + other_profile = os.path.join(here, "files", "dummy-profile") + profile.merge(other_profile) + + # make sure to add a pref file for each preference_file_names in the dummy-profile + prefs = {} + for name in profile.preference_file_names: + path = os.path.join(profile.profile, name) + assert os.path.isfile(path) + + try: + prefs.update(Preferences.read_json(path)) + except ValueError: + prefs.update(Preferences.read_prefs(path)) + + assert "foo" in prefs + assert len(prefs) == len(profile.preference_file_names) + 1 + assert all(name in prefs for name in profile.preference_file_names) + + # for Google Chrome currently we ignore webext in profile prefs + if cls == Profile: + assert len(profile._addons) == 1 + assert profile._addons[0].endswith("empty.xpi") + assert os.path.exists(profile._addons[0]) + else: + assert len(profile._addons) == 0 + + +if __name__ == "__main__": + mozunit.main() |