| 
									
										
										
										
											2020-08-11 12:32:44 -06:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2022-02-04 14:30:57 -05:00
										 |  |  | const fs = require('fs').promises; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | const assert = require('assert'); | 
					
						
							| 
									
										
										
										
											2020-08-11 12:32:44 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 14:30:57 -05:00
										 |  |  | const pkgInstall = require('../src/cli/package-install'); | 
					
						
							| 
									
										
										
										
											2020-08-11 12:32:44 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 14:30:57 -05:00
										 |  |  | describe('Package install lib', () => { | 
					
						
							| 
									
										
										
										
											2022-02-08 11:00:35 -05:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Important: | 
					
						
							|  |  |  | 	 *   - The tests here have a beforeEach() run prior to each test, it resets | 
					
						
							|  |  |  | 	 *     package.json and install/package.json back to identical states. | 
					
						
							|  |  |  | 	 *   - Update `source` and `current` for testing. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2022-02-04 14:30:57 -05:00
										 |  |  | 	describe('updatePackageFile()', () => { | 
					
						
							|  |  |  | 		let source; | 
					
						
							| 
									
										
										
										
											2022-02-08 11:00:35 -05:00
										 |  |  | 		let current; | 
					
						
							| 
									
										
										
										
											2022-02-04 14:30:57 -05:00
										 |  |  | 		const sourcePackagePath = path.resolve(__dirname, '../install/package.json'); | 
					
						
							|  |  |  | 		const packageFilePath = path.resolve(__dirname, '../package.json'); | 
					
						
							| 
									
										
										
										
											2020-08-11 12:32:44 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 14:30:57 -05:00
										 |  |  | 		before(async () => { | 
					
						
							|  |  |  | 			// Move `install/package.json` and `package.json` out of the way for now
 | 
					
						
							|  |  |  | 			await fs.copyFile(sourcePackagePath, path.resolve(__dirname, '../install/package.json.bak')); // safekeeping
 | 
					
						
							|  |  |  | 			await fs.copyFile(packageFilePath, path.resolve(__dirname, '../package.json.bak')); // safekeeping
 | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2020-08-11 12:32:44 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 14:30:57 -05:00
										 |  |  | 		beforeEach(async () => { | 
					
						
							|  |  |  | 			await fs.copyFile(path.resolve(__dirname, '../install/package.json.bak'), sourcePackagePath); | 
					
						
							| 
									
										
										
										
											2022-02-17 14:10:24 -05:00
										 |  |  | 			await fs.copyFile(sourcePackagePath, packageFilePath); // match files for testing
 | 
					
						
							| 
									
										
										
										
											2022-02-04 14:30:57 -05:00
										 |  |  | 			source = JSON.parse(await fs.readFile(sourcePackagePath)); | 
					
						
							| 
									
										
										
										
											2022-02-08 11:00:35 -05:00
										 |  |  | 			current = JSON.parse(await fs.readFile(packageFilePath)); | 
					
						
							| 
									
										
										
										
											2022-02-04 14:30:57 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2020-08-11 12:32:44 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 14:30:57 -05:00
										 |  |  | 		it('should remove non-`nodebb-` modules not specified in `install/package.json`', async () => { | 
					
						
							| 
									
										
										
										
											2022-02-04 15:22:15 -05:00
										 |  |  | 			source.dependencies.dotenv = '16.0.0'; | 
					
						
							|  |  |  | 			await fs.writeFile(packageFilePath, JSON.stringify(source, null, 4)); | 
					
						
							|  |  |  | 			delete source.dependencies.dotenv; | 
					
						
							| 
									
										
										
										
											2022-02-04 14:30:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			pkgInstall.updatePackageFile(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// assert it removed the extra package
 | 
					
						
							|  |  |  | 			const packageCleaned = JSON.parse(await fs.readFile(packageFilePath, 'utf8')); | 
					
						
							|  |  |  | 			assert(!packageCleaned.dependencies.dotenv, 'dependency was not removed'); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should merge new root level properties from `install/package.json` into `package.json`', async () => { | 
					
						
							|  |  |  | 			source.bin = './nodebb'; | 
					
						
							|  |  |  | 			await fs.writeFile(sourcePackagePath, JSON.stringify(source, null, 4)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			pkgInstall.updatePackageFile(); | 
					
						
							|  |  |  | 			const updated = JSON.parse(await fs.readFile(packageFilePath, 'utf8')); | 
					
						
							|  |  |  | 			assert.deepStrictEqual(updated, source); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2022-02-04 13:24:57 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 14:30:57 -05:00
										 |  |  | 		it('should add new dependencies', async () => { | 
					
						
							|  |  |  | 			source.dependencies['nodebb-plugin-foobar'] = '1.0.0'; | 
					
						
							|  |  |  | 			await fs.writeFile(sourcePackagePath, JSON.stringify(source, null, 4)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			pkgInstall.updatePackageFile(); | 
					
						
							|  |  |  | 			const updated = JSON.parse(await fs.readFile(packageFilePath, 'utf8')); | 
					
						
							|  |  |  | 			assert.deepStrictEqual(updated, source); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should update version on dependencies', async () => { | 
					
						
							|  |  |  | 			source.dependencies['nodebb-plugin-mentions'] = '1.0.0'; | 
					
						
							|  |  |  | 			await fs.writeFile(sourcePackagePath, JSON.stringify(source, null, 4)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			pkgInstall.updatePackageFile(); | 
					
						
							|  |  |  | 			const updated = JSON.parse(await fs.readFile(packageFilePath, 'utf8')); | 
					
						
							|  |  |  | 			assert.deepStrictEqual(updated, source); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should deep merge nested objects', async () => { | 
					
						
							| 
									
										
										
										
											2022-02-08 11:00:35 -05:00
										 |  |  | 			current.scripts.postinstall = 'echo "I am a silly bean";'; | 
					
						
							|  |  |  | 			await fs.writeFile(packageFilePath, JSON.stringify(current, null, 4)); | 
					
						
							| 
									
										
										
										
											2022-02-04 14:30:57 -05:00
										 |  |  | 			source.scripts.preinstall = 'echo "What are you?";'; | 
					
						
							|  |  |  | 			await fs.writeFile(sourcePackagePath, JSON.stringify(source, null, 4)); | 
					
						
							|  |  |  | 			source.scripts.postinstall = 'echo "I am a silly bean";'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			pkgInstall.updatePackageFile(); | 
					
						
							|  |  |  | 			const updated = JSON.parse(await fs.readFile(packageFilePath, 'utf8')); | 
					
						
							|  |  |  | 			assert.deepStrictEqual(updated, source); | 
					
						
							|  |  |  | 			assert.strictEqual(updated.scripts.postinstall, 'echo "I am a silly bean";'); | 
					
						
							|  |  |  | 			assert.strictEqual(updated.scripts.preinstall, 'echo "What are you?";'); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-08 11:13:51 -05:00
										 |  |  | 		it('should remove extraneous devDependencies', async () => { | 
					
						
							|  |  |  | 			current.devDependencies.expect = '27.5.1'; | 
					
						
							|  |  |  | 			await fs.writeFile(packageFilePath, JSON.stringify(current, null, 4)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			pkgInstall.updatePackageFile(); | 
					
						
							|  |  |  | 			const updated = JSON.parse(await fs.readFile(packageFilePath, 'utf8')); | 
					
						
							|  |  |  | 			assert.strictEqual(updated.devDependencies.hasOwnProperty('expect'), false); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-17 14:10:24 -05:00
										 |  |  | 		it('should handle if there is no package.json', async () => { | 
					
						
							|  |  |  | 			await fs.unlink(packageFilePath); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			pkgInstall.updatePackageFile(); | 
					
						
							|  |  |  | 			const updated = JSON.parse(await fs.readFile(packageFilePath, 'utf8')); | 
					
						
							|  |  |  | 			assert.deepStrictEqual(updated, source); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 14:30:57 -05:00
										 |  |  | 		after(async () => { | 
					
						
							|  |  |  | 			// Clean up
 | 
					
						
							|  |  |  | 			await fs.rename(path.resolve(__dirname, '../install/package.json.bak'), sourcePackagePath); | 
					
						
							|  |  |  | 			await fs.rename(path.resolve(__dirname, '../package.json.bak'), packageFilePath); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2020-08-11 12:32:44 -06:00
										 |  |  | }); |