Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(canary): add tests to canary #694

Merged
merged 7 commits into from
Nov 27, 2023
Merged
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
23 changes: 22 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ Once a pull request has been created, it will initiate continuous integration bu

After all participants on pull request are satisfied to the changes, we will merge your code into the secretlint master branch. Yay!


## Add New Rule

This project has a template for creating a new rule.
Expand All @@ -266,6 +265,28 @@ npm run gen:rule
2. Implement it and run tests!
3. Update README.md

## Add a rule to preset rules

This project has two preset rules:

- [secretlint-rule-preset-canary](./packages/@secretlint/secretlint-rule-preset-canary)
- [secretlint-rule-preset-recommend](./packages/@secretlint/secretlint-rule-preset-recommend)

At first, you need to add a rule to [secretlint-rule-preset-canary](./packages/@secretlint/secretlint-rule-preset-canary).

1. Add rule to `packages/@secretlint/secretlint-rule-preset-canary/package.json`'s `devDependencies`
2. Add rule to `packages/@secretlint/secretlint-rule-preset-canary/src/index.ts`
3. `npm run import-test`
4. `npm test`

After testing on canary, you can sync to [secretlint-rule-preset-recommend](./packages/@secretlint/secretlint-rule-preset-recommend).

```sh
cd packages/@secretlint/secretlint-rule-preset-recommend
npm run sync-canary
npm test
```

## Benchmark

Secretlint has a continuous benchmark:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ const rmCurrentSnapshots = () => {
await rmCurrentSnapshots();
ruleDirs.forEach(copyTestFiles);

console.log("Need to update snapshots via $ npm run updateSnapshots");
console.log("Need to update snapshots:");
console.log("$ npm run updateSnapshots");
console.log("Copied snapshot is a bit different from preset.");
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"./package.json": "./package.json"
},
"main": "./module/index.js",
"browser": "browser/index.js",
"types": "./module/index.d.ts",
"directories": {
"lib": "lib",
Expand All @@ -47,6 +46,7 @@
"prepublishOnly": "npm run clean && npm run build",
"prettier": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\"",
"prepublish": "npm run --if-present build",
"import-test": "node import-tests.js && npm run build && npm run updateSnapshot",
"test": "node --loader ts-node/esm --test test/index.test.ts",
"updateSnapshot": "UPDATE_SNAPSHOT=1 npm test",
"watch": "tsc --build --watch"
Expand All @@ -58,8 +58,6 @@
},
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-replace": "^5.0.5",
"@rollup/plugin-typescript": "^11.1.5",
"@secretlint/secretlint-rule-aws": "^7.2.0",
"@secretlint/secretlint-rule-azure": "^7.2.0",
Expand All @@ -79,7 +77,7 @@
"prettier": "^2.8.1",
"rimraf": "^3.0.2",
"rollup": "^3.29.4",
"rollup-plugin-polyfill-node": "^0.12.0",
"rollup-plugin-polyfill-node": "^0.13.0",
"ts-node": "^10.9.1",
"typescript": "^5.1.6"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,55 +1,24 @@
import resolve from "@rollup/plugin-node-resolve";
import typescript from "@rollup/plugin-typescript";
import commonjs from "@rollup/plugin-commonjs";
import nodePolyfills from "rollup-plugin-polyfill-node";
import replace from "@rollup/plugin-replace";

import { defineConfig } from "rollup";

export default defineConfig([
{
input: "src/index.ts",
output: [
{
dir: "module/",
format: "esm",
exports: "named",
sourcemap: true,
interop: "esModule"
}
],
plugins: [resolve({ preferBuiltins: true }), commonjs({
// disable __esModule interop
// Node.js ESM does not recognize `__esModule` flag interop
defaultIsModuleExports: true
}), typescript()]
},
{
input: "src/index.ts",
output: [
{
dir: "browser/",
format: "esm",
exports: "named",
sourcemap: true,
// disable __esModule interop
// Node.js ESM does not recognize `__esModule` flag interop
interop: "esModule"
}
],
// Delete process.version
plugins: [replace({
"__DEV__": false,
"process.platform": null,
"process.version": null,
"process.env.NODE_ENV": JSON.stringify("production"),
preventAssignment: false
}), resolve({ preferBuiltins: false }), commonjs({
// disable __esModule interop
export default {
input: "src/index.ts",
// gcp rule is broken by moduleSideEffects option
// treeshake: "smallest",
output: [
{
dir: "module/",
format: "esm",
exports: "named",
sourcemap: true,
// Node.js ESM does not recognize `__esModule` flag interop
defaultIsModuleExports: true
}), typescript({
tsconfig: "tsconfig.browser.json"
}), nodePolyfills()]
}
]);
interop: "esModule",
}
],
plugins: [resolve({ preferBuiltins: true }), commonjs({
// disable __esModule interop
// Node.js ESM does not recognize `__esModule` flag interop
defaultIsModuleExports: true
}), typescript()]
};
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
import { creator as rule } from "../src/index.js";

