Skip to content

Commit b3d4e24

Browse files
committed
update tests
distutils has been deprecated for years; use shutil instead add unit test for `get_previous_comment()`
1 parent 006736a commit b3d4e24

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[
2+
{
3+
"url": "https://api.github.com/repos/test-user/test-repo/issues/comments/1953679626",
4+
"body": "**Memory usage change @ 525def8e855e5f227a4b4a0b6f84c34cd288a5ea**\n\nBoard|flash|%|RAM for global variables|%\n-|-|-|-|-\n`arduino:avr:nano`|0 - 0|0.0 - 0.0|0 - 0|0.0 - 0.0\n`arduino:samd:mkrzero`|0 - 0|0.0 - 0.0|0 - 0|0.0 - 0.0\n\n<details>\n<summary>Click for full report table</summary>\n\nBoard|`examples/Getting_Started_SimpleClient_Mesh`<br>flash|%|`examples/Getting_Started_SimpleClient_Mesh`<br>RAM for global variables|%|`examples/Getting_Started_SimpleServer_Mesh`<br>flash|%|`examples/Getting_Started_SimpleServer_Mesh`<br>RAM for global variables|%|`examples/InteractiveServer_Mesh`<br>flash|%|`examples/InteractiveServer_Mesh`<br>RAM for global variables|%|`examples/MQTT/mqtt_basic`<br>flash|%|`examples/MQTT/mqtt_basic`<br>RAM for global variables|%|`examples/MQTT/mqtt_basic_2`<br>flash|%|`examples/MQTT/mqtt_basic_2`<br>RAM for global variables|%|`examples/SimpleClient_Mesh`<br>flash|%|`examples/SimpleClient_Mesh`<br>RAM for global variables|%\n-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-\n`arduino:avr:nano`|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0\n`arduino:samd:mkrzero`|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0\n\n</details>\n\n<details>\n<summary>Click for full report CSV</summary>\n\n```\nBoard,examples/Getting_Started_SimpleClient_Mesh<br>flash,%,examples/Getting_Started_SimpleClient_Mesh<br>RAM for global variables,%,examples/Getting_Started_SimpleServer_Mesh<br>flash,%,examples/Getting_Started_SimpleServer_Mesh<br>RAM for global variables,%,examples/InteractiveServer_Mesh<br>flash,%,examples/InteractiveServer_Mesh<br>RAM for global variables,%,examples/MQTT/mqtt_basic<br>flash,%,examples/MQTT/mqtt_basic<br>RAM for global variables,%,examples/MQTT/mqtt_basic_2<br>flash,%,examples/MQTT/mqtt_basic_2<br>RAM for global variables,%,examples/SimpleClient_Mesh<br>flash,%,examples/SimpleClient_Mesh<br>RAM for global variables,%\narduino:avr:nano,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0\narduino:samd:mkrzero,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0\n```\n</details>"
5+
},
6+
{
7+
"url": "https://api.github.com/repos/test-user/test-repo/issues/comments/1953996601",
8+
"body": "**Memory usage change @ 862a917fe24c6f737abc336b681b7326c0fcceec**\n\nBoard|flash|%|RAM for global variables|%\n-|-|-|-|-\n`arduino:avr:nano`|0 - 0|0.0 - 0.0|0 - 0|0.0 - 0.0\n`arduino:samd:mkrzero`|0 - 0|0.0 - 0.0|0 - 0|0.0 - 0.0\n\n<details>\n<summary>Click for full report table</summary>\n\nBoard|`examples/Getting_Started_SimpleClient_Mesh`<br>flash|%|`examples/Getting_Started_SimpleClient_Mesh`<br>RAM for global variables|%|`examples/Getting_Started_SimpleServer_Mesh`<br>flash|%|`examples/Getting_Started_SimpleServer_Mesh`<br>RAM for global variables|%|`examples/InteractiveServer_Mesh`<br>flash|%|`examples/InteractiveServer_Mesh`<br>RAM for global variables|%|`examples/MQTT/mqtt_basic`<br>flash|%|`examples/MQTT/mqtt_basic`<br>RAM for global variables|%|`examples/MQTT/mqtt_basic_2`<br>flash|%|`examples/MQTT/mqtt_basic_2`<br>RAM for global variables|%|`examples/SimpleClient_Mesh`<br>flash|%|`examples/SimpleClient_Mesh`<br>RAM for global variables|%\n-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-\n`arduino:avr:nano`|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0\n`arduino:samd:mkrzero`|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0\n\n</details>\n\n<details>\n<summary>Click for full report CSV</summary>\n\n```\nBoard,examples/Getting_Started_SimpleClient_Mesh<br>flash,%,examples/Getting_Started_SimpleClient_Mesh<br>RAM for global variables,%,examples/Getting_Started_SimpleServer_Mesh<br>flash,%,examples/Getting_Started_SimpleServer_Mesh<br>RAM for global variables,%,examples/InteractiveServer_Mesh<br>flash,%,examples/InteractiveServer_Mesh<br>RAM for global variables,%,examples/MQTT/mqtt_basic<br>flash,%,examples/MQTT/mqtt_basic<br>RAM for global variables,%,examples/MQTT/mqtt_basic_2<br>flash,%,examples/MQTT/mqtt_basic_2<br>RAM for global variables,%,examples/SimpleClient_Mesh<br>flash,%,examples/SimpleClient_Mesh<br>RAM for global variables,%\narduino:avr:nano,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0\narduino:samd:mkrzero,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0\n```\n</details>"
9+
},
10+
{
11+
"url": "https://api.github.com/repos/test-user/test-repo/issues/comments/1954284300",
12+
"body": "**Memory usage change @ 0098017901c516c6cd49e248f1aabd6b30c91aa9**\n\nBoard|flash|%|RAM for global variables|%\n-|-|-|-|-\n`arduino:avr:nano`|0 - 0|0.0 - 0.0|0 - 0|0.0 - 0.0\n`arduino:samd:mkrzero`|0 - 0|0.0 - 0.0|0 - 0|0.0 - 0.0\n\n<details>\n<summary>Click for full report table</summary>\n\nBoard|`examples/Getting_Started_SimpleClient_Mesh`<br>flash|%|`examples/Getting_Started_SimpleClient_Mesh`<br>RAM for global variables|%|`examples/Getting_Started_SimpleServer_Mesh`<br>flash|%|`examples/Getting_Started_SimpleServer_Mesh`<br>RAM for global variables|%|`examples/InteractiveServer_Mesh`<br>flash|%|`examples/InteractiveServer_Mesh`<br>RAM for global variables|%|`examples/MQTT/mqtt_basic`<br>flash|%|`examples/MQTT/mqtt_basic`<br>RAM for global variables|%|`examples/MQTT/mqtt_basic_2`<br>flash|%|`examples/MQTT/mqtt_basic_2`<br>RAM for global variables|%|`examples/SimpleClient_Mesh`<br>flash|%|`examples/SimpleClient_Mesh`<br>RAM for global variables|%\n-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-\n`arduino:avr:nano`|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0\n`arduino:samd:mkrzero`|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0\n\n</details>\n\n<details>\n<summary>Click for full report CSV</summary>\n\n```\nBoard,examples/Getting_Started_SimpleClient_Mesh<br>flash,%,examples/Getting_Started_SimpleClient_Mesh<br>RAM for global variables,%,examples/Getting_Started_SimpleServer_Mesh<br>flash,%,examples/Getting_Started_SimpleServer_Mesh<br>RAM for global variables,%,examples/InteractiveServer_Mesh<br>flash,%,examples/InteractiveServer_Mesh<br>RAM for global variables,%,examples/MQTT/mqtt_basic<br>flash,%,examples/MQTT/mqtt_basic<br>RAM for global variables,%,examples/MQTT/mqtt_basic_2<br>flash,%,examples/MQTT/mqtt_basic_2<br>RAM for global variables,%,examples/SimpleClient_Mesh<br>flash,%,examples/SimpleClient_Mesh<br>RAM for global variables,%\narduino:avr:nano,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0\narduino:samd:mkrzero,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0\n```\n</details>"
13+
},
14+
{
15+
"url": "https://api.github.com/repos/test-user/test-repo/issues/comments/1955441026",
16+
"body": "**Memory usage change @ f9881f578cf28800a2076ae709434249e9cb9d67**\n\nBoard|flash|%|RAM for global variables|%\n-|-|-|-|-\n`arduino:avr:nano`|0 - 0|0.0 - 0.0|0 - 0|0.0 - 0.0\n`arduino:samd:mkrzero`|0 - 0|0.0 - 0.0|0 - 0|0.0 - 0.0\n\n<details>\n<summary>Click for full report table</summary>\n\nBoard|`examples/Getting_Started_SimpleClient_Mesh`<br>flash|%|`examples/Getting_Started_SimpleClient_Mesh`<br>RAM for global variables|%|`examples/Getting_Started_SimpleServer_Mesh`<br>flash|%|`examples/Getting_Started_SimpleServer_Mesh`<br>RAM for global variables|%|`examples/InteractiveServer_Mesh`<br>flash|%|`examples/InteractiveServer_Mesh`<br>RAM for global variables|%|`examples/MQTT/mqtt_basic`<br>flash|%|`examples/MQTT/mqtt_basic`<br>RAM for global variables|%|`examples/MQTT/mqtt_basic_2`<br>flash|%|`examples/MQTT/mqtt_basic_2`<br>RAM for global variables|%|`examples/SimpleClient_Mesh`<br>flash|%|`examples/SimpleClient_Mesh`<br>RAM for global variables|%\n-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-\n`arduino:avr:nano`|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0\n`arduino:samd:mkrzero`|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0\n\n</details>\n\n<details>\n<summary>Click for full report CSV</summary>\n\n```\nBoard,examples/Getting_Started_SimpleClient_Mesh<br>flash,%,examples/Getting_Started_SimpleClient_Mesh<br>RAM for global variables,%,examples/Getting_Started_SimpleServer_Mesh<br>flash,%,examples/Getting_Started_SimpleServer_Mesh<br>RAM for global variables,%,examples/InteractiveServer_Mesh<br>flash,%,examples/InteractiveServer_Mesh<br>RAM for global variables,%,examples/MQTT/mqtt_basic<br>flash,%,examples/MQTT/mqtt_basic<br>RAM for global variables,%,examples/MQTT/mqtt_basic_2<br>flash,%,examples/MQTT/mqtt_basic_2<br>RAM for global variables,%,examples/SimpleClient_Mesh<br>flash,%,examples/SimpleClient_Mesh<br>RAM for global variables,%\narduino:avr:nano,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0\narduino:samd:mkrzero,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0\n```\n</details>"
17+
},
18+
{
19+
"url": "https://api.github.com/repos/test-user/test-repo/issues/comments/1955465432",
20+
"body": "NOT A BOT COMMENT"
21+
}
22+
]

