From f04d4cf843d18fba0c286fdb2dae08dac97a626f Mon Sep 17 00:00:00 2001 From: gfxmonk Date: Sun, 16 Jan 2011 10:36:23 +1100 Subject: [PATCH] Added a 'value' attribute for environment bindings 'value' acts just like 'insert', except it does not get prefixed by the path to the implementation. It is an error to specify both 'value' and 'insert'. --- tests/Source.xml | 1 + tests/testautopolicy.py | 2 ++ tests/testreader.py | 31 +++++++++++++++++++++++++++++++ tests/testselections.py | 8 +++++++- zeroinstall/injector/model.py | 30 +++++++++++++++++++++++------- 5 files changed, 64 insertions(+), 8 deletions(-) diff --git a/tests/Source.xml b/tests/Source.xml index 729c97a..64509bf 100644 --- a/tests/Source.xml +++ b/tests/Source.xml @@ -10,6 +10,7 @@ + diff --git a/tests/testautopolicy.py b/tests/testautopolicy.py index 7e5a373..f8aabf7 100755 --- a/tests/testautopolicy.py +++ b/tests/testautopolicy.py @@ -148,6 +148,7 @@ class TestAutoPolicy(BaseTest): + @@ -182,6 +183,7 @@ class TestAutoPolicy(BaseTest): os.environ['FOO_PATH']) self.assertEquals(cached_impl + '/.:/a:/b', os.environ['BAR_PATH']) + self.assertEquals('val', os.environ['NO_PATH']) self.assertEquals(os.path.join(local_impl, 'group'), os.environ['SELF_GROUP']) self.assertEquals(os.path.join(local_impl, 'impl'), os.environ['SELF_IMPL']) diff --git a/tests/testreader.py b/tests/testreader.py index 30dc77c..a92678b 100755 --- a/tests/testreader.py +++ b/tests/testreader.py @@ -35,6 +35,26 @@ class TestReader(BaseTest): """ % (foo_iface_uri, version)) tmp.flush() return tmp + + def write_with_bindings(self, bindings): + tmp = tempfile.NamedTemporaryFile(prefix = 'test-') + tmp.write( +""" + + Foo + Foo + Foo + + + %s + + + +""" % (foo_iface_uri, foo_iface_uri, bindings)) + tmp.flush() + return tmp def testNoVersion(self): tmp = self.write_with_version('') @@ -48,9 +68,20 @@ class TestReader(BaseTest): tmp = self.write_with_version('min-injector-version="1000"') try: reader.check_readable(foo_iface_uri, tmp.name) + self.fail() except reader.InvalidInterface, ex: assert "1000" in str(ex) + def testCantUseBothInsertAndValueInEnvironmentBinding(self): + tmp = self.write_with_bindings(""" + + """) + try: + reader.check_readable(foo_iface_uri, tmp.name) + self.fail() + except reader.InvalidInterface, ex: + assert "Binding contains both 'insert' and 'value'" in str(ex) + def testRequiresVersion(self): tmp = tempfile.NamedTemporaryFile(prefix = 'test-') tmp.write( diff --git a/tests/testselections.py b/tests/testselections.py index 161d3ff..f086358 100755 --- a/tests/testselections.py +++ b/tests/testselections.py @@ -52,7 +52,13 @@ class TestSelections(BaseTest): self.assertEquals(1, len(sels[1].dependencies)) dep = sels[1].dependencies[0] self.assertEquals('http://foo/Compiler.xml', dep.interface) - self.assertEquals(1, len(dep.bindings)) + self.assertEquals(2, len(dep.bindings)) + self.assertEquals('bin', dep.bindings[0].insert) + self.assertEquals('PATH', dep.bindings[0].name) + + self.assertEquals('bin', dep.bindings[1].value) + self.assertEquals('NO_PATH', dep.bindings[1].name) + self.assertEquals(["sha1=345"], sels[0].digests) s1 = selections.Selections(p) diff --git a/zeroinstall/injector/model.py b/zeroinstall/injector/model.py index 20c9d28..cebfff7 100644 --- a/zeroinstall/injector/model.py +++ b/zeroinstall/injector/model.py @@ -115,9 +115,13 @@ def process_binding(e): binding = EnvironmentBinding(e.getAttribute('name'), insert = e.getAttribute('insert'), default = e.getAttribute('default'), + value = e.getAttribute('value'), mode = mode) if not binding.name: raise InvalidInterface(_("Missing 'name' in binding")) - if binding.insert is None: raise InvalidInterface(_("Missing 'insert' in binding")) + if binding.insert is None and binding.value is None: + raise InvalidInterface(_("Missing 'insert' or 'value' in binding")) + if binding.insert is not None and binding.value is not None: + raise InvalidInterface(_("Binding contains both 'insert' and 'value'")) return binding elif e.name == 'overlay': return OverlayBinding(e.getAttribute('src'), e.getAttribute('mount-point')) @@ -226,21 +230,25 @@ class Binding(object): class EnvironmentBinding(Binding): """Indicate the chosen implementation using an environment variable.""" - __slots__ = ['name', 'insert', 'default', 'mode'] + __slots__ = ['name', 'insert', 'default', 'mode', 'value'] PREPEND = 'prepend' APPEND = 'append' REPLACE = 'replace' - def __init__(self, name, insert, default = None, mode = PREPEND): - """mode argument added in version 0.28""" + def __init__(self, name, insert, default = None, mode = PREPEND, value=None): + """ + mode argument added in version 0.28 + value argument added in version 0.52 + """ self.name = name self.insert = insert self.default = default self.mode = mode + self.value = value def __str__(self): - return _("") % {'name': self.name,'mode': self.mode, 'insert': self.insert} + return _("") % {'name': self.name,'mode': self.mode, 'insert': self.insert, 'value': self.value} __repr__ = __str__ @@ -249,7 +257,12 @@ class EnvironmentBinding(Binding): @param path: the path to the selected implementation @param old_value: the current value of the environment variable @return: the new value for the environment variable""" - extra = os.path.join(path, self.insert) + + if self.insert is not None: + extra = os.path.join(path, self.insert) + else: + assert self.value is not None + extra = self.value if self.mode == EnvironmentBinding.REPLACE: return extra @@ -270,7 +283,10 @@ class EnvironmentBinding(Binding): """ env_elem = doc.createElementNS(XMLNS_IFACE, 'environment') env_elem.setAttributeNS(None, 'name', self.name) - env_elem.setAttributeNS(None, 'insert', self.insert) + if self.insert is not None: + env_elem.setAttributeNS(None, 'insert', self.insert) + else: + env_elem.setAttributeNS(None, 'value', self.value) if self.default: env_elem.setAttributeNS(None, 'default', self.default) return env_elem -- 2.11.4.GIT