gitly
Version:
An API to download and/or extract git repositories
117 lines (116 loc) • 5.19 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_fs_1 = require("node:fs");
const node_path_1 = require("node:path");
const shelljs_1 = require("shelljs");
const download_1 = __importDefault(require("../download"));
const error_1 = require("../error");
describe('utils/download (no cache)', () => {
const options = {
temp: (0, node_path_1.join)(__dirname, 'output', 'download'),
};
beforeAll(() => {
(0, shelljs_1.rm)('-rf', (0, node_path_1.join)(__dirname, 'output', 'download'));
});
beforeEach(() => {
(0, shelljs_1.rm)('-rf', (0, node_path_1.join)(__dirname, 'output', 'download'));
});
afterAll(() => {
(0, shelljs_1.rm)('-rf', (0, node_path_1.join)(__dirname, 'output', 'download'));
});
it('should download "lukeed/gittar"', async () => {
expect.assertions(2);
const path = await (0, download_1.default)('lukeed/gittar', options);
expect(path).toBeTruthy();
expect((0, node_fs_1.existsSync)(path)).toBe(true);
});
it('should download "lukeed/gittar#v0.1.1"', async () => {
expect.assertions(2);
const path = await (0, download_1.default)('lukeed/gittar#v0.1.1', options);
expect(path).toBeTruthy();
expect((0, node_fs_1.existsSync)(path)).toBe(true);
});
it('should download "https://github.com/lukeed/gittar"', async () => {
expect.assertions(2);
const path = await (0, download_1.default)('https://github.com/lukeed/gittar', options);
expect(path).toBeTruthy();
expect((0, node_fs_1.existsSync)(path)).toBe(true);
});
it('should download "https://github.com/lukeed/gittar#v0.1.1"', async () => {
expect.assertions(2);
const path = await (0, download_1.default)('https://github.com/lukeed/gittar#v0.1.1', options);
expect(path).toBeTruthy();
expect((0, node_fs_1.existsSync)(path)).toBe(true);
});
it('should download "github.com/lukeed/gittar"', async () => {
expect.assertions(2);
const path = await (0, download_1.default)('github.com/lukeed/gittar', options);
expect(path).toBeTruthy();
expect((0, node_fs_1.existsSync)(path)).toBe(true);
});
it('should download "github.com/lukeed/gittar#v0.1.1"', async () => {
expect.assertions(2);
const path = await (0, download_1.default)('github.com/lukeed/gittar#v0.1.1', options);
expect(path).toBeTruthy();
expect((0, node_fs_1.existsSync)(path)).toBe(true);
});
it('should download "github:lukeed/gittar"', async () => {
expect.assertions(2);
const path = await (0, download_1.default)('github:lukeed/gittar', options);
expect(path).toBeTruthy();
expect((0, node_fs_1.existsSync)(path)).toBe(true);
});
it('should download "github:lukeed/gittar#v0.1.1"', async () => {
expect.assertions(2);
const path = await (0, download_1.default)('github:lukeed/gittar#v0.1.1', options);
expect(path).toBeTruthy();
expect((0, node_fs_1.existsSync)(path)).toBe(true);
});
it('should download "gitlab:Rich-Harris/buble#v0.15.2"', async () => {
expect.assertions(2);
const path = await (0, download_1.default)('gitlab:Rich-Harris/buble#v0.15.2', options);
expect(path).toBeTruthy();
expect((0, node_fs_1.existsSync)(path)).toBe(true);
});
it('should return an empty string when a repo is not found', async () => {
expect.assertions(1);
expect(await (0, download_1.default)('github:doesnotexist123xyz/gittar#v0.1.1')).toEqual('');
});
it('should throw an error when a repo is not found (with options', async () => {
expect.assertions(1);
try {
await (0, download_1.default)('github:doesnotexist123xyz/gittar#v0.1.1', {
throw: true,
});
}
catch (error) {
expect(error).toBeInstanceOf(error_1.GitlyDownloadError);
}
});
});
describe('utils/download (cached)', () => {
const options = {
temp: (0, node_path_1.join)(__dirname, 'output', 'download', 'cache'),
cache: true,
};
const isCached = (ms) => Date.now() - ms <= 15;
beforeAll(async () => {
(0, shelljs_1.rm)('-rf', (0, node_path_1.join)(__dirname, 'output', 'download', 'cache'));
// Predownload
const path = await (0, download_1.default)('lukeed/gittar', { temp: options.temp });
expect((0, node_fs_1.existsSync)(path)).toBe(true);
});
afterAll(async () => {
(0, shelljs_1.rm)('-rf', (0, node_path_1.join)(__dirname, 'output', 'download', 'cache'));
});
it('should return a path to the cached zipped file', async () => {
const start = Date.now();
const path = await (0, download_1.default)('lukeed/gittar', options);
expect(isCached(start)).toBe(true);
expect(path).toBeTruthy();
expect((0, node_fs_1.existsSync)(path)).toBe(true);
});
});