def test_get_set_del_non_ascii_vars(self): set_env_var(NON_ASCII_VAR, NON_ASCII_VAL) for k, v in os.environ.items(): assert_true(isinstance(k, str) and isinstance(v, str)) assert_equals(get_env_var(NON_ASCII_VAR), NON_ASCII_VAL) assert_equals(del_env_var(NON_ASCII_VAR), NON_ASCII_VAL) assert_none(get_env_var(NON_ASCII_VAR))
def _get_variable(self, var): """'var' is an instance of a VariableSplitter""" # 1) Handle reserved syntax if var.identifier not in "$@%": value = "%s{%s}" % (var.identifier, var.base) LOGGER.warn("Syntax '%s' is reserved for future use. Please " "escape it like '\\%s'." % (value, value)) return value # 2) Handle environment variables and Java system properties elif var.identifier == "%": name = var.get_replaced_base(self).strip() if not name: return "%%{%s}" % var.base value = utils.get_env_var(name) if value is not None: return value value = getJavaSystemProperty(name) if value is not None: return value raise DataError("Environment variable '%s' does not exist" % name) # 3) Handle ${scalar} variables and @{list} variables without index elif var.index is None: name = "%s{%s}" % (var.identifier, var.get_replaced_base(self)) return self[name] # 4) Handle items from list variables e.g. @{var}[1] else: try: index = int(self.replace_string(var.index)) name = "@{%s}" % var.get_replaced_base(self) return self[name][index] except (ValueError, DataError, IndexError): raise DataError("Non-existing variable '@{%s}[%s]'" % (var.base, var.index))
def find(self, name): var_name, has_default, default_value = name[2:-1].partition('=') value = get_env_var(var_name) if value is not None: return value if has_default: return default_value variable_not_found(name, get_env_vars(), "Environment variable '%s' not found." % name)
def _get_variable(self, var): """'var' is an instance of a VariableSplitter""" # 1) Handle reserved syntax if var.identifier not in '$@%': value = '%s{%s}' % (var.identifier, var.base) LOGGER.warn("Syntax '%s' is reserved for future use. Please " "escape it like '\\%s'." % (value, value)) return value # 2) Handle environment variables and Java system properties elif var.identifier == '%': name = var.get_replaced_base(self).strip() if not name: return '%%{%s}' % var.base value = utils.get_env_var(name) if value is not None: return value value = getJavaSystemProperty(name) if value is not None: return value msg = "Environment variable '%%{%s}' not found." % name self._raise_non_existing_variable('%%{%s}' % name, msg, env_vars=True) # 3) Handle ${scalar} variables and @{list} variables without index elif var.index is None: name = '%s{%s}' % (var.identifier, var.get_replaced_base(self)) return self[name] # 4) Handle items from list variables e.g. @{var}[1] else: try: index = int(self.replace_string(var.index)) name = '@{%s}' % var.get_replaced_base(self) return self[name][index] except (ValueError, DataError, IndexError): msg = ("Variable '@{%s}[%s]' not found." % (var.base, var.index)) self._raise_non_existing_variable(var.base, msg)
def test_get_set_del_non_ascii_vars(self): set_env_var(NON_ASCII_VAR, NON_ASCII_VAL) assert_equal(get_env_var(NON_ASCII_VAR), NON_ASCII_VAL) assert_equal(del_env_var(NON_ASCII_VAR), NON_ASCII_VAL) assert_none(get_env_var(NON_ASCII_VAR))
def test_get_env_var(self): assert_not_none(get_env_var('PATH')) assert_equal(get_env_var(TEST_VAR), TEST_VAL) assert_none(get_env_var('NoNeXiStInG')) assert_equal(get_env_var('NoNeXiStInG', 'default'), 'default')
def test_get_env_var(self): assert_not_none(get_env_var("PATH")) assert_equals(get_env_var(TEST_VAR), TEST_VAL) assert_none(get_env_var("NoNeXiStInG")) assert_equals(get_env_var("NoNeXiStInG", "default"), "default")