Skip to content

Commit

Permalink
Create a tool-test-helper action (#420)
Browse files Browse the repository at this point in the history
Analogous to the linter one
  • Loading branch information
laurit17 authored Aug 3, 2023
1 parent ad4a99b commit c5998f8
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 2 deletions.
12 changes: 11 additions & 1 deletion .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,23 @@ actions:
- git_hooks: [pre-push]

- id: linter-test-helper
display_name: Test Generator
display_name: Linter Test Generator
description: Generate boilerplate test code when new linters are defined
runtime: python
packages_file: repo-tools/linter-test-helper/requirements.txt
run: python3 repo-tools/linter-test-helper/generate scan ${workspace}
triggers:
- files: [linters/**]

- id: tool-test-helper
display_name: Tool Test Generator
description: Generate boilerplate test code when new tools are defined
runtime: python
packages_file: repo-tools/tool-test-helper/requirements.txt
run: python3 repo-tools/tool-test-helper/generate scan ${workspace}
triggers:
- files: [tools/**]

- id: remove-release-snapshots
display_name: Remove Release Snapshots
description: Remove release tag from snapshots that were added to the release
Expand All @@ -64,3 +73,4 @@ actions:
- npm-check-pre-push
- remove-release-snapshots
- repo-tests
- tool-test-helper
25 changes: 24 additions & 1 deletion repo-tools/linter-test-helper/generate
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,33 @@ from datetime import datetime, timedelta
import click

INITIAL_PLUGIN_CONTENTS = """version: 0.1
lint:
# Tools can be either runtime package-based or
# download-based. This boilerplate assumes the (far more verbose) latter case.
downloads:
- name: NAME_HERE
# executable: true
# NOTE: These are (common) sample values. Please replace with real values.
downloads:
- os:
linux: linux
macos: darwin
windows: windows
cpu:
amd64: amd64
arm64: arm64
url: https://URL_HERE/${version}/${os}-${cpu}-NAME_HERE
# Fill out this part if the linter is tool-based (most common case) - otherwise delete
tools:
definitions:
- name: NAME_HERE
# RUNTIME_OR_DOWNLOAD_INFO_HERE
# download: NAME_HERE
known_good_version: VERSION_HERE
shims: [NAME_HERE]
lint:
definitions:
- name: NAME_HERE
# TOOL_OR_RUNTIME_OR_DOWNLOAD_INFO_HERE
known_good_version: # VERSION_HERE
commands:
- name: LINT_OR_FORMAT_HERE
Expand Down
99 changes: 99 additions & 0 deletions repo-tools/tool-test-helper/generate
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env python3

import os
from datetime import datetime, timedelta

import click

INITIAL_PLUGIN_CONTENTS = """version: 0.1
# Tools can be either runtime package-based or
# download-based. This boilerplate assumes the (far more verbose) latter case.
downloads:
- name: NAME_HERE
# executable: true
# NOTE: These are (common) sample values. Please replace with real values.
downloads:
- os:
linux: linux
macos: darwin
windows: windows
cpu:
amd64: amd64
arm64: arm64
url: https://URL_HERE/${version}/${os}-${cpu}-NAME_HERE
tools:
definitions:
- name: NAME_HERE
# RUNTIME_OR_DOWNLOAD_INFO_HERE
# download: NAME_HERE
known_good_version: VERSION_HERE
shims: [NAME_HERE]
"""

INITIAL_TEST_CONTENTS = """import { makeToolTestConfig, toolTest } from "tests";
toolTest({
toolName: "NAME_HERE",
toolVersion: "VERSION_HERE",
testConfigs: [
makeToolTestConfig({
command: ["TOOL_NAME", ...args],
expectedOut: "OUTPUT_HERE",
}),
],
});
// Guidelines for configuring tests:
// - Usually, just a version or help text command is sufficient
// - add a test for each command that is used in the plugin.yaml
// - exit code 0 is assumed, so set expectedExitCode if it is different
// - expectedOut/expectedErr do a substring match, so you can just put a portion of the output
//
// If you are unable to write a test for this tool, please document why in your PR, and add
// it to the list in tests/repo_tests/test_coverage_test.ts
"""


@click.group()
def cli():
pass


@cli.command()
@click.argument("workspace")
def scan(workspace):
tool_dir = os.path.join(workspace, "tools")
generated_files = False
for tool_name in os.listdir(tool_dir):
subdir_path = os.path.join(tool_dir, tool_name)
if os.path.isfile(subdir_path):
continue

subdir_contents = os.listdir(subdir_path)
# If this is a newly created, empty directory, dump template files
if (
len(subdir_contents) == 0
and os.stat(subdir_path).st_ctime
> (datetime.now() - timedelta(seconds=10)).timestamp()
):
generated_files = True
# Write plugin.yaml
with open(os.path.join(subdir_path, "plugin.yaml"), "w") as plugin_file:
plugin_file.write(INITIAL_PLUGIN_CONTENTS)

# Write test file
with open(
os.path.join(
subdir_path, "{}.test.ts".format(tool_name.replace("-", "_"))
),
"w",
) as test_file:
test_file.write(INITIAL_TEST_CONTENTS.replace("*{}*", tool_name))

print("Created starter files in {}", subdir_path)

if not generated_files:
print("No generated files")


if __name__ == "__main__":
cli()
1 change: 1 addition & 0 deletions repo-tools/tool-test-helper/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
click==8.1.4

0 comments on commit c5998f8

Please sign in to comment.