| 
									
										
										
										
											2018-07-16 08:49:56 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 11:32:01 +01:00
										 |  |  | import {existsSync, readFileSync, readdirSync, statSync} from 'fs'; | 
					
						
							|  |  |  | import * as mockFs from 'mock-fs'; | 
					
						
							| 
									
										
										
										
											2018-07-30 20:06:51 +03:00
										 |  |  | import {join} from 'path'; | 
					
						
							| 
									
										
										
										
											2018-08-09 15:59:10 +01:00
										 |  |  | const Module = require('module'); | 
					
						
							| 
									
										
										
										
											2018-07-16 08:49:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import {mainNgcc} from '../../src/ngcc/src/main'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 11:32:01 +01:00
										 |  |  | describe('ngcc main()', () => { | 
					
						
							| 
									
										
										
										
											2018-07-16 08:49:56 +01:00
										 |  |  |   if (!isInBazel()) { | 
					
						
							|  |  |  |     // These tests should be excluded from the non-Bazel build.
 | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 11:32:01 +01:00
										 |  |  |   beforeEach(createMockFileSystem); | 
					
						
							|  |  |  |   afterEach(restoreRealFileSystem); | 
					
						
							| 
									
										
										
										
											2018-07-16 08:49:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-24 16:53:23 +03:00
										 |  |  |   it('should run ngcc without errors for fesm2015', () => { | 
					
						
							| 
									
										
										
										
											2018-07-30 20:06:51 +03:00
										 |  |  |     const format = 'fesm2015'; | 
					
						
							| 
									
										
										
										
											2018-08-09 15:59:10 +01:00
										 |  |  |     expect(mainNgcc(['-f', format, '-s', '/node_modules'])).toBe(0); | 
					
						
							| 
									
										
										
										
											2018-07-16 08:49:56 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2018-07-24 16:53:23 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 13:06:32 +03:00
										 |  |  |   it('should run ngcc without errors for fesm5', () => { | 
					
						
							| 
									
										
										
										
											2018-07-30 20:06:51 +03:00
										 |  |  |     const format = 'fesm5'; | 
					
						
							| 
									
										
										
										
											2018-08-09 15:59:10 +01:00
										 |  |  |     expect(mainNgcc(['-f', format, '-s', '/node_modules'])).toBe(0); | 
					
						
							| 
									
										
										
										
											2018-07-25 13:06:32 +03:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-24 16:53:23 +03:00
										 |  |  |   it('should run ngcc without errors for esm2015', () => { | 
					
						
							| 
									
										
										
										
											2018-07-30 20:06:51 +03:00
										 |  |  |     const format = 'esm2015'; | 
					
						
							| 
									
										
										
										
											2018-08-09 15:59:10 +01:00
										 |  |  |     expect(mainNgcc(['-f', format, '-s', '/node_modules'])).toBe(0); | 
					
						
							| 
									
										
										
										
											2018-07-24 16:53:23 +03:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2018-07-25 13:06:32 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |   it('should run ngcc without errors for esm5', () => { | 
					
						
							| 
									
										
										
										
											2018-07-30 20:06:51 +03:00
										 |  |  |     const format = 'esm5'; | 
					
						
							| 
									
										
										
										
											2018-08-09 15:59:10 +01:00
										 |  |  |     expect(mainNgcc(['-f', format, '-s', '/node_modules'])).toBe(0); | 
					
						
							| 
									
										
										
										
											2018-08-28 11:32:01 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 13:06:32 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 11:32:01 +01:00
										 |  |  | function createMockFileSystem() { | 
					
						
							| 
									
										
										
										
											2018-08-09 15:59:10 +01:00
										 |  |  |   const packagesPath = join(process.env.TEST_SRCDIR !, 'angular/packages'); | 
					
						
							| 
									
										
										
										
											2018-08-28 11:32:01 +01:00
										 |  |  |   mockFs({'/node_modules/@angular': loadPackages(packagesPath)}); | 
					
						
							| 
									
										
										
										
											2018-08-09 15:59:10 +01:00
										 |  |  |   spyOn(Module, '_resolveFilename').and.callFake(mockResolve); | 
					
						
							| 
									
										
										
										
											2018-08-28 11:32:01 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-25 13:06:32 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 11:32:01 +01:00
										 |  |  | function restoreRealFileSystem() { | 
					
						
							|  |  |  |   mockFs.restore(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Load the built Angular packages into an in-memory structure. | 
					
						
							|  |  |  |  * @param packagesPath the path to the folder containing the built packages. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function loadPackages(packagesPath: string): Directory { | 
					
						
							|  |  |  |   const packagesDirectory: Directory = {}; | 
					
						
							|  |  |  |   readdirSync(packagesPath).forEach(name => { | 
					
						
							|  |  |  |     const packagePath = join(packagesPath, name); | 
					
						
							|  |  |  |     if (!statSync(packagePath).isDirectory()) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     const npmPackagePath = join(packagePath, 'npm_package'); | 
					
						
							|  |  |  |     if (!existsSync(npmPackagePath)) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     packagesDirectory[name] = loadDirectory(npmPackagePath); | 
					
						
							| 
									
										
										
										
											2018-07-25 13:06:32 +03:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2018-08-28 11:32:01 +01:00
										 |  |  |   return packagesDirectory; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Load real files from the filesystem into an "in-memory" structure, | 
					
						
							|  |  |  |  * which can be used with `mock-fs`. | 
					
						
							|  |  |  |  * @param directoryPath the path to the directory we want to load. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function loadDirectory(directoryPath: string): Directory { | 
					
						
							|  |  |  |   const directory: Directory = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   readdirSync(directoryPath).forEach(item => { | 
					
						
							|  |  |  |     const itemPath = join(directoryPath, item); | 
					
						
							|  |  |  |     if (statSync(itemPath).isDirectory()) { | 
					
						
							|  |  |  |       directory[item] = loadDirectory(itemPath); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       directory[item] = readFileSync(itemPath, 'utf-8'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return directory; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | interface Directory { | 
					
						
							|  |  |  |   [pathSegment: string]: string|Directory; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function isInBazel() { | 
					
						
							|  |  |  |   return process.env.TEST_SRCDIR != null; | 
					
						
							| 
									
										
										
										
											2018-08-09 15:59:10 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function mockResolve(p: string): string|null { | 
					
						
							|  |  |  |   if (existsSync(p)) { | 
					
						
							|  |  |  |     const stat = statSync(p); | 
					
						
							|  |  |  |     if (stat.isFile()) { | 
					
						
							|  |  |  |       return p; | 
					
						
							|  |  |  |     } else if (stat.isDirectory()) { | 
					
						
							|  |  |  |       const pIndex = mockResolve(p + '/index'); | 
					
						
							|  |  |  |       if (pIndex && existsSync(pIndex)) { | 
					
						
							|  |  |  |         return pIndex; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   for (const ext of ['.js', '.d.ts']) { | 
					
						
							|  |  |  |     if (existsSync(p + ext)) { | 
					
						
							|  |  |  |       return p + ext; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return null; | 
					
						
							| 
									
										
										
										
											2018-08-28 11:32:01 +01:00
										 |  |  | } |