• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# Copyright 2021 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.
5import unittest
6import tempfile
7import textwrap
8import os
9
10import json_gn_editor
11
12
13class BuildFileTest(unittest.TestCase):
14    def test_avoid_reformatting_gn_file_if_no_ast_changed(self):
15        text = textwrap.dedent('''\
16        android_library("target_name") {
17          deps =[":local_dep"]} #shouldn't change
18        ''')
19        with tempfile.NamedTemporaryFile(mode='w') as f:
20            f.write(text)
21            f.flush()
22            with json_gn_editor.BuildFile(f.name, '/') as build_file:
23                pass
24            with open(f.name, 'r') as f_after:
25                self.assertEqual(f_after.read(), text)
26
27    def test_split_dep_works_for_full_relative_abs_deps(self):
28        with tempfile.TemporaryDirectory() as rootdir:
29            java_subdir = os.path.join(rootdir, 'java')
30            os.mkdir(java_subdir)
31            build_gn_path = os.path.join(java_subdir, 'BUILD.gn')
32            with open(build_gn_path, 'w') as f:
33                f.write(
34                    textwrap.dedent('''\
35            android_library("java") {
36            }
37
38            android_library("target1") {
39              deps = [ "//java:java" ]
40            }
41
42            android_library("target2") {
43              deps += [ ":java" ]
44            }
45
46            android_library("target3") {
47              public_deps = [ "//java" ]
48            }
49            '''))
50            with json_gn_editor.BuildFile(build_gn_path,
51                                          rootdir) as build_file:
52                # Test both explicit and implied dep resolution works.
53                build_file.split_dep('//java:java', '//other_dir:other_dep')
54                build_file.split_dep('//java', '//other_dir:other_dep2')
55            with open(build_gn_path, 'r') as f:
56                self.assertEqual(
57                    f.read(),
58                    textwrap.dedent('''\
59            android_library("java") {
60            }
61
62            android_library("target1") {
63              deps = [
64                "//java:java",
65                "//other_dir:other_dep",
66                "//other_dir:other_dep2",
67              ]
68            }
69
70            android_library("target2") {
71              deps += [
72                ":java",
73                "//other_dir:other_dep",
74                "//other_dir:other_dep2",
75              ]
76            }
77
78            android_library("target3") {
79              public_deps = [
80                "//java",
81                "//other_dir:other_dep",
82                "//other_dir:other_dep2",
83              ]
84            }
85            '''))
86
87    def test_split_dep_does_not_duplicate_deps(self):
88        with tempfile.TemporaryDirectory() as rootdir:
89            java_subdir = os.path.join(rootdir, 'java')
90            os.mkdir(java_subdir)
91            build_gn_path = os.path.join(java_subdir, 'BUILD.gn')
92            with open(build_gn_path, 'w') as f:
93                f.write(
94                    textwrap.dedent('''\
95            android_library("target") {
96              deps = [
97                "//java",
98                "//other_dir:other_dep",
99              ]
100            }
101            '''))
102            with json_gn_editor.BuildFile(build_gn_path,
103                                          rootdir) as build_file:
104                build_file.split_dep('//java:java', '//other_dir:other_dep')
105            with open(build_gn_path, 'r') as f:
106                self.assertEqual(
107                    f.read(),
108                    textwrap.dedent('''\
109            android_library("target") {
110              deps = [
111                "//java",
112                "//other_dir:other_dep",
113              ]
114            }
115            '''))
116
117
118if __name__ == '__main__':
119    unittest.main()
120