fweikert | 69895ba | 2022-07-13 04:56:04 -0700 | [diff] [blame] | 1 | # Copyright 2022 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 | from __future__ import absolute_import |
| 16 | from __future__ import division |
| 17 | from __future__ import print_function |
| 18 | |
| 19 | import os |
| 20 | import unittest |
| 21 | from absl.testing import parameterized |
| 22 | from scripts.docs import rewriter |
| 23 | |
| 24 | |
| 25 | class CanRewriteTest(parameterized.TestCase): |
| 26 | |
| 27 | @parameterized.parameters(("/file/doc.md", True), ("/path/_book.yaml", True), |
| 28 | ("http://www.bazel.build/foo.html", True), |
| 29 | ("/dir/test.txt", False), |
| 30 | ("/images/aspects.svg", False)) |
| 31 | def testCanRewrite(self, path, expected_can_rewrite): |
| 32 | self.assertEqual(rewriter.can_rewrite(path), expected_can_rewrite) |
| 33 | |
| 34 | |
| 35 | def read_data_file(basename, in_or_out_fragment): |
| 36 | path = os.path.join( |
| 37 | os.getenv("TEST_SRCDIR"), "io_bazel/scripts/docs/testdata", |
| 38 | in_or_out_fragment, basename) |
| 39 | with open(path, "rt", encoding="utf-8") as f: |
| 40 | return path, f.read() |
| 41 | |
| 42 | |
| 43 | class RewriteLinksTest(parameterized.TestCase): |
| 44 | |
| 45 | @parameterized.parameters(("_book.yaml"), ("doc.md"), |
| 46 | ("markdown_with_html.md"), ("site.html"), |
| 47 | ("yaml_with_html.yaml")) |
| 48 | def testRewrite(self, basename): |
| 49 | input_path, content = read_data_file(basename, "input") |
| 50 | _, version = read_data_file("VERSION", "input") |
| 51 | |
| 52 | actual = rewriter.rewrite_links(input_path, content, version) |
| 53 | |
| 54 | _, expected = read_data_file(basename, "expected_output") |
| 55 | |
| 56 | self.assertEqual(actual, expected) |
| 57 | |
| 58 | |
| 59 | if __name__ == "__main__": |
| 60 | unittest.main() |