cli.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. from click.testing import CliRunner
  19. from tests.testing.constants import DEV_MODE
  20. class CliTestWrapper:
  21. """Wrap command click CliRunner.invoke."""
  22. def __init__(self, *args, **kwargs):
  23. runner = CliRunner()
  24. self.result = runner.invoke(*args, **kwargs)
  25. self.show_result_output()
  26. def _assert_output(self, output: str = None, fuzzy: bool = False):
  27. """Assert between `CliRunner.invoke.result.output` and parameter `output`.
  28. :param output: The output will check compare to the ``CliRunner.invoke.output``.
  29. :param fuzzy: A flag define whether assert :param:`output` in fuzzy or not.
  30. Check if `CliRunner.invoke.output` contain :param:`output` is set ``True``
  31. and CliRunner.invoke.output equal to :param:`output` if we set it ``False``.
  32. """
  33. if not output:
  34. return
  35. if fuzzy:
  36. assert output in self.result.output
  37. else:
  38. assert self.result.output.rstrip("\n") == output
  39. def show_result_output(self):
  40. """Print `CliRunner.invoke.result` output content in debug mode.
  41. It read variable named `PY_DOLPHINSCHEDULER_DEV_MODE` from env, when it set to `true` or `t` or `1`
  42. will print result output when class :class:`CliTestWrapper` is initialization.
  43. """
  44. if DEV_MODE:
  45. print(f"\n{self.result.output}\n")
  46. def assert_success(self, output: str = None, fuzzy: bool = False):
  47. """Assert test is success.
  48. It would check whether `CliRunner.invoke.exit_code` equals to `0`, with no
  49. exception at the same time. It's also can test the content of `CliRunner.invoke.output`.
  50. :param output: The output will check compare to the ``CliRunner.invoke.output``.
  51. :param fuzzy: A flag define whether assert :param:`output` in fuzzy or not.
  52. Check if `CliRunner.invoke.output` contain :param:`output` is set ``True``
  53. and CliRunner.invoke.output equal to :param:`output` if we set it ``False``.
  54. """
  55. assert self.result.exit_code == 0
  56. if self.result.exception:
  57. raise self.result.exception
  58. self._assert_output(output, fuzzy)
  59. def assert_fail(self, ret_code: int, output: str = None, fuzzy: bool = False):
  60. """Assert test is fail.
  61. It would check whether `CliRunner.invoke.exit_code` equals to :param:`ret_code`,
  62. and it will also can test the content of `CliRunner.invoke.output`.
  63. :param ret_code: The returning code of this fail test.
  64. :param output: The output will check compare to the ``CliRunner.invoke.output``.
  65. :param fuzzy: A flag define whether assert :param:`output` in fuzzy or not.
  66. Check if `CliRunner.invoke.output` contain :param:`output` is set ``True``
  67. and CliRunner.invoke.output equal to :param:`output` if we set it ``False``.
  68. """
  69. assert ret_code == self.result.exit_code
  70. self._assert_output(output, fuzzy)