Skip to content
Closed
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
33 changes: 33 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ url = "2.5.4"
webpki-root-certs = "1.0.8"

[dev-dependencies]
rstest = { version = "0.28.0", default-features = false }
Comment thread
velocitysystems marked this conversation as resolved.
Resolved
tokio = { version = "1.45.1", features = ["rt-multi-thread", "macros", "time"] }
wiremock = "0.6.2"

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
"eslint": "eslint .",
"markdownlint": "markdownlint-cli2",
"prepare": "rollup -c",
"rust:lint": "cargo lint-clippy && cargo lint-fmt",
"rust:lint": "cargo lint-clippy && cargo lint-fmt && npm run rust:lint:rstest",
"rust:lint:fix": "cargo fix-clippy && cargo fix-fmt",
"rust:lint:rstest": "node scripts/lint-rstest-attributes.cjs src",
"test": "vitest run && cargo test --workspace --lib",
"test:ts": "vitest run",
"test:ts:watch": "vitest",
Expand Down
139 changes: 139 additions & 0 deletions scripts/lint-rstest-attributes.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/**

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 P2 — Remove the rstest lint script and the rust:lint:rstest step from this PR

The PR adds scripts/lint-rstest-attributes.cjs to rust:lint, but the tauri-plugin-connectivity adoption of rstest added no such script.

Details

The commit body says the script is "the same script as in the sibling plugins". No sibling has it on its default branch. The connectivity rust:lint script is still cargo lint-clippy && cargo lint-fmt. The script exists only in this PR, tauri-plugin-sqlite#73 and tauri-plugin-media-parser#32.

The script has no tests and cannot be imported, because it calls main() on load and exports nothing. A later regex change can turn the check off without a signal. Its documented default roots are crates src, but this repo has no crates/. Run with no arguments, it fails with ENOENT: scandir 'crates'.

Removing it removes the guard against dropped cases. rstest generates only case_1 when #[test] is above the cases. Today all 45 #[rstest] functions in this PR use a safe order.

Delete scripts/lint-rstest-attributes.cjs. In package.json, set rust:lint back to cargo lint-clippy && cargo lint-fmt and remove rust:lint:rstest. Remove the lint-script paragraph from the commit body. If the team wants this guard later, add it to shared tooling so that every plugin gets it.

Comment added by AI model Claude

* Checks the attribute order of #[rstest] functions in the Rust sources.
*
* On rstest 0.28, a test attribute placed before a #[case] silently drops cases, and
* clippy does not warn: `#[rstest] #[test] #[case] #[case]` generates only the first
* case. This script flags any test attribute (#[test], #[tokio::test] or another
* #[<path>::test]) above the last #[case]. The forms this repo uses are:
*
* sync: #[rstest], the #[case] lines, then the fn (rstest adds #[test] itself)
* async: #[rstest], the #[case] lines, #[tokio::test], then the fn
*
* Usage: node scripts/lint-rstest-attributes.cjs [dir ...] (default: crates src)
*/

'use strict';

const fs = require('fs/promises'),
path = require('path');

const REPO_ROOT = path.resolve(__dirname, '..'),
DEFAULT_ROOTS = [ 'crates', 'src' ],
SKIP_DIRS = new Set([ 'target', 'node_modules' ]),
RSTEST_ATTR = /^#\[(?:(?:::)?rstest::)?rstest\s*[\](]/,
CASE_ATTR = /^#\[case\b/,
PLAIN_TEST_ATTR = /^#\[test\s*\]/,
// #[tokio::test], #[tokio::test(flavor = "multi_thread")] or any #[<path>::test]
PATH_TEST_ATTR = /^#\[((?:::)?(?:\w+::)+test)\s*[\](]/,
FN_LINE = /^(?:(?:pub(?:\([^)]*\))?|const|async|unsafe)\s+)*fn\s/;

async function findRustFiles(dir) {
const entries = await fs.readdir(dir, { withFileTypes: true });

const nested = await Promise.all(entries.map((entry) => {
const entryPath = path.join(dir, entry.name);

if (entry.isDirectory()) {
return SKIP_DIRS.has(entry.name) ? [] : findRustFiles(entryPath);
}
return entry.isFile() && entry.name.endsWith('.rs') ? [ entryPath ] : [];
}));

return nested.flat();
}

function checkAttributes(file, attrs) {
const lastCase = attrs.findLastIndex((attr) => { return CASE_ATTR.test(attr.text); });

return attrs.flatMap((attr, index) => {
const pathTest = PATH_TEST_ATTR.exec(attr.text),
at = `${file}:${attr.line}:`;

if (PLAIN_TEST_ATTR.test(attr.text) && index < lastCase) {
return [ `${at} #[test] before the last #[case] silently drops cases; remove it (rstest adds the test attribute itself)` ];
}
if (pathTest && index < lastCase) {
return [ `${at} #[${pathTest[1]}] before the last #[case] silently drops cases; move it after the last #[case]` ];
}
return [];
});
}

/**
* A block starts at an #[rstest] attribute and runs until its fn line. Inside it, only a
* line starting with #[ begins an attribute: other lines are comments, blank lines or
* the rest of a multi-line attribute.
*/
function checkSource(file, source) {
const problems = [];

let attrs = null,
rstestLine = 0,
functionCount = 0;

source.split(/\r?\n/).forEach((rawLine, index) => {
const line = rawLine.trim();

if (attrs === null) {
if (RSTEST_ATTR.test(line)) {
attrs = [];
rstestLine = index + 1;
}
return;
}

if (FN_LINE.test(line)) {
problems.push(...checkAttributes(file, attrs));
functionCount += 1;
attrs = null;
} else if (line.startsWith('#[')) {
attrs.push({ line: index + 1, text: line });
}
});

if (attrs !== null) {
problems.push(`${file}:${rstestLine}: #[rstest] has no fn line after it; check its attributes`);
}

return { problems, functionCount };
}

function defaultRoots() {
return DEFAULT_ROOTS.map((dir) => {
return path.relative(process.cwd(), path.join(REPO_ROOT, dir));
});
}

async function main() {
const args = process.argv.slice(2),
roots = args.length > 0 ? args : defaultRoots(),
files = (await Promise.all(roots.map(findRustFiles))).flat().sort();

const results = await Promise.all(files.map(async (file) => {
return checkSource(file, await fs.readFile(file, 'utf8'));
}));

const problems = results.flatMap((result) => { return result.problems; }),
functionCount = results.reduce((sum, result) => { return sum + result.functionCount; }, 0);

if (problems.length > 0) {
process.stderr.write(`${problems.join('\n')}\n${problems.length} rstest attribute problem(s) found\n`);
process.exitCode = 1;
return;
}

// Finding nothing means the roots or RSTEST_ATTR are wrong, not that every
// function is fine: passing here would turn this check off without a trace.
if (functionCount === 0) {
process.stderr.write(`no #[rstest] functions found in ${files.length} files; check the roots and RSTEST_ATTR\n`);
process.exitCode = 1;
return;
}

process.stdout.write(`rstest attributes OK: checked ${functionCount} #[rstest] functions in ${files.length} files\n`);
}

main().catch((err) => {
process.stderr.write(`${err.stack || err}\n`);
process.exitCode = 1;
});
Loading