|
| 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 | +} |
0 commit comments