From cc1a82cb5fd994803b82587f619227d68b314853 Mon Sep 17 00:00:00 2001 From: mario-vimal Date: Sat, 9 Feb 2019 00:32:51 +0530 Subject: [PATCH 1/3] Add test for gitHandler --- test/gitHandlers.js | 113 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 test/gitHandlers.js diff --git a/test/gitHandlers.js b/test/gitHandlers.js new file mode 100644 index 0000000..d615a96 --- /dev/null +++ b/test/gitHandlers.js @@ -0,0 +1,113 @@ +process.env.NODE_ENV = 'test'; +const path = require('path'); +const chai = require('chai'); +// eslint-disable-next-line +const shell = require('shelljs'); +const git = require('simple-git/promise'); +const fs = require('fs'); +const randomString = require('randomstring'); +const GitHandler = require('../api/utils/gitHandlers'); + +appPath = path.resolve('../codecharacter-server-2019/'); +const getUserDir = username => `${appPath}/storage/codes/${username}`; + +describe('Test Git Handlers', async () => { + const username = 'gitHandler_testusername'; + const userDir = getUserDir(username); + const InitialCommitMessage = 'Initial Commit'; + const text = new Array(10); + // eslint-disable-next-line no-undef + before(async () => { + for (let index = 0; index < 10; index += 1) { + text[index] = randomString.generate(10); + } + }); + // eslint-disable-next-line no-undef + after(async () => { + await shell.rm('-rf', userDir); + }); + it('test createUserDir', async () => { + const handlerResponse = await GitHandler.createUserDir(username); + chai.assert(await shell.test('-d', userDir) === true); + const logResult = await git(userDir).log(); + chai.assert(await logResult.latest.message.includes(InitialCommitMessage)); + chai.assert(handlerResponse === true); + }); + + it('test latestCommit', async () => { + fs.writeFile(`${userDir}/code.cpp`, text[0], async () => {}); + await git(userDir).init(); + await git(userDir).add('./*'); + await git(userDir).commit(text[0]); + const latestCommit = await GitHandler.latestCommit(username); + chai.assert(latestCommit.message.includes(text[0])); + }); + + it('test commitLog', async () => { + fs.writeFile(`${userDir}/code.cpp`, text[1], async () => {}); + await git(userDir).init(); + await git(userDir).add('./*'); + await git(userDir).commit(text[1]); + const commitLog = await GitHandler.commitLog(username); + chai.assert(commitLog[0].message.includes(text[1])); + chai.assert(commitLog[1].message.includes(text[0])); + chai.assert(commitLog[2].message.includes(InitialCommitMessage)); + }); + + it('test add', async () => { + await git(userDir).commit(text[2]); + let logResult = await git(userDir).log(); + let latestCommit = logResult.latest; + chai.assert(!latestCommit.message.includes(text[2])); + fs.writeFile(`${userDir}/code.cpp`, text[2], async () => {}); + await GitHandler.add(username); + await git(userDir).commit(text[2]); + logResult = await git(userDir).log(); + latestCommit = logResult.latest; + chai.assert(latestCommit.message.includes(text[2])); + }); + + it('test diff', async () => { + let diff = await GitHandler.diff(username); + chai.assert(diff === ''); + fs.writeFile(`${userDir}/code.cpp`, text[3], async () => {}); + diff = await GitHandler.diff(username); + chai.assert(diff !== ''); + }); + + it('test diff staged', async () => { + fs.writeFile(`${userDir}/code.cpp`, text[4], async () => {}); + let diff = await GitHandler.diffStaged(username); + chai.assert(diff === ''); + await git(userDir).init(); + await git(userDir).add('./*'); + diff = await GitHandler.diffStaged(username); + chai.assert(diff !== ''); + }); + + it('test commit', async () => { + fs.writeFile(`${userDir}/code.cpp`, text[5], async () => {}); + await git(userDir).init(); + await git(userDir).add('./*'); + await GitHandler.commit(username, text[5]); + const logResult = await git(userDir).log(); + const latestCommit = logResult.latest; + chai.assert(latestCommit.message.includes(text[5])); + }); + + it('test getFile', async () => { + fs.writeFile(`${userDir}/code.cpp`, text[6], async () => {}); + const log = await git(userDir).log(); + let file = await GitHandler.getFile(username, 'code.cpp', log.latest.hash); + chai.assert(file === text[5]); + file = await GitHandler.getFile(username); + chai.assert(file === text[6]); + }); + + it('test setFile', async () => { + await GitHandler.setFile(username, text[7]); + const catResult = await shell.cat(`${userDir}/code.cpp`); + const result = catResult.stdout; + chai.assert(result === text[7]); + }); +}); From 9d2247eef1663134f857a6856ffc3a4757f1d060 Mon Sep 17 00:00:00 2001 From: mario-vimal Date: Sat, 9 Feb 2019 00:57:24 +0530 Subject: [PATCH 2/3] Fix await fs write --- test/gitHandlers.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/test/gitHandlers.js b/test/gitHandlers.js index d615a96..32fb302 100644 --- a/test/gitHandlers.js +++ b/test/gitHandlers.js @@ -6,16 +6,19 @@ const shell = require('shelljs'); const git = require('simple-git/promise'); const fs = require('fs'); const randomString = require('randomstring'); +const util = require('util'); const GitHandler = require('../api/utils/gitHandlers'); +const fsWriteFile = util.promisify(fs.writeFile); + appPath = path.resolve('../codecharacter-server-2019/'); const getUserDir = username => `${appPath}/storage/codes/${username}`; describe('Test Git Handlers', async () => { const username = 'gitHandler_testusername'; const userDir = getUserDir(username); - const InitialCommitMessage = 'Initial Commit'; - const text = new Array(10); + const initialCommitMessage = 'Initial Commit'; + const text = new Array(8); // eslint-disable-next-line no-undef before(async () => { for (let index = 0; index < 10; index += 1) { @@ -30,12 +33,12 @@ describe('Test Git Handlers', async () => { const handlerResponse = await GitHandler.createUserDir(username); chai.assert(await shell.test('-d', userDir) === true); const logResult = await git(userDir).log(); - chai.assert(await logResult.latest.message.includes(InitialCommitMessage)); + chai.assert(await logResult.latest.message.includes(initialCommitMessage)); chai.assert(handlerResponse === true); }); it('test latestCommit', async () => { - fs.writeFile(`${userDir}/code.cpp`, text[0], async () => {}); + await fsWriteFile(`${userDir}/code.cpp`, text[0]); await git(userDir).init(); await git(userDir).add('./*'); await git(userDir).commit(text[0]); @@ -44,14 +47,14 @@ describe('Test Git Handlers', async () => { }); it('test commitLog', async () => { - fs.writeFile(`${userDir}/code.cpp`, text[1], async () => {}); + await fsWriteFile(`${userDir}/code.cpp`, text[1]); await git(userDir).init(); await git(userDir).add('./*'); await git(userDir).commit(text[1]); const commitLog = await GitHandler.commitLog(username); chai.assert(commitLog[0].message.includes(text[1])); chai.assert(commitLog[1].message.includes(text[0])); - chai.assert(commitLog[2].message.includes(InitialCommitMessage)); + chai.assert(commitLog[2].message.includes(initialCommitMessage)); }); it('test add', async () => { @@ -59,7 +62,7 @@ describe('Test Git Handlers', async () => { let logResult = await git(userDir).log(); let latestCommit = logResult.latest; chai.assert(!latestCommit.message.includes(text[2])); - fs.writeFile(`${userDir}/code.cpp`, text[2], async () => {}); + await fsWriteFile(`${userDir}/code.cpp`, text[2]); await GitHandler.add(username); await git(userDir).commit(text[2]); logResult = await git(userDir).log(); @@ -70,13 +73,13 @@ describe('Test Git Handlers', async () => { it('test diff', async () => { let diff = await GitHandler.diff(username); chai.assert(diff === ''); - fs.writeFile(`${userDir}/code.cpp`, text[3], async () => {}); + await fsWriteFile(`${userDir}/code.cpp`, text[3]); diff = await GitHandler.diff(username); chai.assert(diff !== ''); }); it('test diff staged', async () => { - fs.writeFile(`${userDir}/code.cpp`, text[4], async () => {}); + await fsWriteFile(`${userDir}/code.cpp`, text[4]); let diff = await GitHandler.diffStaged(username); chai.assert(diff === ''); await git(userDir).init(); @@ -86,7 +89,7 @@ describe('Test Git Handlers', async () => { }); it('test commit', async () => { - fs.writeFile(`${userDir}/code.cpp`, text[5], async () => {}); + await fsWriteFile(`${userDir}/code.cpp`, text[5]); await git(userDir).init(); await git(userDir).add('./*'); await GitHandler.commit(username, text[5]); @@ -96,7 +99,7 @@ describe('Test Git Handlers', async () => { }); it('test getFile', async () => { - fs.writeFile(`${userDir}/code.cpp`, text[6], async () => {}); + await fsWriteFile(`${userDir}/code.cpp`, text[6]); const log = await git(userDir).log(); let file = await GitHandler.getFile(username, 'code.cpp', log.latest.hash); chai.assert(file === text[5]); From a25397df42a3c8d0e4dc3e41e98e0cf1c668bf3e Mon Sep 17 00:00:00 2001 From: mario-vimal Date: Sun, 10 Feb 2019 07:15:15 +0530 Subject: [PATCH 3/3] Fix multiple git init --- test/gitHandlers.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/gitHandlers.js b/test/gitHandlers.js index 32fb302..2243cf7 100644 --- a/test/gitHandlers.js +++ b/test/gitHandlers.js @@ -39,7 +39,6 @@ describe('Test Git Handlers', async () => { it('test latestCommit', async () => { await fsWriteFile(`${userDir}/code.cpp`, text[0]); - await git(userDir).init(); await git(userDir).add('./*'); await git(userDir).commit(text[0]); const latestCommit = await GitHandler.latestCommit(username); @@ -48,7 +47,6 @@ describe('Test Git Handlers', async () => { it('test commitLog', async () => { await fsWriteFile(`${userDir}/code.cpp`, text[1]); - await git(userDir).init(); await git(userDir).add('./*'); await git(userDir).commit(text[1]); const commitLog = await GitHandler.commitLog(username); @@ -82,7 +80,6 @@ describe('Test Git Handlers', async () => { await fsWriteFile(`${userDir}/code.cpp`, text[4]); let diff = await GitHandler.diffStaged(username); chai.assert(diff === ''); - await git(userDir).init(); await git(userDir).add('./*'); diff = await GitHandler.diffStaged(username); chai.assert(diff !== ''); @@ -90,7 +87,6 @@ describe('Test Git Handlers', async () => { it('test commit', async () => { await fsWriteFile(`${userDir}/code.cpp`, text[5]); - await git(userDir).init(); await git(userDir).add('./*'); await GitHandler.commit(username, text[5]); const logResult = await git(userDir).log();