cli.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. """Utils of command line test."""
  18. import os
  19. from click.testing import CliRunner
  20. class CliTestWrapper:
  21. """Wrap command click CliRunner.invoke."""
  22. _dev_mode_env_name = "PY_DOLPHINSCHEDULER_DEV_MODE"
  23. _dev_mode_true_val = {"true", "t", "1"}
  24. def __init__(self, *args, **kwargs):
  25. runner = CliRunner()
  26. self.result = runner.invoke(*args, **kwargs)
  27. self.show_result_output()
  28. def _assert_output(self, output: str = None, fuzzy: bool = False):
  29. """Assert between `CliRunner.invoke.result.output` and parameter `output`.
  30. :param output: The output will check compare to the ``CliRunner.invoke.output``.
  31. :param fuzzy: A flag define whether assert :param:`output` in fuzzy or not.
  32. Check if `CliRunner.invoke.output` contain :param:`output` is set ``True``
  33. and CliRunner.invoke.output equal to :param:`output` if we set it ``False``.
  34. """
  35. if not output:
  36. return
  37. if fuzzy:
  38. assert output in self.result.output
  39. else:
  40. assert self.result.output.rstrip("\n") == output
  41. def show_result_output(self):
  42. """Print `CliRunner.invoke.result` output content in debug mode.
  43. It read variable named `PY_DOLPHINSCHEDULER_DEV_MODE` from env, when it set to `true` or `t` or `1`
  44. will print result output when class :class:`CliTestWrapper` is initialization.
  45. """
  46. dev_mode = str(os.getenv(self._dev_mode_env_name))
  47. if dev_mode.strip().lower() in self._dev_mode_true_val:
  48. print(f"\n{self.result.output}\n")
  49. def assert_success(self, output: str = None, fuzzy: bool = False):
  50. """Assert test is success.
  51. It would check whether `CliRunner.invoke.exit_code` equals to `0`, with no
  52. exception at the same time. It's also can test the content of `CliRunner.invoke.output`.
  53. :param output: The output will check compare to the ``CliRunner.invoke.output``.
  54. :param fuzzy: A flag define whether assert :param:`output` in fuzzy or not.
  55. Check if `CliRunner.invoke.output` contain :param:`output` is set ``True``
  56. and CliRunner.invoke.output equal to :param:`output` if we set it ``False``.
  57. """
  58. assert self.result.exit_code == 0
  59. if self.result.exception:
  60. raise self.result.exception
  61. self._assert_output(output, fuzzy)
  62. def assert_fail(self, ret_code: int, output: str = None, fuzzy: bool = False):
  63. """Assert test is fail.
  64. It would check whether `CliRunner.invoke.exit_code` equals to :param:`ret_code`,
  65. and it will also can test the content of `CliRunner.invoke.output`.
  66. :param ret_code: The returning code of this fail test.
  67. :param output: The output will check compare to the ``CliRunner.invoke.output``.
  68. :param fuzzy: A flag define whether assert :param:`output` in fuzzy or not.
  69. Check if `CliRunner.invoke.output` contain :param:`output` is set ``True``
  70. and CliRunner.invoke.output equal to :param:`output` if we set it ``False``.
  71. """
  72. assert ret_code == self.result.exit_code
  73. self._assert_output(output, fuzzy)