check_gn_headers_unittest.py 2.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#!/usr/bin/env python
# Copyright 2017 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import logging
import json
import os
import unittest
import check_gn_headers


ninja_input = r'''
obj/a.o: #deps 1, deps mtime 123 (VALID)
    ../../a.cc
    ../../dir/path/b.h
    ../../c.hh

obj/b.o: #deps 1, deps mtime 123 (STALE)
    ../../b.cc
    ../../dir2/path/b.h
    ../../c2.hh

obj/c.o: #deps 1, deps mtime 123 (VALID)
    ../../c.cc
    ../../build/a.h
    gen/b.h
    ../../dir3/path/b.h
    ../../c3.hh
'''
ninja_input_win = ninja_input.replace('/', '\\')


gn_input = json.loads(r'''
{
   "others": [],
   "targets": {
      "//:All": {
      },
      "//:base": {
         "sources": [ "//base/a.cc", "//base/a.h", "//base/b.hh" ],
         "visibility": [ "*" ]
      }
    }
}
''')


whitelist = r'''
   white-front.c
a/b/c/white-end.c # comment
 dir/white-both.c  #more comment

# empty line above
a/b/c
'''


class CheckGnHeadersTest(unittest.TestCase):
  def testNinja(self):
61
    headers = check_gn_headers.ParseNinjaDepsOutput(ninja_input)
62 63 64 65 66 67 68 69 70 71 72 73
    expected = set([
        'dir/path/b.h',
        'c.hh',
        'dir3/path/b.h',
        'c3.hh',
    ])
    self.assertEquals(headers, expected)

  def testNinjaWin(self):
    old_sep = os.sep
    os.sep = '\\'

74
    headers = check_gn_headers.ParseNinjaDepsOutput(ninja_input_win)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    expected = set([
        'dir\\path\\b.h',
        'c.hh',
        'dir3\\path\\b.h',
        'c3.hh',
    ])
    self.assertEquals(headers, expected)

    os.sep = old_sep

  def testGn(self):
    headers = check_gn_headers.ParseGNProjectJSON(gn_input)
    expected = set([
        'base/a.h',
        'base/b.hh',
    ])
    self.assertEquals(headers, expected)

  def testWhitelist(self):
    output = check_gn_headers.ParseWhiteList(whitelist)
    expected = set([
        'white-front.c',
        'a/b/c/white-end.c',
        'dir/white-both.c',
        'a/b/c',
    ])
    self.assertEquals(output, expected)


if __name__ == '__main__':
  logging.getLogger().setLevel(logging.DEBUG)
  unittest.main(verbosity=2)