wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 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 | |
| 6 | import logging |
| 7 | import json |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 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 |
wychen | 0735fd76 | 2017-06-03 07:53:26 | [diff] [blame] | 27 | ../../out/Release/gen/no.h |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 28 | ../../dir3/path/b.h |
| 29 | ../../c3.hh |
| 30 | ''' |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 31 | |
| 32 | |
| 33 | gn_input = json.loads(r''' |
| 34 | { |
| 35 | "others": [], |
| 36 | "targets": { |
| 37 | "//:All": { |
| 38 | }, |
| 39 | "//:base": { |
wychen | 5523578 | 2017-04-28 01:59:15 | [diff] [blame] | 40 | "public": [ "//base/p.h" ], |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 41 | "sources": [ "//base/a.cc", "//base/a.h", "//base/b.hh" ], |
| 42 | "visibility": [ "*" ] |
wychen | 5523578 | 2017-04-28 01:59:15 | [diff] [blame] | 43 | }, |
| 44 | "//:star_public": { |
| 45 | "public": "*", |
wychen | 0362911 | 2017-05-25 20:37:18 | [diff] [blame] | 46 | "sources": [ "//base/c.h", "//tmp/gen/a.h" ], |
wychen | 5523578 | 2017-04-28 01:59:15 | [diff] [blame] | 47 | "visibility": [ "*" ] |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | } |
| 51 | ''') |
| 52 | |
| 53 | |
| 54 | whitelist = 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): |
wychen | 0735fd76 | 2017-06-03 07:53:26 | [diff] [blame] | 66 | headers = check_gn_headers.ParseNinjaDepsOutput( |
wychen | 8cc3123 | 2017-06-13 10:21:23 | [diff] [blame] | 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 | } |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 74 | self.assertEquals(headers, expected) |
| 75 | |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 76 | def testGn(self): |
wychen | 0362911 | 2017-05-25 20:37:18 | [diff] [blame] | 77 | headers = check_gn_headers.ParseGNProjectJSON(gn_input, |
| 78 | 'out/Release', 'tmp') |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 79 | expected = set([ |
| 80 | 'base/a.h', |
| 81 | 'base/b.hh', |
wychen | 5523578 | 2017-04-28 01:59:15 | [diff] [blame] | 82 | 'base/c.h', |
| 83 | 'base/p.h', |
wychen | 0362911 | 2017-05-25 20:37:18 | [diff] [blame] | 84 | 'out/Release/gen/a.h', |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 85 | ]) |
| 86 | self.assertEquals(headers, expected) |
| 87 | |
| 88 | def testWhitelist(self): |
| 89 | output = check_gn_headers.ParseWhiteList(whitelist) |
| 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.assertEquals(output, expected) |
| 97 | |
| 98 | |
| 99 | if __name__ == '__main__': |
| 100 | logging.getLogger().setLevel(logging.DEBUG) |
| 101 | unittest.main(verbosity=2) |