test_compat.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import pytest
  2. import click
  3. from click._compat import should_strip_ansi
  4. from click._compat import WIN
  5. def test_legacy_callbacks(runner):
  6. def legacy_callback(ctx, value):
  7. return value.upper()
  8. @click.command()
  9. @click.option("--foo", callback=legacy_callback)
  10. def cli(foo):
  11. click.echo(foo)
  12. with pytest.warns(DeprecationWarning, match="2-arg style"):
  13. result = runner.invoke(cli, ["--foo", "wat"])
  14. assert result.exit_code == 0
  15. assert "WAT" in result.output
  16. def test_bash_func_name():
  17. from click._bashcomplete import get_completion_script
  18. script = get_completion_script("foo-bar baz_blah", "_COMPLETE_VAR", "bash").strip()
  19. assert script.startswith("_foo_barbaz_blah_completion()")
  20. assert "_COMPLETE_VAR=complete $1" in script
  21. def test_zsh_func_name():
  22. from click._bashcomplete import get_completion_script
  23. script = get_completion_script("foo-bar", "_COMPLETE_VAR", "zsh").strip()
  24. assert script.startswith("#compdef foo-bar")
  25. assert "compdef _foo_bar_completion foo-bar;" in script
  26. assert "(( ! $+commands[foo-bar] )) && return 1" in script
  27. @pytest.mark.xfail(WIN, reason="Jupyter not tested/supported on Windows")
  28. def test_is_jupyter_kernel_output():
  29. class JupyterKernelFakeStream(object):
  30. pass
  31. # implementation detail, aka cheapskate test
  32. JupyterKernelFakeStream.__module__ = "ipykernel.faked"
  33. assert not should_strip_ansi(stream=JupyterKernelFakeStream())