mirror of
https://github.com/kemko/resume.git
synced 2026-01-01 15:55:47 +03:00
chore: add basic tests (#4)
* chore: add basic tests * chore: address feedback
This commit is contained in:
5
.github/workflows/generate-pdf.yml
vendored
5
.github/workflows/generate-pdf.yml
vendored
@@ -28,7 +28,10 @@ jobs:
|
||||
node-version: "20.5.0"
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm install -g pnpm && pnpm install
|
||||
run: npm install -g pnpm && pnpm install
|
||||
|
||||
- name: Run tests
|
||||
run: pnpm test
|
||||
|
||||
- name: Generate PDF
|
||||
run: pnpm build
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
import assert from 'node:assert';
|
||||
import { describe, it } from "node:test";
|
||||
|
||||
export const markdownFilePath = "./resume.md";
|
||||
export const outputFilePath = "output/art_rosnovsky_software_engineer.pdf";
|
||||
|
||||
describe("generatePdf", () => {
|
||||
it("should throw an error if the markdown file does not exist", async () => {
|
||||
assert(true)
|
||||
});
|
||||
|
||||
it("should throw an error if the mdToPdf function fails", async () => {
|
||||
assert(true)
|
||||
});
|
||||
|
||||
it("should successfully convert the markdown to a pdf and write it to the output file", async () => {
|
||||
assert(true)
|
||||
});
|
||||
});
|
||||
28
index.ts
28
index.ts
@@ -1,27 +1,13 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { mdToPdf } from "md-to-pdf";
|
||||
import { generatePdf } from './utils/generatePdf';
|
||||
|
||||
export const markdownFilePath = "./resume.md";
|
||||
export const outputFilePath = "output/art_rosnovsky_software_engineer.pdf";
|
||||
|
||||
export async function generatePdf() {
|
||||
if (!fs.existsSync(markdownFilePath)) {
|
||||
console.error(`File ${markdownFilePath} does not exist.`);
|
||||
throw new Error(`File ${markdownFilePath} does not exist.`);
|
||||
}
|
||||
const INPUT_FILE_PATH = `${process.cwd()}/resume.md`;
|
||||
const OUTPUT_FILE_PATH = `${process.cwd()}/output/art_rosnovsky_software_engineer.pdf`;
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const pdf = await mdToPdf({ path: markdownFilePath });
|
||||
|
||||
if (pdf) {
|
||||
fs.writeFileSync(path.resolve(__dirname, outputFilePath), pdf.content);
|
||||
console.log(`PDF has been written to ${outputFilePath}`);
|
||||
}
|
||||
await generatePdf(INPUT_FILE_PATH, OUTPUT_FILE_PATH);
|
||||
} catch (error: any) {
|
||||
console.error(`Error occurred: ${error.message}`);
|
||||
throw new Error(error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
generatePdf();
|
||||
})
|
||||
|
||||
Binary file not shown.
18
utils/fsChecks.ts
Normal file
18
utils/fsChecks.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import fs from 'node:fs';
|
||||
|
||||
export const checkFileExists = (filePath: string) => {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error(`File ${filePath} does not exist.`);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export const checkFolderExists = (folderPath: string) => {
|
||||
const folder = folderPath.split('/').slice(0, -1).join('/');
|
||||
if (!fs.existsSync(folder)) {
|
||||
console.error(`Folder ${folder} does not exist.`);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
42
utils/generatePdf.test.ts
Normal file
42
utils/generatePdf.test.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import assert from 'node:assert';
|
||||
import { after, afterEach, describe, test } from 'node:test';
|
||||
import { generatePdf } from "./generatePdf";
|
||||
import fs from 'node:fs';
|
||||
|
||||
const mockMarkdownFilePathDoesntExist = `${process.cwd()}/resume_not_found.md`;
|
||||
const mockMarkdownFilePathExists = `${process.cwd()}/resume.md`;
|
||||
const outputFilePath = `${process.cwd()}/outputings/some_random_file.pdf`;
|
||||
|
||||
// set current working directory to the root of the project
|
||||
|
||||
|
||||
describe('generatePdf', () => {
|
||||
test('should throw an error when markdown file not found', async () => {
|
||||
assert.rejects(generatePdf(mockMarkdownFilePathDoesntExist, outputFilePath), {
|
||||
name: 'Error',
|
||||
message: `File ${mockMarkdownFilePathDoesntExist} does not exist. How am I supposed to build you a resume, mate?`
|
||||
});
|
||||
})
|
||||
|
||||
test('should create an output folder if it doesn\'t exist', async () => {
|
||||
const folder = outputFilePath.split('/').slice(0, -1).join('/');
|
||||
await generatePdf(mockMarkdownFilePathExists, outputFilePath)
|
||||
assert.strictEqual(fs.existsSync(folder), true);
|
||||
|
||||
after(() => {
|
||||
fs.rmSync(folder, { recursive: true, force: true });
|
||||
});
|
||||
})
|
||||
|
||||
test('should generate a pdf from markdown', async () => {
|
||||
const folder = outputFilePath.split('/').slice(0, -1).join('/');
|
||||
|
||||
await generatePdf(mockMarkdownFilePathExists, outputFilePath);
|
||||
|
||||
assert.strictEqual(fs.existsSync(outputFilePath), true);
|
||||
|
||||
after(() => {
|
||||
fs.rmSync(folder, { recursive: true, force: true });
|
||||
});
|
||||
})
|
||||
})
|
||||
25
utils/generatePdf.ts
Normal file
25
utils/generatePdf.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import path from 'node:path';
|
||||
import fs from 'node:fs';
|
||||
import { checkFileExists, checkFolderExists } from './fsChecks';
|
||||
import { markdownToPdf } from './markdownToPdf';
|
||||
|
||||
export async function generatePdf(inputFilePath: string, outputFilePath: string) {
|
||||
if (!checkFileExists(inputFilePath)) {
|
||||
throw new Error(`File ${inputFilePath} does not exist. How am I supposed to build you a resume, mate?`);
|
||||
}
|
||||
|
||||
if (!checkFolderExists(outputFilePath)) {
|
||||
fs.mkdirSync(outputFilePath.split('/').slice(0, -1).join("/"), { recursive: true });
|
||||
}
|
||||
|
||||
try {
|
||||
const pdf = await markdownToPdf(inputFilePath);
|
||||
if (pdf) {
|
||||
fs.writeFileSync(path.resolve(__dirname, outputFilePath), pdf.content);
|
||||
return;
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error(`Error occurred: ${error.message}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
11
utils/markdownToPdf.ts
Normal file
11
utils/markdownToPdf.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import mdToPdf from 'md-to-pdf';
|
||||
|
||||
export const markdownToPdf = async (inputFilePath: string) => {
|
||||
try {
|
||||
const pdf = await mdToPdf({ path: inputFilePath });
|
||||
return pdf;
|
||||
} catch (error: any) {
|
||||
console.error(`Error occurred: ${error.message}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user