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

Align DEVELOPMENT.md with last release #43

Merged
merged 3 commits into from
Jun 7, 2022
Merged
Changes from 2 commits
Commits
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
66 changes: 40 additions & 26 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ We are an open source project on GitHub and would enjoy your contributions! Plea

- Node >= 16.13.2
- npm >= 8.x
- Go >= 1.17

### Getting the code

Expand All @@ -23,15 +24,14 @@ npm install

## Writing TextMate Grammar

The following workflow is recommended to work on Terraform TextMate grammar:
The following workflow is recommended to work on any of the HashiCorp TextMate grammars in this repo:

1. Add or modify grammar to the `terraform.tmGrammar.json` file inside the `syntaxes` directory.
1. Add a new unit test file or modify existing unit test files inside the test/unit/terraform directory.
1. Run `npm run test:grammar` until the tests pass
1. Add a new example Terraform file or modify existing unit test files inside the test/snapshot/terraform directory.
1. Run `npm run test:snap` until the tests pass.
1. Add or modify grammar in the `src/_main.yml` or product specific YAML file in like `src/terraform.yml`.
1. Change directory to `builder` and run `go run main.go build`
1. Add a new test case file or modify existing test case files inside the `tests/snapshot/<lang>` directory. Make sure to put the new file in the correct folder or it will not be tested.
1. Run `npm run test:snap:<lang>` until the tests pass.

> Tip: Running `npm run test:snap -- -u` after modifying the tmGrammar file will give you a quick visual representation of how the tokens are being resolved. This can aid in crafting unit tests.
> Tip: Running `npm run test:snap:update` after modifying the tmGrammar file will give you a quick visual representation of how the tokens are being resolved. This can aid in crafting unit tests.

## Tests

Expand All @@ -40,33 +40,47 @@ Automated `unit` tests can be written using [vscode-tmgrammar-test](https://gith
To start the test suite from the command-line run:

```bash
npm test:unit
npm test
```

## Writing Grammar Unit Tests

Unit tests are Terraform files with `vscode-tmgrammar-test` token lines specifying which TextMate grammer should be resolved.

For example:
## Writing Grammar Snapshot Tests

> Note: This is shortened to demonstrate, actual syntax will vary
Snapshot tests comprise of two files: example files and their companion `snap` file:

```bash
tests/snapshot/terraform/blocks.tf
tests/snapshot/terraform/blocks.tf.snap
```
resource "example" "thing" {
; ^^^^^^^ source.terraform string.quoted.double.terraform
; ^^^^^ source.terraform string.quoted.double.terraform
; ^ source.terraform punctuation.section.block.begin.terraform
}
; <- source.terraform punctuation.section.block.end.terraform

```
These files are grouped by HashiCorp product inside folders named for the product they target.

## Writing Grammar Snapshot Tests
Snapshot test example files are HashiCorp product files without any `vscode-tmgrammar-test` token lines. Each example file is exactly how you would see it used in production. This ensures scope, inheritance, and resolution of tokens happen exactly as they would on a user's machine.

Snapshot tests comprise of two files: example files and their companion `snap` file.
The companion `snap` file is named the same as the example file with the `.snap` extension, and is the tmGrammar represenation of all resolved tokens. This file is commited alongside the example file. If anything changes with regards to how the tokens are resolved, the snapshot test will fail.

Snapshot test example files are Terraform files without any `vscode-tmgrammar-test` token lines. Each example file is exactly how you would see it used in production. This ensures scope, inheritance, and resolution of tokens happen exactly as they would on a user's machine.
> Note: If modifying an existing snapshot test, run `npm run test:snap:update` to update the snapshot file. This will update the snapshot file with the new modified grammar. Be sure to do this after you've tested using `npm run test:snap` and are sure that the modified grammar is correct, otherwise you may get false positives.

The companion `snap` file is named the same as the example file with the `.snap` extension, and is the tmGrammar represenation of all resolved tokens. This file is commited alongside the example file. If anything changes with regards to how the tokens are resolved, the snapshot test will fail.
For example:

> Note: If modifying an existing snapshot test, run `npm run test:snap -- -u` to update the snapshot file. This will update the snapshot file with the new modified grammar. Be sure to do this after you've tested using `npm run test:grammar` and are sure that the modified grammar is correct, otherwise you may get false positives.
```bash
#tests/snapshot/terraform/blocks.tf
resource "aws_instance" "web" {
ami = "ami-a1b2c3d4"
instance_type = "t2.micro"
timeouts {
create = "60m"
delete = "2h"
}
}

#tests/snapshot/terraform/blocks.tf.snap
>resource "aws_instance" "web" {
#^^^^^^^^ source.hcl.terraform meta.block.hcl entity.name.type.terraform
# ^ source.hcl.terraform meta.block.hcl
# ^^^^^^^^^^^^^^ source.hcl.terraform meta.block.hcl variable.other.enummember.hcl
# ^ source.hcl.terraform meta.block.hcl
# ^^^^^ source.hcl.terraform meta.block.hcl variable.other.enummember.hcl
# ^ source.hcl.terraform meta.block.hcl
# ^ source.hcl.terraform meta.block.hcl punctuation.section.block.begin.hcl
> ami = "ami-a1b2c3d4"
```