UNPKG

gitly

Version:

An API to download and/or extract git repositories

114 lines (113 loc) 4.96 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = require("fs"); const path_1 = require("path"); const shelljs_1 = require("shelljs"); const download_1 = __importDefault(require("../download")); const error_1 = require("../error"); describe('utils/fetch (no cache)', () => { const options = { temp: (0, path_1.join)(__dirname, 'output', 'fetch', '.gitcopy'), }; beforeEach(async () => { (0, shelljs_1.rm)('-rf', (0, path_1.join)(__dirname, 'output', 'fetch', '.gitcopy')); }); afterAll(async () => { (0, shelljs_1.rm)('-rf', (0, path_1.join)(__dirname, 'output', 'fetch', '.gitcopy')); }); it('should fetch "lukeed/gittar"', async () => { expect.assertions(2); const path = await (0, download_1.default)('lukeed/gittar', options); expect(path).toBeTruthy(); expect((0, fs_1.existsSync)(path)).toBe(true); }); it('should fetch "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, fs_1.existsSync)(path)).toBe(true); }); it('should fetch "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, fs_1.existsSync)(path)).toBe(true); }); it('should fetch "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, fs_1.existsSync)(path)).toBe(true); }); it('should fetch "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, fs_1.existsSync)(path)).toBe(true); }); it('should fetch "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, fs_1.existsSync)(path)).toBe(true); }); it('should fetch "github:lukeed/gittar"', async () => { expect.assertions(2); const path = await (0, download_1.default)('github:lukeed/gittar', options); expect(path).toBeTruthy(); expect((0, fs_1.existsSync)(path)).toBe(true); }); it('should fetch "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, fs_1.existsSync)(path)).toBe(true); }); it('should fetch "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, 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/fetch (cached)', () => { const options = { temp: (0, path_1.join)(__dirname, 'output', 'fetch', 'cache'), cache: true, }; const isCached = (ms) => Date.now() - ms <= 15; beforeAll(async () => { (0, shelljs_1.rm)('-rf', (0, path_1.join)(__dirname, 'output', 'fetch', 'cache')); // Prefetch const path = await (0, download_1.default)('lukeed/gittar', { temp: options.temp }); expect((0, fs_1.existsSync)(path)).toBe(true); }); afterAll(async () => { (0, shelljs_1.rm)('-rf', (0, path_1.join)(__dirname, 'output', 'fetch', '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, fs_1.existsSync)(path)).toBe(true); }); });