Skip to content

Commit b82cc5e

Browse files
authored
[BugFix] Get snap info from environ (#34)
Signed-off-by: Douglas Chiang <[email protected]>
1 parent bef7f2e commit b82cc5e

File tree

4 files changed

+27
-149
lines changed

4 files changed

+27
-149
lines changed

yarf/output/__init__.py

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import json
44
import logging
55
import os
6-
import subprocess
76
import tempfile
87
from pathlib import Path
98
from typing import Any, Callable, Optional
@@ -179,7 +178,6 @@ def get_yarf_snap_info() -> dict[str, str] | None:
179178
date.
180179
181180
Raises:
182-
RuntimeError: When the subprocess failed to run
183181
ValueError: When match installed YARF information cannot match with YARF snap info
184182
185183
Returns:
@@ -189,58 +187,15 @@ def get_yarf_snap_info() -> dict[str, str] | None:
189187
return None
190188

191189
try:
192-
# Run `snap info` command
193-
result = subprocess.run(
194-
["snap", "info", "yarf"],
195-
capture_output=True,
196-
text=True,
197-
check=True,
198-
)
199-
output = result.stdout
200-
except subprocess.CalledProcessError as e:
201-
raise RuntimeError("Subprocess execution failed.") from e
202-
203-
# Get the installed snap info
204-
installed_yarf_info = {}
205-
channels = {}
206-
for line in output.splitlines():
207-
if line.strip().startswith("name:"):
208-
parts = line.split()
209-
installed_yarf_info["name"] = parts[1]
210-
211-
elif line.strip().startswith("latest/"):
212-
parts = line.split()
213-
channel = parts[0].rstrip(":")
214-
version = parts[1]
215-
date = parts[2] if len(parts) > 2 else "Unknown"
216-
revision = parts[3] if len(parts) > 3 else "Unknown"
217-
channels[channel] = {
218-
"channel": channel,
219-
"version": version,
220-
"revision": revision.strip("()"),
221-
"date": date,
222-
}
223-
224-
elif line.strip().startswith("installed:"):
225-
parts = line.split()
226-
installed_yarf_info["version"] = parts[1]
227-
installed_yarf_info["revision"] = parts[2].strip("()")
228-
229-
elif line.strip().startswith("tracking:"):
230-
parts = line.split()
231-
installed_yarf_info["channel"] = parts[1]
232-
233-
for channel, info in channels.items():
234-
if (
235-
info["channel"] == installed_yarf_info["channel"]
236-
and info["revision"] == installed_yarf_info["revision"]
237-
and info["version"] == installed_yarf_info["version"]
238-
):
239-
return channels[channel] | installed_yarf_info
240-
241-
raise ValueError(
242-
"Cannot match installed YARF information with YARF snap info."
243-
)
190+
yarf_snap_info = {
191+
"name": os.environ["SNAP_NAME"],
192+
"version": os.environ["SNAP_VERSION"],
193+
"revision": os.environ["SNAP_REVISION"],
194+
}
195+
except KeyError as exc:
196+
raise ValueError("Cannot get installed YARF information.") from exc
197+
198+
return yarf_snap_info # type: ignore[return-value]
244199

245200

246201
def import_supported_formats() -> None:

yarf/output/test_submission_schema.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ def get_origin(self) -> dict[str, Any]:
158158
"name": current_yarf_info["name"],
159159
"version": current_yarf_info["version"],
160160
"revision": current_yarf_info["revision"],
161-
"date": current_yarf_info["date"],
162161
}
163162
else:
164163
origin["version"] = metadata.version("yarf")

yarf/output/tests/test_init.py

Lines changed: 18 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import os
2-
import subprocess
32
from pathlib import Path
4-
from textwrap import dedent
53
from typing import Any
64
from unittest.mock import ANY, MagicMock, Mock, call, patch, sentinel
75

@@ -55,110 +53,39 @@ class TestModule(OutputConverterBase):
5553
with pytest.raises(TypeError):
5654
TestModule()
5755

