Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
55 changes: 55 additions & 0 deletions .github/pr-checks/fix-has-test.js
Original file line number Diff line number Diff line change
@@ -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 = /!\[[^\]]*\]\([^)]+\)|<img\s|https:\/\/github\.com\/user-attachments\/assets\//i;

/** The label that marks a visual fix, proven by screenshots. */
const VISUAL = 'visual';

/**
* MT8: a pull request whose title has the type `fix` changes a test, which
* should fail without the fix, or carries the `visual` label and shows the
* fix in images in its description.
*/
const fixHasTest = {
name: 'fix-has-test',

async findings(pr) {
if (prTitle.parse(pr.title)?.type !== 'fix') return [];
if (pr.labels.includes(VISUAL)) {
if (IMAGE.test(pr.body)) return [];
return [
{
message: `the \`${VISUAL}\` label says this fix is visual, but the description has no image: add screenshots from before and after the fix.`,
},
];
}
if ((await pr.files()).some(fixHasTest.isTest)) return [];
return [
{
message:
`this \`fix\` changes no test: add one that fails without the fix, in a \`tests.rs\`, ` +
`a \`tests/\` directory or a \`#[test]\` beside the code. A visual fix carries the ` +
`\`${VISUAL}\` label and screenshots from before and after in its description instead.`,
},
];
},

/** Whether a changed file is, or adds, a test. */
isTest({ path, status, patch }) {
if (status === 'removed') return false;
return TEST_PATH.test(path) || TEST_ATTRIBUTE.test(patch ?? '');
},
};

module.exports = fixHasTest;
42 changes: 42 additions & 0 deletions .github/pr-checks/fix-has-test.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

const test = require('node:test');
const assert = require('node:assert/strict');
const fixHasTest = require('./fix-has-test.js');
const { Fake } = require('./testing.js');

const file = (filename, patch = '', status = 'modified') => ({ 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 ['![before](https://x/a.png)', '<img src="b.png">', '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/);
});
11 changes: 11 additions & 0 deletions .github/pr-checks/pull-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions .github/pr-checks/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions .github/pr-checks/testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions crates/sourceorigin-standard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down