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()
46 def tearDownClass(cls):
52 Sets up common facilities for testing this TestCase:
53 - custom PATH and HOME environmental variables
54 - creates a temporal folder to which those point.
55 It saves the old path and home vars so they can be restored later.
57 cls.old_path = os.environ['PATH']
58 cls.old_home = os.environ['HOME']
59 cls.tempdir = tempfile.mkdtemp(prefix="leap_tests-")
60 cls.home = cls.tempdir
61 bin_tdir = os.path.join(
64 os.environ["PATH"] = bin_tdir
65 os.environ["HOME"] = cls.tempdir
70 Cleanup common facilities used for testing this TestCase:
71 - restores the default PATH and HOME variables
72 - removes the temporal folder
74 os.environ["PATH"] = cls.old_path
75 os.environ["HOME"] = cls.old_home
76 # safety check! please do not wipe my home...
77 # XXX needs to adapt to non-linuces
79 cls.tempdir.startswith('/tmp/leap_tests-') or
80 cls.tempdir.startswith('/var/folder'),
81 "beware! tried to remove a dir which does not "
82 "live in temporal folder!")
83 shutil.rmtree(cls.tempdir)
85 # you have to override these methods
86 # this way we ensure we did not put anything
87 # here that you can forget to call.
91 raise NotImplementedError("abstract base class")
95 raise NotImplementedError("abstract base class")
101 def _missing_test_for_plat(self, do_raise=False):
103 Raises NotImplementedError for this platform
106 :param do_raise: flag to actually raise exception
110 raise NotImplementedError(
111 "This test is not implemented "
112 "for the running platform: %s" %
115 def get_tempfile(self, filename):
117 Returns the path of a given filename
118 prepending the temporal dir associated with this
121 :param filename: the filename
124 return os.path.join(self.tempdir, filename)
126 def touch(self, filepath):
128 Touches a filepath, creating folders along
131 :param filepath: path to be touched
134 folder, filename = os.path.split(filepath)
135 if not os.path.isdir(folder):
137 self.assertTrue(os.path.isdir(folder))
138 with open(filepath, 'w') as fp:
140 self.assertTrue(os.path.isfile(filepath))
142 def chmod600(self, filepath):
146 :param filepath: filepath to be chmodded
149 check_and_fix_urw_only(filepath)