UNPKG

backport

Version:

A CLI tool that automates the process of backporting commits

62 lines 3.32 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const promises_1 = __importDefault(require("fs/promises")); const os_1 = __importDefault(require("os")); const make_dir_1 = __importDefault(require("make-dir")); const globalConfig_1 = require("./globalConfig"); describe('config', () => { afterEach(() => jest.clearAllMocks()); beforeEach(() => { jest.spyOn(os_1.default, 'homedir').mockReturnValue('/myHomeDir'); }); describe('getGlobalConfig', () => { let res; beforeEach(async () => { jest.spyOn(promises_1.default, 'chmod').mockResolvedValueOnce(); jest.spyOn(promises_1.default, 'writeFile').mockResolvedValueOnce(); jest.spyOn(promises_1.default, 'readFile').mockResolvedValueOnce(JSON.stringify({ accessToken: 'myAccessToken', })); res = await (0, globalConfig_1.getGlobalConfig)(); }); it("should create config if it doesn't exist", () => { expect(promises_1.default.writeFile).toHaveBeenCalledWith('/myHomeDir/.backport/config.json', expect.any(String), { flag: 'wx', mode: 384 }); }); it("should create .backport folder if it doesn't exist", () => { expect(make_dir_1.default).toHaveBeenCalledWith('/myHomeDir/.backport'); }); it('should load config', () => { expect(promises_1.default.readFile).toHaveBeenCalledWith('/myHomeDir/.backport/config.json', 'utf8'); }); it('should return config', () => { expect(res).toEqual({ accessToken: 'myAccessToken', }); }); }); describe('createGlobalConfigIfNotExist', () => { it("should create config if it does't exist", async () => { jest.spyOn(promises_1.default, 'writeFile').mockResolvedValueOnce(undefined); const didCreate = await (0, globalConfig_1.createGlobalConfigIfNotExist)('/path/to/globalConfig', 'myConfigTemplate'); expect(didCreate).toEqual(true); expect(promises_1.default.writeFile).toHaveBeenCalledWith('/path/to/globalConfig', expect.stringContaining('myConfigTemplate'), { flag: 'wx', mode: 384 }); }); it('should not fail if config already exists', async () => { const err = new Error(); err.code = 'EEXIST'; jest.spyOn(promises_1.default, 'writeFile').mockRejectedValueOnce(err); const didCreate = await (0, globalConfig_1.createGlobalConfigIfNotExist)('/path/to/global/config.json', 'myConfigTemplate'); expect(didCreate).toEqual(false); }); it("should fail gracefully if .backport folder doesn't exist", async () => { const err = new Error(); err.code = 'ENOENT'; jest.spyOn(promises_1.default, 'writeFile').mockRejectedValueOnce(err); await expect(() => (0, globalConfig_1.createGlobalConfigIfNotExist)('/path/to/global/config.json', 'myConfigTemplate')).rejects.toThrow('The .backport folder (/path/to/global/config.json) does not exist.'); }); }); }); //# sourceMappingURL=globalConfig.test.js.map