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 }}
commit-msg-template:
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
default: |
flake.lock: Updated {{ flake_input }}
flake.lock: Updated in {{ flake_dot_lock_dir }}
commit-msg:
description: "The message provided with the commit"
description: |
The message provided with the commit.
required: false
default: "flake.lock: Update"
base:
description: "Sets the pull request base branch. Defaults to the branch checked out in the workflow."
required: false
@ -41,6 +46,12 @@ inputs:
description: "The title of the PR to be created"
required: false
default: "flake.lock: Update"
pr-body-template:
description: |
TODO
required: false
default: |
Just testing.
pr-body:
description: "The body of the PR to be created"
required: false

View file

@ -64,7 +64,11 @@ class UpdateFlakeLockAction extends DetSysAction {
const commitMessage =
this.commitMessage !== ""
? this.commitMessage
: renderCommitMessage(this.commitMessageTemplate, flakeDotLock);
: renderCommitMessage(
this.commitMessageTemplate,
flakeDir,
flakeDotLock,
);
// 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}

View file

@ -1,31 +1,58 @@
import { renderCommitMessage } from "./template.js";
import { describe, expect, test } from "vitest";
import { renderCommitMessage, renderPullRequestBody } from "./template.js";
import { template } from "handlebars";
import { Test, describe, expect, test } from "vitest";
describe("templating", () => {
test("commit message", () => {
type TestCase = {
template: string;
flakeDotLockDir: string;
flakeDotLock: string;
expected: string;
};
const testCases: TestCase[] = [
{
template: "Updating lockfile at {{ flake_dot_lock }}",
template: "Updating flake.lock in dir {{ flake_dot_lock_dir }}",
flakeDotLockDir: ".",
flakeDotLock: "./flake.lock",
expected: "Updating lockfile at ./flake.lock",
expected: "Updating flake.lock in dir .",
},
{
template:
"Here I go doing some updating of my pristine flake.lock at {{ flake_dot_lock }}",
flakeDotLockDir: "subflake",
flakeDotLock: "subflake/flake.lock",
expected:
"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 }) => {
expect(renderCommitMessage(template, flakeDotLock)).toEqual(expected);
testCases.forEach(
({ 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";
export function renderPullRequestBody(template: string): string {
const tpl = Handlebars.compile(template);
return tpl({});
}
export function renderCommitMessage(
template: string,
flakeDotLockDir: string,
flakeDotLock: 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 {