Skip to content

Commit 2c249da

Browse files
committed
Cleanup code and tests
1 parent c0553b9 commit 2c249da

5 files changed

Lines changed: 58 additions & 78 deletions

File tree

.github/workflows/test.yaml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
test:
3030
strategy:
3131
matrix:
32-
python-version: ['3.7', '3.8', '3.9', '3.10']
32+
python-version: ['3.6', '3.7', '3.8', '3.9', '3.10']
3333

3434
runs-on: ubuntu-latest
3535

@@ -41,11 +41,9 @@ jobs:
4141
python-version: ${{ matrix.python-version }}
4242

4343
- name: Install Dependencies
44-
run: python -m pip install --upgrade setuptools pip virtualenv tox coverage
44+
run: python -m pip install --upgrade setuptools pip virtualenv tox
4545
- name: Run Tests
46-
run: |
47-
tox -e py
48-
coverage xml
46+
run: tox -e py
4947

5048
- name: Upload coverage to Codecov
5149
uses: codecov/codecov-action@v3

github_action_utils.py

Lines changed: 43 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
def _print_command(
3535
command: CommandTypes,
3636
command_message: str,
37-
property_string: Union[str, None] = None,
37+
options_string: Union[str] = "",
3838
) -> None:
3939
"""
4040
Helper function to print GitHub action commands to the shell.
@@ -44,14 +44,11 @@ def _print_command(
4444
:returns: None
4545
"""
4646
# https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions
47-
if command == "endgroup":
48-
echo_message = f"{COMMAND_MARKER}endgroup{COMMAND_MARKER}"
49-
else:
50-
echo_message = (
51-
f"{COMMAND_MARKER}{command} "
52-
f"{property_string or ''}"
53-
f"{COMMAND_MARKER}{_escape_data(command_message)}"
54-
)
47+
echo_message = (
48+
f"{COMMAND_MARKER}{command} "
49+
f"{options_string or ''}"
50+
f"{COMMAND_MARKER}{_escape_data(command_message)}"
51+
)
5552

5653
print(echo_message)
5754

@@ -119,29 +116,12 @@ def _to_camel_case(text: str) -> str:
119116
return f"{text[:1].lower()}{text.title().replace('_', '')[1:]}"
120117

121118

