예제 #1
0
 def setUp(self):
     """Call before every test case."""
     self.d = tempfile.mkdtemp(prefix="f2b-temp")
     self.c = ConfigReader(basedir=self.d)
예제 #2
0
class ConfigReaderTest(unittest.TestCase):
    def setUp(self):
        """Call before every test case."""
        self.d = tempfile.mkdtemp(prefix="f2b-temp")
        self.c = ConfigReader(basedir=self.d)

    def tearDown(self):
        """Call after every test case."""
        shutil.rmtree(self.d)

    def _write(self, fname, value):
        # verify if we don't need to create .d directory
        if os.path.sep in fname:
            d = os.path.dirname(fname)
            d_ = os.path.join(self.d, d)
            if not os.path.exists(d_):
                os.makedirs(d_)
        open("%s/%s" % (self.d, fname), "w").write("""
[section]
option = %s
""" % value)

    def _remove(self, fname):
        os.unlink("%s/%s" % (self.d, fname))
        self.assertTrue(self.c.read('c'))  # we still should have some

    def _getoption(self, f='c'):
        self.assertTrue(self.c.read(f))  # we got some now
        return self.c.getOptions('section', [("int", 'option')])['option']

    def testInaccessibleFile(self):
        f = os.path.join(self.d, "d.conf")  # inaccessible file
        self._write('d.conf', 0)
        self.assertEqual(self._getoption('d'), 0)
        os.chmod(f, 0)
        # fragile test and known to fail e.g. under Cygwin where permissions
        # seems to be not enforced, thus condition
        if not os.access(f, os.R_OK):
            self.assertFalse(
                self.c.read('d'))  # should not be readable BUT present
        else:
            # SkipTest introduced only in 2.7 thus can't yet use generally
            # raise unittest.SkipTest("Skipping on %s -- access rights are not enforced" % platform)
            pass

    def testOptionalDotDDir(self):
        self.assertFalse(self.c.read('c'))  # nothing is there yet
        self._write("c.conf", "1")
        self.assertEqual(self._getoption(), 1)
        self._write("c.conf", "2")  # overwrite
        self.assertEqual(self._getoption(), 2)
        self._write("c.d/98.conf", "998")  # add 1st override in .d/
        self.assertEqual(self._getoption(), 998)
        self._write("c.d/90.conf",
                    "990")  # add previously sorted override in .d/
        self.assertEqual(self._getoption(), 998)  #  should stay the same
        self._write(
            "c.d/99.conf", "999"
        )  # now override in a way without sorting we possibly get a failure
        self.assertEqual(self._getoption(), 999)
        self._remove("c.d/99.conf")
        self.assertEqual(self._getoption(), 998)
        self._remove("c.d/98.conf")
        self.assertEqual(self._getoption(), 990)
        self._remove("c.d/90.conf")
        self.assertEqual(self._getoption(), 2)
        self._write("c.local", "3")  # add override in .local
        self.assertEqual(self._getoption(), 3)
        self._write("c.d/5.local", "9")  # add override in c.d/*.local
        self.assertEqual(self._getoption(), 9)
        self._remove("c.conf")  #  we allow to stay without .conf
        self.assertEqual(self._getoption(), 9)
        self._write("c.conf", "1")
        self._remove("c.d/5.local")
        self._remove("c.local")
        self.assertEqual(self._getoption(), 1)
예제 #3
0
	def setUp(self):
		"""Call before every test case."""
		self.d = tempfile.mkdtemp(prefix="f2b-temp")
		self.c = ConfigReader(basedir=self.d)
