|
| 1 | +# Copyright 2025 The Kubernetes Authors. |
| 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 shutil |
| 16 | +import sys |
| 17 | +import unittest |
| 18 | +import yaml |
| 19 | +from pathlib import Path |
| 20 | +from unittest.mock import patch |
| 21 | + |
| 22 | +sys.path.insert(0, str(Path(__file__).resolve().parent)) |
| 23 | + |
| 24 | +import linking |
| 25 | + |
| 26 | +class TestUpdateMkdocsYml(unittest.TestCase): |
| 27 | + """Tests for the _update_mkdocs_yml_redirects function.""" |
| 28 | + |
| 29 | + def setUp(self) -> None: |
| 30 | + """Set up a temporary directory for each test.""" |
| 31 | + self.test_dir = Path("./temp_test_yml_updates") |
| 32 | + if self.test_dir.exists(): |
| 33 | + shutil.rmtree(self.test_dir) |
| 34 | + self.test_dir.mkdir() |
| 35 | + self.mkdocs_yml_path = self.test_dir / "mkdocs.yml" |
| 36 | + # Patch the function to use our temporary file path |
| 37 | + self.patcher = patch("linking.Path") |
| 38 | + self.mock_path = self.patcher.start() |
| 39 | + self.mock_path.return_value = self.mkdocs_yml_path |
| 40 | + |
| 41 | + def tearDown(self) -> None: |
| 42 | + """Clean up the temporary directory after each test.""" |
| 43 | + shutil.rmtree(self.test_dir) |
| 44 | + self.patcher.stop() |
| 45 | + |
| 46 | + def test_updates_yml_with_no_plugins_section(self): |
| 47 | + """Test that the function adds plugins and redirects correctly.""" |
| 48 | + # Arrange |
| 49 | + self.mkdocs_yml_path.write_text("site_name: My Docs") |
| 50 | + redirects = {"old/path.md": "new/path.md"} |
| 51 | + |
| 52 | + # Act |
| 53 | + result = linking._update_mkdocs_yml_redirects(redirects) |
| 54 | + |
| 55 | + # Assert |
| 56 | + self.assertTrue(result) |
| 57 | + with open(self.mkdocs_yml_path, "r") as f: |
| 58 | + config = yaml.safe_load(f) |
| 59 | + self.assertIn("plugins", config) |
| 60 | + self.assertIn({"redirects": {"redirect_maps": redirects}}, config["plugins"]) |
| 61 | + |
| 62 | + def test_updates_yml_with_string_redirects_plugin(self): |
| 63 | + """Test updating when 'redirects' is just a string in the plugins list.""" |
| 64 | + # Arrange |
| 65 | + self.mkdocs_yml_path.write_text("plugins:\n - redirects") |
| 66 | + redirects = {"old/path.md": "new/path.md"} |
| 67 | + |
| 68 | + # Act |
| 69 | + result = linking._update_mkdocs_yml_redirects(redirects) |
| 70 | + |
| 71 | + # Assert |
| 72 | + self.assertTrue(result) |
| 73 | + with open(self.mkdocs_yml_path, "r") as f: |
| 74 | + config = yaml.safe_load(f) |
| 75 | + self.assertIn({"redirects": {"redirect_maps": redirects}}, config["plugins"]) |
| 76 | + self.assertNotIn("redirects", config["plugins"]) |
| 77 | + |
| 78 | + def test_updates_yml_with_null_redirect_maps(self): |
| 79 | + """Test handling of 'redirect_maps: null'.""" |
| 80 | + # Arrange |
| 81 | + self.mkdocs_yml_path.write_text("plugins:\n - redirects:\n redirect_maps:") |
| 82 | + redirects = {"old/path.md": "new/path.md"} |
| 83 | + |
| 84 | + # Act |
| 85 | + result = linking._update_mkdocs_yml_redirects(redirects) |
| 86 | + |
| 87 | + # Assert |
| 88 | + self.assertTrue(result) |
| 89 | + with open(self.mkdocs_yml_path, "r") as f: |
| 90 | + config = yaml.safe_load(f) |
| 91 | + self.assertEqual(config["plugins"][0]["redirects"]["redirect_maps"], redirects) |
| 92 | + |
| 93 | + def test_does_not_write_if_no_changes_needed(self): |
| 94 | + """Test that the file is not modified if redirects are already present.""" |
| 95 | + # Arrange |
| 96 | + redirects = {"old/path.md": "new/path.md"} |
| 97 | + config_dict = { |
| 98 | + "plugins": [{"redirects": {"redirect_maps": redirects}}] |
| 99 | + } |
| 100 | + self.mkdocs_yml_path.write_text(yaml.dump(config_dict)) |
| 101 | + initial_mtime = self.mkdocs_yml_path.stat().st_mtime |
| 102 | + |
| 103 | + # Act |
| 104 | + result = linking._update_mkdocs_yml_redirects(redirects) |
| 105 | + |
| 106 | + # Assert |
| 107 | + self.assertTrue(result) |
| 108 | + final_mtime = self.mkdocs_yml_path.stat().st_mtime |
| 109 | + self.assertEqual(initial_mtime, final_mtime) |
0 commit comments