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
6import logging
7import json
8import unittest
9import check_gn_headers
10
11
12ninja_input = r'''
13obj/a.o: #deps 1, deps mtime 123 (VALID)
14    ../../a.cc
15    ../../dir/path/b.h
16    ../../c.hh
17
18obj/b.o: #deps 1, deps mtime 123 (STALE)
19    ../../b.cc
20    ../../dir2/path/b.h
21    ../../c2.hh
22
23obj/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
33gn_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
54whitelist = r'''
55   white-front.c
56a/b/c/white-end.c # comment
57 dir/white-both.c  #more comment
58
59# empty line above
60a/b/c
61'''
62
63
64class 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.assertEquals(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.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
99if __name__ == '__main__':
100  logging.getLogger().setLevel(logging.DEBUG)
101  unittest.main(verbosity=2)
102