Skip to content

Commit

Permalink
Fix: fontPipe - Synchronous log
Browse files Browse the repository at this point in the history
  • Loading branch information
black7375 committed Jan 9, 2023
1 parent 142235c commit 4018361
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 215 deletions.
129 changes: 64 additions & 65 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,98 +7,98 @@ import { timeout, cssFile, textFile, fontPath, fontDir, fontName, unlink, rmdir
// https://github.com/piscinajs/piscina/issues/83
describe("FontRange Offline Feature", () => {
const saveDir = join(fontDir, "offline");
beforeAll(async () => {
return await fontRange(fontPath, cssFile, {
saveDir,
nameFormat: "{NAME}.subset.{INDEX}{EXT}"
});
}, timeout);
afterAll(() => {
rmdir(saveDir);
});

it("Font Created Check", async () => {
await fontRange(fontPath, cssFile, {
saveDir,
nameFormat: "{NAME}.subset.{INDEX}{EXT}"
});

const parsed = await parseCSS(fontDir, cssFile);
const parsedL = parsed.length;
for (let counts = 0; counts < parsedL; counts++) {
const fontFile = join(saveDir, fontName + ".subset." + counts + ".woff2");
expect(existsSync(fontFile)).toBe(true);
}
});
}, timeout);
});

describe("FontRange Options with srcIndex", () => {
const saveDir = join(fontDir, "srcIndex");
beforeAll(async () => {
return await fontRange(fontPath, cssFile, {
afterAll(() => {
rmdir(saveDir);
});

it("Font Created Check", async () => {
await fontRange(fontPath, cssFile, {
saveDir,
nameFormat: "{NAME}.subset.{INDEX}{EXT}",
logFormat: "{ORIGIN} to {OUTPUT}",
fromCSS: "srcIndex"
});
}, timeout);
afterAll(() => {
rmdir(saveDir);
});

it("Font Created Check", async () => {
// Partial file checks
const fontFile1 = join(saveDir, fontName + ".subset." + 119 + ".woff2");
const fontFile2 = join(saveDir, fontName + ".subset." + 120 + ".woff2");
expect(existsSync(fontFile1)).toBe(false);
expect(existsSync(fontFile2)).toBe(true);
});
}, timeout);
});

describe("FontRange srcName Option", () => {
const saveDir = join(fontDir, "srcName");
beforeAll(async () => {
return await fontRange(fontPath, cssFile, {
saveDir,
fromCSS: "srcName"
});
}, timeout);
afterAll(() => {
rmdir(saveDir);
});

it("Font Created Check", async () => {
await fontRange(fontPath, cssFile, {
saveDir,
fromCSS: "srcName"
});

// Partial file checks
const fontFile1 = join(saveDir, "PbykFmXiEBPT4ITbgNA5Cgm20xz64px_1hVWr0wuPNGmlQNMEfD4.0.woff2" );
const fontFile2 = join(saveDir, "PbykFmXiEBPT4ITbgNA5Cgm20xz64px_1hVWr0wuPNGmlQNMEfD4.120.woff2");
expect(existsSync(fontFile1)).toBe(true);
expect(existsSync(fontFile2)).toBe(true);
});
}, timeout);
});

// online
describe("FontRange Online Feature", () => {
const saveDir = join(fontDir, "online");
beforeAll(async () => {
return await fontRange(fontPath, targets.korean, saveDir);
}, timeout);
afterAll(() => {
rmdir(saveDir);
});

it("Check CSS Download", () => {
it("Font Created Check", async () => {
await fontRange(fontPath, targets.korean, saveDir);

// CSS Downlad check
const cssPath = join(saveDir, "Noto Sans KR.css");
expect(existsSync(cssPath)).toBe(true);
});

it("Font Created Check", async () => {
// Create check
const parsed = await parseCSS(saveDir, cssFile);
const parsedL = parsed.length;
for (let counts = 0; counts < parsedL; counts++) {
const fontFile = join(saveDir, fontName + "_" + counts + ".woff2");
expect(existsSync(fontFile)).toBe(true);
}
});
}, timeout);
});

describe("FontSubset Format & Glyph Feature", () => {
const saveDir = join(fontDir, "format");
beforeAll(async () => {
afterAll(() => {
rmdir(saveDir);
});

it("Font Created Check", async () => {
const font1 = await fontSubset(fontPath, {
saveDir,
text: "abcd",
Expand All @@ -124,13 +124,8 @@ describe("FontSubset Format & Glyph Feature", () => {
text: "abcd",
format: "woff-zopfli"
});
return Promise.all([font1, font2, font3, font4, font5])
}, timeout);
afterAll(() => {
rmdir(saveDir);
});
await Promise.all([font1, font2, font3, font4, font5])

it("Font Created Check", async () => {
const fontFile1 = join(saveDir, fontName + "_" + ".otf" );
const fontFile2 = join(saveDir, fontName + "_" + ".ttf" );
const fontFile3 = join(saveDir, fontName + "_" + ".woff2");
Expand All @@ -141,36 +136,33 @@ describe("FontSubset Format & Glyph Feature", () => {
expect(existsSync(fontFile3)).toBe(true);
expect(existsSync(fontFile4)).toBe(true);
expect(existsSync(fontFile5)).toBe(true);
});
}, timeout);
});

describe("FontSubset Glyphs File Feature", () => {
beforeAll(async () => {
return await fontSubset(fontPath, {
it("Font Created Check", async () => {
await fontSubset(fontPath, {
textFile,
nameFormat: "{NAME}.glyphFile{INDEX}{EXT}"
});
}, timeout);

it("Font Created Check", async () => {
const fontFile = join(fontDir, fontName + ".glyphFile" + ".woff2");
expect(existsSync(fontFile)).toBe(true);

unlink(fontFile);
});
}, timeout);
});

describe("FontPipe Feature", () => {
beforeAll(async () => {
it("Font Created Check", async () => {
console.log = jest.fn();
const pipe = [
{ fontPath },
{ fontPath, option: { textFile, nameFormat: "{NAME}.pipe.{INDEX}{EXT}" } },
{ fontPath, option: { cssFile, nameFormat: "{NAME}.pipe.{INDEX}{EXT}" } }
];
return await fontPipe(pipe);
}, timeout * 3);
await fontPipe(pipe);

it("Font Created Check", async () => {
const fontFile1 = join(fontDir, fontName + "_" + ".woff2");
const fontFile2 = join(fontDir, fontName + ".pipe." + ".woff2");

Expand All @@ -186,60 +178,63 @@ describe("FontPipe Feature", () => {
expect(existsSync(eachFontPath)).toBe(true);
unlink(eachFontPath);
}
});
expect(console.log).not.toHaveBeenCalled();
}, timeout * 2);
});

describe("FontPipe Shard with Args", () => {
const saveDir = join(fontDir, "shardArgs");
beforeAll(async () => {
afterAll(() => {
rmdir(saveDir);
});

it("Font Created Check", async () => {
console.log = jest.fn();
const pipe = [
{ fontPath, option: { saveDir } },
{ fontPath, option: { saveDir, textFile, nameFormat: "{NAME}.pipe.{INDEX}{EXT}" } },
{ fontPath, option: { saveDir, cssFile, nameFormat: "{NAME}.pipe.{INDEX}{EXT}" } }
];
return await fontPipe(pipe, "1/2");
}, timeout);
afterAll(() => {
rmdir(saveDir);
});
await fontPipe(pipe, "1/2");

it("Font Created Check", async () => {
const fontFile1 = join(saveDir, fontName + "_" + ".woff2");
const fontFile2 = join(saveDir, fontName + ".pipe." + ".woff2");

expect(existsSync(fontFile1)).toBe(true);
expect(existsSync(fontFile2)).toBe(true);
});
expect(console.log).toHaveBeenCalledWith("== 1/2 ==========");
}, timeout);
});

describe("FontPipe Shard with Env", () => {
const saveDir = join(fontDir, "shardEnv");
const OLD_ENV = process.env;
beforeAll(async () => {
afterAll(() => {
process.env = OLD_ENV;
rmdir(saveDir);
});

it("Font Created Check", async () => {
console.log = jest.fn();
process.env = {
...OLD_ENV,
...OLD_ENV,
SHARD: "2/2"
};
const pipe = [
{ fontPath, option: { saveDir } },
{ fontPath, option: { saveDir, textFile, nameFormat: "{NAME}.pipe.{INDEX}{EXT}" } },
{ fontPath, option: { saveDir, cssFile, nameFormat: "{NAME}.pipe.{INDEX}{EXT}" } }
];
return await fontPipe(pipe);
}, timeout);
afterAll(() => {
process.env = OLD_ENV;
rmdir(saveDir);
});
await fontPipe(pipe);

it("Font Created Check", async () => {
const parsed = await parseCSS(fontDir, cssFile);
const parsedL = parsed.length;
for (let counts = 0; counts < parsedL; counts++) {
const eachFontPath = join(saveDir, fontName + ".pipe." + counts + ".woff2");
expect(existsSync(eachFontPath)).toBe(true);
}
});
expect(console.log).toHaveBeenCalledWith("== 2/2 ==========");
}, timeout);
});

describe("FontPipe Shard Error", () => {
Expand All @@ -257,12 +252,16 @@ describe("FontPipe Shard Error", () => {
});

it("Number parsing", async() => {
console.log = jest.fn();
await expect(run("a120")).rejects.toThrow("<index> must be a positive number");
await expect(run("a/20")).rejects.toThrow("<index> must be a positive number");
await expect(run("0/20")).rejects.toThrow("<index> must be a positive number");
await expect(run("1/a2")).rejects.toThrow("<total> must be a positive number");
expect(console.log).not.toHaveBeenCalled();
});
it("Index size", async() => {
console.log = jest.fn();
await expect(run("22/2")).rejects.toThrow("<index> must be less then <total>");
expect(console.log).not.toHaveBeenCalled();
});
});
29 changes: 24 additions & 5 deletions __tests__/preset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,37 @@ describe("Preset Check", () => {
});

describe("Pyftsubset Check", () => {
const fontFile = join(fontDir, fontName + ".preset.woff2");
it("Subset check with no Log", async () => {
const fontFile = join(fontDir, fontName + ".preset.woff2");
console.log = jest.fn();

beforeAll(() => {
return subset([
subset({options: [
fontPath,
"--output-file=" + fontFile,
"--text-file=" + textFile,
...defaultArgs
]);
]});

expect(console.log).not.toHaveBeenCalled();
expect(existsSync(fontFile)).toBe(true);
unlink(fontFile);
});

it("Subset check", async () => {
it("Subset check with log", async () => {
const fontFile = join(fontDir, fontName + ".log.woff2");
console.log = jest.fn();

subset({
options: [
fontPath,
"--output-file=" + fontFile,
"--text-file=" + textFile,
...defaultArgs
],
log: "== pyftsubset run =="
});

expect(console.log).toHaveBeenCalledWith("== pyftsubset run ==");
expect(existsSync(fontFile)).toBe(true);
unlink(fontFile);
});
Expand Down
Loading

0 comments on commit 4018361

Please sign in to comment.