More test cases:

This commit is contained in:
Luc Perkins 2024-06-04 08:44:31 -07:00
parent 09b0ac8cd3
commit 8c5e8043f8
No known key found for this signature in database
GPG key ID: 16DB1108FB591835
4 changed files with 63 additions and 12 deletions

View file

@ -11,14 +11,19 @@ inputs:
default: ${{ github.token }} default: ${{ github.token }}
commit-msg-template: commit-msg-template:
description: | description: |
The commit message template to use. Variable: `flake_input`. The commit message template to use. You can use these variables in your template:
* `{{ flake_dot_lock }}` is the path to the `flake.lock` file being updated
* `{{ flake_dot_lock_dir }}` is the `flake.lock` file's directory
If you set both this and `commit-msg`, the `commit-msg` setting is used.
required: false required: false
default: | default: |
flake.lock: Updated {{ flake_input }} flake.lock: Updated in {{ flake_dot_lock_dir }}
commit-msg: commit-msg:
description: "The message provided with the commit" description: |
The message provided with the commit.
required: false required: false
default: "flake.lock: Update"
base: base:
description: "Sets the pull request base branch. Defaults to the branch checked out in the workflow." description: "Sets the pull request base branch. Defaults to the branch checked out in the workflow."
required: false required: false
@ -41,6 +46,12 @@ inputs:
description: "The title of the PR to be created" description: "The title of the PR to be created"
required: false required: false
default: "flake.lock: Update" default: "flake.lock: Update"
pr-body-template:
description: |
TODO
required: false
default: |
Just testing.
pr-body: pr-body:
description: "The body of the PR to be created" description: "The body of the PR to be created"
required: false required: false

View file

@ -64,7 +64,11 @@ class UpdateFlakeLockAction extends DetSysAction {
const commitMessage = const commitMessage =
this.commitMessage !== "" this.commitMessage !== ""
? this.commitMessage ? this.commitMessage
: renderCommitMessage(this.commitMessageTemplate, flakeDotLock); : renderCommitMessage(
this.commitMessageTemplate,
flakeDir,
flakeDotLock,
);
// Nix command of this form: // Nix command of this form:
// nix ${maybe nix options} flake ${"update" or "lock"} ${maybe --update-input flags} --commit-lock-file --commit-lockfile-summary ${commit message} // nix ${maybe nix options} flake ${"update" or "lock"} ${maybe --update-input flags} --commit-lock-file --commit-lockfile-summary ${commit message}

View file

@ -1,31 +1,58 @@
import { renderCommitMessage } from "./template.js"; import { renderCommitMessage, renderPullRequestBody } from "./template.js";
import { describe, expect, test } from "vitest"; import { template } from "handlebars";
import { Test, describe, expect, test } from "vitest";
describe("templating", () => { describe("templating", () => {
test("commit message", () => { test("commit message", () => {
type TestCase = { type TestCase = {
template: string; template: string;
flakeDotLockDir: string;
flakeDotLock: string; flakeDotLock: string;
expected: string; expected: string;
}; };
const testCases: TestCase[] = [ const testCases: TestCase[] = [
{ {
template: "Updating lockfile at {{ flake_dot_lock }}", template: "Updating flake.lock in dir {{ flake_dot_lock_dir }}",
flakeDotLockDir: ".",
flakeDotLock: "./flake.lock", flakeDotLock: "./flake.lock",
expected: "Updating lockfile at ./flake.lock", expected: "Updating flake.lock in dir .",
}, },
{ {
template: template:
"Here I go doing some updating of my pristine flake.lock at {{ flake_dot_lock }}", "Here I go doing some updating of my pristine flake.lock at {{ flake_dot_lock }}",
flakeDotLockDir: "subflake",
flakeDotLock: "subflake/flake.lock", flakeDotLock: "subflake/flake.lock",
expected: expected:
"Here I go doing some updating of my pristine flake.lock at subflake/flake.lock", "Here I go doing some updating of my pristine flake.lock at subflake/flake.lock",
}, },
{
template: "This variable doesn't exist: {{ foo }}",
flakeDotLockDir: ".",
flakeDotLock: "./flake.lock",
expected: "This variable doesn't exist: ",
},
]; ];
testCases.forEach(({ template, flakeDotLock, expected }) => { testCases.forEach(
expect(renderCommitMessage(template, flakeDotLock)).toEqual(expected); ({ template, flakeDotLockDir, flakeDotLock, expected }) => {
expect(
renderCommitMessage(template, flakeDotLockDir, flakeDotLock),
).toEqual(expected);
},
);
});
test("pull request body", () => {
type TestCase = {
template: string;
expected: string;
};
const testCases: TestCase[] = [];
testCases.forEach(({ template, expected }) => {
expect(renderPullRequestBody(template)).toEqual(expected);
}); });
}); });
}); });

View file

@ -1,10 +1,19 @@
import Handlebars from "handlebars"; import Handlebars from "handlebars";
export function renderPullRequestBody(template: string): string {
const tpl = Handlebars.compile(template);
return tpl({});
}
export function renderCommitMessage( export function renderCommitMessage(
template: string, template: string,
flakeDotLockDir: string,
flakeDotLock: string, flakeDotLock: string,
): string { ): string {
return render(template, { flake_dot_lock: flakeDotLock }); return render(template, {
flake_dot_lock_dir: flakeDotLockDir,
flake_dot_lock: flakeDotLock,
});
} }
function render(template: string, inputs: Record<string, string>): string { function render(template: string, inputs: Record<string, string>): string {