-
Notifications
You must be signed in to change notification settings - Fork 1
Add tests for gitHandler functions #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mario-vimal
wants to merge
3
commits into
master
Choose a base branch
from
Test-GitHandlers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| 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 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(8); | ||
| // 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 () => { | ||
| await fsWriteFile(`${userDir}/code.cpp`, text[0]); | ||
| 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 () => { | ||
| await fsWriteFile(`${userDir}/code.cpp`, text[1]); | ||
| 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])); | ||
| await fsWriteFile(`${userDir}/code.cpp`, text[2]); | ||
| 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 === ''); | ||
| await fsWriteFile(`${userDir}/code.cpp`, text[3]); | ||
| diff = await GitHandler.diff(username); | ||
| chai.assert(diff !== ''); | ||
| }); | ||
|
|
||
| it('test diff staged', async () => { | ||
| await fsWriteFile(`${userDir}/code.cpp`, text[4]); | ||
| let diff = await GitHandler.diffStaged(username); | ||
| chai.assert(diff === ''); | ||
| await git(userDir).add('./*'); | ||
| diff = await GitHandler.diffStaged(username); | ||
| chai.assert(diff !== ''); | ||
| }); | ||
|
|
||
| it('test commit', async () => { | ||
| await fsWriteFile(`${userDir}/code.cpp`, text[5]); | ||
| 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 () => { | ||
| 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]); | ||
| 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]); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.