def test_no_print_to_stdout(self): """Test that messages printed to stderr have leading newline.""" captured_output = testutil.CapturedOutput() with captured_output: util.print_message("hello") self.assertEqual(captured_output.stdout, str())
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_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_execute_with_failure(self): """Execute a command with failure.""" with testutil.CapturedOutput(): self.assertEqual(1, util.execute(Mock(), util.output_on_fail, "false"))
def test_description_after_fat_arrow_first_level(self): """Description printed after fat arrow on first level.""" captured_output = testutil.CapturedOutput() with captured_output: with util.Task("Description"): pass self.assertEqual(captured_output.stderr, "\n==> Description")
def test_instant_failure_calls_through_to_container(self): """Execute a command with failure.""" container = Mock() with testutil.CapturedOutput(): util.execute(container, util.output_on_fail, "false") self.assertThat(container.note_failure.call_args_list, Not(Equals(list())))
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_execute_with_success_running_output(self): """Execute a command with success, but show output.""" captured_output = testutil.CapturedOutput() with captured_output: util.execute(Mock(), util.running_output, "python", "--version") self.assertThat(captured_output.stderr, DocTestMatches("\nPython ...", doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE))
def test_description_after_dots_second_level(self): """Description printed after dots on first level.""" captured_output = testutil.CapturedOutput() with captured_output: with util.Task("Description"): with util.Task("Secondary Description"): pass self.assertEqual("\n==> Description" "\n ... Secondary Description\n", captured_output.stderr)
def test_prepended_environment_variables_in_os_environ_list(self): """Prepended environment variables appear in the semicolon list.""" with testutil.CapturedOutput(): util.prepend_environment_variable(Mock(), "VAR", "VALUE") util.prepend_environment_variable(Mock(), "VAR", "SECOND_VALUE") self.assertThat(os.environ["VAR"].split(os.pathsep), MatchesAll(Contains("VALUE"), Contains("SECOND_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 output_is_on_level_after_task_description(self): """Command output gets printed to level after task description.""" captured_output = testutil.CapturedOutput() with captured_output: with util.Task("Description"): util.IndentedLogger().message("command_output\n" "command_output\n") self.assertEqual(captured_output.stderr, "\n==> Description" "\n command_output" "\n command_output" "\n")
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_nest_to_third_level(self): """Nest to third level with dots.""" captured_output = testutil.CapturedOutput() with captured_output: with util.Task("Description"): with util.Task("Secondary Description"): with util.Task("Tertiary Description"): pass self.assertEqual("\n==> Description" "\n ... Secondary Description" "\n ... Tertiary Description\n", captured_output.stderr)
def test_running_output_no_double_leading_slash_n(self): """Using running_output does not allow double-leading slash-n.""" captured_output = testutil.CapturedOutput() with captured_output: util.execute(Mock(), util.running_output, "python", "-c", "print(\"\")") self.assertThat(captured_output.stderr, DocTestMatches("\n", doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE))
def test_execute_with_failure_output(self): """Execute a command with failure, showing output.""" if "POLYSQUARE_ALWAYS_PRINT_PROCESS_OUTPUT" in os.environ: del os.environ["POLYSQUARE_ALWAYS_PRINT_PROCESS_OUTPUT"] captured_output = testutil.CapturedOutput() with captured_output: util.execute(Mock(), util.output_on_fail, "python", "/does-not-exist") self.assertThat(captured_output.stderr.strip(), Contains("/does-not-exist"))
def test_running_stderr_at_end(self): """Execute a command with success, but display stderr at end.""" captured_output = testutil.CapturedOutput() with captured_output: util.execute(Mock(), util.running_output, "python", "-c", "import sys; " "sys.stdout.write('a\\nb'); " "sys.stderr.write('c'); " "sys.stdout.write('d')") self.assertEqual(captured_output.stderr.replace("\r\n", "\n"), "\na\nbd\nc\n")
def test_execute_passes_environment_variables(self): """Pass specified environment variables to subprocess.""" captured_output = testutil.CapturedOutput() with captured_output: util.execute(Mock(), util.running_output, "python", "-c", "import os; print(os.environ['KEY'])", env={"KEY": "VALUE"}) self.assertThat(captured_output.stderr, DocTestMatches("...VALUE...", doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE))
def test_override_suppressed_output(self): """Override suppressed output with environment variable.""" os.environ["POLYSQUARE_ALWAYS_PRINT_PROCESS_OUTPUT"] = "1" captured_output = testutil.CapturedOutput() with captured_output: util.execute(Mock(), util.output_on_fail, "python", "-c", "print('Hello')") self.assertThat(captured_output.stderr, DocTestMatches("...Hello...", doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE | doctest.REPORT_NDIFF))
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_execute_show_dots_for_long_running_processes(self): """Show dots for long running processes.""" if "POLYSQUARE_ALWAYS_PRINT_PROCESS_OUTPUT" in os.environ: del os.environ["POLYSQUARE_ALWAYS_PRINT_PROCESS_OUTPUT"] captured_output = testutil.CapturedOutput() with captured_output: util.execute(Mock(), util.long_running_suppressed_output(dot_timeout=1), "sleep", "3") # There will be fewer dots as the watcher thread start a little # later than the subprocess does. However, there can be some cases # where there's a little bit of lag between terminating threads, so # there might be three dots. Match both cases. self.assertThat(captured_output.stderr.strip(), MatchesAny(Equals(".."), # suppress(PYC90) Equals("..."))) # suppress(PYC90)
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")))
def test_output_on_fail_handles_utf8(self): """Handle utf-8 strings correctly when showing failure output.""" if "POLYSQUARE_ALWAYS_PRINT_PROCESS_OUTPUT" in os.environ: del os.environ["POLYSQUARE_ALWAYS_PRINT_PROCESS_OUTPUT"] if (platform.python_implementation() != "CPython" or platform.system() == "Windows" or sys.version_info.major != 3): expected = " ..." else: expected = u"\N{check mark} ..." captured_output = testutil.CapturedOutput() with captured_output: util.execute(Mock(), util.running_output, "python", _utf8_print_cmd()) self.assertThat(captured_output.stderr[1:], DocTestMatches(expected, doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE))