Skip to content

Commit

Permalink
feat: add exclude to test checkPersian
Browse files Browse the repository at this point in the history
  • Loading branch information
hosseinmd committed Aug 28, 2022
1 parent 440c295 commit 65d0aa8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 56 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"build": "yarn lerna-clean && yarn lerna-build",
"lint": "eslint . --ext tsx,ts --fix",
"lint-commit": "cross-env COMMIT=true eslint . --ext tsx,ts --fix",
"test": "yarn build && react-app-rewired test --silent --detectOpenHandles",
"test": "yarn build && react-app-rewired test --detectOpenHandles",
"prettierAll": "prettier --write .",
"storybook": "start-storybook -p 6006 -s public",
"storybook-build": "yarn build && build-storybook -s public",
Expand Down
102 changes: 47 additions & 55 deletions src/tests/src/persianChars.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,72 @@
import fs from "fs";
import path from "path";

function readRoot(resolver: (...dirs: string[]) => string) {
return fs.readdirSync(resolver(""));
}

function recursiveChecker(
componentDir: string,
resolver: (...dirs: string[]) => string,
isIgnore: (dir: string) => boolean,
) {
const files = fs.readdirSync(resolver(componentDir));
files.forEach((file) => {
if (fs.lstatSync(resolver(componentDir, file)).isDirectory()) {
if (
file === "__tests__" ||
file === "__mocks__" ||
file === "__snapshots__"
) {
return;
}

recursiveChecker(resolver(componentDir, file), resolver);
if (fs.lstatSync(componentDir).isDirectory()) {
if (
isIgnore(componentDir) ||
componentDir.endsWith("__tests__") ||
componentDir.endsWith("__mocks__") ||
componentDir.endsWith("__snapshots__")
) {
return;
}
const regexCodeFiles = /.(ts|js|tsx|jsx)$/;
const files = fs.readdirSync(componentDir);
files.forEach((file) => {
recursiveChecker(path.join(componentDir, file), resolver, isIgnore);
});

if (!regexCodeFiles.test(file)) {
return;
}
return;
}

if (/\.(test|spec)\.(t|j)sx?$/g.test(file) && /\.d\.tsx?$/g.test(file)) {
return;
}
const regexCodeFiles = /.(ts|js|tsx|jsx)$/;

if (!regexCodeFiles.test(componentDir)) {
return;
}

const content = fs.readFileSync(resolver(componentDir, file)).toString();
if (
/\.(test|spec)\.(t|j)sx?$/g.test(componentDir) &&
/\.d\.tsx?$/g.test(componentDir)
) {
return;
}

if (content.split("\n")[0].includes("//ignore-localize")) return;
const content = fs.readFileSync(componentDir).toString();

content.split("\n").forEach((line, index) => {
const matched = line.match(/[آ-ی]/g);
if (content.split("\n")[0].includes("//ignore-localize")) return;

if (matched) {
const error = new Error(
`An error occured in ${componentDir
.split(/[\\?]/)
.join("/")}/${file}:${index + 1}:0 path at line ${line}`,
);
error.stack = `Error:\nat (${resolver(componentDir, file)}:${
content.split("\n").forEach((line, index) => {
const matched = line.match(/[آ-ی]/g);

if (matched) {
const error = new Error(
`An error occured in ${componentDir.split(/[\\?]/).join("/")}:${
index + 1
}:0)\n`;
test(componentDir, () =>
expect(() => {
throw error;
}).not.toThrow(),
);
}
});
}:0 path at line ${line}`,
);
error.stack = `Error:\nat (${componentDir}:${index + 1}:0)\n`;
test(componentDir, () =>
expect(() => {
throw error;
}).not.toThrow(),
);
}
});
}

function checkPersian(dirname: string) {
function checkPersian(dirname: string, exclude: string[] = []) {
const resolver = (...dirs: string[]) => path.resolve(dirname, `../`, ...dirs);

const isIgnore = (_dirname: string) =>
!!exclude.find((dir) => resolver(dir) === _dirname);

describe("Persian Characters Localizing", () => {
const rootDirectories = readRoot(resolver);
rootDirectories.forEach((rootDirectory) => {
if (fs.lstatSync(resolver(rootDirectory)).isDirectory()) {
const componentsDirs = fs.readdirSync(resolver(rootDirectory));
componentsDirs.forEach((componentsDir) => {
if (fs.lstatSync(resolver(rootDirectory, componentsDir)).isFile()) {
return;
}
recursiveChecker(path.join(rootDirectory, componentsDir), resolver);
});
}
});
recursiveChecker(resolver(""), resolver, isIgnore);

it("fake", () => {
expect(true).toBe(true);
Expand Down

0 comments on commit 65d0aa8

Please sign in to comment.