Skip to content

Commit

Permalink
feat: extend multiple templates validation to trestle author folders (#…
Browse files Browse the repository at this point in the history
…1430)

* feat: extend multiple templates validation to trestle author folders command

Signed-off-by: Alejandro Jose Leiva Palomo <[email protected]>

* docs: adding documentation to ne feature

Signed-off-by: Alejandro Jose Leiva Palomo <[email protected]>

* fix: addressing validation through name of template instead of type field

Signed-off-by: Alejandro Jose Leiva Palomo <[email protected]>

* fix: update guidance to correct wording

Signed-off-by: Alejandro Jose Leiva Palomo <[email protected]>

* fix: add x trestle ignore field in header to correct mismatch

Signed-off-by: Alejandro Jose Leiva Palomo <[email protected]>

* fix: correcting mdformat

Signed-off-by: Alejandro Jose Leiva Palomo <[email protected]>

* fix: correct code linting problem

Signed-off-by: Alejandro Jose Leiva Palomo <[email protected]>

* fix: return template type field to template header

Signed-off-by: Alejandro Jose Leiva Palomo <[email protected]>

* docs: correcting documentation for x-trestle-template-type field

Signed-off-by: Alejandro Jose Leiva Palomo <[email protected]>

---------

Signed-off-by: Alejandro Jose Leiva Palomo <[email protected]>
  • Loading branch information
AleJo2995 authored Aug 24, 2023
1 parent 0593651 commit c7bef58
Show file tree
Hide file tree
Showing 14 changed files with 403 additions and 12 deletions.
55 changes: 55 additions & 0 deletions docs/trestle_author.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,61 @@ Running `trestle author docs validate -tn docs_task -gh="Governed section"` will
- If `--template-version 1.0.0` (`-tv`) is passed the header field `x-trestle-template-version` will be ignored and document will be forcefully validated against template of version `1.0.0`.
Use this for testing purposes _only_ when you need to validate the document against a specific template. By default the template version will be determined based on `x-trestle-template-version` in the document.

### Validating the documents against different templates

Validation against multiple templates as stated before can be done, but there is another scenario that you can leverage on trestle to have multiple documents in the task folder corresponding to a single template.

For that to happen you will need to provide your template with the following parameter at the yaml header level, matching the type of template to be implemented so the validation can occur:

> x-trestle-template-type: insert_template_type_here
Please, take into consideration that for the validation to happen you will also need to provide each instance document in the task folder a field called `x-trestle-template-type: insert_template_type_here` in the yaml header matching with the template name.

```yaml
---
authors: tmp
owner: tmp
valid:
from: null
to: null
x-trestle-template-type: insert_template_type_here
---
```

With that, you will be able to create more than 1 instance document per template and give the instance the desired name.

For instance, let´s consider the next folder structure:

```text
trestle_root
┣ .trestle
┃ ┣ author
┃ ┃ ┣ my_task_2
┃ ┃ ┃ ┣ 0.0.1
┃ ┃ ┃ ┃ ┣ a_template.md
┃ ┃ ┃ ┃ ┣ another_template.md
┃ ┃ ┃ ┃ ┗ arhitecture.drawio
┃ ┗ config.ini
trestle_root
┣ .trestle
┣ my_task_2
┃ ┣ sample_folder_0
┃ ┃ ┣ a_template_1.md
┃ ┃ ┣ a_template_2.md
┃ ┃ ┣ arhitecture_1.drawio
┃ ┃ ┗ another_template_123.md
```

If you noticed, names are no longer needed to match with exact template names, and that´s because validation will run through `x-trestle-template-type` field defined at the instance header, not through the name.

To validate the documents against their respective templates using `x-trestle-template-type`, run:

> trestle author folders validate -tn my_task_name -vtt
Now, `-vtt` stands for validate template type. Validate template type option will provide you the ability to have more than 1 instance per template validated.

</details>

<details markdown>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
authors:
- Tim
- Jane
- Sally
owner: Joe
valid:
from: 2020-01-01
to: 2099-12-31
x-trestle-template-type: architecture
---

# System architecture

Here is some content

## Overview

And some more

## Security model

And even more
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
authors:
- Tim
- Jane
- Sally
owner: Joe
valid:
from: 2020-01-01
to: 2099-12-31
x-trestle-template-type: architecture
---

# System architecture

Here is some content

## Overview

And some more

## Security model

And even more
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
authors:
- Tim
- Jane
- Sally
owner: Joe
valid:
from: 2020-01-01
to: 2099-12-31
x-trestle-template-type: network
---

# Network architecture

Lots of stuff about the network overall including some diagrams.

## External interconnections

Here I put a table which describes the connections beyond my audit boundary with 3rd parties.

## Corporate interconnections

Here I describe interconnections into corporate systems.

## Out of scope interconnections

Here I describe interconnections that are out of scope because they occur outside of the current audit boundary.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
authors:
- Tim
- Jane
- Sally
owner: Joe
valid:
from: 2020-01-01
to: 2099-12-31
x-trestle-template-type: network
---

# Network architecture

Lots of stuff about the network overall including some diagrams.

## External interconnections

Here I put a table which describes the connections beyond my audit boundary with 3rd parties.

## Corporate interconnections

Here I describe interconnections into corporate systems.

## Out of scope interconnections

Here I describe interconnections that are out of scope because they occur outside of the current audit boundary.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
authors:
- Tim
- Jane
- Sally
owner: Joe
valid:
from: 2020-01-01
to: 2099-12-31
---

# System architecture

Here is some content

## Overview

And some more

## Security model

