[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/githubdelegate/GitPython/master/git/test/test_config.py [Back]  [Original]

# test_config.py
# Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
#
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php

from git.test.lib import (
    TestCase,
    fixture_path,
    assert_equal,
)
from gitdb.test.lib import with_rw_directory
from git import (
    GitConfigParser
)
from git.compat import (
    string_types,
)
import io
import os
from git.config import cp


class TestBase(TestCase):

    def _to_memcache(self, file_path):
        fp = open(file_path, "rb")
        sio = io.BytesIO(fp.read())
        sio.name = file_path
        return sio

    def test_read_write(self):
        # writer must create the exact same file as the one read before
        for filename in ("git_config", "git_config_global"):
            file_obj = self._to_memcache(fixture_path(filename))
            w_config = GitConfigParser(file_obj, read_only=False)
            w_config.read()                 # enforce reading
            assert w_config._sections
            w_config.write()                # enforce writing

            # we stripped lines when reading, so the results differ
            assert file_obj.getvalue()
            self.assertEqual(file_obj.getvalue(), self._to_memcache(fixture_path(filename)).getvalue())

            # creating an additional config writer must fail due to exclusive access
            self.failUnlessRaises(IOError, GitConfigParser, file_obj, read_only=False)

            # should still have a lock and be able to make changes
            assert w_config._lock._has_lock()

            # changes should be written right away
            sname = "my_section"
            
            val = "myvalue"
            w_config.add_section(sname)
            assert w_config.has_section(sname)
            w_config.set(sname, oname, val)
            assert w_config.has_option(sname, oname)
            assert w_config.get(sname, oname) == val

            sname_new = "new_section"
            
            ival = 10
            w_config.set_value(sname_new, oname_new, ival)
            assert w_config.get_value(sname_new, oname_new) == ival

            file_obj.seek(0)
            r_config = GitConfigParser(file_obj, read_only=True)
            assert r_config.has_section(sname)
            assert r_config.has_option(sname, oname)
            assert r_config.get(sname, oname) == val
            w_config.release()
        # END for each filename

    def test_multi_line_config(self):
        file_obj = self._to_memcache(fixture_path("git_config_with_comments"))
        config = GitConfigParser(file_obj, read_only=False)
        ev = "ruby -e '\n"
        ev += "		system %(git), %(merge-file), %(--marker-size=%L), %(%A), %(%O), %(%B)\n"
        ev += "		b = File.read(%(%A))\n"
        ev += "		b.sub!(/^ (\\d+). do\\n=+\\nActiveRecord::Schema\\."
        ev += "define.:version => (\\d+). do\\n>+ .*/) do\n"
        ev += "		  %(ActiveRecord::Schema.define(:version => #{[$1, $2].max}) do)\n"
        ev += "		end\n"
        ev += "		File.open(%(%A), %(w)) {|f| f.write(b)}\n"
        ev += "		exit 1 if b.include?(%(

Web Proxy Viewer  |  New URL  |  Original Page