reportsizedeltas/tests/test_reportsizedeltas.py

+39-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import distutils.dir_util
1+
import shutil
22
import filecmp
33
import json
44
import os
@@ -526,8 +526,8 @@ def test_get_sketches_reports(test_data_folder_name):
526526

527527
artifacts_folder_object = tempfile.TemporaryDirectory(prefix="test_reportsizedeltas-")
528528
try:
529-
distutils.dir_util.copy_tree(
530-
src=str(current_test_data_path.joinpath("artifacts")), dst=artifacts_folder_object.name
529+
shutil.copytree(
530+
src=str(current_test_data_path.joinpath("artifacts")), dst=artifacts_folder_object.name, dirs_exist_ok=True
531531
)
532532
except Exception: # pragma: no cover
533533
artifacts_folder_object.cleanup()
@@ -730,7 +730,7 @@ def test_generate_report():
730730

731731
artifacts_folder_object = tempfile.TemporaryDirectory(prefix="test_reportsizedeltas-")
732732
try:
733-
distutils.dir_util.copy_tree(src=str(sketches_report_path), dst=artifacts_folder_object.name)
733+
shutil.copytree(src=str(sketches_report_path), dst=artifacts_folder_object.name, dirs_exist_ok=True)
734734
except Exception: # pragma: no cover
735735
artifacts_folder_object.cleanup()
736736
raise
@@ -768,6 +768,7 @@ def test_comment_report():
768768
report_size_deltas = get_reportsizedeltas_object(repository_name=repository_name)
769769

770770
report_size_deltas.http_request = unittest.mock.MagicMock()
771+
report_size_deltas.get_previous_comment = unittest.mock.Mock(return_value=None)
771772

772773
report_size_deltas.comment_report(pr_number=pr_number, report_markdown=report_markdown)
773774

@@ -778,9 +779,41 @@ def test_comment_report():
778779
report_size_deltas.http_request.assert_called_once_with(
779780
url="https://api.github.com/repos/" + repository_name + "/issues/" + str(pr_number) + "/comments",
780781
data=report_data,
782+
method=None,
781783
)
782784

783785

786+
def test_get_previous_comment():
787+
pr_number = 42
788+
repository_name = "test_user/test_repo"
789+
url = f"https://api.github.com/repos/{repository_name}/issues/{pr_number}"
790+
791+
report_size_deltas = get_reportsizedeltas_object(repository_name=repository_name)
792+
793+
json_comments = json.loads((test_data_path / "test_get_previous_comment" / "comments.json").read_bytes())
794+
795+
def side_effect(url: str):
796+
ret_val = {"page": 1}
797+
if url.endswith("/comments"):
798+
return {"json_data": json_comments, **ret_val}
799+
return {"json_data": {"comments": len(json_comments)}, **ret_val}
800+
801+
report_size_deltas.http_request = unittest.mock.Mock()
802+
report_size_deltas.get_json_response = unittest.mock.Mock(side_effect=side_effect)
803+
804+
comment_url = report_size_deltas.get_previous_comment(url=url)
805+
assert comment_url == json_comments[3]["url"]
806+
807+
for comment in json_comments[:3]:
808+
if comment["body"].startswith(report_size_deltas.report_key_beginning):
809+
report_size_deltas.http_request.assert_any_call(url=comment["url"], method="DELETE")
810+
811+
# It would be nicer to assert that a call has not been made.
812+
# Here, we just assert that the first 3 out of 4 bot comments were deleted.
813+
# Implicitly, this also means the non-bot comment (`json_comment[4]`) was not deleted.
814+
assert report_size_deltas.http_request.call_count == 3
815+
816+
784817
def test_api_request():
785818
response_data = {"json_data": {"foo": "bar"}, "additional_pages": False, "page_count": 1}
786819
request = "test_request"
@@ -868,7 +901,7 @@ def test_http_request():
868901

869902
report_size_deltas.http_request(url=url, data=data)
870903

871-
report_size_deltas.raw_http_request.assert_called_once_with(url=url, data=data)
904+
report_size_deltas.raw_http_request.assert_called_once_with(url=url, data=data, method=None)
872905

873906

874907
def test_raw_http_request(mocker):
@@ -896,6 +929,7 @@ def add_unredirected_header(self):
896929
urllib.request.Request.assert_called_once_with(
897930
url=url,
898931
data=data,
932+
method=None,
899933
)
900934
request.add_unredirected_header.assert_has_calls(
901935
calls=[

0 commit comments

Comments
 (0)