And even more
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
authors:
- Tim
- Jane
- Sally
owner: Joe
valid:
from: 2020-01-01
to: 2099-12-31
---

# System architecture

Here is some content

## Overview

And some more

## Security model

And even more
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
authors:
- Tim
- Jane
- Sally
owner: Joe
valid:
from: 2020-01-01
to: 2099-12-31
---

# Network architecture

Lots of stuff about the network overall including some diagrams.

## External interconnections

Here I put a table which describes the connections beyond my audit boundary with 3rd parties.

## Corporate interconnections

Here I describe interconnections into corporate systems.

## Out of scope interconnections

Here I describe interconnections that are out of scope because they occur outside of the current audit boundary.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
authors:
- Tim
- Jane
- Sally
owner: Joe
valid:
from: 2020-01-01
to: 2099-12-31
---

# Network architecture

Lots of stuff about the network overall including some diagrams.

## External interconnections

Here I put a table which describes the connections beyond my audit boundary with 3rd parties.

## Corporate interconnections

Here I describe interconnections into corporate systems.

## Out of scope interconnections

Here I describe interconnections that are out of scope because they occur outside of the current audit boundary.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
authors: tmp
owner: tmp
valid:
from: null
to: null
x-trestle-template-type: architecture
---
# System architecture

## Overview

## Security model
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
authors: tmp
owner: tmp
valid:
from:
to:
x-trestle-template-type: network
---

# Network architecture

## External interconnections

## Corporate interconnections

## Out of scope interconnections
60 changes: 60 additions & 0 deletions tests/trestle/core/commands/author/folders_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,66 @@ def test_drawio_versioning_validation(
assert rc == 0


def test_validate_template_with_type_field(
testdata_dir: pathlib.Path, tmp_trestle_dir: pathlib.Path, monkeypatch: MonkeyPatch
) -> None:
"""Test behaviour when validating an instance with x-trestle-template-type field."""
task_template_folder = tmp_trestle_dir / '.trestle/author/test_task/'
test_template_folder = testdata_dir / 'author/governed_folders/template_folder_with_template_type'
test_instances_folder = testdata_dir / 'author/governed_folders/good_instance_with_template_type'
task_instance_folder = tmp_trestle_dir / 'test_task/folder_1'

hidden_file = testdata_dir / pathlib.Path(
'author/governed_folders/template_folder_with_drawio/.hidden_does_not_affect'
)
test_utils.make_file_hidden(hidden_file)

test_utils.copy_tree_or_file_with_hidden(test_template_folder, task_template_folder)

shutil.copytree(test_instances_folder, task_instance_folder)
# test validate short
command_string_validate_content = 'trestle author folders validate -tn test_task -hv -vtt'
monkeypatch.setattr(sys, 'argv', command_string_validate_content.split())
rc = trestle.cli.Trestle().run()
assert rc == 0

# test validate long
command_string_validate_content = 'trestle author folders validate -tn test_task -hv --validate-template-type'
monkeypatch.setattr(sys, 'argv', command_string_validate_content.split())
rc = trestle.cli.Trestle().run()
assert rc == 0


def test_validate_template_with_type_field_unhappy(
testdata_dir: pathlib.Path, tmp_trestle_dir: pathlib.Path, monkeypatch: MonkeyPatch
) -> None:
"""Test hunhappy behaviour when validating an instance with x-trestle-template-type field."""
task_template_folder = tmp_trestle_dir / '.trestle/author/test_task/'
test_template_folder = testdata_dir / 'author/governed_folders/template_folder_with_template_type'
test_instances_folder = testdata_dir / 'author/governed_folders/good_instance_without_template_type'
task_instance_folder = tmp_trestle_dir / 'test_task/folder_1'

hidden_file = testdata_dir / pathlib.Path(
'author/governed_folders/template_folder_with_drawio/.hidden_does_not_affect'
)
test_utils.make_file_hidden(hidden_file)

test_utils.copy_tree_or_file_with_hidden(test_template_folder, task_template_folder)

shutil.copytree(test_instances_folder, task_instance_folder)
# test validate short
command_string_validate_content = 'trestle author folders validate -tn test_task -hv -vtt'
monkeypatch.setattr(sys, 'argv', command_string_validate_content.split())
rc = trestle.cli.Trestle().run()
assert rc == 1

# test validate long
command_string_validate_content = 'trestle author folders validate -tn test_task -hv --validate-template-type'
monkeypatch.setattr(sys, 'argv', command_string_validate_content.split())
rc = trestle.cli.Trestle().run()
assert rc == 1


def test_heading_levels_hierarchy(
testdata_dir: pathlib.Path, tmp_trestle_dir: pathlib.Path, monkeypatch: MonkeyPatch
) -> None:
Expand Down
5 changes: 5 additions & 0 deletions trestle/core/commands/author/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,14 @@
'Enable to validate README.md files. Required if readme files are included in the' + 'template.'
)

TEMPLATE_TYPE_VALIDATE_SHORT = '-vtt'
TEMPLATE_TYPE_VALIDATE_LONG = '--validate-template-type'
TEMPLATE_TYPE_VALIDATE_HELP = 'Validate that template and instance files match with x-trestle-template-type field'

START_TEMPLATE_VERSION = '0.0.1' # first ever template version, all templates without version will be defaulted to this
TRESTLE_RESOURCES = 'trestle.resources'
TEMPLATE_VERSION_HEADER = 'x-trestle-template-version'
TEMPLATE_TYPE_HEADER = 'x-trestle-template-type'

# Governed heading - capability: To be removed
GH_SHORT = '-gh'
Expand Down
Loading

0 comments on commit c7bef58

Please sign in to comment.