test.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2021 Project CHIP Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import difflib
  16. import logging
  17. import os
  18. import subprocess
  19. import sys
  20. from typing import List
  21. import coloredlogs
  22. SCRIPT_ROOT = os.path.dirname(__file__)
  23. def build_expected_output(root: str, out: str) -> List[str]:
  24. with open(os.path.join(SCRIPT_ROOT, 'expected_test_cmakelists.txt'), 'rt') as file:
  25. for line in file.readlines():
  26. yield line.replace("{root}", root).replace("{out}", out)
  27. def build_actual_output(root: str, out: str) -> List[str]:
  28. # Fake out that we have a project root
  29. binary = os.path.join(SCRIPT_ROOT, '../gn_to_cmakelists.py')
  30. project = os.path.join(SCRIPT_ROOT, "test_project.json")
  31. cmake = os.path.join(SCRIPT_ROOT, "../../../out/CMakeLists.txt")
  32. subprocess.run([
  33. binary,
  34. project,
  35. ], stdout=subprocess.PIPE, check=True, encoding='UTF-8', )
  36. with open(cmake, 'rt') as f:
  37. for line in f.readlines():
  38. yield line
  39. def main():
  40. coloredlogs.install(level=logging.INFO,
  41. fmt='%(asctime)s %(name)s %(levelname)-7s %(message)s')
  42. ROOT = '/TEST/BUILD/ROOT'
  43. OUT = '/OUTPUT/DIR'
  44. expected = [line for line in build_expected_output(ROOT, OUT)]
  45. actual = [line for line in build_actual_output(ROOT, OUT)]
  46. diffs = [line for line in difflib.unified_diff(expected, actual)]
  47. if diffs:
  48. logging.error("DIFFERENCE between expected and generated output")
  49. for line in diffs:
  50. logging.warning(" " + line.strip())
  51. sys.exit(1)
  52. if __name__ == '__main__':
  53. main()