File: test_rules_test.py

package info (click to toggle)
bazel-bootstrap 4.2.3%2Bds-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 85,476 kB
  • sloc: java: 721,710; sh: 55,859; cpp: 35,359; python: 12,139; xml: 295; objc: 269; makefile: 113; ansic: 106; ruby: 3
file content (155 lines) | stat: -rwxr-xr-x 5,024 bytes parent folder | download | duplicates (4)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Copyright 2018 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

from src.test.py.bazel import test_base


class TestRulesTest(test_base.TestBase):

  def _FailWithOutput(self, output):
    self.fail('FAIL:\n | %s\n---' % '\n | '.join(output))

  def _AssertPasses(self, target):
    exit_code, stdout, stderr = self.RunBazel(
        ['test', target, '--test_output=errors'])
    if exit_code != 0:
      self._FailWithOutput(stdout + stderr)

  def _AssertFails(self, target):
    exit_code, stdout, stderr = self.RunBazel(['test', target])
    if exit_code == 0:
      self._FailWithOutput(stdout + stderr)

  def testContent(self):
    self.ScratchFile('WORKSPACE')
    self.CopyFile(
        self.Rlocation('io_bazel/tools/build_rules/test_rules.bzl'),
        'foo/test_rules.bzl')
    self.CopyFile(
        self.Rlocation('io_bazel/tools/build_rules/test_rules_private.bzl'),
        'foo/test_rules_private.bzl')
    self.ScratchFile('foo/tested_file.txt',
                     ['The quick brown', 'fox jumps over', 'the lazy dog.'])
    self.ScratchFile('foo/BUILD', [
        'load(":test_rules.bzl", "file_test")',
        '',
        'file_test(',
        '    name = "pos",',
        '    content = "The quick brown\\nfox jumps over\\nthe lazy dog.\\n",',
        '    file = "tested_file.txt",',
        ')',
        '',
        'file_test(',
        '    name = "neg",',
        '    content = "quick",',
        '    file = "tested_file.txt",',
        ')',
    ])
    self._AssertPasses('//foo:pos')
    self._AssertFails('//foo:neg')

  def testRegexpWithoutMatches(self):
    self.ScratchFile('WORKSPACE')
    self.CopyFile(
        self.Rlocation('io_bazel/tools/build_rules/test_rules.bzl'),
        'foo/test_rules.bzl')
    self.ScratchFile('foo/tested_file.txt',
                     ['The quick brown', 'fox jumps over', 'the lazy dog.'])
    self.ScratchFile('foo/BUILD', [
        'load(":test_rules.bzl", "file_test")',
        '',
        'file_test(',
        '    name = "pos",',
        '    file = "tested_file.txt",',
        '    regexp = "o[vwx]",',
        ')',
        '',
        'file_test(',
        '    name = "neg",',
        '    file = "tested_file.txt",',
        '    regexp = "o[abc]",',
        ')',
    ])
    self._AssertPasses('//foo:pos')
    self._AssertFails('//foo:neg')

  def testRegexpWithMatches(self):
    self.ScratchFile('WORKSPACE')
    self.CopyFile(
        self.Rlocation('io_bazel/tools/build_rules/test_rules.bzl'),
        'foo/test_rules.bzl')
    self.ScratchFile('foo/tested_file.txt',
                     ['The quick brown', 'fox jumps over', 'the lazy dog.'])
    self.ScratchFile(
        'foo/BUILD',
        [
            'load(":test_rules.bzl", "file_test")',
            '',
            'file_test(',
            '    name = "pos",',
            '    file = "tested_file.txt",',
            # grep -c returns the number of matching lines, not the number of
            # matches
            '    matches = 2,',
            '    regexp = "o[vwx]",',
            ')',
            '',
            'file_test(',
            '    name = "neg",',
            '    file = "tested_file.txt",',
            '    matches = 3,',
            '    regexp = "o[vwx]",',
            ')',
        ])
    self._AssertPasses('//foo:pos')
    self._AssertFails('//foo:neg')

  def testBadArgs(self):
    self.ScratchFile('WORKSPACE')
    self.CopyFile(
        self.Rlocation('io_bazel/tools/build_rules/test_rules.bzl'),
        'foo/test_rules.bzl')
    self.ScratchFile('foo/tested_file.txt',
                     ['The quick brown', 'fox jumps over', 'the lazy dog.'])
    self.ScratchFile('foo/BUILD', [
        'load(":test_rules.bzl", "file_test")',
        '',
        'file_test(',
        '    name = "neither_content_nor_regex",',
        '    file = "tested_file.txt",',
        ')',
        '',
        'file_test(',
        '    name = "both_content_and_regex",',
        '    file = "tested_file.txt",',
        '    content = "x",',
        '    regexp = "x",',
        ')',
        '',
        'file_test(',
        '    name = "content_with_matches",',
        '    file = "tested_file.txt",',
        '    content = "hello",',
        '    matches = 1,',
        ')',
    ])
    self._AssertFails('//foo:neither_content_nor_regex')
    self._AssertFails('//foo:both_content_and_regex')
    self._AssertFails('//foo:content_with_matches')


if __name__ == '__main__':
  unittest.main()