Skip to content

Commit 2bea7bc

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

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

tests/e2e/tests/commands/new.ts

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import * as path from 'path';
2+
import * as fs from 'fs';
3+
import {ng} from '../../utils/process';
4+
import {deleteDir} from '../../utils/fs';
5+
6+
export default function() {
7+
const parentTestprojectDir = process.cwd();
8+
const ngNewProjectName = 'new-test-project';
9+
10+
return Promise.resolve()
11+
12+
// The test setup already creates a project, let's clean that before running `ng new`.
13+
.then(() => disableTestSetupProject(parentTestprojectDir))
14+
15+
// Run `ng new` and tests...
16+
.then(() => process.chdir(parentTestprojectDir))
17+
.then(() => ng('new', ngNewProjectName))
18+
.then(() => process.chdir(path.join(parentTestprojectDir, ngNewProjectName)))
19+
20+
// Try to run the unit tests.
21+
.then(() => ng('test', '--single-run'))
22+
23+
// Revert disabling test-setup project, and prepare folder so test cleanup doesn't fail.
24+
.then(() => prepareGitForTestCleanup(parentTestprojectDir, ngNewProjectName));
25+
}
26+
27+
const angularCliPaths = ['.git', 'angular-cli.json', 'package.json'];
28+
const fileRenameSuffix = '.ng-backup';
29+
30+
// Change the project inherited from Setup into normal
31+
// non angular-cli project folder, so we can call `ng new`.
32+
function disableTestSetupProject (parentTestprojectDir: string) {
33+
return Promise.resolve()
34+
.then(() => process.chdir(parentTestprojectDir))
35+
36+
// Rename the files that define the folder as an angular-cli project
37+
// and that interfere with creating a child one.
38+
.then(() => angularCliPaths.map(
39+
filePath => path.join(parentTestprojectDir, filePath)
40+
))
41+
.then((filaPaths) => filaPaths.forEach(
42+
filePath => fs.rename(filePath, filePath + fileRenameSuffix)
43+
));
44+
}
45+
46+
function prepareGitForTestCleanup (parentTestprojectDir: string, projectName: string) {
47+
return Promise.resolve()
48+
// The post-test cleanup breaks if there's another git project inside it.
49+
.then(() => deleteDir(path.join(parentTestprojectDir, projectName)))
50+
51+
.then(() => process.chdir(parentTestprojectDir))
52+
53+
// Restore renamed files.
54+
.then(() => angularCliPaths.map(
55+
filePath => path.join(parentTestprojectDir, filePath) + fileRenameSuffix
56+
))
57+
.then((filaPaths) => filaPaths.forEach(
58+
filePath => fs.rename(filePath, filePath.replace(fileRenameSuffix, ''))
59+
));
60+
}

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)