Laszlo Csomor | 470a913 | 2018-10-12 11:05:02 -0700 | [diff] [blame] | 1 | # Copyright 2018 The Bazel Authors. All rights reserved. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import unittest |
| 16 | |
| 17 | from src.test.py.bazel import test_base |
| 18 | |
| 19 | |
| 20 | class TestRulesTest(test_base.TestBase): |
| 21 | |
| 22 | def _FailWithOutput(self, output): |
| 23 | self.fail('FAIL:\n | %s\n---' % '\n | '.join(output)) |
| 24 | |
| 25 | def _AssertPasses(self, target): |
| 26 | exit_code, stdout, stderr = self.RunBazel( |
| 27 | ['test', target, '--test_output=errors']) |
| 28 | if exit_code != 0: |
| 29 | self._FailWithOutput(stdout + stderr) |
| 30 | |
| 31 | def _AssertFails(self, target): |
| 32 | exit_code, stdout, stderr = self.RunBazel(['test', target]) |
| 33 | if exit_code == 0: |
| 34 | self._FailWithOutput(stdout + stderr) |
| 35 | |
| 36 | def testContent(self): |
| 37 | self.ScratchFile('WORKSPACE') |
| 38 | self.CopyFile( |
| 39 | self.Rlocation('io_bazel/tools/build_rules/test_rules.bzl'), |
| 40 | 'foo/test_rules.bzl') |
Laszlo Csomor | 77b9875 | 2019-05-17 03:15:22 -0700 | [diff] [blame] | 41 | self.CopyFile( |
| 42 | self.Rlocation('io_bazel/tools/build_rules/test_rules_private.bzl'), |
| 43 | 'foo/test_rules_private.bzl') |
Laszlo Csomor | 470a913 | 2018-10-12 11:05:02 -0700 | [diff] [blame] | 44 | self.ScratchFile('foo/tested_file.txt', |
| 45 | ['The quick brown', 'fox jumps over', 'the lazy dog.']) |
| 46 | self.ScratchFile('foo/BUILD', [ |
| 47 | 'load(":test_rules.bzl", "file_test")', |
| 48 | '', |
| 49 | 'file_test(', |
| 50 | ' name = "pos",', |
| 51 | ' content = "The quick brown\\nfox jumps over\\nthe lazy dog.\\n",', |
| 52 | ' file = "tested_file.txt",', |
| 53 | ')', |
| 54 | '', |
| 55 | 'file_test(', |
| 56 | ' name = "neg",', |
| 57 | ' content = "quick",', |
| 58 | ' file = "tested_file.txt",', |
| 59 | ')', |
| 60 | ]) |
| 61 | self._AssertPasses('//foo:pos') |
| 62 | self._AssertFails('//foo:neg') |
| 63 | |
| 64 | def testRegexpWithoutMatches(self): |
| 65 | self.ScratchFile('WORKSPACE') |
| 66 | self.CopyFile( |
| 67 | self.Rlocation('io_bazel/tools/build_rules/test_rules.bzl'), |
| 68 | 'foo/test_rules.bzl') |
| 69 | self.ScratchFile('foo/tested_file.txt', |
| 70 | ['The quick brown', 'fox jumps over', 'the lazy dog.']) |
| 71 | self.ScratchFile('foo/BUILD', [ |
| 72 | 'load(":test_rules.bzl", "file_test")', |
| 73 | '', |
| 74 | 'file_test(', |
| 75 | ' name = "pos",', |
| 76 | ' file = "tested_file.txt",', |
| 77 | ' regexp = "o[vwx]",', |
| 78 | ')', |
| 79 | '', |
| 80 | 'file_test(', |
| 81 | ' name = "neg",', |
| 82 | ' file = "tested_file.txt",', |
| 83 | ' regexp = "o[abc]",', |
| 84 | ')', |
| 85 | ]) |
| 86 | self._AssertPasses('//foo:pos') |
| 87 | self._AssertFails('//foo:neg') |
| 88 | |
| 89 | def testRegexpWithMatches(self): |
| 90 | self.ScratchFile('WORKSPACE') |
| 91 | self.CopyFile( |
| 92 | self.Rlocation('io_bazel/tools/build_rules/test_rules.bzl'), |
| 93 | 'foo/test_rules.bzl') |
| 94 | self.ScratchFile('foo/tested_file.txt', |
| 95 | ['The quick brown', 'fox jumps over', 'the lazy dog.']) |
| 96 | self.ScratchFile( |
| 97 | 'foo/BUILD', |
| 98 | [ |
| 99 | 'load(":test_rules.bzl", "file_test")', |
| 100 | '', |
| 101 | 'file_test(', |
| 102 | ' name = "pos",', |
| 103 | ' file = "tested_file.txt",', |
| 104 | # grep -c returns the number of matching lines, not the number of |
| 105 | # matches |
| 106 | ' matches = 2,', |
| 107 | ' regexp = "o[vwx]",', |
| 108 | ')', |
| 109 | '', |
| 110 | 'file_test(', |
| 111 | ' name = "neg",', |
| 112 | ' file = "tested_file.txt",', |
| 113 | ' matches = 3,', |
| 114 | ' regexp = "o[vwx]",', |
| 115 | ')', |
| 116 | ]) |
| 117 | self._AssertPasses('//foo:pos') |
| 118 | self._AssertFails('//foo:neg') |
| 119 | |
| 120 | def testBadArgs(self): |
| 121 | self.ScratchFile('WORKSPACE') |
| 122 | self.CopyFile( |
| 123 | self.Rlocation('io_bazel/tools/build_rules/test_rules.bzl'), |
| 124 | 'foo/test_rules.bzl') |
| 125 | self.ScratchFile('foo/tested_file.txt', |
| 126 | ['The quick brown', 'fox jumps over', 'the lazy dog.']) |
| 127 | self.ScratchFile('foo/BUILD', [ |
| 128 | 'load(":test_rules.bzl", "file_test")', |
| 129 | '', |
| 130 | 'file_test(', |
| 131 | ' name = "neither_content_nor_regex",', |
| 132 | ' file = "tested_file.txt",', |
| 133 | ')', |
| 134 | '', |
| 135 | 'file_test(', |
| 136 | ' name = "both_content_and_regex",', |
| 137 | ' file = "tested_file.txt",', |
| 138 | ' content = "x",', |
| 139 | ' regexp = "x",', |
| 140 | ')', |
| 141 | '', |
| 142 | 'file_test(', |
| 143 | ' name = "content_with_matches",', |
| 144 | ' file = "tested_file.txt",', |
| 145 | ' content = "hello",', |
| 146 | ' matches = 1,', |
| 147 | ')', |
| 148 | ]) |
| 149 | self._AssertFails('//foo:neither_content_nor_regex') |
| 150 | self._AssertFails('//foo:both_content_and_regex') |
| 151 | self._AssertFails('//foo:content_with_matches') |
| 152 | |
| 153 | |
| 154 | if __name__ == '__main__': |
| 155 | unittest.main() |