예제 #4
0
class ConfigReaderTest(unittest.TestCase):

	def setUp(self):
		"""Call before every test case."""
		self.d = tempfile.mkdtemp(prefix="f2b-temp")
		self.c = ConfigReader(basedir=self.d)

	def tearDown(self):
		"""Call after every test case."""
		shutil.rmtree(self.d)

	def _write(self, fname, value):
		# verify if we don't need to create .d directory
		if os.path.sep in fname:
			d = os.path.dirname(fname)
			d_ = os.path.join(self.d, d)
			if not os.path.exists(d_):
				os.makedirs(d_)
		open("%s/%s" % (self.d, fname), "w").write("""
[section]
option = %s
""" % value)

	def _remove(self, fname):
		os.unlink("%s/%s" % (self.d, fname))
		self.assertTrue(self.c.read('c'))	# we still should have some


	def _getoption(self, f='c'):
		self.assertTrue(self.c.read(f))	# we got some now
		return self.c.getOptions('section', [("int", 'option')])['option']


	def testInaccessibleFile(self):
		f = os.path.join(self.d, "d.conf")  # inaccessible file
		self._write('d.conf', 0)
		self.assertEqual(self._getoption('d'), 0)
		os.chmod(f, 0)
		self.assertFalse(self.c.read('d'))	# should not be readable BUT present


	def testOptionalDotDDir(self):
		self.assertFalse(self.c.read('c'))	# nothing is there yet
		self._write("c.conf", "1")
		self.assertEqual(self._getoption(), 1)
		self._write("c.conf", "2")		# overwrite
		self.assertEqual(self._getoption(), 2)
		self._write("c.local", "3")		# add override in .local
		self.assertEqual(self._getoption(), 3)
		self._write("c.d/98.conf", "998") # add 1st override in .d/
		self.assertEqual(self._getoption(), 998)
		self._write("c.d/90.conf", "990") # add previously sorted override in .d/
		self.assertEqual(self._getoption(), 998) #  should stay the same
		self._write("c.d/99.conf", "999") # now override in a way without sorting we possibly get a failure
		self.assertEqual(self._getoption(), 999)
		self._remove("c.d/99.conf")
		self.assertEqual(self._getoption(), 998)
		self._remove("c.d/98.conf")
		self.assertEqual(self._getoption(), 990)
		self._remove("c.d/90.conf")
		self.assertEqual(self._getoption(), 3)
		self._remove("c.conf")			#  we allow to stay without .conf
		self.assertEqual(self._getoption(), 3)
		self._write("c.conf", "1")
		self._remove("c.local")
		self.assertEqual(self._getoption(), 1)
예제 #5
0
class ConfigReaderTest(unittest.TestCase):
    def setUp(self):
        """Call before every test case."""
        self.d = tempfile.mkdtemp(prefix="f2b-temp")
        self.c = ConfigReader(basedir=self.d)

    def tearDown(self):
        """Call after every test case."""
        shutil.rmtree(self.d)

    def _write(self, fname, value):
        # verify if we don't need to create .d directory
        if os.path.sep in fname:
            d = os.path.dirname(fname)
            d_ = os.path.join(self.d, d)
            if not os.path.exists(d_):
                os.makedirs(d_)
        open("%s/%s" % (self.d, fname), "w").write("""
[section]
option = %s
""" % value)

    def _remove(self, fname):
        os.unlink("%s/%s" % (self.d, fname))
        self.assertTrue(self.c.read('c'))  # we still should have some

    def _getoption(self, f='c'):
        self.assertTrue(self.c.read(f))  # we got some now
        return self.c.getOptions('section', [("int", 'option')])['option']

    def testInaccessibleFile(self):
        f = os.path.join(self.d, "d.conf")  # inaccessible file
        self._write('d.conf', 0)
        self.assertEqual(self._getoption('d'), 0)
        os.chmod(f, 0)
        self.assertFalse(
            self.c.read('d'))  # should not be readable BUT present

    def testOptionalDotDDir(self):
        self.assertFalse(self.c.read('c'))  # nothing is there yet
        self._write("c.conf", "1")
        self.assertEqual(self._getoption(), 1)
        self._write("c.conf", "2")  # overwrite
        self.assertEqual(self._getoption(), 2)
        self._write("c.local", "3")  # add override in .local
        self.assertEqual(self._getoption(), 3)
        self._write("c.d/98.conf", "998")  # add 1st override in .d/
        self.assertEqual(self._getoption(), 998)
        self._write("c.d/90.conf",
                    "990")  # add previously sorted override in .d/
        self.assertEqual(self._getoption(), 998)  #  should stay the same
        self._write(
            "c.d/99.conf", "999"
        )  # now override in a way without sorting we possibly get a failure
        self.assertEqual(self._getoption(), 999)
        self._remove("c.d/99.conf")
        self.assertEqual(self._getoption(), 998)
        self._remove("c.d/98.conf")
        self.assertEqual(self._getoption(), 990)
        self._remove("c.d/90.conf")
        self.assertEqual(self._getoption(), 3)
        self._remove("c.conf")  #  we allow to stay without .conf
        self.assertEqual(self._getoption(), 3)
        self._write("c.conf", "1")
        self._remove("c.local")
        self.assertEqual(self._getoption(), 1)
