blob: 731eeeb2073765206d5f25815f6bd993c9696906 [file] [log] [blame]
fweikert69895ba2022-07-13 04:56:04 -07001# 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
15from __future__ import absolute_import
16from __future__ import division
17from __future__ import print_function
18
19import os
20import unittest
21from absl.testing import parameterized
22from scripts.docs import rewriter
23
24
25class 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
35def 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
43class 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
59if __name__ == "__main__":
60 unittest.main()