Skip to content

Commit

Permalink
chore(new): add e2e test for ng new command
Browse files Browse the repository at this point in the history
  • Loading branch information
Meligy committed Nov 25, 2016
1 parent 132a9b8 commit 6854bb5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
45 changes: 45 additions & 0 deletions tests/e2e/tests/commands/new.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {deleteFile, deleteDir} from '../../utils/fs';
import * as path from 'path';
import {ng, git} from '../../utils/process';
import {gitCommit} from '../../utils/git';

export default function() {
const parentTestprojectDir = process.cwd();
const projectName = 'new-test-project';

return Promise.resolve()
// The test setup already creates a project, lets' clean that before running `ng new`
.then(() => removeSetupProject(parentTestprojectDir))

// Run `ng new`
.then(() => ng('new', projectName))
.then(() => process.chdir(path.join(parentTestprojectDir, projectName)))

// Try to run the unit tests.
.then(() => ng('test', '--single-run'))

// Run post-test steps whether test passes or fails
.then(prepareGitForTestCleanup, prepareGitForTestCleanup);
}

// Change the project inherited from Setup into normal
// non angular-cli project folder, so we can call `ng new`
function removeSetupProject (parentTestprojectDir: string) {
return Promise.resolve()

// Change the project inherited from Setup into normal
// non angular-cli project folder, so we can call `ng new`
.then(() => process.chdir(parentTestprojectDir))
.then(() => deleteDir('.git'))
.then(() => deleteFile('angular-cli.json'))
.then(() => deleteFile('package.json'));
}

// The post-test cleanup breaks if it doesn't have a git repository with a branch and a commit
function prepareGitForTestCleanup() {
return Promise.resolve()
.then(() => git('config', 'user.email', 'angular-core+e2e@google.com'))
.then(() => git('config', 'user.name', 'Angular CLI E2e'))
.then(() => git('config', 'commit.gpgSign', 'false'))
.then(() => gitCommit('ng-new test'));
}
21 changes: 21 additions & 0 deletions tests/e2e/utils/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ export function createDir(path: string) {
_recursiveMkDir(path);
}

export function deleteDir(path: string) {
return Promise.resolve()
.then(() => _recursiveRmDir(path));
}

// Based on http://stackoverflow.com/a/12761924/146656
function _recursiveRmDir(path: string) {
if ( fs.existsSync(path) ) {
const files = fs.readdirSync(path);
files.forEach(function(file, index){
const curPath = path + '/' + file;
if (fs.lstatSync(curPath).isDirectory()) { // recurse
_recursiveRmDir(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}


function _recursiveMkDir(path: string) {
if (fs.existsSync(path)) {
Expand Down

0 comments on commit 6854bb5

Please sign in to comment.