UNPKG

ogit

Version:

A lazy developer's Git CLI made simple. Makes using git on cloud IDEs (i.e. C9) a walk in the park.

461 lines (460 loc) 26.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); require("reflect-metadata"); const git_1 = require("../git"); const fs = require("fs"); const SimpleGit = require("simple-git/promise"); const uuid = require("uuid"); const models_1 = require("../../models"); xdescribe('ogit', () => { describe('wrapper', () => { describe('git', () => { describe('status', () => { it('should return a list of created files when a file has created', (done) => tslib_1.__awaiter(this, void 0, void 0, function* () { const fileName = uuid.v4() + '.txt'; createAndWriteToFile(fileName); const status = yield git_1.GitFacade.status(); status.created.forEach((stat) => tslib_1.__awaiter(this, void 0, void 0, function* () { if (stat.path === fileName) { fs.unlinkSync(fileName); done(); } })); })); it('should return a list of changed files when a file has changed', (done) => tslib_1.__awaiter(this, void 0, void 0, function* () { fs.appendFileSync('tslint.json', '{}'); const status = yield git_1.GitFacade.status(); status.modified.forEach((stat) => tslib_1.__awaiter(this, void 0, void 0, function* () { if (stat.path === 'tslint.json') { yield SimpleGit().raw(['checkout', '--', 'tslint.json']); done(); } })); })); it('should return a list of deleted files when a file has been deleted', (done) => tslib_1.__awaiter(this, void 0, void 0, function* () { fs.unlinkSync('tslint.json'); const status = yield git_1.GitFacade.status(); status.deleted.forEach((stat) => tslib_1.__awaiter(this, void 0, void 0, function* () { if (stat.path === 'tslint.json') { yield SimpleGit().raw(['checkout', '--', 'tslint.json']); done(); } })); })); it('should return a list of added files when a file has been added', (done) => tslib_1.__awaiter(this, void 0, void 0, function* () { const fileName = uuid.v4() + '.txt'; createAndWriteToFile(fileName); yield SimpleGit().raw(['add', '.']); const status = yield git_1.GitFacade.status(); status.added.forEach((stat) => tslib_1.__awaiter(this, void 0, void 0, function* () { if (stat.path === fileName) { yield SimpleGit().raw(['reset', fileName]); fs.unlinkSync(fileName); done(); } })); })); }); describe('originUrl', () => { it('should return the right value', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const originUrl = yield git_1.GitFacade.originUrl(); expect(originUrl).toEqual('https://ssiraj@bitbucket.org/ssiraj/ogit.git'); })); }); describe('initialized', () => { it('should be already initalized', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const alreadyInitialized = yield git_1.GitFacade.initialize(); expect(alreadyInitialized).toBeFalsy(); })); }); describe('listBranches', () => { it('should return a list', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const branchesSummary = yield git_1.GitFacade.listBranches(); expect(branchesSummary.length > 0).toBeTruthy(); })); it('should return a list containing local branch master', (done) => tslib_1.__awaiter(this, void 0, void 0, function* () { const branchesSummary = yield git_1.GitFacade.listBranches(); for (let branchSummary of branchesSummary) { if (branchSummary.name === 'master' && branchSummary.isLocal === true) { done(); return; } } })); it('should return a list containing remote branch master', (done) => tslib_1.__awaiter(this, void 0, void 0, function* () { const branchesSummary = yield git_1.GitFacade.listBranches(); for (let branchSummary of branchesSummary) { if (branchSummary.name === 'origin/master' && branchSummary.isLocal === false) { done(); return; } } })); }); describe('optimizeRepo', () => { it('should not fail', () => tslib_1.__awaiter(this, void 0, void 0, function* () { let success = true; try { yield git_1.GitFacade.optimizeRepo(); } catch (err) { console.log(err); success = false; } expect(success).toBeTruthy(); })); }); describe('addToRepo', () => { it('should be able to add new files', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const file1 = uuid.v4() + '.txt'; createAndWriteToFile(file1); yield git_1.GitFacade.addToRepo(file1); const file2 = uuid.v4() + '.txt'; createAndWriteToFile(file2); yield git_1.GitFacade.addToRepo(file2); const file3 = uuid.v4() + '.txt'; createAndWriteToFile(file3); yield git_1.GitFacade.addToRepo(file3); const status = yield git_1.GitFacade.status(); let found = 0; for (let i = 0; i < status.added.length; i++) { if (status.added[i].path === file1 || status.added[i].path === file2 || status.added[i].path === file3) yield SimpleGit().raw(['reset', status.added[i].path]); found++; } fs.unlinkSync(file1); fs.unlinkSync(file2); fs.unlinkSync(file3); expect(found).toBe(3); })); }); describe('getLastCommitMessage', () => { it('should return a string value', () => tslib_1.__awaiter(this, void 0, void 0, function* () { console.log('test'); const response = yield git_1.GitFacade.getLastCommitMessage(); expect(typeof response === 'string').toBeTruthy(); expect(response).toBeDefined(); })); }); describe('getLastCommitHash', () => { it('should return a string value', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const response = yield git_1.GitFacade.getLastCommitHash(); expect(typeof response === 'string').toBeTruthy(); expect(response).toBeDefined(); })); }); describe('getFileNamesFromLastCommit', () => { it('should return a string array', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const response = yield git_1.GitFacade.getFileNamesFromCommit(yield git_1.GitFacade.getLastCommitHash()); expect(response).toBeDefined(); })); }); describe('ammendLastCommit', () => { it('should amend a new file to the commit', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const lastCommitHashBeforeTest = yield git_1.GitFacade.getLastCommitHash(); const file1 = uuid.v4() + '.txt'; createAndWriteToFile(file1); yield git_1.GitFacade.addToRepo(file1); const message = 'testing ammendLastCommit > should add a new file to the commit'; yield git_1.GitFacade.commit(message, [file1], true); const file2 = uuid.v4() + '.txt'; createAndWriteToFile(file2); yield git_1.GitFacade.addToRepo(file2); const commitSummary = yield git_1.GitFacade.ammendLastCommit([file2], message); expect(yield git_1.GitFacade.getLastCommitMessage()).toBe(message); const fileNames = yield git_1.GitFacade.getFileNamesFromCommit(commitSummary.commit); yield SimpleGit().raw(['reset', '--hard', lastCommitHashBeforeTest]); expect(fileNames.length).toBe(2); let found = 0; for (let i = 0; i < fileNames.length; i++) { if (fileNames[i] === file1 || fileNames[i] === file2) { found++; } } expect(found).toBe(2); })); }); //efa0e915a7d4c27fca2002350e47aceda4e6b872 - Fix for scenario where there was not files in the commit describe('getMessageFromCommitHash', () => { it('should return the subject as a string for a commit', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const response = yield git_1.GitFacade.getMessageFromCommitHash('efa0e915a7d4c27fca2002350e47aceda4e6b872'); expect(response).toBe('Fix for scenario where there was not files in the commit'); })); }); describe('revertCommit', () => { it('should keep the files in fileSystem', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const lastCommitHashBeforeTest = yield git_1.GitFacade.getLastCommitHash(); const file1 = uuid.v4() + '.txt'; createAndWriteToFile(file1); yield git_1.GitFacade.addToRepo(file1); const message = 'testing revertCommit > should keep the files in fileSystem'; const summary = yield git_1.GitFacade.commit(message, [file1], true); yield git_1.GitFacade.revertCommit(summary.commit); expect(fs.existsSync(file1)).toBeTruthy(); yield SimpleGit().raw(['reset', '--hard', lastCommitHashBeforeTest]); })); it('should cleanup the hash from repo', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const lastCommitHashBeforeTest = yield git_1.GitFacade.getLastCommitHash(); const file1 = uuid.v4() + '.txt'; createAndWriteToFile(file1); yield git_1.GitFacade.addToRepo(file1); const message = 'testing revertCommit > should cleanup the hash from repo'; const summary = yield git_1.GitFacade.commit(message, [file1], true); yield git_1.GitFacade.revertCommit(summary.commit); const hashExists = yield SimpleGit().raw([ 'branch', '--contains', summary.commit ]); expect(hashExists).toBeNull(); yield SimpleGit().raw(['reset', '--hard', lastCommitHashBeforeTest]); })); }); describe('deleteCommit', () => { it('should delete the files in fileSystem', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const lastCommitHashBeforeTest = yield git_1.GitFacade.getLastCommitHash(); const file1 = uuid.v4() + '.txt'; createAndWriteToFile(file1); yield git_1.GitFacade.addToRepo(file1); const message = 'testing revertCommit > should delete the files in fileSystem'; const summary = yield git_1.GitFacade.commit(message, [file1], true); yield git_1.GitFacade.deleteCommit(summary.commit); expect(fs.existsSync(file1)).toBeFalsy(); yield SimpleGit().raw(['reset', '--hard', lastCommitHashBeforeTest]); })); it('should cleanup the hash from repo', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const lastCommitHashBeforeTest = yield git_1.GitFacade.getLastCommitHash(); const file1 = uuid.v4() + '.txt'; createAndWriteToFile(file1); yield git_1.GitFacade.addToRepo(file1); const message = 'testing revertCommit > should cleanup the hash from repo'; const summary = yield git_1.GitFacade.commit(message, [file1], true); yield git_1.GitFacade.deleteCommit(summary.commit); const hashExists = yield SimpleGit().raw([ 'branch', '--contains', summary.commit ]); expect(hashExists).toBeNull(); yield SimpleGit().raw(['reset', '--hard', lastCommitHashBeforeTest]); })); }); describe('createBranch', () => { it('should be able to create a new branch', (done) => tslib_1.__awaiter(this, void 0, void 0, function* () { const newBranchName = 'branch_' + uuid.v4(); yield git_1.GitFacade.createBranch(newBranchName, 'origin/develop'); const branches = yield git_1.GitFacade.listBranches(); for (let branch of branches) { console.log('Branch ' + branch.name); if (branch.name === newBranchName) { yield SimpleGit().deleteLocalBranch(newBranchName); done(); } } })); }); describe('switchBranch', () => { it('should be able to switch to a new branch', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const newBranchName = 'branch_' + uuid.v4(); const currentBranchName = yield git_1.GitFacade.getCurrentBranchName(); yield git_1.GitFacade.createBranch(newBranchName, 'origin/develop'); yield git_1.GitFacade.switchBranch(newBranchName); expect(yield git_1.GitFacade.getCurrentBranchName()).toBe(newBranchName); yield git_1.GitFacade.switchBranch(currentBranchName); yield SimpleGit().deleteLocalBranch(newBranchName); })); }); describe('renameBranch', () => { it('should be able to rename current branch', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const fakeBranchName = 'test-remote-branch'; const currentBranchName = yield git_1.GitFacade.getCurrentBranchName(); yield git_1.GitFacade.renameBranch(currentBranchName, fakeBranchName); expect(yield git_1.GitFacade.getCurrentBranchName()).toBe(fakeBranchName); yield git_1.GitFacade.renameBranch(fakeBranchName, currentBranchName); expect(yield git_1.GitFacade.getCurrentBranchName()).toBe(currentBranchName); })); }); describe('deleteLocalBranch', () => { it('should be able to delete a local branch', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const newBranchName = 'branch_' + uuid.v4(); yield git_1.GitFacade.createBranch(newBranchName, 'origin/develop'); yield git_1.GitFacade.deleteLocalBranch(newBranchName); })); }); describe('stash', () => { it('clearStash should clear all the stashed changes', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const file1 = uuid.v4() + '.txt'; createAndWriteToFile(file1); yield SimpleGit().raw(['stash', '-u']); expect(fs.existsSync(file1)).toBeFalsy(); yield git_1.GitFacade.clearStash(); expect(fs.existsSync(file1)).toBeFalsy(); })); }); describe('getStashes', () => { it('should return a list of stash entries', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const file1 = uuid.v4() + '.txt'; createAndWriteToFile(file1); const file2 = uuid.v4() + '.txt'; createAndWriteToFile(file2); yield SimpleGit().raw(['stash', '-u', '-m getStashes test']); const stashes = yield git_1.GitFacade.getStashes(); yield git_1.GitFacade.clearStash(); const currentBranchName = yield git_1.GitFacade.getCurrentBranchName(); expect(stashes.length).toBe(1); expect(stashes[0]).toEqual({ stashNumber: 0, branchName: currentBranchName, stashName: 'getStashes test', files: [file1, file2].sort() }); })); }); describe('deleteStash', () => { it('should delete a stash entry based on number', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const file1 = uuid.v4() + '.txt'; createAndWriteToFile(file1); const file2 = uuid.v4() + '.txt'; createAndWriteToFile(file2); yield SimpleGit().raw(['stash', '-u', '-m deleteStash test1']); const file3 = uuid.v4() + '.txt'; createAndWriteToFile(file3); yield SimpleGit().raw(['stash', '-u', '-m deleteStash test2']); yield git_1.GitFacade.deleteStash(1, ''); const stashes = yield git_1.GitFacade.getStashes(); yield git_1.GitFacade.clearStash(); const currentBranchName = yield git_1.GitFacade.getCurrentBranchName(); expect(stashes.length).toBe(1); expect(stashes[0]).toEqual({ stashNumber: 0, branchName: currentBranchName, stashName: 'deleteStash test2', files: [file3] }); })); }); describe('unstash', () => { it('should pop a stash entry based on number', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const file1 = uuid.v4() + '.txt'; createAndWriteToFile(file1); const file2 = uuid.v4() + '.txt'; createAndWriteToFile(file2); yield SimpleGit().raw(['stash', '-u', '-m unstash test1']); yield git_1.GitFacade.unstash(0, ''); yield git_1.GitFacade.clearStash(); expect(fs.existsSync(file1)).toBeTruthy(); expect(fs.existsSync(file2)).toBeTruthy(); fs.unlinkSync(file1); fs.unlinkSync(file2); })); it('should leave the other stash entries intact', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const file1 = uuid.v4() + '.txt'; createAndWriteToFile(file1); const file2 = uuid.v4() + '.txt'; createAndWriteToFile(file2); yield SimpleGit().raw(['stash', '-u', '-m unstash test1']); const file3 = uuid.v4() + '.txt'; createAndWriteToFile(file3); yield SimpleGit().raw(['stash', '-u', '-m unstash test2']); yield git_1.GitFacade.unstash(1, ''); const stashes = yield git_1.GitFacade.getStashes(); yield git_1.GitFacade.clearStash(); expect(fs.existsSync(file1)).toBeTruthy(); expect(fs.existsSync(file2)).toBeTruthy(); fs.unlinkSync(file1); fs.unlinkSync(file2); const currentBranchName = yield git_1.GitFacade.getCurrentBranchName(); expect(stashes.length).toBe(1); expect(stashes[0]).toEqual({ stashNumber: 0, branchName: currentBranchName, stashName: 'unstash test2', files: [file3] }); })); }); describe('stash', () => { it('should stash a partial selection of files', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const file1 = uuid.v4() + '.txt'; createAndWriteToFile(file1); const file2 = uuid.v4() + '.txt'; createAndWriteToFile(file2); const file3 = uuid.v4() + '.txt'; createAndWriteToFile(file3); yield git_1.GitFacade.stash('test stash1', [file1, file2]); expect(fs.existsSync(file1)).toBeFalsy(); expect(fs.existsSync(file2)).toBeFalsy(); expect(fs.existsSync(file3)).toBeTruthy(); const stashes = yield git_1.GitFacade.getStashes(); yield git_1.GitFacade.clearStash(); const currentBranchName = yield git_1.GitFacade.getCurrentBranchName(); expect(stashes[0]).toEqual({ stashNumber: 0, branchName: currentBranchName, stashName: 'test stash1', files: [file1, file2].sort() }); fs.unlinkSync(file3); })); it('should stash all the files', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const file1 = uuid.v4() + '.txt'; createAndWriteToFile(file1); const file2 = uuid.v4() + '.txt'; createAndWriteToFile(file2); const file3 = uuid.v4() + '.txt'; createAndWriteToFile(file3); yield git_1.GitFacade.stash('test stash2', [file1, file2, file3], false); expect(fs.existsSync(file1)).toBeFalsy(); expect(fs.existsSync(file2)).toBeFalsy(); expect(fs.existsSync(file3)).toBeFalsy(); const stashes = yield git_1.GitFacade.getStashes(); yield git_1.GitFacade.clearStash(); expect(stashes[0].files).toContain(file1); expect(stashes[0].files).toContain(file2); expect(stashes[0].files).toContain(file3); })); }); describe('revertFile', () => { it('should be able to delete an un-stages file', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const file1 = uuid.v4() + '.txt'; createAndWriteToFile(file1); const gitFile = new models_1.GitFile(file1, ' ', models_1.ChangeTypes.New); yield git_1.GitFacade.revertFile(gitFile); expect(fs.existsSync(file1)).toBeFalsy(); })); it('should be able to delete a newly added file', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const file1 = uuid.v4() + '.txt'; createAndWriteToFile(file1); yield git_1.GitFacade.addToRepo(file1); const gitFile = new models_1.GitFile(file1, ' ', models_1.ChangeTypes.Added); yield git_1.GitFacade.revertFile(gitFile); expect(fs.existsSync(file1)).toBeFalsy(); })); it('should be able to revert an updated file', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const file = 'package-lock.json'; createAndWriteToFile(file); const gitFile = new models_1.GitFile(file, ' ', models_1.ChangeTypes.Modified); yield git_1.GitFacade.revertFile(gitFile); expect(fs.existsSync(file)).toBeTruthy(); const status = yield git_1.GitFacade.status(); status.modified.forEach(gitFile => { if (gitFile.path === file) { fail(); } }); })); }); }); }); }); function createAndWriteToFile(fileName) { const fd = fs.openSync(fileName, 'w'); fs.writeFileSync(fileName, 'blah!'); fs.closeSync(fd); return fd; }