test_check_package.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. """Test cases for utils/check-package.
  2. It does not inherit from infra.basetest.BRTest and therefore does not generate
  3. a logfile. Only when the tests fail there will be output to the console.
  4. The make target ('make check-package') is already used by the job
  5. 'check-package' and won't be tested here.
  6. """
  7. import os
  8. import subprocess
  9. import unittest
  10. import infra
  11. def call_script(args, env, cwd):
  12. """Call a script and return stdout and stderr as lists."""
  13. out, err = subprocess.Popen(args, cwd=cwd, stdout=subprocess.PIPE,
  14. stderr=subprocess.PIPE, env=env).communicate()
  15. return out.splitlines(), err.splitlines()
  16. class TestCheckPackage(unittest.TestCase):
  17. """Test the various ways the script can be called.
  18. The script can be called either using relative path, absolute path or from
  19. PATH.
  20. The files to be checked can be passed as arguments using either relative
  21. path or absolute path.
  22. When in in-tree mode (without -b) some in-tree files and also all
  23. out-of-tree files are ignored.
  24. When in out-tree mode (with -b) the script does generate warnings for these
  25. but ignores external.mk.
  26. """
  27. WITH_EMPTY_PATH = {}
  28. WITH_UTILS_IN_PATH = {"PATH": infra.basepath("utils") + ":" + os.environ["PATH"]}
  29. relative = [
  30. # base_script base_file rel_script rel_file rel_cwd
  31. ["utils/check-package", "package/atop/atop.mk", "./utils/check-package", "package/atop/atop.mk", ""],
  32. ["utils/check-package", "package/atop/atop.mk", "./utils/check-package", "./package/atop/atop.mk", ""],
  33. ["utils/check-package", "package/atop/atop.mk", "../../utils/check-package", "atop.mk", "package/atop"],
  34. ["utils/check-package", "package/atop/atop.mk", "../../utils/check-package", "./atop.mk", "package/atop"],
  35. ["utils/check-package", "package/atop/atop.mk", "../utils/check-package", "atop/atop.mk", "package"],
  36. ["utils/check-package", "package/atop/atop.mk", "../utils/check-package", "./atop/atop.mk", "package"],
  37. ["utils/check-package", "package/atop/Config.in", "./utils/check-package", "package/atop/Config.in", ""],
  38. ["utils/check-package", "package/atop/Config.in", "./utils/check-package", "./package/atop/Config.in", ""],
  39. ["utils/check-package", "package/atop/Config.in", "../../utils/check-package", "Config.in", "package/atop"],
  40. ["utils/check-package", "package/atop/Config.in", "../../utils/check-package", "./Config.in", "package/atop"],
  41. ["utils/check-package", "package/atop/Config.in", "../utils/check-package", "atop/Config.in", "package"],
  42. ["utils/check-package", "package/atop/Config.in", "../utils/check-package", "./atop/Config.in", "package"]]
  43. def assert_file_was_processed(self, stderr):
  44. """Infer from check-package stderr if at least one file was processed
  45. and fail otherwise."""
  46. self.assertIn("lines processed", stderr[0], stderr)
  47. processed = int(stderr[0].split()[0])
  48. self.assertGreater(processed, 0)
  49. def assert_file_was_ignored(self, stderr):
  50. """Infer from check-package stderr if no file was processed and fail
  51. otherwise."""
  52. self.assertIn("lines processed", stderr[0], stderr)
  53. processed = int(stderr[0].split()[0])
  54. self.assertEqual(processed, 0)
  55. def assert_warnings_generated_for_file(self, stderr):
  56. """Infer from check-package stderr if at least one warning was generated
  57. and fail otherwise."""
  58. self.assertIn("warnings generated", stderr[1], stderr)
  59. generated = int(stderr[1].split()[0])
  60. self.assertGreater(generated, 0)
  61. def test_run(self):
  62. """Test the various ways the script can be called in a simple top to
  63. bottom sequence."""
  64. # an intree file can be checked by the script called from relative path,
  65. # absolute path and from PATH
  66. for base_script, base_file, rel_script, rel_file, rel_cwd in self.relative:
  67. abs_script = infra.basepath(base_script)
  68. abs_file = infra.basepath(base_file)
  69. cwd = infra.basepath(rel_cwd)
  70. _, m = call_script([rel_script, rel_file],
  71. self.WITH_EMPTY_PATH, cwd)
  72. self.assert_file_was_processed(m)
  73. _, m = call_script([abs_script, rel_file],
  74. self.WITH_EMPTY_PATH, cwd)
  75. self.assert_file_was_processed(m)
  76. _, m = call_script(["check-package", rel_file],
  77. self.WITH_UTILS_IN_PATH, cwd)
  78. self.assert_file_was_processed(m)
  79. _, m = call_script([rel_script, abs_file],
  80. self.WITH_EMPTY_PATH, cwd)
  81. self.assert_file_was_processed(m)
  82. _, m = call_script([abs_script, abs_file],
  83. self.WITH_EMPTY_PATH, cwd)
  84. self.assert_file_was_processed(m)
  85. _, m = call_script(["check-package", abs_file],
  86. self.WITH_UTILS_IN_PATH, cwd)
  87. self.assert_file_was_processed(m)
  88. # some intree files are ignored
  89. _, m = call_script(["./utils/check-package", "package/pkg-generic.mk"],
  90. self.WITH_EMPTY_PATH, infra.basepath())
  91. self.assert_file_was_ignored(m)
  92. _, m = call_script(["./utils/check-package", "-b", "package/pkg-generic.mk"],
  93. self.WITH_EMPTY_PATH, infra.basepath())
  94. self.assert_file_was_processed(m)
  95. # an out-of-tree file can be checked by the script called from relative
  96. # path, absolute path and from PATH
  97. for base_script, base_file, rel_script, rel_file, rel_cwd in self.relative:
  98. abs_script = infra.basepath(base_script)
  99. abs_file = infra.basepath(base_file)
  100. cwd = infra.basepath(rel_cwd)
  101. _, m = call_script([rel_script, "-b", rel_file],
  102. self.WITH_EMPTY_PATH, cwd)
  103. self.assert_file_was_processed(m)
  104. _, m = call_script([abs_script, "-b", rel_file],
  105. self.WITH_EMPTY_PATH, cwd)
  106. self.assert_file_was_processed(m)
  107. _, m = call_script(["check-package", "-b", rel_file],
  108. self.WITH_UTILS_IN_PATH, cwd)
  109. self.assert_file_was_processed(m)
  110. _, m = call_script([rel_script, "-b", abs_file],
  111. self.WITH_EMPTY_PATH, cwd)
  112. self.assert_file_was_processed(m)
  113. _, m = call_script([abs_script, "-b", abs_file],
  114. self.WITH_EMPTY_PATH, cwd)
  115. self.assert_file_was_processed(m)
  116. _, m = call_script(["check-package", "-b", abs_file],
  117. self.WITH_UTILS_IN_PATH, cwd)
  118. self.assert_file_was_processed(m)
  119. # out-of-tree files are are ignored without -b but can generate warnings
  120. # with -b
  121. abs_path = infra.filepath("tests/utils/br2-external")
  122. rel_file = "Config.in"
  123. abs_file = os.path.join(abs_path, rel_file)
  124. _, m = call_script(["check-package", rel_file],
  125. self.WITH_UTILS_IN_PATH, abs_path)
  126. self.assert_file_was_ignored(m)
  127. _, m = call_script(["check-package", abs_file],
  128. self.WITH_UTILS_IN_PATH, infra.basepath())
  129. self.assert_file_was_ignored(m)
  130. w, m = call_script(["check-package", "-b", rel_file],
  131. self.WITH_UTILS_IN_PATH, abs_path)
  132. self.assert_file_was_processed(m)
  133. self.assert_warnings_generated_for_file(m)
  134. self.assertIn("{}:1: empty line at end of file".format(rel_file), w)
  135. w, m = call_script(["check-package", "-b", abs_file],
  136. self.WITH_UTILS_IN_PATH, infra.basepath())
  137. self.assert_file_was_processed(m)
  138. self.assert_warnings_generated_for_file(m)
  139. self.assertIn("{}:1: empty line at end of file".format(abs_file), w)
  140. # external.mk is ignored only when in the root path of a br2-external
  141. rel_file = "external.mk"
  142. abs_file = os.path.join(abs_path, rel_file)
  143. _, m = call_script(["check-package", "-b", rel_file],
  144. self.WITH_UTILS_IN_PATH, abs_path)
  145. self.assert_file_was_ignored(m)
  146. _, m = call_script(["check-package", "-b", abs_file],
  147. self.WITH_UTILS_IN_PATH, infra.basepath())
  148. self.assert_file_was_ignored(m)
  149. abs_path = infra.filepath("tests/utils/br2-external/package/external")
  150. abs_file = os.path.join(abs_path, rel_file)
  151. w, m = call_script(["check-package", "-b", rel_file],
  152. self.WITH_UTILS_IN_PATH, abs_path)
  153. self.assert_file_was_processed(m)
  154. self.assert_warnings_generated_for_file(m)
  155. self.assertIn("{}:1: should be 80 hashes (http://nightly.buildroot.org/#writing-rules-mk)".format(rel_file), w)
  156. w, m = call_script(["check-package", "-b", abs_file],
  157. self.WITH_UTILS_IN_PATH, infra.basepath())
  158. self.assert_file_was_processed(m)
  159. self.assert_warnings_generated_for_file(m)
  160. self.assertIn("{}:1: should be 80 hashes (http://nightly.buildroot.org/#writing-rules-mk)".format(abs_file), w)