import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import test from "node:test";
// Test target is bundled file
import { creator as rule } from "../module/index.js";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
test("@secretlint/secretlint-rule-preset-canary", async (t) => {
const snapshot = (await import("@secretlint/tester")).snapshot;
await snapshot({
defaultConfig: {
rules: [
{
id: "@secretlint/secretlint-rule-preset-canary",
rule,
rules: [],
options: {},
},
],
},
updateSnapshot: !!process.env.UPDATE_SNAPSHOT,
snapshotDirectory: new URL("snapshots", import.meta.url),
}).forEach((name, test) => {
return t.test(name, async (context) => {
const status = await test();
if (status === "skip") {
context.skip();
}
const eachRulesDir = fs
.readdirSync(path.join(__dirname, "snapshots"), {
withFileTypes: true,
})
.filter((dirent) => dirent.isDirectory());
for (const ruleDir of eachRulesDir) {
const ruleDirPath = path.join(__dirname, "snapshots", ruleDir.name);
await snapshot({
defaultConfig: {
rules: [
{
id: "@secretlint/secretlint-rule-preset-canary",
rule,
rules: [],
options: {},
},
],
},
updateSnapshot: !!process.env.UPDATE_SNAPSHOT,
snapshotDirectory: ruleDirPath,
}).forEach((name, test) => {
return t.test(ruleDir.name + " > " + name, async (context) => {
const status = await test();
if (status === "skip") {
context.skip();
}
});
});
});
}
});

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is AWS Access Key ID: AKIAIOSFODNN7SECRETS
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"filePath": "[SNAPSHOT]/ng.AWS Access Key ID/input",
"messages": [
{
"message": "found AWS Access Key ID: AKIAIOSFODNN7SECRETS",
"range": [
27,
47
],
"type": "message",
"ruleId": "@secretlint/secretlint-rule-aws",
"ruleParentId": "@secretlint/secretlint-rule-preset-canary",
"loc": {
"start": {
"line": 1,
"column": 27
},
"end": {
"line": 1,
"column": 47
}
},
"severity": "error",
"messageId": "AWSAccessKeyID",
"docsUrl": "https://github.com/secretlint/secretlint/blob/master/packages/%40secretlint/secretlint-rule-aws/README.md#AWSAccessKeyID",
"data": {
"ID": "AKIAIOSFODNN7SECRETS"
}
}
],
"sourceContent": "This is AWS Access Key ID: AKIAIOSFODNN7SECRETS\n",
"sourceContentType": "text"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"filePath": "[SNAPSHOT]/aws/input",
"filePath": "[SNAPSHOT]/ng.AWS Account ID/input",
"messages": [
{
"message": "found AWS Account ID: \"AWS_ACCOUNT_ID\"=>0000-0000-0000",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AWS_SECRET_ACCESS_KEY = wJalrXUtnFEMI/K7MDENG/bPxRfiCYSECRETSKEY
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"filePath": "[SNAPSHOT]/ng.AWS Secret Access Key/input",
"messages": [
{
"message": "found AWS Secret Access Key: wJalrXUtnFEMI/K7MDENG/bPxRfiCYSECRETSKEY",
"range": [
0,
40
],
"type": "message",
"ruleId": "@secretlint/secretlint-rule-aws",
"ruleParentId": "@secretlint/secretlint-rule-preset-canary",
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 40
}
},
"severity": "error",
"messageId": "AWSSecretAccessKey",
"docsUrl": "https://github.com/secretlint/secretlint/blob/master/packages/%40secretlint/secretlint-rule-aws/README.md#AWSSecretAccessKey",
"data": {
"KEY": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYSECRETSKEY"
}
}
],
"sourceContent": "AWS_SECRET_ACCESS_KEY = wJalrXUtnFEMI/K7MDENG/bPxRfiCYSECRETSKEY\n",
"sourceContentType": "text"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# THIS IS NOT AWS SECRET
ANY_KEY = ZHVtbXkgdGV4dCBkYXRhIGRhdGEgZGF0YQ==
# This is encrypted
AWS_SECRET_ACCESS_KEY = R6WCNd0erkAuZyD+Fe8C4IwjpYQtniKqnHAzE7Pk5fPrBRpwOnuYxA==
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"filePath": "[SNAPSHOT]/ok.AWS Secret Access Key/input",
"messages": [],
"sourceContent": "# THIS IS NOT AWS SECRET\nANY_KEY = ZHVtbXkgdGV4dCBkYXRhIGRhdGEgZGF0YQ==\n# This is encrypted\nAWS_SECRET_ACCESS_KEY = R6WCNd0erkAuZyD+Fe8C4IwjpYQtniKqnHAzE7Pk5fPrBRpwOnuYxA==\n",
"sourceContentType": "text"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"rules": [
{
"id": "@secretlint/secretlint-rule-aws",
"options": {
"allows": [
"wJalrXUtnFEMI/K7MDENG/bPxRfiCYSECRETSKEY"
]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AWS_SECRET_ACCESS_KEY = wJalrXUtnFEMI/K7MDENG/bPxRfiCYSECRETSKEY
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"filePath": "[SNAPSHOT]/options.allows/input",
"messages": [],
"sourceContent": "AWS_SECRET_ACCESS_KEY = wJalrXUtnFEMI/K7MDENG/bPxRfiCYSECRETSKEY\n",
"sourceContentType": "text"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ad_tenant_id: "dac6fb3d-713c-4555-8a97-4089aa3849be"
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"filePath": "[SNAPSHOT]/ng.ad_tenant_id/input",
"messages": [
{
"message": "found Azure AD tenant ID: dac6fb3d-713c-4555-8a97-4089aa3849be",
"range": [
0,
36
],
"type": "message",
"ruleId": "@secretlint/secretlint-rule-azure",
"ruleParentId": "@secretlint/secretlint-rule-preset-canary",
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 36
}
},
"severity": "error",
"messageId": "AzureTenantId",
"docsUrl": "https://github.com/secretlint/secretlint/blob/master/packages/%40secretlint/secretlint-rule-azure/README.md#AzureTenantId",
"data": {
"ID": "dac6fb3d-713c-4555-8a97-4089aa3849be"
}
}
],
"sourceContent": "ad_tenant_id: \"dac6fb3d-713c-4555-8a97-4089aa3849be\"\n",
"sourceContentType": "text"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AZURE_CLIENT_ID=dac6fb3d-713c-4555-8a97-4089aa3849be
Loading
Loading