]> arthur.barton.de Git - bup.git/commitdiff
git: teach git_config_get() to read from a file
authorJohannes Berg <johannes@sipsolutions.net>
Wed, 18 Dec 2019 21:03:58 +0000 (22:03 +0100)
committerRob Browning <rlb@defaultvalue.org>
Sun, 13 Jun 2021 16:32:17 +0000 (11:32 -0500)
We want to use git_config_get() to have a bup config file
in the future, so teach git_config_get() to read from a
file and add some tests for it.

Use this also to test the opttype conversions from the
previous patch.

Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
Reviewed-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
lib/bup/git.py
test/int/sample.conf [new file with mode: 0644]
test/int/test_git.py

index 5144e70dd8bf8c3993d5bcb590d7294614f3fc74..ec84dd7b0e071aca2af8888f80a08416d3fa9d38 100644 (file)
@@ -66,8 +66,11 @@ def _git_exo(cmd, **kwargs):
         raise GitError('%r returned %d' % (cmd, proc.returncode))
     return result
 
-def git_config_get(option, repo_dir=None, opttype=None):
+def git_config_get(option, repo_dir=None, opttype=None, cfg_file=None):
+    assert not (repo_dir and cfg_file), "repo_dir and cfg_file cannot both be used"
     cmd = [b'git', b'config', b'--null']
+    if cfg_file:
+        cmd.extend([b'--file', cfg_file])
     if opttype == 'int':
         cmd.extend([b'--int'])
     elif opttype == 'bool':
diff --git a/test/int/sample.conf b/test/int/sample.conf
new file mode 100644 (file)
index 0000000..d4ee0d5
--- /dev/null
@@ -0,0 +1,13 @@
+[bup]
+foo = bar
+bup = is great
+;comments=are ignored
+#and=this kind too
+end = end ; comment at the end
+istrue1 = 1
+istrue2 = 2
+istrue3 = true
+isfalse1 = false
+isfalse2 = 0
+isbad = ok
+hex = 0x777
index 0feb4108170dde072eac33298672c5ed97e652ca..0b114f1644bb4d5f73ba2c0660e25319a6a88be9 100644 (file)
@@ -3,6 +3,7 @@ from __future__ import absolute_import, print_function
 import sys
 from binascii import hexlify, unhexlify
 from subprocess import check_call
+from functools import partial
 import struct, os, time
 import pytest
 
@@ -503,3 +504,31 @@ def test_midx_close(tmpdir):
             continue
         # check that we don't have it open anymore
         WVPASSEQ(False, b'deleted' in fn)
+
+def test_config():
+    cfg_file = os.path.join(os.path.dirname(__file__), 'sample.conf')
+    no_such_file = os.path.join(os.path.dirname(__file__), 'nosuch.conf')
+    git_config_get = partial(git.git_config_get, cfg_file=cfg_file)
+    WVPASSEQ(git_config_get(b'bup.foo'), b'bar')
+    WVPASSEQ(git_config_get(b'bup.bup'), b'is great')
+    WVPASSEQ(git_config_get(b'bup.end'), b'end')
+    WVPASSEQ(git_config_get(b'bup.comments'), None)
+    WVPASSEQ(git_config_get(b'bup.;comments'), None)
+    WVPASSEQ(git_config_get(b'bup.and'), None)
+    WVPASSEQ(git_config_get(b'bup.#and'), None)
+
+    WVPASSEQ(git.git_config_get(b'bup.foo', cfg_file=no_such_file), None)
+
+    WVEXCEPT(git.GitError, git_config_get, b'bup.isbad', opttype='bool')
+    WVEXCEPT(git.GitError, git_config_get, b'bup.isbad', opttype='int')
+    WVPASSEQ(git_config_get(b'bup.isbad'), b'ok')
+    WVPASSEQ(True, git_config_get(b'bup.istrue1', opttype='bool'))
+    WVPASSEQ(True, git_config_get(b'bup.istrue2', opttype='bool'))
+    WVPASSEQ(True, git_config_get(b'bup.istrue3', opttype='bool'))
+    WVPASSEQ(False, git_config_get(b'bup.isfalse1', opttype='bool'))
+    WVPASSEQ(False, git_config_get(b'bup.isfalse2', opttype='bool'))
+    WVPASSEQ(None, git_config_get(b'bup.nosuchkey', opttype='bool'))
+    WVPASSEQ(1, git_config_get(b'bup.istrue1', opttype='int'))
+    WVPASSEQ(2, git_config_get(b'bup.istrue2', opttype='int'))
+    WVPASSEQ(0, git_config_get(b'bup.isfalse2', opttype='int'))
+    WVPASSEQ(0x777, git_config_get(b'bup.hex', opttype='int'))