def test_unset_environment_variable_in_os_environ(self):
        """Environment overwritten with None unset in os.environ."""
        with testutil.CapturedOutput():
            util.overwrite_environment_variable(Mock(), "VAR", "VALUE")
            util.overwrite_environment_variable(Mock(), "VAR", None)

        self.assertThat(os.environ, Not(Contains("VAR")))
    def test_overwritten_environment_variables_in_os_environ(self):
        """Test that overwritten environment variables are in os.environ."""
        with testutil.CapturedOutput():
            util.overwrite_environment_variable(Mock(), "VAR", "VALUE")

        self.assertThat(os.environ, Contains("VAR"))
        self.assertEqual(os.environ["VAR"], "VALUE")
    def test_overwritten_environment_variables_evaluated(self, config):
        """Test that overwritten environment variables are in shell output."""
        captured_output = testutil.CapturedOutput()
        with captured_output:
            util.overwrite_environment_variable(config.parent, "VAR", "VALUE")

        # config.env specifies what we expect the exported variable to
        # look like
        self.assertEqual(captured_output.stdout, config.env("VAR", "VALUE"))
    def test_remove_value_from_environment_variable_in_os_environ(self):
        """Remove a value from a colon separated value list in os.environ."""
        with testutil.CapturedOutput():
            util.overwrite_environment_variable(Mock(), "VAR", "VALUE")
            util.prepend_environment_variable(Mock(),
                                              "VAR",
                                              "SECOND_VALUE")
            util.remove_from_environment_variable(Mock(), "VAR", "VALUE")

        self.assertThat(os.environ["VAR"].split(os.pathsep),
                        MatchesAll(Not(Contains("VALUE")),
                                   Contains("SECOND_VALUE")))
    def test_unset_environment_variable_in_parent(self, config):
        """Environment overwritten with None unset in parent."""
        self._require(config.shell)

        captured_output = testutil.CapturedOutput()
        with captured_output:
            util.overwrite_environment_variable(config.parent, "VAR", "VALUE")
            util.overwrite_environment_variable(config.parent, "VAR", None)

        parent_env_value = _get_parent_env_value(config,
                                                 captured_output.stdout,
                                                 "VAR")
        self.assertEqual(parent_env_value.strip(), "")
    def test_prepended_environment_variables_in_parent(self, config):
        """Prepended variables appear in parent shell environment."""
        self._require(config.shell)

        captured_output = testutil.CapturedOutput()
        with captured_output:
            util.overwrite_environment_variable(config.parent, "VAR", "VALUE")
            util.prepend_environment_variable(config.parent,
                                              "VAR",
                                              "SECOND_VALUE")

        parent_env_value = _get_parent_env_value(config,
                                                 captured_output.stdout,
                                                 "VAR")
        self.assertThat(parent_env_value.split(config.sep),
                        MatchesAll(Contains("VALUE"),
                                   Contains("SECOND_VALUE")))
    def test_remove_value_from_environment_variable_in_parent(self, config):
        """Remove a value from a colon separated value list in parent shell."""
        self._require(config.shell)

        captured_output = testutil.CapturedOutput()
        with captured_output:
            util.overwrite_environment_variable(config.parent, "VAR", "VALUE")
            util.prepend_environment_variable(config.parent,
                                              "VAR",
                                              "SECOND_VALUE")
            util.remove_from_environment_variable(config.parent,
                                                  "VAR",
                                                  "VALUE")

        parent_env_value = _get_parent_env_value(config,
                                                 captured_output.stdout,
                                                 "VAR")
        self.assertThat(parent_env_value.split(config.sep),
                        MatchesAll(Not(Contains("VALUE")),
                                   Contains("SECOND_VALUE")))