Skip to content

Commit

Permalink
test(tools): add integration test for scaffold (#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
magurotuna authored Sep 4, 2021
1 parent 5ca58cd commit d11ee57
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
- name: Test
run: |
cargo test --locked --release --all-targets --all-features
deno test ./tools
deno test --unstable --allow-read=. --allow-write=. --allow-run ./tools
- name: Lint
run: deno run --allow-run ./tools/lint.ts --release
Expand Down
6 changes: 6 additions & 0 deletions tools/scaffold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export function genRustContent(
use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::{Program, ProgramRef};
use swc_common::Spanned;
pub struct ${pascalCasedLintName};
Expand Down Expand Up @@ -137,6 +138,11 @@ struct ${pascalCasedLintName}Handler;
impl Handler for ${pascalCasedLintName}Handler {
// implement some methods to achieve the goal of this lint
// This is an example
fn with_stmt(&mut self, with_stmt: &ast_view::WithStmt, ctx: &mut Context) {
ctx.add_diagnostic_with_hint(with_stmt.span(), CODE, MESSAGE, HINT);
}
}
#[cfg(test)]
Expand Down
62 changes: 62 additions & 0 deletions tools/tests/scaffold_integration_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { exists } from "https://deno.land/[email protected]/fs/mod.ts";
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

Deno.test(
"Check if the files created by tools/scaffold.ts pass `cargo check`",
async () => {
const name = "dummy-lint-rule-for-testing";
const filename = name.replaceAll("-", "_");
const rulesPath = "./src/rules.rs";

// Preserve the original content of src/rules.rs
const rulesRs = await Deno.readTextFile(rulesPath);

try {
console.log(`Run the scaffold script to create ${name} rule`);
const p1 = Deno.run({
cmd: [
"deno",
"run",
"--allow-write=.",
"--allow-read=.",
"./tools/scaffold.ts",
name,
],
});
const s1 = await p1.status();
p1.close();

assertEquals(s1.code, 0);
console.log("Scaffold succeeded");

// Check if `cargo check` passes
console.log("Run `cargo check`");
const p2 = Deno.run({
cmd: ["cargo", "check", "--all-targets", "--all-features"],
});
const s2 = await p2.status();
p2.close();

assertEquals(s2.code, 0);
console.log("`cargo check` succeeded");
} finally {
console.log("Start cleanup...");
console.log("Restoring src/rules.rs...");
await Deno.writeTextFile(rulesPath, rulesRs);

console.log(`Deleting src/rules/${filename}.rs...`);
const rsPath = `./src/rules/${filename}.rs`;
if (await exists(rsPath)) {
await Deno.remove(rsPath);
}

console.log(`Deleting docs/rules/${filename}.md...`);
const mdPath = `./docs/rules/${filename}.md`;
if (await exists(mdPath)) {
await Deno.remove(mdPath);
}

console.log("Cleanup finished");
}
},
);
6 changes: 6 additions & 0 deletions tools/tests/scaffold_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Deno.test("the content of .rs", () => {
use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::{Program, ProgramRef};
use swc_common::Spanned;
pub struct FooBarBaz;
Expand Down Expand Up @@ -129,6 +130,11 @@ struct FooBarBazHandler;
impl Handler for FooBarBazHandler {
// implement some methods to achieve the goal of this lint
// This is an example
fn with_stmt(&mut self, with_stmt: &ast_view::WithStmt, ctx: &mut Context) {
ctx.add_diagnostic_with_hint(with_stmt.span(), CODE, MESSAGE, HINT);
}
}
#[cfg(test)]
Expand Down

0 comments on commit d11ee57

Please sign in to comment.