diff --git a/.github/pr-checks/fix-has-test.js b/.github/pr-checks/fix-has-test.js
new file mode 100644
index 0000000..a118a07
--- /dev/null
+++ b/.github/pr-checks/fix-has-test.js
@@ -0,0 +1,55 @@
+'use strict';
+
+const prTitle = require('./pr-title.js');
+
+/** Paths of test files: a `tests` or `test` directory, a sibling `tests.rs`,
+ * and the `_test`, `.test` and `.spec` files of other languages. */
+const TEST_PATH = /(^|\/)(tests?|__tests__)\/|(^|\/)tests\.rs$|[._-](test|spec)\.\w+$|(^|\/)test_\w+\.py$/;
+
+/** An added line holding a test attribute, such as `#[test]` or
+ * `#[tokio::test]`, for a test beside the code it tests. */
+const TEST_ATTRIBUTE = /^\+\s*#\[(\w+::)*\w*test(_case)?\b/m;
+
+/** An image in a description: Markdown, HTML, or a pasted attachment. */
+const IMAGE = /!\[[^\]]*\]\([^)]+\)|
({ filename, patch, status });
+const findings = async (pr, changed = []) => fixHasTest.findings(await Fake.pr(pr, { changed }));
+
+test('a pull request of another type passes without a test', async () => {
+ assert.deepEqual(await findings({ title: 'feat(rules): a' }, [file('src/lib.rs')]), []);
+});
+
+test('a fix changing a test file passes', async () => {
+ for (const path of ['crates/a/src/rules/tests.rs', 'tests/cli.rs', 'web/src/a.test.ts', 'b_test.go', 'test_c.py']) {
+ assert.deepEqual(await findings({ title: 'fix: a' }, [file('src/lib.rs'), file(path)]), [], path);
+ }
+});
+
+test('a fix adding a test beside the code passes', async () => {
+ for (const attribute of ['#[test]', '#[tokio::test]', '#[rstest]', '#[test_case(1)]']) {
+ const patch = `@@ -1,1 +1,3 @@\n fn a() {}\n+ ${attribute}\n+fn b() {}`;
+ assert.deepEqual(await findings({ title: 'fix: a' }, [file('src/lib.rs', patch)]), [], attribute);
+ }
+});
+
+test('a fix without a test fails with how to add one', async () => {
+ const removed = file('src/tests.rs', '', 'removed');
+ const unchanged = file('src/lib.rs', '@@ -1 +1 @@\n-#[test]\n+fn b() {}');
+ const [finding] = await findings({ title: 'fix(rules)!: a' }, [removed, unchanged]);
+ assert.match(finding.message, /^this `fix` changes no test: add one that fails without the fix/);
+});
+
+test('a visual fix passes with an image and fails without one', async () => {
+ const labels = [{ name: 'visual' }];
+ for (const body of ['', '
', 'https://github.com/user-attachments/assets/1']) {
+ assert.deepEqual(await findings({ title: 'fix: a', labels, body }), [], body);
+ }
+ const [finding] = await findings({ title: 'fix: a', labels, body: 'Looks better.' });
+ assert.match(finding.message, /the description has no image/);
+});
diff --git a/.github/pr-checks/pull-request.js b/.github/pr-checks/pull-request.js
index 809bbb1..8907e06 100644
--- a/.github/pr-checks/pull-request.js
+++ b/.github/pr-checks/pull-request.js
@@ -37,6 +37,17 @@ class PullRequest {
return commits.map(({ sha, commit }) => ({ sha, message: commit.message }));
}
+ /** Each file it changes: `{ path, status, patch }`, where `patch` is the
+ * unified diff, or `undefined` when GitHub leaves out a large one. */
+ async files() {
+ const files = await this.github.paginate(this.github.rest.pulls.listFiles, {
+ ...this.repo,
+ pull_number: this.number,
+ per_page: 100,
+ });
+ return files.map(({ filename, status, patch }) => ({ path: filename, status, patch }));
+ }
+
/** The text of the file at `path` in commit `sha`, or `null` if it has
* none. */
async fileAt(sha, path) {
diff --git a/.github/pr-checks/run.js b/.github/pr-checks/run.js
index 6616fe3..329bceb 100644
--- a/.github/pr-checks/run.js
+++ b/.github/pr-checks/run.js
@@ -15,6 +15,7 @@ class Runner {
require('./pr-title.js'),
require('./pr-body.js'),
require('./no-co-authors.js'),
+ require('./fix-has-test.js'),
];
/** Runs the check named in `CHECK` with the inputs in `INPUTS`. Each
diff --git a/.github/pr-checks/testing.js b/.github/pr-checks/testing.js
index 4589904..2d83808 100644
--- a/.github/pr-checks/testing.js
+++ b/.github/pr-checks/testing.js
@@ -30,9 +30,10 @@ class Fake {
/** A `github` and `context` for pull request number 1, from base commit
* `base` to head commit `head`, with the fields of `pr` over a passing
- * default, `files[sha][path]` the text of each file and `commits` its
- * commits, as `{ sha, commit: { message } }`. */
- static github(pr = {}, { files = {}, commits = [] } = {}) {
+ * default, `files[sha][path]` the text of each file, `commits` its
+ * commits, as `{ sha, commit: { message } }`, and `changed` the files it
+ * changes, as `{ filename, status, patch }`. */
+ static github(pr = {}, { files = {}, commits = [], changed = [] } = {}) {
const data = {
number: 1,
title: 'feat: a change',
@@ -50,7 +51,11 @@ class Fake {
return {
github: {
rest: {
- pulls: { get: async () => ({ data }), listCommits: async () => ({ data: commits }) },
+ pulls: {
+ get: async () => ({ data }),
+ listCommits: async () => ({ data: commits }),
+ listFiles: async () => ({ data: changed }),
+ },
repos: { getContent },
},
paginate: async (method, parameters) => (await method(parameters)).data,
diff --git a/.github/workflows/pr-checks.yml b/.github/workflows/pr-checks.yml
index e910e52..5fde7ab 100644
--- a/.github/workflows/pr-checks.yml
+++ b/.github/workflows/pr-checks.yml
@@ -37,7 +37,7 @@ jobs:
strategy:
fail-fast: false
matrix:
- check: [pr-title, pr-body, no-co-authors]
+ check: [pr-title, pr-body, no-co-authors, fix-has-test]
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
diff --git a/crates/sourceorigin-standard/README.md b/crates/sourceorigin-standard/README.md
index 2d4d5fa..021d9e6 100644
--- a/crates/sourceorigin-standard/README.md
+++ b/crates/sourceorigin-standard/README.md
@@ -138,6 +138,10 @@ check:
reads `Fixes #123`, or `Fixes PQD-123` where issues live in Linear.
- `no-co-authors`: neither the description nor a commit message has a
`Co-authored-by` trailer.
+- `fix-has-test`: a pull request whose title has the type `fix` changes a
+ test (a `tests.rs`, a `tests/` directory, an added `#[test]`, or another
+ language's test file), or carries the `visual` label and images in its
+ description.
A repository calls it from `.github/workflows/pr.yml`, pinned to a commit of
this repository's `main`. Reviews and labels re-run it, as some checks