| 
									
										
										
										
											2017-08-16 03:34:53 +03:00
										 |  |  | const path = require('canonical-path'); | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  | const fs = require('fs-extra'); | 
					
						
							|  |  |  | const glob = require('glob'); | 
					
						
							| 
									
										
										
										
											2017-08-16 03:34:53 +03:00
										 |  |  | const shelljs = require('shelljs'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const exampleBoilerPlate = require('./example-boilerplate'); | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('example-boilerplate tool', () => { | 
					
						
							|  |  |  |   describe('add', () => { | 
					
						
							| 
									
										
										
										
											2017-08-16 03:34:53 +03:00
										 |  |  |     const sharedDir = path.resolve(__dirname, 'shared'); | 
					
						
							|  |  |  |     const sharedNodeModulesDir = path.resolve(sharedDir, 'node_modules'); | 
					
						
							| 
									
										
										
										
											2017-08-22 21:31:15 +02:00
										 |  |  |     const BPFiles = { | 
					
						
							| 
									
										
										
										
											2020-02-13 16:58:04 +02:00
										 |  |  |       cli: 20, | 
					
						
							| 
									
										
										
										
											2018-05-02 23:16:03 -05:00
										 |  |  |       i18n: 2, | 
					
						
							| 
									
										
										
										
											2017-10-30 23:39:58 +01:00
										 |  |  |       universal: 2, | 
					
						
							| 
									
										
										
										
											2017-08-22 21:31:15 +02:00
										 |  |  |       systemjs: 7, | 
					
						
							|  |  |  |       common: 1 | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  |     const exampleFolders = ['a/b', 'c/d']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       spyOn(fs, 'ensureSymlinkSync'); | 
					
						
							| 
									
										
										
										
											2017-10-06 14:48:37 +01:00
										 |  |  |       spyOn(fs, 'existsSync').and.returnValue(true); | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  |       spyOn(exampleBoilerPlate, 'copyFile'); | 
					
						
							| 
									
										
										
										
											2017-08-16 03:34:53 +03:00
										 |  |  |       spyOn(exampleBoilerPlate, 'getFoldersContaining').and.returnValue(exampleFolders); | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  |       spyOn(exampleBoilerPlate, 'loadJsonFile').and.returnValue({}); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should process all the example folders', () => { | 
					
						
							| 
									
										
										
										
											2017-08-16 03:34:53 +03:00
										 |  |  |       const examplesDir = path.resolve(__dirname, '../../content/examples'); | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  |       exampleBoilerPlate.add(); | 
					
						
							| 
									
										
										
										
											2017-08-16 03:34:53 +03:00
										 |  |  |       expect(exampleBoilerPlate.getFoldersContaining) | 
					
						
							|  |  |  |           .toHaveBeenCalledWith(examplesDir, 'example-config.json', 'node_modules'); | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should symlink the node_modules', () => { | 
					
						
							|  |  |  |       exampleBoilerPlate.add(); | 
					
						
							|  |  |  |       expect(fs.ensureSymlinkSync).toHaveBeenCalledTimes(exampleFolders.length); | 
					
						
							| 
									
										
										
										
											2017-08-16 03:34:53 +03:00
										 |  |  |       expect(fs.ensureSymlinkSync).toHaveBeenCalledWith(sharedNodeModulesDir, path.resolve('a/b/node_modules')); | 
					
						
							|  |  |  |       expect(fs.ensureSymlinkSync).toHaveBeenCalledWith(sharedNodeModulesDir, path.resolve('c/d/node_modules')); | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-06 14:48:37 +01:00
										 |  |  |     it('should error if the node_modules folder is missing', () => { | 
					
						
							|  |  |  |       fs.existsSync.and.returnValue(false); | 
					
						
							|  |  |  |       expect(() => exampleBoilerPlate.add()).toThrowError( | 
					
						
							|  |  |  |         `The shared node_modules folder for the examples (${sharedNodeModulesDir}) is missing.\n` + | 
					
						
							|  |  |  |         `Perhaps you need to run "yarn example-use-npm" or "yarn example-use-local" to install the dependencies?`); | 
					
						
							|  |  |  |       expect(fs.ensureSymlinkSync).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-22 21:31:15 +02:00
										 |  |  |     it('should copy all the source boilerplate files for systemjs', () => { | 
					
						
							| 
									
										
										
										
											2017-08-16 03:34:53 +03:00
										 |  |  |       const boilerplateDir = path.resolve(sharedDir, 'boilerplate'); | 
					
						
							| 
									
										
										
										
											2017-08-22 21:31:15 +02:00
										 |  |  |       exampleBoilerPlate.loadJsonFile.and.callFake(filePath => filePath.indexOf('a/b') !== -1 ? { projectType: 'systemjs' } : {}) | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  |       exampleBoilerPlate.add(); | 
					
						
							| 
									
										
										
										
											2017-08-22 21:31:15 +02:00
										 |  |  |       expect(exampleBoilerPlate.copyFile).toHaveBeenCalledTimes( | 
					
						
							|  |  |  |         (BPFiles.cli) + | 
					
						
							|  |  |  |         (BPFiles.systemjs) + | 
					
						
							|  |  |  |         (BPFiles.common * exampleFolders.length) | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  |       // for example
 | 
					
						
							| 
									
										
										
										
											2017-08-16 03:34:53 +03:00
										 |  |  |       expect(exampleBoilerPlate.copyFile).toHaveBeenCalledWith(`${boilerplateDir}/systemjs`, 'a/b', 'package.json'); | 
					
						
							|  |  |  |       expect(exampleBoilerPlate.copyFile).toHaveBeenCalledWith(`${boilerplateDir}/common`, 'a/b', 'src/styles.css'); | 
					
						
							| 
									
										
										
										
											2017-08-22 21:31:15 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should copy all the source boilerplate files for cli', () => { | 
					
						
							| 
									
										
										
										
											2017-08-16 03:34:53 +03:00
										 |  |  |       const boilerplateDir = path.resolve(sharedDir, 'boilerplate'); | 
					
						
							| 
									
										
										
										
											2017-08-22 21:31:15 +02:00
										 |  |  |       exampleBoilerPlate.add(); | 
					
						
							|  |  |  |       expect(exampleBoilerPlate.copyFile).toHaveBeenCalledTimes( | 
					
						
							|  |  |  |         (BPFiles.cli * exampleFolders.length) + | 
					
						
							|  |  |  |         (BPFiles.common * exampleFolders.length) | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       // for example
 | 
					
						
							| 
									
										
										
										
											2017-08-16 03:34:53 +03:00
										 |  |  |       expect(exampleBoilerPlate.copyFile).toHaveBeenCalledWith(`${boilerplateDir}/cli`, 'a/b', 'package.json'); | 
					
						
							|  |  |  |       expect(exampleBoilerPlate.copyFile).toHaveBeenCalledWith(`${boilerplateDir}/common`, 'c/d', 'src/styles.css'); | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 12:59:32 +01:00
										 |  |  |     it('should copy all the source boilerplate files for i18n', () => { | 
					
						
							|  |  |  |       const boilerplateDir = path.resolve(sharedDir, 'boilerplate'); | 
					
						
							|  |  |  |       exampleBoilerPlate.loadJsonFile.and.callFake(filePath => filePath.indexOf('a/b') !== -1 ? { projectType: 'i18n' } : {}) | 
					
						
							|  |  |  |       exampleBoilerPlate.add(); | 
					
						
							|  |  |  |       expect(exampleBoilerPlate.copyFile).toHaveBeenCalledTimes( | 
					
						
							|  |  |  |         (BPFiles.cli + BPFiles.i18n) + | 
					
						
							|  |  |  |         (BPFiles.cli) + | 
					
						
							|  |  |  |         (BPFiles.common * exampleFolders.length) | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       // for example
 | 
					
						
							| 
									
										
										
										
											2018-04-15 21:24:29 -05:00
										 |  |  |       expect(exampleBoilerPlate.copyFile).toHaveBeenCalledWith(`${boilerplateDir}/i18n`, 'a/b', '../cli/angular.json'); | 
					
						
							| 
									
										
										
										
											2017-10-29 12:59:32 +01:00
										 |  |  |       expect(exampleBoilerPlate.copyFile).toHaveBeenCalledWith(`${boilerplateDir}/i18n`, 'a/b', 'package.json'); | 
					
						
							|  |  |  |       expect(exampleBoilerPlate.copyFile).toHaveBeenCalledWith(`${boilerplateDir}/common`, 'c/d', 'src/styles.css'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-30 23:39:58 +01:00
										 |  |  |     it('should copy all the source boilerplate files for universal', () => { | 
					
						
							|  |  |  |       const boilerplateDir = path.resolve(sharedDir, 'boilerplate'); | 
					
						
							|  |  |  |       exampleBoilerPlate.loadJsonFile.and.callFake(filePath => filePath.indexOf('a/b') !== -1 ? { projectType: 'universal' } : {}) | 
					
						
							|  |  |  |       exampleBoilerPlate.add(); | 
					
						
							|  |  |  |       expect(exampleBoilerPlate.copyFile).toHaveBeenCalledTimes( | 
					
						
							|  |  |  |         (BPFiles.cli + BPFiles.universal) + | 
					
						
							|  |  |  |         (BPFiles.cli) + | 
					
						
							|  |  |  |         (BPFiles.common * exampleFolders.length) | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       // for example
 | 
					
						
							|  |  |  |       expect(exampleBoilerPlate.copyFile).toHaveBeenCalledWith(`${boilerplateDir}/universal`, 'a/b', '../cli/tslint.json'); | 
					
						
							| 
									
										
										
										
											2018-05-02 07:13:32 -05:00
										 |  |  |       expect(exampleBoilerPlate.copyFile).toHaveBeenCalledWith(`${boilerplateDir}/universal`, 'a/b', 'angular.json'); | 
					
						
							| 
									
										
										
										
											2017-10-30 23:39:58 +01:00
										 |  |  |       expect(exampleBoilerPlate.copyFile).toHaveBeenCalledWith(`${boilerplateDir}/common`, 'c/d', 'src/styles.css'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  |     it('should try to load the example config file', () => { | 
					
						
							|  |  |  |       exampleBoilerPlate.add(); | 
					
						
							|  |  |  |       expect(exampleBoilerPlate.loadJsonFile).toHaveBeenCalledTimes(exampleFolders.length); | 
					
						
							|  |  |  |       expect(exampleBoilerPlate.loadJsonFile).toHaveBeenCalledWith(path.resolve('a/b/example-config.json')); | 
					
						
							|  |  |  |       expect(exampleBoilerPlate.loadJsonFile).toHaveBeenCalledWith(path.resolve('c/d/example-config.json')); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('remove', () => { | 
					
						
							|  |  |  |     it('should run `git clean`', () => { | 
					
						
							|  |  |  |       spyOn(shelljs, 'exec'); | 
					
						
							|  |  |  |       exampleBoilerPlate.remove(); | 
					
						
							|  |  |  |       expect(shelljs.exec).toHaveBeenCalledWith('git clean -xdfq', {cwd: path.resolve(__dirname, '../../content/examples') }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('getFoldersContaining', () => { | 
					
						
							|  |  |  |     it('should use glob.sync', () => { | 
					
						
							|  |  |  |       spyOn(glob, 'sync').and.returnValue(['a/b/config.json', 'c/d/config.json']); | 
					
						
							|  |  |  |       const result = exampleBoilerPlate.getFoldersContaining('base/path', 'config.json', 'node_modules'); | 
					
						
							|  |  |  |       expect(glob.sync).toHaveBeenCalledWith(path.resolve('base/path/**/config.json'), { ignore: [path.resolve('base/path/**/node_modules/**')] }); | 
					
						
							|  |  |  |       expect(result).toEqual(['a/b', 'c/d']); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('copyFile', () => { | 
					
						
							|  |  |  |     it('should use copySync and chmodSync', () => { | 
					
						
							|  |  |  |       spyOn(fs, 'copySync'); | 
					
						
							|  |  |  |       spyOn(fs, 'chmodSync'); | 
					
						
							|  |  |  |       exampleBoilerPlate.copyFile('source/folder', 'destination/folder', 'some/file/path'); | 
					
						
							|  |  |  |       expect(fs.copySync).toHaveBeenCalledWith( | 
					
						
							|  |  |  |         path.resolve('source/folder/some/file/path'), | 
					
						
							|  |  |  |         path.resolve('destination/folder/some/file/path'), | 
					
						
							|  |  |  |         { overwrite: true }); | 
					
						
							|  |  |  |       expect(fs.chmodSync).toHaveBeenCalledWith(path.resolve('destination/folder/some/file/path'), 444); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('loadJsonFile', () => { | 
					
						
							|  |  |  |     it('should use fs.readJsonSync', () => { | 
					
						
							|  |  |  |       spyOn(fs, 'readJsonSync').and.returnValue({ some: 'value' }); | 
					
						
							|  |  |  |       const result = exampleBoilerPlate.loadJsonFile('some/file'); | 
					
						
							|  |  |  |       expect(fs.readJsonSync).toHaveBeenCalledWith('some/file', {throws: false}); | 
					
						
							|  |  |  |       expect(result).toEqual({ some: 'value' }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should return an empty object if readJsonSync fails', () => { | 
					
						
							|  |  |  |       spyOn(fs, 'readJsonSync').and.returnValue(null); | 
					
						
							|  |  |  |       const result = exampleBoilerPlate.loadJsonFile('some/file'); | 
					
						
							|  |  |  |       expect(result).toEqual({}); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2017-07-27 08:28:54 +01:00
										 |  |  | }); |