Skip to content

Commit 8eea879

Browse files
committed
add test coverage
1 parent ea79f9c commit 8eea879

File tree

2 files changed

+198
-0
lines changed

2 files changed

+198
-0
lines changed

__tests__/test_preview.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 io
16+
import shutil
17+
import sys
18+
import unittest
19+
from pathlib import Path
20+
from unittest.mock import patch
21+
22+
sys.path.insert(0, str(Path(__file__).parent))
23+
24+
import linking
25+
26+
class TestPreviewDocs(unittest.TestCase):
27+
"""Tests for the preview_docs function."""
28+
29+
def setUp(self) -> None:
30+
"""Set up a temporary directory for each test."""
31+
self.test_dir = Path("./temp_test_preview")
32+
if self.test_dir.exists():
33+
shutil.rmtree(self.test_dir)
34+
self.docs_dir = self.test_dir / "my-docs"
35+
self.docs_dir.mkdir(parents=True)
36+
37+
def tearDown(self) -> None:
38+
"""Clean up the temporary directory after each test."""
39+
shutil.rmtree(self.test_dir)
40+
41+
def test_preview_with_mixed_files(self):
42+
"""Test preview output with a mix of files with and without IDs."""
43+
# Arrange
44+
(self.docs_dir / "index.md").write_text("# Welcome")
45+
(self.docs_dir / "has-id.md").write_text("---\nid: existing-id\n---\n# Has ID")
46+
47+
# Act
48+
captured_output = io.StringIO()
49+
with patch("sys.stdout", captured_output):
50+
linking.preview_docs(str(self.docs_dir))
51+
52+
# Assert
53+
output = captured_output.getvalue()
54+
self.assertIn("Files that would be modified (1):", output)
55+
self.assertIn("+ index.md -> ID: 'index'", output)
56+
self.assertIn("Files already with IDs (1):", output)
57+
self.assertIn("* has-id.md -> ID: 'existing-id'", output)
58+
self.assertIn("Would create/update redirect map", output)
59+
60+
def test_preview_with_empty_directory(self):
61+
"""Test preview output for an empty directory."""
62+
# Arrange (directory is already empty)
63+
64+
# Act
65+
captured_output = io.StringIO()
66+
with patch("sys.stdout", captured_output):
67+
linking.preview_docs(str(self.docs_dir))
68+
69+
# Assert
70+
output = captured_output.getvalue()
71+
self.assertIn("No markdown files found", output)
72+
73+
def test_preview_with_nonexistent_directory(self):
74+
"""Test preview output for a non-existent directory."""
75+
# Arrange
76+
non_existent_path = str(self.test_dir / "non-existent")
77+
78+
# Act
79+
captured_output = io.StringIO()
80+
with patch("sys.stdout", captured_output):
81+
linking.preview_docs(non_existent_path)
82+
83+
# Assert
84+
output = captured_output.getvalue()
85+
self.assertIn("ERROR: Directory", output)
86+
self.assertIn("does not exist", output)
87+
88+
if __name__ == "__main__":
89+
unittest.main(verbosity=2)

__tests__/test_updates.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)