Skip to content

Commit

Permalink
fix: add test for ensure file exists
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisTowles committed Sep 29, 2022
1 parent 528329e commit 6f5d2e1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
15 changes: 14 additions & 1 deletion src/folderUtil.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { describe, expect, it } from 'vitest'
import { createImageDirWithImagePath, ensureFileAndGetItsDirectory, ensurePathIsDirectory, ensurePngAddedToFileName, makeImagePath, } from './folderUtil'
import { createImageDirWithImagePath, ensureFileAndGetItsDirectory, ensureFileExistsOrThrow, ensurePathIsDirectory, ensurePngAddedToFileName, makeImagePath, } from './folderUtil'
import * as upath from 'upath';

import * as fse from 'fs-extra';
import { MockLogger } from './test/mockLogger';

describe('FolderUtil', () => {

Expand Down Expand Up @@ -88,4 +89,16 @@ describe('FolderUtil', () => {
})



it('ensureFileExistsOrThrow - if given valid fileName', async () => {

expect(await ensureFileExistsOrThrow(__filename, new MockLogger())).toBeTruthy();
})


it('ensureFileExistsOrThrow - if given invalid fileName', async () => {

await expect(ensureFileExistsOrThrow(upath.join(__dirname, 'not-real.txt'), new MockLogger())).rejects.toThrow('Script file not found:');
})

})
3 changes: 2 additions & 1 deletion src/folderUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ export const createImageDirWithImagePath = async (imagePath: string): Promise<st
}


export async function ensureFileExists(scriptPath: string, logger: ILogger) {
export async function ensureFileExistsOrThrow(scriptPath: string, logger: ILogger): Promise<boolean> {
if (!await fse.pathExists(scriptPath)) {
const errorMsg = `Script file not found: ${scriptPath}`;
logger.showErrorMessage(errorMsg);
throw new Error(errorMsg);
}
return true;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/osTools/linux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import * as upath from 'upath';
import * as fse from 'fs-extra';
import { ILogger } from "../logger";
import { SaveClipboardImageToFileResult } from "../dto/SaveClipboardImageToFileResult";
import { ensureFileExists } from "../folderUtil";
import { ensureFileExistsOrThrow } from "../folderUtil";

export const linuxCreateImageWithXClip = async ({ imagePath, logger }: { imagePath: string; logger: ILogger; }): Promise<SaveClipboardImageToFileResult> => {
let scriptPath = upath.join(__dirname, '../res/linux.sh');

await ensureFileExists(scriptPath, logger);
await ensureFileExistsOrThrow(scriptPath, logger);

return new Promise<SaveClipboardImageToFileResult>((resolve, reject) => {

Expand Down
4 changes: 2 additions & 2 deletions src/osTools/macOS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import * as upath from 'upath';
import * as fse from 'fs-extra';
import { ILogger } from "../logger";
import { SaveClipboardImageToFileResult } from "../dto/SaveClipboardImageToFileResult";
import { ensureFileExists } from "../folderUtil";
import { ensureFileExistsOrThrow } from "../folderUtil";

export const macCreateImageWithAppleScript = async ({ imagePath, logger }: { imagePath: string; logger: ILogger; }): Promise<SaveClipboardImageToFileResult> => {

// Mac
let scriptPath = upath.join(__dirname, '../res/mac.applescript');

await ensureFileExists(scriptPath, logger);
await ensureFileExistsOrThrow(scriptPath, logger);

return new Promise<SaveClipboardImageToFileResult>(async (resolve, reject) => {

Expand Down
4 changes: 2 additions & 2 deletions src/osTools/win32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import * as upath from 'upath';
import * as fse from 'fs-extra';
import { ILogger } from "../logger";
import { SaveClipboardImageToFileResult } from "../dto/SaveClipboardImageToFileResult";
import { ensureFileExists } from "../folderUtil";
import { ensureFileExistsOrThrow } from "../folderUtil";

export const win32CreateImageWithPowershell = async ({ imagePath, logger }: { imagePath: string; logger: ILogger; }): Promise<SaveClipboardImageToFileResult> => {

// Windows
const scriptPath = upath.join(__dirname, '../res/windows.ps1');

await ensureFileExists(scriptPath, logger);
await ensureFileExistsOrThrow(scriptPath, logger);

return new Promise<SaveClipboardImageToFileResult>(async (resolve, reject) => {

Expand Down

0 comments on commit 6f5d2e1

Please sign in to comment.