Updated german (de) translation (Guido Schimmels).
[rox-lib.git] / tests / python / testchoices.py
blobaed57a5d9e2a3325dcf5029f59b3f93e504dd9f2
1 #!/usr/bin/env python2.2
2 import unittest
3 import os, sys, shutil
4 sys.path.append('../../python')
6 from rox import basedir, choices
8 class Null:
9 def write(self, data):
10 pass
11 null = Null()
13 class TestChoices(unittest.TestCase):
14 def setUp(self):
15 os.environ['CHOICESPATH'] = '/tmp/choices:/tmp/choices2'
16 os.environ['XDG_CONFIG_HOME'] = '/tmp/config'
18 if os.path.isdir('/tmp/choices'):
19 shutil.rmtree('/tmp/choices')
21 if os.path.isdir('/tmp/choices2'):
22 shutil.rmtree('/tmp/choices2')
24 if os.path.isdir('/tmp/config'):
25 shutil.rmtree('/tmp/config')
27 reload(choices)
28 reload(basedir)
30 def testDefaults(self):
31 del os.environ['CHOICESPATH']
32 reload(choices)
34 self.assertEquals(
35 [os.path.expanduser('~/Choices'),
36 '/usr/local/share/Choices',
37 '/usr/share/Choices'],
38 choices.paths)
40 def testLoadNothing(self):
41 self.assertEquals('/tmp/choices', choices.paths[0])
42 assert not os.path.exists('/tmp/choices')
44 self.assertEquals(choices.load('Edit', 'Options'), None)
46 def testLoad(self):
47 os.mkdir('/tmp/choices')
48 os.mkdir('/tmp/choices/Edit')
49 self.assertEquals(choices.load('Edit', 'Options'), None)
51 file('/tmp/choices/Edit/Options', 'w').close()
52 self.assertEquals(choices.load('Edit', 'Options'),
53 '/tmp/choices/Edit/Options')
55 os.mkdir('/tmp/choices2')
56 os.mkdir('/tmp/choices2/Edit')
57 self.assertEquals(choices.load('Edit', 'Options'),
58 '/tmp/choices/Edit/Options')
60 file('/tmp/choices2/Edit/Options', 'w').close()
61 self.assertEquals(choices.load('Edit', 'Options'),
62 '/tmp/choices/Edit/Options')
64 os.unlink('/tmp/choices/Edit/Options')
65 self.assertEquals(choices.load('Edit', 'Options'),
66 '/tmp/choices2/Edit/Options')
68 def testMigrateNothing(self):
69 choices.migrate('Edit', 'rox.sourceforge.net')
70 choices.load('Draw', 'Options')
71 try:
72 choices.load('Edit', 'Options')
73 raise Exception('Expected exception!')
74 except AssertionError:
75 pass
76 assert not os.path.exists('/tmp/config')
78 def testMigrateNormal(self):
79 save = choices.save('Edit', 'Options')
80 self.assertEquals(save, '/tmp/choices/Edit/Options')
81 file(save, 'w').close()
82 choices.migrate('Edit', 'rox.sourceforge.net')
84 assert os.path.isfile(
85 '/tmp/config/rox.sourceforge.net/Edit/Options')
86 assert os.path.islink('/tmp/choices/Edit')
88 assert os.path.isfile('/tmp/choices/Edit/Options')
90 def testDoubleMigrate(self):
91 choices.migrate('Edit', 'rox.sourceforge.net')
92 try:
93 choices.migrate('Edit', 'rox.sourceforge.net')
94 raise Exception('Expected exception!')
95 except AssertionError:
96 pass
98 def testFailedMigration(self):
99 save = choices.save('Edit', 'Options')
100 file(save, 'w').close()
101 save2 = basedir.save_config_path('rox.sourceforge.net', 'Edit')
102 file(os.path.join(save2, 'Options'), 'w').close()
103 old, sys.stderr = sys.stderr, null
104 try:
105 choices.migrate('Edit', 'rox.sourceforge.net')
106 finally:
107 sys.stderr = old
108 assert os.path.isdir('/tmp/choices/Edit')
109 assert os.path.isdir('/tmp/config/rox.sourceforge.net/Edit')
111 suite = unittest.makeSuite(TestChoices)
112 if __name__ == '__main__':
113 sys.argv.append('-v')
114 unittest.main()