Skip to content

Commit 6854bb5

Browse files
committed
chore(new): add e2e test for ng new command
1 parent 132a9b8 commit 6854bb5

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

tests/e2e/tests/commands/new.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {deleteFile, deleteDir} from '../../utils/fs';
2+
import * as path from 'path';
3+
import {ng, git} from '../../utils/process';
4+
import {gitCommit} from '../../utils/git';
5+
6+
export default function() {
7+
const parentTestprojectDir = process.cwd();
8+
const projectName = 'new-test-project';
9+
10+
return Promise.resolve()
11+
// The test setup already creates a project, lets' clean that before running `ng new`
12+
.then(() => removeSetupProject(parentTestprojectDir))
13+
14+
// Run `ng new`
15+
.then(() => ng('new', projectName))
16+
.then(() => process.chdir(path.join(parentTestprojectDir, projectName)))
17+
18+
// Try to run the unit tests.
19+
.then(() => ng('test', '--single-run'))
20+
21+
// Run post-test steps whether test passes or fails
22+
.then(prepareGitForTestCleanup, prepareGitForTestCleanup);
23+
}
24+
25+
// Change the project inherited from Setup into normal
26+
// non angular-cli project folder, so we can call `ng new`
27+
function removeSetupProject (parentTestprojectDir: string) {
28+
return Promise.resolve()
29+
30+
// Change the project inherited from Setup into normal
31+
// non angular-cli project folder, so we can call `ng new`
32+
.then(() => process.chdir(parentTestprojectDir))
33+
.then(() => deleteDir('.git'))
34+
.then(() => deleteFile('angular-cli.json'))
35+
.then(() => deleteFile('package.json'));
36+
}
37+
38+
// The post-test cleanup breaks if it doesn't have a git repository with a branch and a commit
39+
function prepareGitForTestCleanup() {
40+
return Promise.resolve()
41+
.then(() => git('config', 'user.email', '[email protected]'))
42+
.then(() => git('config', 'user.name', 'Angular CLI E2e'))
43+
.then(() => git('config', 'commit.gpgSign', 'false'))
44+
.then(() => gitCommit('ng-new test'));
45+
}

tests/e2e/utils/fs.ts

+21
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,27 @@ export function createDir(path: string) {
5757
_recursiveMkDir(path);
5858
}
5959

60+
export function deleteDir(path: string) {
61+
return Promise.resolve()
62+
.then(() => _recursiveRmDir(path));
63+
}
64+
65+
// Based on http://stackoverflow.com/a/12761924/146656
66+
function _recursiveRmDir(path: string) {
67+
if ( fs.existsSync(path) ) {
68+
const files = fs.readdirSync(path);
69+
files.forEach(function(file, index){
70+
const curPath = path + '/' + file;
71+
if (fs.lstatSync(curPath).isDirectory()) { // recurse
72+
_recursiveRmDir(curPath);
73+
} else { // delete file
74+
fs.unlinkSync(curPath);
75+
}
76+
});
77+
fs.rmdirSync(path);
78+
}
79+
}
80+
6081

6182
function _recursiveMkDir(path: string) {
6283
if (fs.existsSync(path)) {

0 commit comments

Comments
 (0)