Skip to content

Commit 94742d1

Browse files
✨ [+feature] Updated script to modify version var in pyproject.toml (#44)
2 parents e0b1d6b + 460a38c commit 94742d1

2 files changed

Lines changed: 92 additions & 22 deletions

File tree

src/AutoGitSemVer/scripts/UpdatePythonVersion.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ def list_commands(self, *args, **kwargs): # pylint: disable=unused-argument
4646
# ----------------------------------------------------------------------
4747
@app.command("EntryPoint", help=__doc__, no_args_is_help=True)
4848
def EntryPoint(
49-
python_filename: Annotated[
49+
filename: Annotated[
5050
Path,
5151
typer.Argument(
5252
dir_okay=False,
5353
exists=True,
5454
resolve_path=True,
55-
help="Name of the python file that contains the __version__ variable.",
55+
help="Name of the python file that contains the __version__ variable or the pyproject.toml file that contains the version variable.",
5656
),
5757
],
5858
working_dir: Annotated[
@@ -76,7 +76,7 @@ def EntryPoint(
7676
with DoneManager.CreateCommandLine(
7777
flags=DoneManagerFlags.Create(verbose=verbose, debug=debug),
7878
) as dm:
79-
working_dir = working_dir or python_filename.parent
79+
working_dir = working_dir or filename.parent
8080

8181
version = GetSemanticVersion(
8282
dm,
@@ -89,17 +89,31 @@ def EntryPoint(
8989

9090
dm.WriteLine("")
9191

92-
with dm.Nested(f"Updating '{python_filename}'...") as update_dm:
93-
with python_filename.open(encoding="utf-8") as f:
92+
with dm.Nested(f"Updating '{filename}'...") as update_dm:
93+
with filename.open(encoding="utf-8") as f:
9494
content = f.read()
9595

96-
match = re.search(
97-
r"^(?P<prefix>\s*__version__\s*=\s*)(?P<quote>['\"])\S*?(?P=quote)(?P<newline>\r?\n)",
98-
content,
99-
flags=re.MULTILINE,
100-
)
96+
if filename.suffix == ".py":
97+
variable_name = "__version__"
98+
99+
regex = re.compile(
100+
rf"^(?P<prefix>\s*{variable_name}\s*=\s*)(?P<quote>['\"])\S*?(?P=quote)(?P<newline>\r?\n)",
101+
flags=re.MULTILINE,
102+
)
103+
elif filename.suffix == ".toml":
104+
variable_name = "version"
105+
106+
regex = re.compile(
107+
rf"^(?P<prefix>{variable_name}\s*=\s*)(?P<quote>['\"])\S*?(?P=quote)(?P<newline>\r?\n)",
108+
flags=re.MULTILINE,
109+
)
110+
else:
111+
error = f"'{filename}' is not a recognized file type."
112+
raise Exception(error)
113+
114+
match = regex.search(content)
101115
if not match:
102-
update_dm.WriteError("A '__version__' variable was not found.\n")
116+
update_dm.WriteError(f"A '{variable_name}' variable was not found.\n")
103117
return
104118

105119
content = "".join(
@@ -114,12 +128,12 @@ def EntryPoint(
114128
],
115129
)
116130

117-
with python_filename.open("w", encoding="utf-8", newline=match.group("newline")) as f:
131+
with filename.open("w", encoding="utf-8", newline=match.group("newline")) as f:
118132
f.write(content)
119133

120134

121135
# ----------------------------------------------------------------------
122136
# ----------------------------------------------------------------------
123137
# ----------------------------------------------------------------------
124138
if __name__ == "__main__":
125-
app()
139+
app() # pragma: no cover

tests/scripts/UpdatePythonVersion_UnitTest.py

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818
from pathlib import Path
1919
from unittest.mock import MagicMock as Mock, mock_open, patch
2020

21-
import pytest
22-
21+
from click.testing import Result
2322
from typer.testing import CliRunner
2423
from AutoGitSemVer import GetSemanticVersionResult
2524
from AutoGitSemVer.scripts.UpdatePythonVersion import app
2625

2726

2827
# ----------------------------------------------------------------------
29-
def test_Default():
28+
def test_Python():
3029
_Execute(
3130
textwrap.dedent(
3231
"""\
@@ -46,7 +45,7 @@ def test_Default():
4645

4746

4847
# ----------------------------------------------------------------------
49-
def test_NoVersion():
48+
def test_NoPythonVersion():
5049
_Execute(
5150
textwrap.dedent(
5251
"""\
@@ -58,15 +57,63 @@ def test_NoVersion():
5857
)
5958

6059

60+
# ----------------------------------------------------------------------
61+
def test_Toml():
62+
_Execute(
63+
textwrap.dedent(
64+
"""\
65+
# Line Before
66+
version = ""
67+
# Line After
68+
""",
69+
),
70+
textwrap.dedent(
71+
"""\
72+
# Line Before
73+
version = "1.2.3"
74+
# Line After
75+
""",
76+
),
77+
file_extension=".toml",
78+
)
79+
80+
81+
# ----------------------------------------------------------------------
82+
def test_NoTomlVersion():
83+
_Execute(
84+
textwrap.dedent(
85+
"""\
86+
# No version in this file
87+
""",
88+
),
89+
"",
90+
file_extension=".toml",
91+
expected_failure=True,
92+
)
93+
94+
95+
# ----------------------------------------------------------------------
96+
def test_UnsupportedFileType():
97+
result = _Execute(
98+
"",
99+
"",
100+
file_extension=".md",
101+
expected_failure=True,
102+
)
103+
exception_message = str(result.exception)
104+
assert "README.md' is not a recognized file type." in exception_message
105+
106+
61107
# ----------------------------------------------------------------------
62108
# ----------------------------------------------------------------------
63109
# ----------------------------------------------------------------------
64110
def _Execute(
65111
original: str,
66112
expected: str,
67113
*,
114+
file_extension: str = ".py",
68115
expected_failure: bool = False,
69-
) -> str:
116+
) -> Result:
70117
# ----------------------------------------------------------------------
71118
def MyWrite(value):
72119
assert value == expected
@@ -86,11 +133,20 @@ def MyWrite(value):
86133
),
87134
patch.object(Path, "open", file_mock),
88135
):
89-
result = CliRunner().invoke(app, [__file__])
136+
if file_extension == ".py":
137+
placeholder_filename = Path(__file__)
138+
elif file_extension == ".toml":
139+
placeholder_filename = Path(__file__).parent.parent.parent / "pyproject.toml"
140+
elif file_extension == ".md":
141+
placeholder_filename = Path(__file__).parent.parent.parent / "README.md"
142+
else:
143+
assert False, file_extension
144+
145+
result = CliRunner().invoke(app, [str(placeholder_filename)])
90146

91147
if expected_failure:
92-
assert result.exit_code != 0
148+
assert result.exit_code != 0, result.output
93149
else:
94-
assert result.exit_code == 0
150+
assert result.exit_code == 0, result.output
95151

96-
return result.output
152+
return result

0 commit comments

Comments
 (0)