Some unit tests appended the local ROX-Lib's location to PYTHONPATH instead
[rox-lib/lack.git] / tests / python / testchoices.py
blob6cbd878c81093951c3c8af1833b26ae64b59be09
1 #!/usr/bin/env python2.2
2 import unittest
3 import os, sys, shutil
4 from os.path import dirname, abspath, join
5 rox_lib = dirname(dirname(dirname(abspath(sys.argv[0]))))
6 sys.path.insert(0, join(rox_lib, 'python'))
8 from rox import basedir, choices
10 class Null:
11 def write(self, data):
12 pass
13 null = Null()
15 class TestChoices(unittest.TestCase):
16 def setUp(self):
17 os.environ['CHOICESPATH'] = '/tmp/choices:/tmp/choices2'
18 os.environ['XDG_CONFIG_HOME'] = '/tmp/config'
20 if os.path.isdir('/tmp/choices'):
21 shutil.rmtree('/tmp/choices')
23 if os.path.isdir('/tmp/choices2'):
24 shutil.rmtree('/tmp/choices2')
26 if os.path.isdir('/tmp/config'):
27 shutil.rmtree('/tmp/config')
29 reload(choices)
30 reload(basedir)
32 def testDefaults(self):
33 del os.environ['CHOICESPATH']
34 reload(choices)
36 self.assertEquals(
37 [os.path.expanduser('~/Choices'),
38 '/usr/local/share/Choices',
39 '/usr/share/Choices'],
40 choices.paths)
42 def testLoadNothing(self):
43 self.assertEquals('/tmp/choices', choices.paths[0])
44 assert not os.path.exists('/tmp/choices')
46 self.assertEquals(choices.load('Edit', 'Options'), None)
48 def testLoad(self):
49 os.mkdir('/tmp/choices')
50 os.mkdir('/tmp/choices/Edit')
51 self.assertEquals(choices.load('Edit', 'Options'), None)
53 file('/tmp/choices/Edit/Options', 'w').close()
54 self.assertEquals(choices.load('Edit', 'Options'),
55 '/tmp/choices/Edit/Options')
57 os.mkdir('/tmp/choices2')
58 os.mkdir('/tmp/choices2/Edit')
59 self.assertEquals(choices.load('Edit', 'Options'),
60 '/tmp/choices/Edit/Options')
62 file('/tmp/choices2/Edit/Options', 'w').close()
63 self.assertEquals(choices.load('Edit', 'Options'),
64 '/tmp/choices/Edit/Options')
66 os.unlink('/tmp/choices/Edit/Options')
67 self.assertEquals(choices.load('Edit', 'Options'),
68 '/tmp/choices2/Edit/Options')
70 def testMigrateNothing(self):
71 choices.migrate('Edit', 'rox.sourceforge.net')
72 choices.load('Draw', 'Options')
73 try:
74 choices.load('Edit', 'Options')
75 raise Exception('Expected exception!')
76 except AssertionError:
77 pass
78 assert not os.path.exists('/tmp/config')
80 def testMigrateNormal(self):
81 save = choices.save('Edit', 'Options')
82 self.assertEquals(save, '/tmp/choices/Edit/Options')
83 file(save, 'w').close()
84 choices.migrate('Edit', 'rox.sourceforge.net')
86 assert os.path.isfile(
87 '/tmp/config/rox.sourceforge.net/Edit/Options')
88 assert os.path.islink('/tmp/choices/Edit')
90 assert os.path.isfile('/tmp/choices/Edit/Options')
92 def testDoubleMigrate(self):
93 choices.migrate('Edit', 'rox.sourceforge.net')
94 try:
95 choices.migrate('Edit', 'rox.sourceforge.net')
96 raise Exception('Expected exception!')
97 except AssertionError:
98 pass
100 def testFailedMigration(self):
101 save = choices.save('Edit', 'Options')
102 file(save, 'w').close()
103 save2 = basedir.save_config_path('rox.sourceforge.net', 'Edit')
104 file(os.path.join(save2, 'Options'), 'w').close()
105 old, sys.stderr = sys.stderr, null
106 try:
107 choices.migrate('Edit', 'rox.sourceforge.net')
108 finally:
109 sys.stderr = old
110 assert os.path.isdir('/tmp/choices/Edit')
111 assert os.path.isdir('/tmp/config/rox.sourceforge.net/Edit')
113 suite = unittest.makeSuite(TestChoices)
114 if __name__ == '__main__':
115 sys.argv.append('-v')
116 unittest.main()