test: fix unit tests

This commit is contained in:
Jelle Glebbeek
2021-10-27 00:38:44 +02:00
parent 37ff064f6e
commit 5428a8e72c
2 changed files with 36 additions and 51 deletions

View File

@@ -2,6 +2,7 @@ const FfmpegUpdater = require("../modules/FfmpegUpdater");
const fs = require("fs");
const axios = require("axios");
const { PassThrough } = require('stream');
const os = require('os');
beforeEach(() => {
jest.clearAllMocks();
@@ -48,6 +49,7 @@ describe("getLocalVersion", () => {
describe('getRemoteVersion', () => {
it('returns null on error', () => {
const axiosGetSpy = jest.spyOn(axios, 'get').mockRejectedValue({response: null});
jest.spyOn(os, 'arch').mockReturnValue('x64');
const instance = new FfmpegUpdater({platform: "darwin"});
return instance.getRemoteVersion().then((data) => {
expect(data).toEqual(null);
@@ -58,6 +60,7 @@ describe('getRemoteVersion', () => {
const axiosGetSpy = jest.spyOn(axios, 'get').mockResolvedValue({
data: { version: "4.2.1", bin: { "windows-32": { ffmpeg: "ffmpeg/link", ffprobe: "ffprobe/link" } } },
});
jest.spyOn(os, 'arch').mockReturnValue('ia32');
Object.defineProperty(process, "platform", {
value: "win32"
});
@@ -118,32 +121,3 @@ describe('checkUpdate', () => {
});
});
});
describe("downloadUpdate", () => {
it('does not write version info and rejects on error', async () => {
const mockReadable = new PassThrough();
const mockWriteable = new PassThrough();
jest.spyOn(fs, 'createWriteStream').mockReturnValueOnce(mockWriteable);
jest.spyOn(axios, 'get').mockResolvedValue({ data: mockReadable });
setTimeout(() => {
mockWriteable.emit('error', "Test error");
}, 100);
const instance = new FfmpegUpdater({ ffmpeg: "path/to/ffmpeg" });
const versionInfoSpy = jest.spyOn(instance, 'writeVersionInfo').mockImplementation(() => {});
const actualPromise = instance.downloadUpdate("link", "v2.0.0");
await expect(actualPromise).rejects.toEqual("Test error");
expect(versionInfoSpy).not.toBeCalled();
});
it('resolves when successful', async () => {
const mockReadable = new PassThrough();
const mockWriteable = new PassThrough();
jest.spyOn(fs, 'createWriteStream').mockReturnValueOnce(mockWriteable);
jest.spyOn(axios, 'get').mockResolvedValue({ data: mockReadable });
setTimeout(() => {
mockWriteable.emit('close');
}, 100);
const instance = new FfmpegUpdater({ ffmpeg: "path/to/ffmpeg" });
const actualPromise = instance.downloadUpdate("an/example/url", "ffmpeg.exe");
await expect(actualPromise).resolves.toBeTruthy();
});
});

View File

@@ -21,15 +21,14 @@ beforeEach(() => {
describe('set executable permissions', () => {
it('sets the chmod of ytdl and ffmpeg to 0o755', () => {
const instance = instanceBuilder(true);
instance.ytdl = "ytdl/path/test.exe";
instance.ffmpeg = "ffmpeg/path/test.exe";
fs.readdirSync = jest.fn().mockReturnValue([instance.ytdl, instance.ffmpeg]);
instance.ffmpeg = "ffmpeg/path/";
const ytdlp = "yt-dlp.exe";
const taskList = "taskList";
fs.readdirSync = jest.fn().mockReturnValue([ytdlp, taskList]);
instance.setPermissions();
expect(fs.chmod).toBeCalledTimes(2);
expect(fs.chmod.mock.calls[0]).toContain(instance.ytdl);
expect(fs.chmod).toBeCalledTimes(1);
expect(fs.chmod.mock.calls[0]).toContain(path.join(instance.ffmpeg, ytdlp));
expect(fs.chmod.mock.calls[0]).toContain(493);
expect(fs.chmod.mock.calls[1]).toContain(instance.ffmpeg);
expect(fs.chmod.mock.calls[1]).toContain(493);
});
});
@@ -55,6 +54,7 @@ describe('generate filepaths', () => {
instance.setPermissions = jest.fn();
instance.createFolder = jest.fn().mockResolvedValue(undefined);
const joinSpy = jest.spyOn(path, 'join').mockReturnValue("path");
jest.spyOn(instance, 'removeLeftOver').mockImplementation(() => Promise.resolve());
await instance.generateFilepaths();
if(platform === "linux" || platform === "win32") expect(joinSpy).toBeCalledTimes(1);
else expect(joinSpy).not.toBeCalled();
@@ -91,25 +91,36 @@ describe('generate filepaths', () => {
});
});
/*describe('create portable folder', () => {
it('does not copy the files if the folder already exists', async () => {
describe('removeLeftOver', () => {
it('removes youtube-dl.exe on win32', async () => {
Object.defineProperty(process, "platform", {
value: "win32"
});
const instance = instanceBuilder(true);
instance.unpackedPrefix = "test/unpacked/prefix";
fs.copyFileSync = jest.fn();
mkdirp.mockResolvedValue(null);
await instance.createAppDataFolder();
expect(fs.copyFileSync).not.toBeCalled();
instance.ffmpeg = "ffmpeg/path";
fs.existsSync = jest.fn().mockImplementation(() => true);
fs.promises.unlink = jest.fn().mockImplementation(() => Promise.resolve());
await instance.removeLeftOver();
expect(fs.promises.unlink).toBeCalledTimes(1);
expect(fs.promises.unlink).toBeCalledWith(path.join("ffmpeg/path", "youtube-dl.exe"));
});
it('copies 4 files if the directory did not exist yet', async () => {
it('removes youtube-dl-unix on other systems', async () => {
Object.defineProperty(process, "platform", {
value: "darwin"
});
const instance = instanceBuilder(true);
fs.copyFileSync = jest.fn();
const joinSpy = jest.spyOn(path, 'join').mockReturnValue("path");
mkdirp.mockResolvedValue("path/to/made/directory");
await instance.createAppDataFolder()
expect(fs.copyFileSync).toBeCalledTimes(4);
joinSpy.mockRestore();
instance.ffmpeg = "ffmpeg/path";
fs.existsSync = jest.fn().mockImplementation(() => true);
fs.promises.unlink = jest.fn().mockImplementation(() => Promise.resolve());
await instance.removeLeftOver();
expect(fs.promises.unlink).toBeCalledTimes(1);
expect(fs.promises.unlink).toBeCalledWith(path.join("ffmpeg/path", "youtube-dl-unix"));
});
});*/
});
function instanceBuilder(packaged, portable) {
const app = {