58-
@patch("yarf.output.subprocess.run")
59-
def test_get_yarf_snap_info(
60-
self,
61-
mock_subprocess_run: MagicMock,
62-
) -> None:
56+
def test_get_yarf_snap_info(self) -> None:
6357
"""
6458
Test whether the "get_yarf_snap_info" method is callable and return
6559
results with expected fields.
6660
"""
67-
mock_subprocess_run.return_value.stdout = dedent(
68-
"""
69-
name: yarf
70-
summary: Yet Another Robot Framework
71-
publisher: Canonical Certification Team (ce-certification-qa)
72-
store-url: https://snapcraft.io/yarf
73-
license: unset
74-
description: |
75-
Yet Another Robot Framework (YARF) is an interface that
76-
allows developers to build complex test scenarios and
77-
bootstrap them locally, then work towards automated runs.
78-
commands:
79-
- yarf
80-
snap-id: zIV9E2VxqRgGhIuttHs8YkCyWGjIOiRm
81-
tracking: latest/beta
82-
refresh-date: 3 days ago, at 16:40 GMT
83-
channels:
84-
latest/stable: –
85-
latest/candidate: –
86-
latest/beta: 1.0.0 2024-12-03 (124) 206MB -
87-
latest/edge: 1.0.0 2024-12-03 (124) 206MB -
88-
installed: 1.0.0 (124) 206MB -
89-
"""
90-
)
91-
9261
expected_result = {
93-
"channel": "latest/beta",
94-
"version": "1.0.0",
95-
"revision": "124",
96-
"date": "2024-12-03",
62+
"version": "2.0.0",
63+
"revision": "300",
9764
"name": "yarf",
9865
}
99-
with patch.dict(os.environ, {"SNAP": str(sentinel.snap_env)}):
66+
with patch.dict(
67+
os.environ,
68+
{
69+
"SNAP": str(sentinel.snap_env),
70+
"SNAP_NAME": "yarf",
71+
"SNAP_VERSION": "2.0.0",
72+
"SNAP_REVISION": "300",
73+
},
74+
):
10075
result = OutputConverterBase.get_yarf_snap_info()
10176
assert expected_result == result
10277

103-
@patch("yarf.output.subprocess.run")
104-
def test_get_yarf_snap_info_runtime_error(
105-
self,
106-
mock_subprocess_run: MagicMock,
107-
) -> None:
108-
"""
109-
Test whether the function get_yarf_snap_info raises runtime error when
110-
subprocess.run raises a CalledProcessError exception.
111-
"""
112-
113-
mock_subprocess_run.side_effect = subprocess.CalledProcessError(
114-
returncode=1,
115-
cmd=["snap", "info", "yarf"],
116-
output="Error: Could not find snap 'yarf'.",
117-
)
118-
119-
with (
120-
patch.dict(os.environ, {"SNAP": str(sentinel.snap_env)}),
121-
pytest.raises(RuntimeError),
122-
):
123-
OutputConverterBase.get_yarf_snap_info()
124-
125-
@patch("yarf.output.subprocess.run")
126-
def test_get_yarf_snap_info_value_error(
127-
self,
128-
mock_subprocess_run: MagicMock,
129-
) -> None:
78+
def test_get_yarf_snap_info_value_error(self) -> None:
13079
"""
13180
Test whether the function get_yarf_snap_info raises value error when
13281
there is a mismatch between the installed YARF and the snap info.
13382
"""
134-
135-
mock_subprocess_run.return_value.stdout = dedent(
136-
"""
137-
name: yarf
138-
summary: Yet Another Robot Framework
139-
publisher: Canonical Certification Team (ce-certification-qa)
140-
store-url: https://snapcraft.io/yarf
141-
license: unset
142-
description: |
143-
Yet Another Robot Framework (YARF) is an interface that
144-
allows developers to build complex test scenarios and
145-
bootstrap them locally, then work towards automated runs.
146-
commands:
147-
- yarf
148-
snap-id: zIV9E2VxqRgGhIuttHs8YkCyWGjIOiRm
149-
tracking: latest/beta
150-
refresh-date: 3 days ago, at 16:40 GMT
151-
channels:
152-
latest/stable: –
153-
latest/candidate: –
154-
latest/beta: 1.0.0 2024-12-03 (124) 206MB -
155-
latest/edge: 1.0.0 2024-12-03 (124) 206MB -
156-
installed: 9.9.9 (999) 206MB -
157-
"""
158-
)
159-
16083
with (
161-
patch.dict(os.environ, {"SNAP": str(sentinel.snap_env)}),
84+
patch.dict(
85+
os.environ,
86+
{"SNAP": str(sentinel.snap_env), "SNAP_NAME": "yarf"},
87+
clear=True,
88+
),
16289
pytest.raises(ValueError),
16390
):
16491
OutputConverterBase.get_yarf_snap_info()

yarf/output/tests/test_test_submission_schema.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,8 @@ def test_get_origin_has_snap(self) -> None:
297297
converter = TestSubmissionSchema()
298298
converter.get_yarf_snap_info = Mock(
299299
return_value={
300-
"channel": "latest/beta",
301300
"version": "1.0.0",
302301
"revision": "124",
303-
"date": "2024-12-03",
304302
"name": "yarf",
305303
}
306304
)
@@ -312,7 +310,6 @@ def test_get_origin_has_snap(self) -> None:
312310
"name": "yarf",
313311
"version": "1.0.0",
314312
"revision": "124",
315-
"date": "2024-12-03",
316313
},
317314
}
318315

0 commit comments

Comments
 (0)