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 |
| 8 | import os |
| 9 | import unittest |
| 10 | import check_gn_headers |
| 11 | |
| 12 | |
| 13 | ninja_input = r''' |
| 14 | obj/a.o: #deps 1, deps mtime 123 (VALID) |
| 15 | ../../a.cc |
| 16 | ../../dir/path/b.h |
| 17 | ../../c.hh |
| 18 | |
| 19 | obj/b.o: #deps 1, deps mtime 123 (STALE) |
| 20 | ../../b.cc |
| 21 | ../../dir2/path/b.h |
| 22 | ../../c2.hh |
| 23 | |
| 24 | obj/c.o: #deps 1, deps mtime 123 (VALID) |
| 25 | ../../c.cc |
| 26 | ../../build/a.h |
| 27 | gen/b.h |
| 28 | ../../dir3/path/b.h |
| 29 | ../../c3.hh |
| 30 | ''' |
| 31 | ninja_input_win = ninja_input.replace('/', '\\') |
| 32 | |
| 33 | |
| 34 | gn_input = json.loads(r''' |
| 35 | { |
| 36 | "others": [], |
| 37 | "targets": { |
| 38 | "//:All": { |
| 39 | }, |
| 40 | "//:base": { |
wychen | 5523578 | 2017-04-28 01:59:15 | [diff] [blame] | 41 | "public": [ "//base/p.h" ], |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 42 | "sources": [ "//base/a.cc", "//base/a.h", "//base/b.hh" ], |
| 43 | "visibility": [ "*" ] |
wychen | 5523578 | 2017-04-28 01:59:15 | [diff] [blame] | 44 | }, |
| 45 | "//:star_public": { |
| 46 | "public": "*", |
wychen | 0362911 | 2017-05-25 20:37:18 | [diff] [blame^] | 47 | "sources": [ "//base/c.h", "//tmp/gen/a.h" ], |
wychen | 5523578 | 2017-04-28 01:59:15 | [diff] [blame] | 48 | "visibility": [ "*" ] |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | } |
| 52 | ''') |
| 53 | |
| 54 | |
| 55 | whitelist = r''' |
| 56 | white-front.c |
| 57 | a/b/c/white-end.c # comment |
| 58 | dir/white-both.c #more comment |
| 59 | |
| 60 | # empty line above |
| 61 | a/b/c |
| 62 | ''' |
| 63 | |
| 64 | |
| 65 | class CheckGnHeadersTest(unittest.TestCase): |
| 66 | def testNinja(self): |
wychen | ef74ec99 | 2017-04-27 06:28:25 | [diff] [blame] | 67 | headers = check_gn_headers.ParseNinjaDepsOutput(ninja_input.split('\n')) |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 68 | expected = set([ |
| 69 | 'dir/path/b.h', |
| 70 | 'c.hh', |
| 71 | 'dir3/path/b.h', |
| 72 | 'c3.hh', |
| 73 | ]) |
| 74 | self.assertEquals(headers, expected) |
| 75 | |
| 76 | def testNinjaWin(self): |
| 77 | old_sep = os.sep |
| 78 | os.sep = '\\' |
| 79 | |
wychen | ef74ec99 | 2017-04-27 06:28:25 | [diff] [blame] | 80 | headers = check_gn_headers.ParseNinjaDepsOutput( |
| 81 | ninja_input_win.split('\n')) |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 82 | expected = set([ |
| 83 | 'dir\\path\\b.h', |
| 84 | 'c.hh', |
| 85 | 'dir3\\path\\b.h', |
| 86 | 'c3.hh', |
| 87 | ]) |
| 88 | self.assertEquals(headers, expected) |
| 89 | |
| 90 | os.sep = old_sep |
| 91 | |
| 92 | def testGn(self): |
wychen | 0362911 | 2017-05-25 20:37:18 | [diff] [blame^] | 93 | headers = check_gn_headers.ParseGNProjectJSON(gn_input, |
| 94 | 'out/Release', 'tmp') |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 95 | expected = set([ |
| 96 | 'base/a.h', |
| 97 | 'base/b.hh', |
wychen | 5523578 | 2017-04-28 01:59:15 | [diff] [blame] | 98 | 'base/c.h', |
| 99 | 'base/p.h', |
wychen | 0362911 | 2017-05-25 20:37:18 | [diff] [blame^] | 100 | 'out/Release/gen/a.h', |
wychen | 037f6e9e | 2017-01-10 17:14:56 | [diff] [blame] | 101 | ]) |
| 102 | self.assertEquals(headers, expected) |
| 103 | |
| 104 | def testWhitelist(self): |
| 105 | output = check_gn_headers.ParseWhiteList(whitelist) |
| 106 | expected = set([ |
| 107 | 'white-front.c', |
| 108 | 'a/b/c/white-end.c', |
| 109 | 'dir/white-both.c', |
| 110 | 'a/b/c', |
| 111 | ]) |
| 112 | self.assertEquals(output, expected) |
| 113 | |
| 114 | |
| 115 | if __name__ == '__main__': |
| 116 | logging.getLogger().setLevel(logging.DEBUG) |
| 117 | unittest.main(verbosity=2) |