Skip to content

Commit 883c3be

Browse files
authored
Refactor devcontainer setup and enhance content fetching with template rendering (#54)
Refactor the devcontainer configuration for improved compatibility and streamline the content fetching process by integrating template rendering. Update the installation commands for Python packages and add tests to ensure the new rendering functionality works as expected.
1 parent d3a00c4 commit 883c3be

4 files changed

Lines changed: 57 additions & 16 deletions

File tree

.devcontainer/devcontainer.json

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
{
2-
"name": "Struct devcontainer",
3-
"image": "mcr.microsoft.com/devcontainers/python:1-3.12-bullseye",
4-
"features": {
5-
"ghcr.io/devcontainers/features/python:1": {
6-
"version": "3.12"
7-
},
2+
"name": "Struct devcontainer",
3+
"image": "mcr.microsoft.com/devcontainers/python:3",
4+
"features": {
5+
"ghcr.io/devcontainers/features/python:1": {},
86
"ghcr.io/gvatsal60/dev-container-features/pre-commit": {},
9-
"ghcr.io/eitsupi/devcontainer-features/go-task:latest": {},
10-
"ghcr.io/devcontainers-extra/features/shfmt:1" : {}
11-
},
12-
"postCreateCommand": "bash ./scripts/devcontainer_start.sh",
13-
"customizations": {
7+
"ghcr.io/eitsupi/devcontainer-features/go-task:latest": {}
8+
},
9+
"postCreateCommand": "bash ./scripts/devcontainer_start.sh",
10+
"customizations": {
1411
"vscode": {
1512
"extensions": [
1613
"ms-python.python",

scripts/devcontainer_start.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
pip3.12 install -r requirements.txt
2-
pip3.12 install -r requirements.dev.txt
3-
pip3.12 install -e .
1+
pip install -r requirements.txt
2+
pip install -r requirements.dev.txt
3+
pip install -e .

struct_module/file_item.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,18 @@ def fetch_content(self):
100100
if self.content_location:
101101
self.logger.debug(f"Fetching content from: {self.content_location}")
102102
try:
103-
self.content = self.content_fetcher.fetch_content(self.content_location)
104-
self.logger.debug(f"Fetched content: {self.content}")
103+
raw_content = self.content_fetcher.fetch_content(
104+
self.content_location)
105+
self.logger.debug(f"Fetched content: {raw_content}")
106+
# Render the fetched content using the template renderer
107+
template_vars = self._merge_default_template_vars(
108+
self.config_variables)
109+
missing_vars = self.template_renderer.prompt_for_missing_vars(
110+
raw_content, template_vars)
111+
template_vars.update(missing_vars)
112+
self.content = self.template_renderer.render_template(
113+
raw_content, template_vars)
114+
self.logger.debug(f"Rendered content: {self.content}")
105115
except Exception as e:
106116
self.logger.error(f"❗ Failed to fetch content from {self.content_location}: {e}")
107117

tests/test_file_item.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,37 @@ def test_fetch_content(file_item):
2222
mock_fetch.return_value = "fetched content"
2323
file_item.fetch_content()
2424
assert file_item.content == "file content"
25+
26+
27+
def test_fetch_content_renders_template(monkeypatch):
28+
properties = {
29+
"name": "test.txt",
30+
"file": "/fake/path.txt",
31+
"config_variables": [],
32+
"input_store": "/tmp/input.json"
33+
}
34+
file_item = FileItem(properties)
35+
36+
# Mock fetch_content to return a template string
37+
monkeypatch.setattr(
38+
file_item.content_fetcher,
39+
"fetch_content",
40+
lambda location: "Hello, {{@ name @}}!"
41+
)
42+
# Mock template renderer methods
43+
rendered = {}
44+
45+
def fake_prompt_for_missing_vars(content, vars):
46+
return {"name": "World"}
47+
48+
def fake_render_template(content, vars):
49+
rendered["content"] = content
50+
rendered["vars"] = vars
51+
return "Hello, World!"
52+
file_item.template_renderer.prompt_for_missing_vars = fake_prompt_for_missing_vars
53+
file_item.template_renderer.render_template = fake_render_template
54+
55+
file_item.fetch_content()
56+
assert file_item.content == "Hello, World!"
57+
assert rendered["content"] == "Hello, {{@ name @}}!"
58+
assert rendered["vars"]["name"] == "World"

0 commit comments

Comments
 (0)