test_clone_cleanup.py (2070B)
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 mozfile 10 import mozunit 11 import pytest 12 from mozprofile.profile import Profile 13 14 """ 15 test cleanup logic for the clone functionality 16 see https://bugzilla.mozilla.org/show_bug.cgi?id=642843 17 """ 18 19 20 @pytest.fixture 21 def profile(tmpdir): 22 # make a profile with one preference 23 path = tmpdir.mkdtemp().strpath 24 profile = Profile(path, preferences={"foo": "bar"}, restore=False) 25 user_js = os.path.join(profile.profile, "user.js") 26 assert os.path.exists(user_js) 27 return profile 28 29 30 def test_restore_true(profile): 31 counter = [0] 32 33 def _feedback(dir, content): 34 # Called by shutil.copytree on each visited directory. 35 # Used here to display info. 36 # 37 # Returns the items that should be ignored by 38 # shutil.copytree when copying the tree, so always returns 39 # an empty list. 40 counter[0] += 1 41 return [] 42 43 # make a clone of this profile with restore=True 44 clone = Profile.clone(profile.profile, restore=True, ignore=_feedback) 45 try: 46 clone.cleanup() 47 48 # clone should be deleted 49 assert not os.path.exists(clone.profile) 50 assert counter[0] > 0 51 finally: 52 mozfile.remove(clone.profile) 53 54 55 def test_restore_false(profile): 56 # make a clone of this profile with restore=False 57 clone = Profile.clone(profile.profile, restore=False) 58 try: 59 clone.cleanup() 60 61 # clone should still be around on the filesystem 62 assert os.path.exists(clone.profile) 63 finally: 64 mozfile.remove(clone.profile) 65 66 67 def test_cleanup_on_garbage_collected(profile): 68 clone = Profile.clone(profile.profile) 69 profile_dir = clone.profile 70 assert os.path.exists(profile_dir) 71 del clone 72 73 # clone should be deleted 74 assert not os.path.exists(profile_dir) 75 76 77 if __name__ == "__main__": 78 mozunit.main()