Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extend multiple templates validation to trestle author folders #1430

Merged
merged 17 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

AleJo2995 marked this conversation as resolved.
Show resolved Hide resolved
AleJo2995 marked this conversation as resolved.
Show resolved Hide resolved
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.

AleJo2995 marked this conversation as resolved.
Show resolved Hide resolved
AleJo2995 marked this conversation as resolved.
Show resolved Hide resolved
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
---
AleJo2995 marked this conversation as resolved.
Show resolved Hide resolved
# 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
---
AleJo2995 marked this conversation as resolved.
Show resolved Hide resolved

# 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
Loading