1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2013 LEAP
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 Common testing facilities
26 import unittest2 as unittest
30 from leap.common.check import leap_assert
31 from leap.common.files import mkdir_p, check_and_fix_urw_only
34 class BaseLeapTest(unittest.TestCase):
38 __name__ = "leap_test"
39 _system = platform.system()
44 sets up common facilities
45 for testing this TestCase
47 cls.old_path = os.environ['PATH']
48 cls.old_home = os.environ['HOME']
49 cls.tempdir = tempfile.mkdtemp(prefix="leap_tests-")
50 cls.home = cls.tempdir
51 bin_tdir = os.path.join(
54 os.environ["PATH"] = bin_tdir
55 os.environ["HOME"] = cls.tempdir
58 def tearDownClass(cls):
60 cleanup common facilities used
61 for testing this TestCase
63 os.environ["PATH"] = cls.old_path
64 os.environ["HOME"] = cls.old_home
65 # safety check! please do not wipe my home...
66 # XXX needs to adapt to non-linuces
68 cls.tempdir.startswith('/tmp/leap_tests-'),
69 "beware! tried to remove a dir which does not "
70 "live in temporal folder!")
71 shutil.rmtree(cls.tempdir)
73 # you have to override these methods
74 # this way we ensure we did not put anything
75 # here that you can forget to call.
79 raise NotImplementedError("abstract base class")
83 raise NotImplementedError("abstract base class")
89 def _missing_test_for_plat(self, do_raise=False):
91 Raises NotImplementedError for this platform
94 @param do_raise: flag to actually raise exception
98 raise NotImplementedError(
99 "This test is not implemented "
100 "for the running platform: %s" %
103 def get_tempfile(self, filename):
105 Returns the path of a given filename
106 prepending the temporal dir associated with this
109 @param filename: the filename
112 return os.path.join(self.tempdir, filename)
114 def touch(self, filepath):
116 Touches a filepath, creating folders along
119 @param filepath: path to be touched
122 folder, filename = os.path.split(filepath)
123 if not os.path.isdir(folder):
125 self.assertTrue(os.path.isdir(folder))
126 with open(filepath, 'w') as fp:
128 self.assertTrue(os.path.isfile(filepath))
130 def chmod600(self, filepath):
134 @param filepath: filepath to be chmodded
137 check_and_fix_urw_only(filepath)