Skip to content

Commit 7a9577f

Browse files
fix: empty tables (#15)
* fix: empty tables * test: add unit test
1 parent aa97cbf commit 7a9577f

File tree

2 files changed

+102
-7
lines changed

2 files changed

+102
-7
lines changed

writer/doc_writer.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def write(output: str, doc: dict, template: str) -> str:
4747
{{ stella.values }}
4848
4949
*Automatic helm documentation generated using [very-doge-wow/stella](https://github.com/very-doge-wow/stella).*
50-
"""
50+
"""
5151

5252
keywords = [
5353
"{{ stella.name }}",
@@ -64,20 +64,23 @@ def write(output: str, doc: dict, template: str) -> str:
6464

6565
# transform dicts to md tables
6666
translated = doc
67-
for key in doc:
68-
if type(doc[key]) == list:
67+
for key in translated:
68+
if type(translated[key]) == list:
6969
logging.debug(f"converting list of dicts {key} to md")
7070

71-
if len(doc[key]) > 0:
71+
if len(translated[key]) > 0:
7272
md = translate_list_of_dicts_to_md(doc[key])
7373
translated[key] = md
74+
else:
75+
md = f"*No {get_name_from_keyword(key)} found.*"
76+
translated[key] = md
7477

7578
result = ""
7679
for line in template_content.split("\n"):
7780
for keyword in keywords:
7881
if keyword in line:
7982
line = line.replace(keyword, str(
80-
doc[keyword.replace("{", "").replace("}", "").strip().replace("stella.", "")]))
83+
translated[get_name_from_keyword(keyword)]))
8184
result += line + "\n"
8285

8386
logging.debug("writing output to file")
@@ -128,3 +131,8 @@ def translate_list_of_dicts_to_md(list_of_dicts: list) -> str:
128131
md += f"| {value.rstrip()} "
129132
md += "|\n"
130133
return md
134+
135+
136+
def get_name_from_keyword(keyword: str) -> str:
137+
result = keyword.replace("{", "").replace("}", "").strip().replace("stella.", "")
138+
return result

writer/doc_writer_test.py

+89-2
Original file line numberDiff line numberDiff line change
@@ -286,5 +286,92 @@ def test_writer_keywords_default_template():
286286
287287
288288
*Automatic helm documentation generated using [very-doge-wow/stella](https://github.com/very-doge-wow/stella).*
289-
290-
"""
289+
290+
"""
291+
292+
293+
def test_writer_empty():
294+
doc = {
295+
"type": "application",
296+
"version": "1.0",
297+
"appVersion": "1.1",
298+
"apiVersion": "1.2",
299+
"name": "unittest",
300+
"description": "simple templating test",
301+
"dependencies": [],
302+
"templates": [],
303+
"objects": [
304+
{
305+
"kind": "Ingress",
306+
"from Template": "template.yaml"
307+
}
308+
],
309+
"values": [
310+
{
311+
"name": "ReplicaCount",
312+
"description": "how many replicas to deploy",
313+
"default": "1",
314+
"example": "replicaCount: 2"
315+
}
316+
]
317+
}
318+
319+
result = doc_writer.write("test/output.md", doc, "")
320+
assert result == """
321+
# unittest
322+
![Version: 1.0](https://img.shields.io/badge/Version-1.0-informational?style=flat-square) ![Version: 1.1](https://img.shields.io/badge/appVersion-1.1-informational?style=flat-square) ![Version: 1.2](https://img.shields.io/badge/apiVersion-1.2-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square)
323+
324+
## Description
325+
simple templating test
326+
327+
## Dependencies
328+
This chart depends on the following subcharts.
329+
330+
*No dependencies found.*
331+
332+
## Templates
333+
The following templates will be deployed.
334+
335+
*No templates found.*
336+
337+
### Objects
338+
The aforementioned templates will deploy the following objects.
339+
340+
| Kind | From template |
341+
|---|---|
342+
| Ingress | template.yaml |
343+
344+
345+
## Values
346+
The following values can/will be used for deployments.
347+
348+
| Name | Description | Default | Example |
349+
|---|---|---|---|
350+
| ReplicaCount | how many replicas to deploy | <pre>1</pre> | <pre>replicaCount: 2</pre> |
351+
352+
353+
*Automatic helm documentation generated using [very-doge-wow/stella](https://github.com/very-doge-wow/stella).*
354+
355+
"""
356+
357+
358+
def test_get_name_from_keyword():
359+
keywords = [
360+
"{{ stella.lol }}",
361+
"{{ stella.banana }}",
362+
"",
363+
"{{stella.rofl}}",
364+
"{{pear}}"
365+
]
366+
367+
results = [
368+
"lol",
369+
"banana",
370+
"",
371+
"rofl",
372+
"pear"
373+
]
374+
375+
for index, keyword in enumerate(keywords):
376+
result = doc_writer.get_name_from_keyword(keyword)
377+
assert results[index] == result

0 commit comments

Comments
 (0)