sphinx-cmd 0.2.2__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {sphinx_cmd-0.2.2/sphinx_cmd.egg-info → sphinx_cmd-0.3.0}/PKG-INFO +22 -1
- {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.0}/README.md +20 -0
- {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.0}/pyproject.toml +5 -1
- {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.0}/sphinx_cmd/__init__.py +1 -1
- {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.0}/sphinx_cmd/commands/rm.py +18 -19
- sphinx_cmd-0.3.0/sphinx_cmd/config.py +81 -0
- {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.0/sphinx_cmd.egg-info}/PKG-INFO +22 -1
- {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.0}/sphinx_cmd.egg-info/SOURCES.txt +3 -0
- {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.0}/sphinx_cmd.egg-info/requires.txt +3 -0
- sphinx_cmd-0.3.0/tests/test_config.py +79 -0
- sphinx_cmd-0.3.0/tests/test_custom_directives.py +125 -0
- {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.0}/LICENSE +0 -0
- {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.0}/MANIFEST.in +0 -0
- {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.0}/setup.cfg +0 -0
- {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.0}/sphinx_cmd/cli.py +0 -0
- {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.0}/sphinx_cmd/commands/__init__.py +0 -0
- {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.0}/sphinx_cmd/commands/mv.py +0 -0
- {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.0}/sphinx_cmd/py.typed +0 -0
- {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.0}/sphinx_cmd.egg-info/dependency_links.txt +0 -0
- {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.0}/sphinx_cmd.egg-info/entry_points.txt +0 -0
- {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.0}/sphinx_cmd.egg-info/top_level.txt +0 -0
- {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.0}/tests/test_cli.py +0 -0
- {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.0}/tests/test_integration.py +0 -0
- {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.0}/tests/test_mv.py +0 -0
- {sphinx_cmd-0.2.2 → sphinx_cmd-0.3.0}/tests/test_rm.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sphinx-cmd
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: Command-line tools for Sphinx documentation management
|
|
5
5
|
Author-email: Jared Dillard <jared.dillard@gmail.com>
|
|
6
6
|
Project-URL: Homepage, https://github.com/jdillard/sphinx-cmd
|
|
@@ -16,6 +16,7 @@ Classifier: Programming Language :: Python :: 3.12
|
|
|
16
16
|
Requires-Python: >=3.9
|
|
17
17
|
Description-Content-Type: text/markdown
|
|
18
18
|
License-File: LICENSE
|
|
19
|
+
Requires-Dist: tomli>=2.0.0; python_version < "3.11"
|
|
19
20
|
Provides-Extra: dev
|
|
20
21
|
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
21
22
|
Requires-Dist: black; extra == "dev"
|
|
@@ -52,6 +53,10 @@ sphinx-cmd rm path/to/docs
|
|
|
52
53
|
sphinx-cmd rm path/to/docs --dry-run
|
|
53
54
|
```
|
|
54
55
|
|
|
56
|
+
### Features
|
|
57
|
+
|
|
58
|
+
- Configure custom directives to be processed
|
|
59
|
+
|
|
55
60
|
### `sphinx-cmd mv`
|
|
56
61
|
|
|
57
62
|
Move/rename .rst files and automatically update all references to them.
|
|
@@ -78,6 +83,22 @@ sphinx-cmd mv old-file.rst new-file.rst --no-update-refs
|
|
|
78
83
|
- Handles relative paths correctly
|
|
79
84
|
- Preserves file relationships
|
|
80
85
|
|
|
86
|
+
## Configuration
|
|
87
|
+
|
|
88
|
+
You can add custom directives to be processed by creating a `.sphinx-cmd.toml` file in your home directory.
|
|
89
|
+
|
|
90
|
+
Add your custom directives to `[directives]` with their respective regex patterns, for example:
|
|
91
|
+
|
|
92
|
+
```toml
|
|
93
|
+
[directives]
|
|
94
|
+
drawio-figure = "^\\s*\\.\\.\\s+drawio-figure::\\s+(.+)$"
|
|
95
|
+
drawio-image = "^\\s*\\.\\.\\s+drawio-image::\\s+(.+)$"
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
> [!NOTE]
|
|
99
|
+
> Each regex pattern must include a capturing group `(.+)` to extract the file path.
|
|
100
|
+
|
|
101
|
+
|
|
81
102
|
## Development
|
|
82
103
|
|
|
83
104
|
```bash
|
|
@@ -24,6 +24,10 @@ sphinx-cmd rm path/to/docs
|
|
|
24
24
|
sphinx-cmd rm path/to/docs --dry-run
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
+
### Features
|
|
28
|
+
|
|
29
|
+
- Configure custom directives to be processed
|
|
30
|
+
|
|
27
31
|
### `sphinx-cmd mv`
|
|
28
32
|
|
|
29
33
|
Move/rename .rst files and automatically update all references to them.
|
|
@@ -50,6 +54,22 @@ sphinx-cmd mv old-file.rst new-file.rst --no-update-refs
|
|
|
50
54
|
- Handles relative paths correctly
|
|
51
55
|
- Preserves file relationships
|
|
52
56
|
|
|
57
|
+
## Configuration
|
|
58
|
+
|
|
59
|
+
You can add custom directives to be processed by creating a `.sphinx-cmd.toml` file in your home directory.
|
|
60
|
+
|
|
61
|
+
Add your custom directives to `[directives]` with their respective regex patterns, for example:
|
|
62
|
+
|
|
63
|
+
```toml
|
|
64
|
+
[directives]
|
|
65
|
+
drawio-figure = "^\\s*\\.\\.\\s+drawio-figure::\\s+(.+)$"
|
|
66
|
+
drawio-image = "^\\s*\\.\\.\\s+drawio-image::\\s+(.+)$"
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
> [!NOTE]
|
|
70
|
+
> Each regex pattern must include a capturing group `(.+)` to extract the file path.
|
|
71
|
+
|
|
72
|
+
|
|
53
73
|
## Development
|
|
54
74
|
|
|
55
75
|
```bash
|
|
@@ -20,7 +20,7 @@ classifiers = [
|
|
|
20
20
|
"Programming Language :: Python :: 3.11",
|
|
21
21
|
"Programming Language :: Python :: 3.12",
|
|
22
22
|
]
|
|
23
|
-
dependencies = []
|
|
23
|
+
dependencies = ["tomli>=2.0.0; python_version < '3.11'"]
|
|
24
24
|
|
|
25
25
|
[project.urls]
|
|
26
26
|
"Homepage" = "https://github.com/jdillard/sphinx-cmd"
|
|
@@ -77,6 +77,10 @@ profile = "black"
|
|
|
77
77
|
[tool.mypy]
|
|
78
78
|
python_version = "3.9"
|
|
79
79
|
|
|
80
|
+
[[tool.mypy.overrides]]
|
|
81
|
+
module = "tomli"
|
|
82
|
+
ignore_missing_imports = true
|
|
83
|
+
|
|
80
84
|
[tool.pytest.ini_options]
|
|
81
85
|
testpaths = ["tests"]
|
|
82
86
|
python_files = ["test_*.py"]
|
|
@@ -4,15 +4,9 @@ Command to delete unused .rst files and their unique assets.
|
|
|
4
4
|
"""
|
|
5
5
|
|
|
6
6
|
import os
|
|
7
|
-
import re
|
|
8
7
|
from collections import defaultdict
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
DIRECTIVE_PATTERNS = {
|
|
12
|
-
"image": re.compile(r"^\s*\.\.\s+image::\s+(.+)$", re.MULTILINE),
|
|
13
|
-
"figure": re.compile(r"^\s*\.\.\s+figure::\s+(.+)$", re.MULTILINE),
|
|
14
|
-
"include": re.compile(r"^\s*\.\.\s+include::\s+(.+)$", re.MULTILINE),
|
|
15
|
-
}
|
|
9
|
+
from sphinx_cmd.config import get_directive_patterns
|
|
16
10
|
|
|
17
11
|
|
|
18
12
|
def find_rst_files(path):
|
|
@@ -39,6 +33,7 @@ def extract_assets(file_path, visited=None):
|
|
|
39
33
|
visited.add(abs_path)
|
|
40
34
|
|
|
41
35
|
asset_directives = {}
|
|
36
|
+
directive_patterns = get_directive_patterns()
|
|
42
37
|
|
|
43
38
|
# If file doesn't exist, skip it
|
|
44
39
|
if not os.path.exists(file_path):
|
|
@@ -47,7 +42,7 @@ def extract_assets(file_path, visited=None):
|
|
|
47
42
|
try:
|
|
48
43
|
with open(file_path, encoding="utf-8") as f:
|
|
49
44
|
content = f.read()
|
|
50
|
-
for directive, pattern in
|
|
45
|
+
for directive, pattern in directive_patterns.items():
|
|
51
46
|
for match in pattern.findall(content):
|
|
52
47
|
asset_path = match.strip()
|
|
53
48
|
asset_full_path = os.path.normpath(
|
|
@@ -99,20 +94,24 @@ def get_transitive_includes(file_path, visited=None):
|
|
|
99
94
|
if not os.path.exists(file_path):
|
|
100
95
|
return includes
|
|
101
96
|
|
|
97
|
+
directive_patterns = get_directive_patterns()
|
|
98
|
+
|
|
102
99
|
try:
|
|
103
100
|
with open(file_path, encoding="utf-8") as f:
|
|
104
101
|
content = f.read()
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
102
|
+
# Only process include directive
|
|
103
|
+
if "include" in directive_patterns:
|
|
104
|
+
pattern = directive_patterns["include"]
|
|
105
|
+
for match in pattern.findall(content):
|
|
106
|
+
include_path = match.strip()
|
|
107
|
+
include_full_path = os.path.normpath(
|
|
108
|
+
os.path.join(os.path.dirname(file_path), include_path)
|
|
109
|
+
)
|
|
110
|
+
includes.add(include_full_path)
|
|
111
|
+
# Recursively get includes from the included file
|
|
112
|
+
includes.update(
|
|
113
|
+
get_transitive_includes(include_full_path, visited.copy())
|
|
114
|
+
)
|
|
116
115
|
except Exception as e:
|
|
117
116
|
print(f"Warning: Could not read {file_path}: {e}")
|
|
118
117
|
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
Configuration handling for sphinx-cmd.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import re
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Dict, Optional
|
|
10
|
+
|
|
11
|
+
# Use the standard library tomllib if Python 3.11+, otherwise use tomli package
|
|
12
|
+
if sys.version_info >= (3, 11):
|
|
13
|
+
import tomllib
|
|
14
|
+
else:
|
|
15
|
+
import tomli as tomllib
|
|
16
|
+
|
|
17
|
+
# Default configuration with built-in directives
|
|
18
|
+
DEFAULT_CONFIG = {
|
|
19
|
+
"directives": {
|
|
20
|
+
"image": r"^\s*\.\.\s+image::\s+(.+)$",
|
|
21
|
+
"figure": r"^\s*\.\.\s+figure::\s+(.+)$",
|
|
22
|
+
"include": r"^\s*\.\.\s+include::\s+(.+)$",
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def get_config_path() -> Optional[Path]:
|
|
28
|
+
"""Get the path to the configuration file."""
|
|
29
|
+
# Check for config in user's home directory
|
|
30
|
+
home_config = Path.home() / ".sphinx-cmd.toml"
|
|
31
|
+
if home_config.exists():
|
|
32
|
+
return home_config
|
|
33
|
+
|
|
34
|
+
return None
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def load_config() -> Dict:
|
|
38
|
+
"""
|
|
39
|
+
Load configuration from a TOML file.
|
|
40
|
+
|
|
41
|
+
The function looks for a config file at:
|
|
42
|
+
- user's home directory
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
Dict: The merged configuration (defaults + user config)
|
|
46
|
+
"""
|
|
47
|
+
config = DEFAULT_CONFIG.copy()
|
|
48
|
+
|
|
49
|
+
config_path = get_config_path()
|
|
50
|
+
if not config_path:
|
|
51
|
+
return config
|
|
52
|
+
|
|
53
|
+
try:
|
|
54
|
+
with open(config_path, "rb") as f:
|
|
55
|
+
user_config = tomllib.load(f)
|
|
56
|
+
|
|
57
|
+
# Merge user directives with default directives
|
|
58
|
+
if "directives" in user_config:
|
|
59
|
+
for name, pattern in user_config["directives"].items():
|
|
60
|
+
config["directives"][name] = pattern
|
|
61
|
+
|
|
62
|
+
except Exception as e:
|
|
63
|
+
print(f"Warning: Error loading config from {config_path}: {e}")
|
|
64
|
+
|
|
65
|
+
return config
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def get_directive_patterns() -> Dict[str, re.Pattern]:
|
|
69
|
+
"""
|
|
70
|
+
Get compiled regex patterns for all directives.
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
Dict[str, re.Pattern]: Dictionary of directive names to compiled regex patterns
|
|
74
|
+
"""
|
|
75
|
+
config = load_config()
|
|
76
|
+
patterns = {}
|
|
77
|
+
|
|
78
|
+
for name, pattern in config["directives"].items():
|
|
79
|
+
patterns[name] = re.compile(pattern, re.MULTILINE)
|
|
80
|
+
|
|
81
|
+
return patterns
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sphinx-cmd
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: Command-line tools for Sphinx documentation management
|
|
5
5
|
Author-email: Jared Dillard <jared.dillard@gmail.com>
|
|
6
6
|
Project-URL: Homepage, https://github.com/jdillard/sphinx-cmd
|
|
@@ -16,6 +16,7 @@ Classifier: Programming Language :: Python :: 3.12
|
|
|
16
16
|
Requires-Python: >=3.9
|
|
17
17
|
Description-Content-Type: text/markdown
|
|
18
18
|
License-File: LICENSE
|
|
19
|
+
Requires-Dist: tomli>=2.0.0; python_version < "3.11"
|
|
19
20
|
Provides-Extra: dev
|
|
20
21
|
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
21
22
|
Requires-Dist: black; extra == "dev"
|
|
@@ -52,6 +53,10 @@ sphinx-cmd rm path/to/docs
|
|
|
52
53
|
sphinx-cmd rm path/to/docs --dry-run
|
|
53
54
|
```
|
|
54
55
|
|
|
56
|
+
### Features
|
|
57
|
+
|
|
58
|
+
- Configure custom directives to be processed
|
|
59
|
+
|
|
55
60
|
### `sphinx-cmd mv`
|
|
56
61
|
|
|
57
62
|
Move/rename .rst files and automatically update all references to them.
|
|
@@ -78,6 +83,22 @@ sphinx-cmd mv old-file.rst new-file.rst --no-update-refs
|
|
|
78
83
|
- Handles relative paths correctly
|
|
79
84
|
- Preserves file relationships
|
|
80
85
|
|
|
86
|
+
## Configuration
|
|
87
|
+
|
|
88
|
+
You can add custom directives to be processed by creating a `.sphinx-cmd.toml` file in your home directory.
|
|
89
|
+
|
|
90
|
+
Add your custom directives to `[directives]` with their respective regex patterns, for example:
|
|
91
|
+
|
|
92
|
+
```toml
|
|
93
|
+
[directives]
|
|
94
|
+
drawio-figure = "^\\s*\\.\\.\\s+drawio-figure::\\s+(.+)$"
|
|
95
|
+
drawio-image = "^\\s*\\.\\.\\s+drawio-image::\\s+(.+)$"
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
> [!NOTE]
|
|
99
|
+
> Each regex pattern must include a capturing group `(.+)` to extract the file path.
|
|
100
|
+
|
|
101
|
+
|
|
81
102
|
## Development
|
|
82
103
|
|
|
83
104
|
```bash
|
|
@@ -4,6 +4,7 @@ README.md
|
|
|
4
4
|
pyproject.toml
|
|
5
5
|
sphinx_cmd/__init__.py
|
|
6
6
|
sphinx_cmd/cli.py
|
|
7
|
+
sphinx_cmd/config.py
|
|
7
8
|
sphinx_cmd/py.typed
|
|
8
9
|
sphinx_cmd.egg-info/PKG-INFO
|
|
9
10
|
sphinx_cmd.egg-info/SOURCES.txt
|
|
@@ -15,6 +16,8 @@ sphinx_cmd/commands/__init__.py
|
|
|
15
16
|
sphinx_cmd/commands/mv.py
|
|
16
17
|
sphinx_cmd/commands/rm.py
|
|
17
18
|
tests/test_cli.py
|
|
19
|
+
tests/test_config.py
|
|
20
|
+
tests/test_custom_directives.py
|
|
18
21
|
tests/test_integration.py
|
|
19
22
|
tests/test_mv.py
|
|
20
23
|
tests/test_rm.py
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import tempfile
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from unittest.mock import patch
|
|
4
|
+
|
|
5
|
+
from sphinx_cmd.config import get_directive_patterns, load_config
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def test_default_config():
|
|
9
|
+
"""Test that default config is returned when no config file exists."""
|
|
10
|
+
with patch("sphinx_cmd.config.get_config_path", return_value=None):
|
|
11
|
+
config = load_config()
|
|
12
|
+
|
|
13
|
+
# Check that default directives are present
|
|
14
|
+
assert "directives" in config
|
|
15
|
+
assert "image" in config["directives"]
|
|
16
|
+
assert "figure" in config["directives"]
|
|
17
|
+
assert "include" in config["directives"]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def test_load_custom_config():
|
|
21
|
+
"""Test loading a custom configuration file."""
|
|
22
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
23
|
+
config_path = Path(tmpdir) / ".sphinx-cmd.toml"
|
|
24
|
+
|
|
25
|
+
# Create a custom config file with proper TOML syntax
|
|
26
|
+
toml_content = """
|
|
27
|
+
[directives]
|
|
28
|
+
image = "^\\\\s*\\\\.\\\\.\\\\s+image::\\\\s+(.+)$"
|
|
29
|
+
figure = "^\\\\s*\\\\.\\\\.\\\\s+figure::\\\\s+(.+)$"
|
|
30
|
+
include = "^\\\\s*\\\\.\\\\.\\\\s+include::\\\\s+(.+)$"
|
|
31
|
+
drawio-figure = "^\\\\s*\\\\.\\\\.\\\\s+drawio-figure::\\\\s+(.+)$"
|
|
32
|
+
drawio-image = "^\\\\s*\\\\.\\\\.\\\\s+drawio-image::\\\\s+(.+)$"
|
|
33
|
+
"""
|
|
34
|
+
with open(config_path, "wb") as f:
|
|
35
|
+
f.write(toml_content.encode())
|
|
36
|
+
|
|
37
|
+
with patch("sphinx_cmd.config.get_config_path", return_value=config_path):
|
|
38
|
+
config = load_config()
|
|
39
|
+
|
|
40
|
+
# Check that custom directives are present
|
|
41
|
+
assert "directives" in config
|
|
42
|
+
assert "drawio-figure" in config["directives"]
|
|
43
|
+
assert "drawio-image" in config["directives"]
|
|
44
|
+
|
|
45
|
+
# Check original directives are still there
|
|
46
|
+
assert "image" in config["directives"]
|
|
47
|
+
assert "figure" in config["directives"]
|
|
48
|
+
assert "include" in config["directives"]
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def test_get_directive_patterns():
|
|
52
|
+
"""Test that directive patterns are compiled correctly."""
|
|
53
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
54
|
+
config_path = Path(tmpdir) / ".sphinx-cmd.toml"
|
|
55
|
+
|
|
56
|
+
# Create a custom config file with drawio directives with proper TOML syntax
|
|
57
|
+
toml_content = """
|
|
58
|
+
[directives]
|
|
59
|
+
drawio-figure = "^\\\\s*\\\\.\\\\.\\\\s+drawio-figure::\\\\s+(.+)$"
|
|
60
|
+
"""
|
|
61
|
+
with open(config_path, "wb") as f:
|
|
62
|
+
f.write(toml_content.encode())
|
|
63
|
+
|
|
64
|
+
with patch("sphinx_cmd.config.get_config_path", return_value=config_path):
|
|
65
|
+
patterns = get_directive_patterns()
|
|
66
|
+
|
|
67
|
+
# Default patterns should be included
|
|
68
|
+
assert "image" in patterns
|
|
69
|
+
assert "figure" in patterns
|
|
70
|
+
assert "include" in patterns
|
|
71
|
+
|
|
72
|
+
# Custom pattern should be included and be a compiled regex
|
|
73
|
+
assert "drawio-figure" in patterns
|
|
74
|
+
|
|
75
|
+
# Test the pattern works
|
|
76
|
+
test_string = ".. drawio-figure:: path/to/diagram.drawio"
|
|
77
|
+
match = patterns["drawio-figure"].findall(test_string)
|
|
78
|
+
assert len(match) == 1
|
|
79
|
+
assert match[0] == "path/to/diagram.drawio"
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import tempfile
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from unittest.mock import Mock, patch
|
|
5
|
+
|
|
6
|
+
from sphinx_cmd.commands.rm import execute, extract_assets
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def test_custom_directive_extraction():
|
|
10
|
+
"""Test that custom directives are extracted from RST files."""
|
|
11
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
12
|
+
# Create a test config with custom directives
|
|
13
|
+
config_path = Path(tmpdir) / ".sphinx-cmd.toml"
|
|
14
|
+
|
|
15
|
+
# Create config with proper TOML syntax
|
|
16
|
+
toml_content = """
|
|
17
|
+
[directives]
|
|
18
|
+
drawio-figure = "^\\\\s*\\\\.\\\\.\\\\s+drawio-figure::\\\\s+(.+)$"
|
|
19
|
+
drawio-image = "^\\\\s*\\\\.\\\\.\\\\s+drawio-image::\\\\s+(.+)$"
|
|
20
|
+
"""
|
|
21
|
+
with open(config_path, "wb") as f:
|
|
22
|
+
f.write(toml_content.encode())
|
|
23
|
+
|
|
24
|
+
# Create a test file with custom directives
|
|
25
|
+
test_file = os.path.join(tmpdir, "test.rst")
|
|
26
|
+
with open(test_file, "w") as f:
|
|
27
|
+
f.write(
|
|
28
|
+
"""
|
|
29
|
+
Test Document
|
|
30
|
+
============
|
|
31
|
+
|
|
32
|
+
.. image:: standard-image.png
|
|
33
|
+
.. drawio-figure:: custom-figure.drawio
|
|
34
|
+
.. drawio-image:: custom-image.drawio
|
|
35
|
+
"""
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
# Mock the config path function to use our temp config
|
|
39
|
+
with patch("sphinx_cmd.config.get_config_path", return_value=config_path):
|
|
40
|
+
# Extract assets
|
|
41
|
+
assets = extract_assets(test_file)
|
|
42
|
+
|
|
43
|
+
# Check we have the right number of assets (3)
|
|
44
|
+
assert len(assets) == 3
|
|
45
|
+
|
|
46
|
+
# Check all asset paths are correct
|
|
47
|
+
img_path = os.path.join(tmpdir, "standard-image.png")
|
|
48
|
+
custom_figure_path = os.path.join(tmpdir, "custom-figure.drawio")
|
|
49
|
+
custom_image_path = os.path.join(tmpdir, "custom-image.drawio")
|
|
50
|
+
|
|
51
|
+
assert img_path in assets
|
|
52
|
+
assert custom_figure_path in assets
|
|
53
|
+
assert custom_image_path in assets
|
|
54
|
+
|
|
55
|
+
# Check the directive types are correct
|
|
56
|
+
assert assets[img_path] == "image"
|
|
57
|
+
assert assets[custom_figure_path] == "drawio-figure"
|
|
58
|
+
assert assets[custom_image_path] == "drawio-image"
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def test_rm_command_with_custom_directives():
|
|
62
|
+
"""Test the rm command with custom directives (using dry-run)."""
|
|
63
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
64
|
+
# Create a test config with custom directives
|
|
65
|
+
config_path = Path(tmpdir) / ".sphinx-cmd.toml"
|
|
66
|
+
|
|
67
|
+
# Create config with proper TOML syntax
|
|
68
|
+
toml_content = """
|
|
69
|
+
[directives]
|
|
70
|
+
drawio-figure = "^\\\\s*\\\\.\\\\.\\\\s+drawio-figure::\\\\s+(.+)$"
|
|
71
|
+
"""
|
|
72
|
+
with open(config_path, "wb") as f:
|
|
73
|
+
f.write(toml_content.encode())
|
|
74
|
+
|
|
75
|
+
# Create test directory
|
|
76
|
+
test_dir = os.path.join(tmpdir, "docs")
|
|
77
|
+
os.makedirs(test_dir)
|
|
78
|
+
|
|
79
|
+
# Create an RST file with custom directives
|
|
80
|
+
rst_content = """
|
|
81
|
+
Test Page
|
|
82
|
+
=========
|
|
83
|
+
|
|
84
|
+
.. drawio-figure:: custom.drawio
|
|
85
|
+
"""
|
|
86
|
+
with open(os.path.join(test_dir, "test.rst"), "w") as f:
|
|
87
|
+
f.write(rst_content)
|
|
88
|
+
|
|
89
|
+
# Create the asset files
|
|
90
|
+
with open(os.path.join(test_dir, "custom.drawio"), "w") as f:
|
|
91
|
+
f.write("fake drawio")
|
|
92
|
+
|
|
93
|
+
# Execute the rm command with mocked config
|
|
94
|
+
with patch("sphinx_cmd.config.get_config_path", return_value=config_path):
|
|
95
|
+
# Setup to capture stdout
|
|
96
|
+
import sys
|
|
97
|
+
from io import StringIO
|
|
98
|
+
|
|
99
|
+
captured_output = StringIO()
|
|
100
|
+
original_stdout = sys.stdout
|
|
101
|
+
sys.stdout = captured_output
|
|
102
|
+
|
|
103
|
+
try:
|
|
104
|
+
args = Mock()
|
|
105
|
+
args.path = test_dir
|
|
106
|
+
args.dry_run = True
|
|
107
|
+
|
|
108
|
+
# Run with dry-run to test detection
|
|
109
|
+
execute(args)
|
|
110
|
+
|
|
111
|
+
# Get the captured output
|
|
112
|
+
output = captured_output.getvalue()
|
|
113
|
+
|
|
114
|
+
# Verify the correct files would be deleted
|
|
115
|
+
assert "[dry-run] Would delete drawio-figure:" in output
|
|
116
|
+
assert "custom.drawio" in output
|
|
117
|
+
assert "[dry-run] Would delete page:" in output
|
|
118
|
+
assert "test.rst" in output
|
|
119
|
+
finally:
|
|
120
|
+
sys.stdout = original_stdout
|
|
121
|
+
|
|
122
|
+
# Files should still exist after dry run
|
|
123
|
+
assert os.path.exists(os.path.join(test_dir, "test.rst"))
|
|
124
|
+
assert os.path.exists(os.path.join(test_dir, "custom.drawio"))
|
|
125
|
+
assert os.path.exists(test_dir)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|