Ejemplo n.º 1
0
    def test_returns_false_if_load_fails_windows(self):
        file_path = self.tempdir.write(fake.config_file(), b'')
        config = ConfigReader(file_path, case_sensitive=True)
        config.set('states', '35', section='country')
        config.set('counties', 'None', section='country')

        environment = os.environ.copy()
        user = '******'
        environment['USER'] = user
        environment['COUNTER'] = 'never'

        config.to_env(environment)
        config.load_env(environment)

        items = config.get_items('main')
        with self.subTest(0):
            self.assertEqual(items['APPDATA'], os.environ['APPDATA'])

        with self.subTest(1):
            self.assertEqual(items['USER'], user)

        with self.subTest(2):
            self.assertEqual(items['ALLUSERSPROFILE'],
                             os.environ['ALLUSERSPROFILE'])

        with self.subTest(3):
            self.assertRaises(KeyError, lambda: items['home'])

        with self.subTest(4):
            self.assertEqual(items['COUNTER'], 'never')
Ejemplo n.º 2
0
    def test_returns_false_if_items_not_match(self):
        file_path_1 = self.tempdir.write(fake.config_file(), b'')
        file_path_2 = self.tempdir.write(fake.config_file(), b'')
        file_ = open(file_path_1, 'w+')

        config = ConfigReader(filename=file_path_2, file_object=file_)
        config.remove_section('main')
        config.set('start', 'True', section='defaults')
        config.set('value', '45', section='defaults')
        items = config.get_items('main')

        with self.subTest(0):
            self.assertIsNone(items)

        items = config.get_items('defaults')

        with self.subTest(1):
            self.assertIsInstance(items, OrderedDict)

        with self.subTest(2):
            expected = OrderedDict([('start', True), ('value', 45)])
            self.assertEqual(items, expected)
        config.close()

        with self.subTest('Test returns false if file object still writable'):
            self.assertRaises(ValueError, file_.write, '')
Ejemplo n.º 3
0
    def test_returns_false_if_changes_not_written_to_file(self):
        file_path = fake.config_file(self.tempdir.path)
        config = ConfigReader(file_path)
        config.set('name', 'first')

        with ConfigReader(file_path) as d, self.subTest(0):
            with self.assertRaises(MissingOptionError):
                d.get('name')

        config.set('name', 'last', commit=True)
        config.close()

        d = ConfigReader(file_path)
        with self.subTest(1):
            self.assertEqual(d.get('name'), 'last')
        d.close()
Ejemplo n.º 4
0
    def test_returns_false_if_error_not_raised(self):
        config = ConfigReader()

        with self.subTest(0):
            self.assertRaises(
                SectionNameNotAllowed,
                lambda: config.set('start', 'True', section='default'))

        with self.subTest(1):
            self.assertRaises(
                SectionNameNotAllowed,
                lambda: config.set('start', 'True', section='deFault'))

        with self.subTest(2):
            self.assertRaises(
                SectionNameNotAllowed,
                lambda: config.set('start', 'True', section='DEFAULT'))
Ejemplo n.º 5
0
    def test_returns_false_if_contents_not_updated(self):
        f = open(self.file_path, 'w+')
        config = ConfigReader(file_object=f)
        config.set('name', 'first')

        with self.subTest(0):
            with open(self.file_path) as f2:
                result = f2.read()
            self.assertNotEqual(result, '')

        config.save()

        with self.subTest(1):
            with open(self.file_path) as f3:
                result = f3.read()
            self.assertNotEqual(result, '')
        f.close()
Ejemplo n.º 6
0
    def test_returns_false_if_section_prepend_failed(self):
        file_path_1 = self._get_config_file()
        file_path_2 = self._get_config_file()
        f = open(file_path_1, 'w+')
        config = ConfigReader(file_path_2, f)
        config.set('country', 'Kenya')
        config.set('continent', 'Africa')
        config.set('state', 'None')
        config.set('count', '0', section='first')
        config.to_env()
        f.close()

        with self.subTest(0):
            self.assertEqual(os.environ.get('MAIN_COUNTRY'), 'Kenya')

        with self.subTest(1):
            self.assertEqual(os.environ.get('MAIN_CONTINENT'), 'Africa')

        with self.subTest(2):
            self.assertEqual(os.environ.get('MAIN_STATE'), 'None')

        with self.subTest(3):
            self.assertEqual(os.environ.get('FIRST_COUNT'), '0')