122-
def _print_log_message(
123-
command: LogCommandTypes,
124-
message: str,
125-
**kwargs: Any,
126-
) -> None:
127-
"""
128-
prints a log message to the GitHub action shell.
129-
130-
:param message: Message to display
131-
:param title: Custom title
132-
:param file: Filename in the repository
133-
:param col: Column number, starting at 1
134-
:param end_column: End column number
135-
:param line: Line number, starting at 1
136-
:param end_line: End line number
137-
:returns: None
138-
"""
139-
property_sting = ",".join(
119+
def _build_options_string(**kwargs: Any) -> str:
120+
return ",".join(
140121
f"{_to_camel_case(key)}={_escape_property(value)}"
141122
for key, value in kwargs.items()
142-
if value
123+
if value is not None
143124
)
144-
_print_command(command, message, property_string=property_sting)
145125

146126

147127
def set_output(name: str, value: Any) -> None:
@@ -155,8 +135,7 @@ def set_output(name: str, value: Any) -> None:
155135
:param value: value of the output
156136
:returns: None
157137
"""
158-
property_string = f"name={_escape_property(name)}"
159-
_print_command("set-output", value, property_string=property_string)
138+
_print_command("set-output", value, options_string=_build_options_string(name=name))
160139

161140

162141
def echo(message: Any) -> None:
@@ -182,7 +161,10 @@ def debug(message: str) -> None:
182161
:param message: message string
183162
:returns: None
184163
"""
185-
_print_log_message("debug", message)
164+
_print_command(
165+
"debug",
166+
message,
167+
)
186168

187169

188170
def notice(
@@ -209,15 +191,17 @@ def notice(
209191
:param end_line: End line number
210192
:returns: None
211193
"""
212-
_print_log_message(
194+
_print_command(
213195
"notice",
214196
message,
215-
title=title,
216-
file=file,
217-
col=col,
218-
end_column=end_column,
219-
line=line,
220-
end_line=end_line,
197+
options_string=_build_options_string(
198+
title=title,
199+
file=file,
200+
col=col,
201+
end_column=end_column,
202+
line=line,
203+
end_line=end_line,
204+
),
221205
)
222206

223207

@@ -245,15 +229,17 @@ def warning(
245229
:param end_line: End line number
246230
:returns: None
247231
"""
248-
_print_log_message(
232+
_print_command(
249233
"warning",
250234
message,
251-
title=title,
252-
file=file,
253-
col=col,
254-
end_column=end_column,
255-
line=line,
256-
end_line=end_line,
235+
options_string=_build_options_string(
236+
title=title,
237+
file=file,
238+
col=col,
239+
end_column=end_column,
240+
line=line,
241+
end_line=end_line,
242+
),
257243
)
258244

259245

@@ -281,15 +267,17 @@ def error(
281267
:param end_line: End line number
282268
:returns: None
283269
"""
284-
_print_log_message(
270+
_print_command(
285271
"error",
286272
message,
287-
title=title,
288-
file=file,
289-
col=col,
290-
end_column=end_column,
291-
line=line,
292-
end_line=end_line,
273+
options_string=_build_options_string(
274+
title=title,
275+
file=file,
276+
col=col,
277+
end_column=end_column,
278+
line=line,
279+
end_line=end_line,
280+
),
293281
)
294282

295283

@@ -304,8 +292,7 @@ def save_state(name: str, value: Any) -> None:
304292
:param value: value of the state environment variable
305293
:returns: None
306294
"""
307-
property_string = f"name={_escape_property(name)}"
308-
_print_command("save-state", value, property_string=property_string)
295+
_print_command("save-state", value, options_string=_build_options_string(name=name))
309296

310297

311298
def get_state(name: str) -> Union[str, None]:
@@ -350,7 +337,7 @@ def end_group() -> None:
350337
351338
:returns: None
352339
"""
353-
_print_command("endgroup", "")
340+
print(f"{COMMAND_MARKER}endgroup{COMMAND_MARKER}")
354341

355342

356343
@contextmanager

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
coverage
22
pytest
3+
pytest-cov

tests/test_github_action_utils.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
@pytest.mark.parametrize(
1212
"test_input,expected",
1313
[
14-
(["endgroup", ""], "::endgroup::\n"),
1514
(
1615
["debug", "test debug", "test=1,test2=2"],
1716
"::debug test=1,test2=2::test debug\n",
@@ -97,10 +96,9 @@ def test__to_camel_case(test_input: str, expected: str) -> None:
9796

9897

9998
@pytest.mark.parametrize(
100-
"input_args,input_kwargs,expected",
99+
"input_kwargs,expected",
101100
[
102101
(
103-
["error", "test error"],
104102
{
105103
"title": "test \ntitle",
106104
"file": "abc.py",
@@ -109,21 +107,14 @@ def test__to_camel_case(test_input: str, expected: str) -> None:
109107
"line": 4,
110108
"end_line": 5,
111109
},
112-
"::error title=test %0Atitle,file=abc.py,col=1,endColumn=2,line=4,endLine=5::test error\n",
110+
"title=test %0Atitle,file=abc.py,col=1,endColumn=2,line=4,endLine=5",
113111
),
114-
(["error", "test debug"], {}, "::error ::test debug\n"),
115-
(["debug", "test debug"], {}, "::debug ::test debug\n"),
112+
({"name": "test-name"}, "name=test-name"),
113+
({}, ""),
116114
],
117115
)
118-
def test__print_log_message(
119-
capfd: Any,
120-
input_args: Any,
121-
input_kwargs: Any,
122-
expected: str,
123-
) -> None:
124-
gha_utils._print_log_message(*input_args, **input_kwargs)
125-
out, err = capfd.readouterr()
126-
assert out == expected
116+
def test__build_options_string(input_kwargs: Any, expected: str) -> None:
117+
assert gha_utils._build_options_string(**input_kwargs) == expected
127118

128119

129120
@pytest.mark.parametrize(

tox.ini

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
[tox]
2-
envlist = py37,py38,py39,py310,black-formatter,isort,mypy
2+
envlist = py36,py37,py38,py39,py310,black-formatter,isort,mypy
33

44
[testenv]
5-
commands = coverage run --source . -m pytest {posargs}
5+
commands = pytest --cov=. --cov-report xml {posargs}
66
deps = -rrequirements-dev.txt
77

88
[testenv:black-formatter]
9+
basepython = python3.10
910
commands = black --check --diff .
1011
deps = black
1112

1213
[testenv:isort]
14+
basepython = python3.10
1315
commands = isort --profile black --check-only --diff .
1416
deps = isort
1517

1618
[testenv:mypy]
19+
basepython = python3.10
1720
commands = mypy --strict --install-types --non-interactive github_action_utils.py
1821
deps = mypy

0 commit comments

Comments
 (0)