view profilemanager/tests/test.py @ 76:e76a122d8622

more verbose assertion
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 07 May 2010 17:22:38 -0700
parents b6bb59b79525
children 145e111903d2
line wrap: on
line source

#!/usr/bin/env python

import os
import time
from profilemanager import ProfileManager

# Get the path to the test profiles.ini file:
from pkg_resources import resource_filename
profiles_dir = os.path.join('tests', 'profiles')
path = os.path.join(profiles_dir, 'profiles.ini')
profiles = resource_filename('profilemanager', path)
profiles_dir = resource_filename('profilemanager', profiles_dir)
assert os.path.exists(profiles), '%s does not exist' % profiles
assert os.path.exists(profiles_dir), '%s does not exist' % profiles_dir

# Instatiate a ProfileManager:
manager = ProfileManager(profiles)

# Remove any profiles that didn't get cleaned up:
profiles_dict = manager.profiles_dict()
for profile in profiles_dict:
  manager.remove(profile)
assert os.listdir(profiles_dir) == ['profiles.ini'], 'profiles_dir isnt empty except profiles.ini'
assert file(profiles).read().strip() == '[General]', 'profiles.ini isnt nearly empty'
    
# Create a new profile:
profiledir = manager.new('testprofile')
assert profiledir.endswith('.testprofile'), 'profiledir should end with testprofile, instead it is %s' % profiledir
assert manager.list() == ['testprofile']

# Clone the profile:
manager.clone('testprofile', 'cloneprofile')
assert sorted(manager.list()) == ['cloneprofile', 'testprofile']
manager.remove('cloneprofile')

# Backup the profile
manager.backup('testprofile')
backups = manager.backups('testprofile')
assert len(backups) == 1, "Expected one backup, instead got %d" % len(backups)
assert backups[0][0].startswith('testprofile')
manager.restore('testprofile')
manager.restore('testprofile', delete=True)
backups = manager.backups('testprofile')
assert not backups
# TODO: test the backup with a populated profile

# Test dated backups
manager.backup('testprofile')
old_backups = manager.backups('testprofile')
assert len(old_backups) == 1, "Expected one backup, instead got %d" % len(old_backups)
old_time = int(time.time())
time.sleep(3)
manager.backup('testprofile')
new_backups = manager.backups('testprofile')
assert len(new_backups) == 2, "Expected two backups, instead got %d" % len(new_backups)
restore_time = old_time + 1
restored = manager.restore('testprofile', date=restore_time)
assert restored == old_backups[0], "Tried to restore from %d [%s], but got [%s]" % (restore_time, old_backups[0], restored)
# TODO: test with alternate forms of date

# Restore the newest profile
restored = manager.restore('testprofile')
assert restored != old_backups[0], "Tried to restore the newest but got the old one [%s]" % old_backups[0]
manager.restore('testprofile', delete=True)
manager.restore('testprofile', delete=True)
assert not manager.backups() 
assert manager.list() == ['testprofile']

# Cleanup:
manager.remove('testprofile')
assert manager.list() == []
f = file(profiles, 'w')
print >> f, '[General]'