check_gn_headers_unittest.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python
  2. # Copyright 2017 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. import logging
  6. import json
  7. import unittest
  8. import check_gn_headers
  9. ninja_input = r'''
  10. obj/a.o: #deps 1, deps mtime 123 (VALID)
  11. ../../a.cc
  12. ../../dir/path/b.h
  13. ../../c.hh
  14. obj/b.o: #deps 1, deps mtime 123 (STALE)
  15. ../../b.cc
  16. ../../dir2/path/b.h
  17. ../../c2.hh
  18. obj/c.o: #deps 1, deps mtime 123 (VALID)
  19. ../../c.cc
  20. ../../build/a.h
  21. gen/b.h
  22. ../../out/Release/gen/no.h
  23. ../../dir3/path/b.h
  24. ../../c3.hh
  25. '''
  26. gn_input = json.loads(r'''
  27. {
  28. "others": [],
  29. "targets": {
  30. "//:All": {
  31. },
  32. "//:base": {
  33. "public": [ "//base/p.h" ],
  34. "sources": [ "//base/a.cc", "//base/a.h", "//base/b.hh" ],
  35. "visibility": [ "*" ]
  36. },
  37. "//:star_public": {
  38. "public": "*",
  39. "sources": [ "//base/c.h", "//tmp/gen/a.h" ],
  40. "visibility": [ "*" ]
  41. }
  42. }
  43. }
  44. ''')
  45. whitelist = r'''
  46. white-front.c
  47. a/b/c/white-end.c # comment
  48. dir/white-both.c #more comment
  49. # empty line above
  50. a/b/c
  51. '''
  52. class CheckGnHeadersTest(unittest.TestCase):
  53. def testNinja(self):
  54. headers = check_gn_headers.ParseNinjaDepsOutput(
  55. ninja_input.split('\n'), 'out/Release', False)
  56. expected = {
  57. 'dir/path/b.h': ['obj/a.o'],
  58. 'c.hh': ['obj/a.o'],
  59. 'dir3/path/b.h': ['obj/c.o'],
  60. 'c3.hh': ['obj/c.o'],
  61. }
  62. self.assertEquals(headers, expected)
  63. def testGn(self):
  64. headers = check_gn_headers.ParseGNProjectJSON(gn_input,
  65. 'out/Release', 'tmp')
  66. expected = set([
  67. 'base/a.h',
  68. 'base/b.hh',
  69. 'base/c.h',
  70. 'base/p.h',
  71. 'out/Release/gen/a.h',
  72. ])
  73. self.assertEquals(headers, expected)
  74. def testWhitelist(self):
  75. output = check_gn_headers.ParseWhiteList(whitelist)
  76. expected = set([
  77. 'white-front.c',
  78. 'a/b/c/white-end.c',
  79. 'dir/white-both.c',
  80. 'a/b/c',
  81. ])
  82. self.assertEquals(output, expected)
  83. if __name__ == '__main__':
  84. logging.getLogger().setLevel(logging.DEBUG)
  85. unittest.main(verbosity=2)