Skip to content

Commit 6895258

Browse files
yt-msMidnighter
authored andcommitted
feat(Client): suppress archiving if location is set to None
1 parent c9b7eb7 commit 6895258

File tree

5 files changed

+35
-11
lines changed

5 files changed

+35
-11
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ History
55
Next Release
66
------------
77
* Fix: Better error on bad ModelItem constructor argument (#50)
8+
* Fix: Suppress archiving of downloaded workspaces by setting archive location to None (#54)
89

910
0.2.1 (2020-11-27)
1011
------------------

src/structurizr/api/structurizr_client.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class StructurizrClient:
5555
user (str): A string identifying the user (e.g. an e-mail address or username).
5656
agent (str): A string identifying the agent (e.g. 'structurizr-java/1.2.0').
5757
workspace_archive_location (pathlib.Path): A directory for archiving downloaded
58-
workspaces.
58+
workspaces, or None to suppress archiving.
5959
6060
"""
6161

@@ -276,13 +276,14 @@ def _add_headers(
276276

277277
def _archive_workspace(self, json: str) -> None:
278278
"""Store the workspace."""
279-
location = self._create_archive_filename()
280-
logger.debug(
281-
f"Archiving workspace {self.workspace_id} to"
282-
f" '{self.workspace_archive_location}'."
283-
)
284-
with gzip.open(location, mode="wt") as handle:
285-
handle.write(json)
279+
if self.workspace_archive_location is not None:
280+
location = self._create_archive_filename()
281+
logger.debug(
282+
f"Archiving workspace {self.workspace_id} to"
283+
f" '{self.workspace_archive_location}'."
284+
)
285+
with gzip.open(location, mode="wt") as handle:
286+
handle.write(json)
286287

287288
def _create_archive_filename(self) -> Path:
288289
"""Generate a filename for a workspace archive."""

src/structurizr/api/structurizr_client_settings.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from getpass import getuser
2121
from pathlib import Path
2222
from socket import getfqdn
23+
from typing import Optional
2324

2425

2526
try:
@@ -98,10 +99,11 @@ class StructurizrClientSettings(BaseSettings):
9899
env="STRUCTURIZR_AGENT",
99100
description="A string identifying the agent (e.g. 'structurizr-java/1.2.0').",
100101
)
101-
workspace_archive_location: DirectoryPath = Field(
102+
workspace_archive_location: Optional[DirectoryPath] = Field(
102103
default=Path.cwd(),
103104
env="STRUCTURIZR_WORKSPACE_ARCHIVE_LOCATION",
104-
description="A directory for archiving downloaded workspaces.",
105+
description="A directory for archiving downloaded workspaces, or None to "
106+
"suppress archiving.",
105107
)
106108

107109
class Config:

tests/unit/api/test_structurizr_client.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ def test_archive_workspace(client, mocker):
117117
mocked_handle.write.assert_called_once_with('{"mock_key":"mock_value"}')
118118

119119

120+
def test_suppressing_archive(mock_settings, mocker):
121+
"""Test that when the archive location is None then no archive is written."""
122+
old_settings = mock_settings._asdict()
123+
old_settings["workspace_archive_location"] = None
124+
new_mock_settings = MockSettings(**old_settings)
125+
client = StructurizrClient(settings=new_mock_settings)
126+
127+
mocked_open = mocker.mock_open(mock=mocker.Mock(spec_set=GzipFile))
128+
mocker.patch("gzip.open", mocked_open)
129+
130+
client._archive_workspace('{"mock_key":"mock_value"}')
131+
assert not mocked_open.called
132+
133+
120134
def test_httpx_response_raw_path_behaviour():
121135
"""Make sure that `Response.raw_path` continues to do what we need.
122136

tests/unit/api/test_structurizr_client_settings.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ def dotenv(tmpdir, monkeypatch, archive_location):
137137
"api_secret": "ae140655-da7c-4a8d-9467-5a7d9792fca0",
138138
"workspace_archive_location": ".",
139139
},
140+
{
141+
"workspace_id": 19,
142+
"api_key": "7f4e4edc-f61c-4ff2-97c9-ea4bc2a7c98c",
143+
"api_secret": "ae140655-da7c-4a8d-9467-5a7d9792fca0",
144+
"workspace_archive_location": None,
145+
},
140146
],
141147
)
142148
def test_init_from_arguments(attributes: dict):
@@ -145,7 +151,7 @@ def test_init_from_arguments(attributes: dict):
145151
for attr, expected in attributes.items():
146152
value = getattr(settings, attr)
147153
if attr in ("api_key", "api_secret", "workspace_archive_location"):
148-
value = str(value)
154+
value = None if value is None else str(value)
149155
assert value == expected
150156

151157

0 commit comments

Comments
 (0)