Skip to content

Commit

Permalink
feat: add script to write test files for V2 tests (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
elsapet authored Jan 24, 2024
1 parent bd688a6 commit 5de0fd6
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions scripts/write_v2_testfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const fs = require("fs");
const path = require("path");

function testjsContent(filePath, testCase) {
testName = filePath.split("/").pop();

return `const {
createNewInvoker,
getEnvironment,
} = require("../../../helper.js")
const { ruleId, ruleFile, testBase } = getEnvironment(__dirname)
describe(ruleId, () => {
const invoke = createNewInvoker(ruleId, ruleFile, testBase)
test("${testName}", () => {
const testCase = "${testCase}"
const results = invoke(testCase)
expect(results.Missing).toEqual([])
expect(results.Extra).toEqual([])
})
})`;
}

function writeTestFileIfNotExists(filePath) {
try {
filePath = filePath.replace(".yml", "");
filePath = filePath.replace("/rules/", "/tests/");

var content =
"// Use bearer:expected <rule_name> to flag expected findings";
if (filePath.includes("python") || filePath.includes("ruby")) {
content = "# Use bearer:expected <rule_name> to flag expected findings";
}

var testdataFileName = "";
if (filePath.includes("go")) {
testdataFileName = "main.go";
}
if (filePath.includes("java")) {
testdataFileName = "main.java";
}
if (filePath.includes("javascript")) {
// ordering is important here
// javascript must come after java
testdataFileName = "app.js";
}
if (filePath.includes("php")) {
testdataFileName = "index.php";
}
if (filePath.includes("python")) {
testdataFileName = "main.py";
}
if (filePath.includes("ruby")) {
testdataFileName = "main.rb";
}

const testdataFilePath = path.join(filePath, "testdata", testdataFileName);

// write testdata file
if (!fs.existsSync(testdataFilePath)) {
const directoryPath = path.dirname(testdataFilePath);

if (!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath, { recursive: true });
}

fs.writeFileSync(testdataFilePath, content, "utf8");
console.error(`testdata file written successfully - ${testdataFilePath}`);
} else {
console.error(`testdata file already exists - ${testdataFilePath}`);
}

// write test.js file
const testFilePath = path.join(filePath, "test.js");
if (!fs.existsSync(testFilePath)) {
const directoryPath = path.dirname(testFilePath);

if (!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath, { recursive: true });
}

fs.writeFileSync(
testFilePath,
testjsContent(filePath, testdataFileName),
"utf8",
);
console.error(`test.js file written successfully - ${testFilePath}`);
} else {
console.error(`test.js file already exists - ${testFilePath}`);
}
} catch (err) {
console.error("Error writing test file:", err);
}
}

writeTestFileIfNotExists(process.argv[2]);

0 comments on commit 5de0fd6

Please sign in to comment.