예제 #6
0
class ConfigReaderTest(unittest.TestCase):

	def setUp(self):
		"""Call before every test case."""
		self.d = tempfile.mkdtemp(prefix="f2b-temp")
		self.c = ConfigReader(basedir=self.d)

	def tearDown(self):
		"""Call after every test case."""
		shutil.rmtree(self.d)

	def _write(self, fname, value):
		# verify if we don't need to create .d directory
		if os.path.sep in fname:
			d = os.path.dirname(fname)
			d_ = os.path.join(self.d, d)
			if not os.path.exists(d_):
				os.makedirs(d_)
		open("%s/%s" % (self.d, fname), "w").write("""
[section]
option = %s
""" % value)

	def _remove(self, fname):
		os.unlink("%s/%s" % (self.d, fname))
		self.assertTrue(self.c.read('c'))	# we still should have some


	def _getoption(self, f='c'):
		self.assertTrue(self.c.read(f))	# we got some now
		return self.c.getOptions('section', [("int", 'option')])['option']


	def testInaccessibleFile(self):
		f = os.path.join(self.d, "d.conf")  # inaccessible file
		self._write('d.conf', 0)
		self.assertEqual(self._getoption('d'), 0)
		os.chmod(f, 0)
		# fragile test and known to fail e.g. under Cygwin where permissions
		# seems to be not enforced, thus condition
		if not os.access(f, os.R_OK):
			self.assertFalse(self.c.read('d'))	# should not be readable BUT present
		else:
			# SkipTest introduced only in 2.7 thus can't yet use generally
			# raise unittest.SkipTest("Skipping on %s -- access rights are not enforced" % platform)
			pass


	def testOptionalDotDDir(self):
		self.assertFalse(self.c.read('c'))	# nothing is there yet
		self._write("c.conf", "1")
		self.assertEqual(self._getoption(), 1)
		self._write("c.conf", "2")		# overwrite
		self.assertEqual(self._getoption(), 2)
		self._write("c.d/98.conf", "998") # add 1st override in .d/
		self.assertEqual(self._getoption(), 998)
		self._write("c.d/90.conf", "990") # add previously sorted override in .d/
		self.assertEqual(self._getoption(), 998) #  should stay the same
		self._write("c.d/99.conf", "999") # now override in a way without sorting we possibly get a failure
		self.assertEqual(self._getoption(), 999)
		self._remove("c.d/99.conf")
		self.assertEqual(self._getoption(), 998)
		self._remove("c.d/98.conf")
		self.assertEqual(self._getoption(), 990)
		self._remove("c.d/90.conf")
		self.assertEqual(self._getoption(), 2)
		self._write("c.local", "3")		# add override in .local
		self.assertEqual(self._getoption(), 3)
		self._write("c.d/5.local", "9")		# add override in c.d/*.local
		self.assertEqual(self._getoption(), 9)
		self._remove("c.conf")			#  we allow to stay without .conf
		self.assertEqual(self._getoption(), 9)
		self._write("c.conf", "1")
		self._remove("c.d/5.local")
		self._remove("c.local")
		self.assertEqual(self._getoption(), 1)