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