2021-11-07 04:14:14 +00:00
|
|
|
import * as fs from "fs";
|
|
|
|
import * as path from "path";
|
|
|
|
import { expect } from "chai";
|
|
|
|
|
2021-11-07 05:58:51 +00:00
|
|
|
import * as misc from "../src/misc";
|
2021-11-07 04:14:14 +00:00
|
|
|
|
2021-11-07 05:13:40 +00:00
|
|
|
describe("Misc: hidden file", () => {
|
2021-11-07 04:14:14 +00:00
|
|
|
it("should find hidden file correctly", () => {
|
2021-11-07 05:58:51 +00:00
|
|
|
let item = "";
|
2021-11-07 04:14:14 +00:00
|
|
|
expect(misc.isHiddenPath(item)).to.be.false;
|
|
|
|
|
2021-11-07 05:58:51 +00:00
|
|
|
item = ".";
|
2021-11-07 04:14:14 +00:00
|
|
|
expect(misc.isHiddenPath(item)).to.be.false;
|
|
|
|
|
2021-11-07 05:58:51 +00:00
|
|
|
item = "..";
|
2021-11-07 04:14:14 +00:00
|
|
|
expect(misc.isHiddenPath(item)).to.be.false;
|
|
|
|
|
2021-11-07 05:58:51 +00:00
|
|
|
item = "/x/y/z/../././../a/b/c";
|
2021-11-07 04:14:14 +00:00
|
|
|
expect(misc.isHiddenPath(item)).to.be.false;
|
|
|
|
|
2021-11-07 05:58:51 +00:00
|
|
|
item = ".hidden";
|
2021-11-07 04:14:14 +00:00
|
|
|
expect(misc.isHiddenPath(item)).to.be.true;
|
|
|
|
|
2021-11-07 05:58:51 +00:00
|
|
|
item = "_hidden_loose";
|
2021-11-07 04:14:14 +00:00
|
|
|
expect(misc.isHiddenPath(item)).to.be.true;
|
|
|
|
expect(misc.isHiddenPath(item, false)).to.be.false;
|
|
|
|
|
2021-11-07 05:58:51 +00:00
|
|
|
item = "/sdd/_hidden_loose";
|
2021-11-07 04:14:14 +00:00
|
|
|
expect(misc.isHiddenPath(item)).to.be.true;
|
|
|
|
|
2021-11-07 05:58:51 +00:00
|
|
|
item = "what/../_hidden_loose/what/what/what";
|
2021-11-07 04:14:14 +00:00
|
|
|
expect(misc.isHiddenPath(item)).to.be.true;
|
|
|
|
|
2021-11-07 05:58:51 +00:00
|
|
|
item = "what/../_hidden_loose/what/what/what";
|
2021-11-07 04:14:14 +00:00
|
|
|
expect(misc.isHiddenPath(item, false)).to.be.false;
|
|
|
|
|
2021-11-07 05:58:51 +00:00
|
|
|
item = "what/../_hidden_loose/../.hidden/what/what/what";
|
2021-11-07 04:14:14 +00:00
|
|
|
expect(misc.isHiddenPath(item, false)).to.be.true;
|
|
|
|
});
|
|
|
|
});
|
2021-11-07 05:58:51 +00:00
|
|
|
|
|
|
|
describe("Misc: get folder levels", () => {
|
|
|
|
it("should ignore empty path", () => {
|
|
|
|
const item = "";
|
|
|
|
expect(misc.getFolderLevels(item)).to.be.empty;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should ignore single file", () => {
|
|
|
|
const item = "xxx";
|
|
|
|
expect(misc.getFolderLevels(item)).to.be.empty;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should detect path ending with /", () => {
|
|
|
|
const item = "xxx/";
|
|
|
|
const res = ["xxx"];
|
|
|
|
expect(misc.getFolderLevels(item)).to.deep.equal(res);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should correctly split folders and files", () => {
|
|
|
|
const item = "xxx/yyy/zzz.md";
|
|
|
|
const res = ["xxx", "xxx/yyy"];
|
|
|
|
expect(misc.getFolderLevels(item)).to.deep.equal(res);
|
|
|
|
|
|
|
|
const item2 = "xxx/yyy/zzz";
|
|
|
|
const res2 = ["xxx", "xxx/yyy"];
|
|
|
|
expect(misc.getFolderLevels(item2)).to.deep.equal(res2);
|
|
|
|
|
|
|
|
const item3 = "xxx/yyy/zzz/";
|
|
|
|
const res3 = ["xxx", "xxx/yyy", "xxx/yyy/zzz"];
|
|
|
|
expect(misc.getFolderLevels(item3)).to.deep.equal(res3);
|
|
|
|
});
|
|
|
|
});
|
2021-11-09 02:00:44 +00:00
|
|
|
|
|
|
|
describe("Misc: vaild file name tests", () => {
|
|
|
|
it("should treat no ascii correctly", async () => {
|
|
|
|
const x = misc.isVaildText("😄🍎 apple 苹果");
|
|
|
|
// console.log(x)
|
|
|
|
expect(x).to.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should find not-printable chars correctly", async () => {
|
|
|
|
const x = misc.isVaildText("😄🍎 apple 苹果\u0000");
|
|
|
|
// console.log(x)
|
|
|
|
expect(x).to.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should allow spaces/slashes/...", async () => {
|
|
|
|
const x = misc.isVaildText("😄🍎 apple 苹果/-_=/\\*%^&@#$`");
|
|
|
|
expect(x).to.be.true;
|
|
|
|
});
|
|
|
|
});
|
2021-11-21 07:31:20 +00:00
|
|
|
|
|
|
|
describe("Misc: get dirname", () => {
|
|
|
|
it("should return itself for folder", async () => {
|
|
|
|
const x = misc.getPathFolder("ssss/");
|
|
|
|
// console.log(x)
|
|
|
|
expect(x).to.equal("ssss/");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return folder for file", async () => {
|
|
|
|
const x = misc.getPathFolder("sss/yyy");
|
|
|
|
// console.log(x)
|
|
|
|
expect(x).to.equal("sss/");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should treat / specially", async () => {
|
|
|
|
const x = misc.getPathFolder("/");
|
|
|
|
expect(x).to.equal("/");
|
|
|
|
|
|
|
|
const y = misc.getPathFolder("/abc");
|
|
|
|
expect(y).to.equal("/");
|
|
|
|
});
|
|
|
|
});
|