blob: 6616e6d72c047d65e24526b965650cb72ca5dbc4 [file] [log] [blame]
Greg Estrenc1d70872020-08-25 07:17:08 -07001# Lint as: python3
2# Copyright 2020 The Bazel Authors. All rights reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15"""Tests for lib.py."""
16import unittest
17from src.test.py.bazel import test_base
18# Do not edit this line. Copybara replaces it with PY2 migration helper..third_party.bazel.tools.ctexplain.bazel_api as bazel_api
19# Do not edit this line. Copybara replaces it with PY2 migration helper..third_party.bazel.tools.ctexplain.lib as lib
20from tools.ctexplain.types import Configuration
21from tools.ctexplain.types import HostConfiguration
22from tools.ctexplain.types import NullConfiguration
23
24
25class LibTest(test_base.TestBase):
26
27 _bazel: bazel_api.BazelApi = None
28
29 def setUp(self):
30 test_base.TestBase.setUp(self)
31 self._bazel = bazel_api.BazelApi(self.RunBazel)
32 self.ScratchFile('WORKSPACE')
33 self.CreateWorkspaceWithDefaultRepos('repo/WORKSPACE')
34
35 def tearDown(self):
36 test_base.TestBase.tearDown(self)
37
38 def testAnalyzeBuild(self):
39 self.ScratchFile('testapp/defs.bzl', [
40 'def _impl(ctx):',
41 ' pass',
42 'rule_with_host_dep = rule(',
43 ' implementation = _impl,',
44 ' attrs = { "host_deps": attr.label_list(cfg = "host") })',
45 ])
46 self.ScratchFile('testapp/BUILD', [
47 'load("//testapp:defs.bzl", "rule_with_host_dep")',
48 'rule_with_host_dep(name = "a", host_deps = [":h"])',
49 'filegroup(name = "h", srcs = ["h.src"])'
50 ])
51 cts = lib.analyze_build(self._bazel, ('//testapp:a',), ())
52 # Remove boilerplate deps to focus on targets declared here.
53 cts = [ct for ct in cts if ct.label.startswith('//testapp')]
54
55 self.assertListEqual([ct.label for ct in cts],
56 ['//testapp:a', '//testapp:h', '//testapp:h.src'])
57 # Don't use assertIsInstance because we don't want to match subclasses.
58 self.assertEqual(Configuration, type(cts[0].config))
59 self.assertEqual('HOST', cts[1].config_hash)
60 self.assertIsInstance(cts[1].config, HostConfiguration)
61 self.assertEqual('null', cts[2].config_hash)
62 self.assertIsInstance(cts[2].config, NullConfiguration)
63
64 def testAnalyzeBuildNoRepeats(self):
65 self.ScratchFile('testapp/defs.bzl', [
66 'def _impl(ctx):',
67 ' pass',
68 'rule_with_host_dep = rule(',
69 ' implementation = _impl,',
70 ' attrs = { "host_deps": attr.label_list(cfg = "host") })',
71 ])
72 self.ScratchFile('testapp/BUILD', [
73 'load("//testapp:defs.bzl", "rule_with_host_dep")',
74 'rule_with_host_dep(name = "a", host_deps = [":h", ":other"])',
75 'rule_with_host_dep(name = "other")',
76 'filegroup(name = "h", srcs = ["h.src", ":other"])'
77 ])
78 cts = lib.analyze_build(self._bazel, ('//testapp:a',), ())
79 # Remove boilerplate deps to focus on targets declared here.
80 cts = [ct for ct in cts if ct.label.startswith('//testapp')]
81
82 # Even though the build references //testapp:other twice, it only appears
83 # once.
84 self.assertListEqual(
85 [ct.label for ct in cts],
86 ['//testapp:a', '//testapp:h', '//testapp:other', '//testapp:h.src'])
87
88
89if __name__ == '__main__':
90 unittest.main()