def test_env_set_pre(self): """ TEST: set predefined environment variable from [env] entry unconditionally EXP: the old value gets overwritten """ self.dbgfunc() sname = 'env' evname = 'UTIL_TEST' pre_val = "one:two:three" add = "four:five:six" exp = add # make sure the target env variable is set to a known value with U.tmpenv(evname, pre_val): # create a config object with an 'env' section and a non-'+' option cfg = hx.cfg.config() cfg.add_section(sname) cfg.set(sname, evname, add) # pass the config object to U.env_update() U.env_update(cfg) # verify that the target env variable now contains the new value # and the old value is gone self.expected(exp, os.environ[evname]) self.assertTrue( pre_val not in os.environ[evname], "The old value should be gone but still seems " + "to be hanging around")
def test_expand_filled(self): """ Test expand on 'before $VAR after', 'before/${VAR}/after', 'before.${VAR:-default-value}.after' Note that the default value is allowed but not used in the expansion. Python does not natively support that aspect of shell variable expansion. """ self.dbgfunc() vname = "EXPAND_FILLED" with U.tmpenv(vname, "SOMETHING"): bare_str = "before $%s after" % vname exp = "before SOMETHING after" actual = U.expand(bare_str) self.expected(exp, actual) braced_str = "before/${%s}/after" % vname exp = "before/SOMETHING/after" actual = U.expand(braced_str) self.expected(exp, actual) def_str = "before.${%s:-default-value}.after" % vname exp = "before.SOMETHING.after" actual = U.expand(def_str) self.expected(exp, actual)
def test_env_set_folded_none(self): """ TEST: set undefined environment variable from a folded [env] entry unconditionally EXP: the value gets set """ self.dbgfunc() sname = 'env' evname = 'UTIL_TEST' newval = "one:\n two:\n three" exp = re.sub("\n\s*", "", newval) # make sure the target env variable is not defined with U.tmpenv(evname, None): # create a config object with an 'env' section and a non-'+' option cfg = hx.cfg.config() cfg.add_section(sname) cfg.set(sname, evname, newval) # pass the config object to U.env_update() U.env_update(cfg) # verify that the variable was set to the expected value self.expected(exp, os.environ[evname])
def test_env_add_pre(self): """ TEST: add to a predefined environment variable from [env] entry EXP: payload is appended to the old value """ self.dbgfunc() sname = 'env' evname = 'UTIL_TEST' pre_val = "one:two:three" add = "four:five:six" exp = ":".join([pre_val, add]) # make sure the target env variable is set to a known value with U.tmpenv(evname, pre_val): # create a config object with an 'env' section and a '+' option cfg = hx.cfg.config() cfg.add_section(sname) cfg.set(sname, evname, "+" + add) # pass the config object to U.env_update() U.env_update(cfg) # verify that the target env variable now contains both old and # added values self.expected(exp, os.environ[evname])
def test_env_add_folded_pre(self): """ TEST: add to a preset environment variable from a folded [env] entry EXP: the value gets set to the payload with the whitespace squeezed out """ self.dbgfunc() sname = 'env' evname = 'UTIL_TEST' pre_val = "one:two:three" add = "four:\n five:\n six" exp = ":".join([pre_val, re.sub("\n\s*", "", add)]) # make sure the target env variable has the expected value with U.tmpenv(evname, pre_val): # create a config object with an 'env' section and a folded '+' # option cfg = hx.cfg.config() cfg.add_section(sname) cfg.set(sname, evname, '+' + add) # pass the config object to U.env_update() U.env_update(cfg) # verify that the variable was set to the expected value self.expected(exp, os.environ[evname])
def test_env_add_folded_none(self): """ TEST: add to an undefined environment variable from a folded [env] entry EXP: the value gets set to the payload with the whitespace squeezed out """ self.dbgfunc() sname = 'env' evname = 'UTIL_TEST' add = "four:\n five:\n six" exp = re.sub("\n\s*", "", add) # make sure the target env variable is not defined with U.tmpenv(evname, None): # create a config object with an 'env' section and a '+' option cfg = hx.cfg.config() cfg.add_section(sname) cfg.set(sname, evname, '+' + add) # pass the config object to U.env_update() U.env_update(cfg) # verify that the variable was set to the expected value self.expected(exp, os.environ[evname])
def test_expand_empty(self): """ Test expanding an empty variable with simple, braced, and default syntaxes """ self.dbgfunc() vname = "EXPAND_EMPTY" with U.tmpenv(vname, ""): bare_str = "before $%s after" % vname exp = "before after" actual = U.expand(bare_str) self.expected(exp, actual) braced_str = "before/${%s}/after" % vname exp = "before//after" actual = U.expand(braced_str) self.expected(exp, actual) def_str = "before.${%s:-default-value}.after" % vname exp = "before..after" actual = U.expand(def_str) self.expected(exp, actual)