| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  | // Imports
 | 
					
						
							|  |  |  | import * as fs from 'fs'; | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  | import {normalize} from 'path'; | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  | import * as shell from 'shelljs'; | 
					
						
							|  |  |  | import {BuildCleaner} from '../../lib/clean-up/build-cleaner'; | 
					
						
							| 
									
										
										
										
											2017-06-25 01:40:04 +03:00
										 |  |  | import {HIDDEN_DIR_PREFIX} from '../../lib/common/constants'; | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  | import {GithubPullRequests} from '../../lib/common/github-pull-requests'; | 
					
						
							| 
									
										
										
										
											2018-08-27 18:07:25 +03:00
										 |  |  | import {Logger} from '../../lib/common/utils'; | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  | const EXISTING_BUILDS = [10, 20, 30, 40]; | 
					
						
							|  |  |  | const EXISTING_DOWNLOADS = [ | 
					
						
							| 
									
										
										
										
											2018-09-15 15:31:39 +03:00
										 |  |  |   '10-ABCDEF0-build.zip', | 
					
						
							|  |  |  |   '10-1234567-build.zip', | 
					
						
							|  |  |  |   '20-ABCDEF0-build.zip', | 
					
						
							|  |  |  |   '20-1234567-build.zip', | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  | ]; | 
					
						
							|  |  |  | const OPEN_PRS = [10, 40]; | 
					
						
							| 
									
										
										
										
											2018-05-12 15:39:16 +01:00
										 |  |  | const ANY_DATE = jasmine.any(String); | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  | // Tests
 | 
					
						
							|  |  |  | describe('BuildCleaner', () => { | 
					
						
							| 
									
										
										
										
											2018-08-27 18:07:25 +03:00
										 |  |  |   let loggerErrorSpy: jasmine.Spy; | 
					
						
							|  |  |  |   let loggerLogSpy: jasmine.Spy; | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |   let cleaner: BuildCleaner; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-12 15:39:16 +01:00
										 |  |  |   beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2018-08-27 18:07:25 +03:00
										 |  |  |     loggerErrorSpy = spyOn(Logger.prototype, 'error'); | 
					
						
							|  |  |  |     loggerLogSpy = spyOn(Logger.prototype, 'log'); | 
					
						
							| 
									
										
										
										
											2018-09-15 15:31:39 +03:00
										 |  |  |     cleaner = new BuildCleaner('/foo/bar', 'baz', 'qux', '12345', '/downloads', 'build.zip'); | 
					
						
							| 
									
										
										
										
											2018-05-12 15:39:16 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('constructor()', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should throw if \'buildsDir\' is empty', () => { | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       expect(() => new BuildCleaner('', 'baz', 'qux', '12345', 'downloads', 'build.zip')). | 
					
						
							| 
									
										
										
										
											2017-02-27 22:40:13 +02:00
										 |  |  |         toThrowError('Missing or empty required parameter \'buildsDir\'!'); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |     it('should throw if \'githubOrg\' is empty', () => { | 
					
						
							|  |  |  |       expect(() => new BuildCleaner('/foo/bar', '', 'qux', '12345', 'downloads', 'build.zip')). | 
					
						
							|  |  |  |         toThrowError('Missing or empty required parameter \'githubOrg\'!'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should throw if \'githubRepo\' is empty', () => { | 
					
						
							|  |  |  |       expect(() => new BuildCleaner('/foo/bar', 'baz', '', '12345', 'downloads', 'build.zip')). | 
					
						
							|  |  |  |         toThrowError('Missing or empty required parameter \'githubRepo\'!'); | 
					
						
							| 
									
										
										
										
											2017-02-27 22:40:13 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should throw if \'githubToken\' is empty', () => { | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       expect(() => new BuildCleaner('/foo/bar', 'baz', 'qux', '', 'downloads', 'build.zip')). | 
					
						
							| 
									
										
										
										
											2017-02-27 22:40:13 +02:00
										 |  |  |         toThrowError('Missing or empty required parameter \'githubToken\'!'); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-15 15:31:39 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |     it('should throw if \'downloadsDir\' is empty', () => { | 
					
						
							|  |  |  |       expect(() => new BuildCleaner('/foo/bar', 'baz', 'qux', '12345', '', 'build.zip')). | 
					
						
							|  |  |  |         toThrowError('Missing or empty required parameter \'downloadsDir\'!'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-15 15:31:39 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |     it('should throw if \'artifactPath\' is empty', () => { | 
					
						
							|  |  |  |       expect(() => new BuildCleaner('/foo/bar', 'baz', 'qux', '12345', 'downloads', '')). | 
					
						
							|  |  |  |         toThrowError('Missing or empty required parameter \'artifactPath\'!'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('cleanUp()', () => { | 
					
						
							|  |  |  |     let cleanerGetExistingBuildNumbersSpy: jasmine.Spy; | 
					
						
							|  |  |  |     let cleanerGetOpenPrNumbersSpy: jasmine.Spy; | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |     let cleanerGetExistingDownloadsSpy: jasmine.Spy; | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |     let cleanerRemoveUnnecessaryBuildsSpy: jasmine.Spy; | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |     let cleanerRemoveUnnecessaryDownloadsSpy: jasmine.Spy; | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       cleanerGetExistingBuildNumbersSpy = spyOn(cleaner, 'getExistingBuildNumbers') | 
					
						
							|  |  |  |         .and.callFake(() => Promise.resolve(EXISTING_BUILDS)); | 
					
						
							|  |  |  |       cleanerGetOpenPrNumbersSpy = spyOn(cleaner, 'getOpenPrNumbers') | 
					
						
							|  |  |  |         .and.callFake(() => Promise.resolve(OPEN_PRS)); | 
					
						
							|  |  |  |       cleanerGetExistingDownloadsSpy = spyOn(cleaner, 'getExistingDownloads') | 
					
						
							|  |  |  |         .and.callFake(() => Promise.resolve(EXISTING_DOWNLOADS)); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       cleanerRemoveUnnecessaryBuildsSpy = spyOn(cleaner, 'removeUnnecessaryBuilds'); | 
					
						
							|  |  |  |       cleanerRemoveUnnecessaryDownloadsSpy = spyOn(cleaner, 'removeUnnecessaryDownloads'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-27 16:45:26 +03:00
										 |  |  |     it('should return a promise', async () => { | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       const promise = cleaner.cleanUp(); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |       expect(promise).toEqual(jasmine.any(Promise)); | 
					
						
							| 
									
										
										
										
											2018-08-27 16:45:26 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // Do not complete the test and release the spies synchronously, to avoid running the actual implementations.
 | 
					
						
							|  |  |  |       await promise; | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |     it('should get the open PRs', async () => { | 
					
						
							|  |  |  |       await cleaner.cleanUp(); | 
					
						
							|  |  |  |       expect(cleanerGetOpenPrNumbersSpy).toHaveBeenCalled(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should get the existing builds', async () => { | 
					
						
							|  |  |  |       await cleaner.cleanUp(); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |       expect(cleanerGetExistingBuildNumbersSpy).toHaveBeenCalled(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |     it('should get the existing downloads', async () => { | 
					
						
							|  |  |  |       await cleaner.cleanUp(); | 
					
						
							|  |  |  |       expect(cleanerGetExistingDownloadsSpy).toHaveBeenCalled(); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |     it('should pass existing builds and open PRs to \'removeUnnecessaryBuilds()\'', async () => { | 
					
						
							|  |  |  |       await cleaner.cleanUp(); | 
					
						
							|  |  |  |       expect(cleanerRemoveUnnecessaryBuildsSpy).toHaveBeenCalledWith(EXISTING_BUILDS, OPEN_PRS); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |     it('should pass existing downloads and open PRs to \'removeUnnecessaryDownloads()\'', async () => { | 
					
						
							|  |  |  |       await cleaner.cleanUp(); | 
					
						
							|  |  |  |       expect(cleanerRemoveUnnecessaryDownloadsSpy).toHaveBeenCalledWith(EXISTING_DOWNLOADS, OPEN_PRS); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |     it('should reject if \'getOpenPrNumbers()\' rejects', async () => { | 
					
						
							|  |  |  |       try { | 
					
						
							|  |  |  |         cleanerGetOpenPrNumbersSpy.and.callFake(() => Promise.reject('Test')); | 
					
						
							|  |  |  |         await cleaner.cleanUp(); | 
					
						
							|  |  |  |       } catch (err) { | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |         expect(err).toBe('Test'); | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |     it('should reject if \'getExistingBuildNumbers()\' rejects', async () => { | 
					
						
							|  |  |  |       try { | 
					
						
							|  |  |  |         cleanerGetExistingBuildNumbersSpy.and.callFake(() => Promise.reject('Test')); | 
					
						
							|  |  |  |         await cleaner.cleanUp(); | 
					
						
							|  |  |  |       } catch (err) { | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |         expect(err).toBe('Test'); | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |     it('should reject if \'getExistingDownloads()\' rejects', async () => { | 
					
						
							|  |  |  |       try { | 
					
						
							|  |  |  |         cleanerGetExistingDownloadsSpy.and.callFake(() => Promise.reject('Test')); | 
					
						
							|  |  |  |         await cleaner.cleanUp(); | 
					
						
							|  |  |  |       } catch (err) { | 
					
						
							|  |  |  |         expect(err).toBe('Test'); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |     it('should reject if \'removeUnnecessaryBuilds()\' rejects', async () => { | 
					
						
							|  |  |  |       try { | 
					
						
							|  |  |  |         cleanerRemoveUnnecessaryBuildsSpy.and.callFake(() => Promise.reject('Test')); | 
					
						
							|  |  |  |         await cleaner.cleanUp(); | 
					
						
							|  |  |  |       } catch (err) { | 
					
						
							|  |  |  |         expect(err).toBe('Test'); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-15 15:31:39 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |     it('should reject if \'removeUnnecessaryDownloads()\' rejects', async () => { | 
					
						
							|  |  |  |       try { | 
					
						
							|  |  |  |         cleanerRemoveUnnecessaryDownloadsSpy.and.callFake(() => Promise.reject('Test')); | 
					
						
							|  |  |  |         await cleaner.cleanUp(); | 
					
						
							|  |  |  |       } catch (err) { | 
					
						
							|  |  |  |         expect(err).toBe('Test'); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-09-15 15:31:39 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('getExistingBuildNumbers()', () => { | 
					
						
							|  |  |  |     let fsReaddirSpy: jasmine.Spy; | 
					
						
							|  |  |  |     let readdirCb: (err: any, files?: string[]) => void; | 
					
						
							|  |  |  |     let promise: Promise<number[]>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       fsReaddirSpy = spyOn(fs, 'readdir').and.callFake((_: string, cb: typeof readdirCb) => readdirCb = cb); | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       promise = cleaner.getExistingBuildNumbers(); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should return a promise', () => { | 
					
						
							|  |  |  |       expect(promise).toEqual(jasmine.any(Promise)); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should get the contents of the builds directory', () => { | 
					
						
							|  |  |  |       expect(fsReaddirSpy).toHaveBeenCalled(); | 
					
						
							|  |  |  |       expect(fsReaddirSpy.calls.argsFor(0)[0]).toBe('/foo/bar'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should reject if an error occurs while getting the files', done => { | 
					
						
							|  |  |  |       promise.catch(err => { | 
					
						
							|  |  |  |         expect(err).toBe('Test'); | 
					
						
							|  |  |  |         done(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       readdirCb('Test'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should resolve with the returned files (as numbers)', done => { | 
					
						
							|  |  |  |       promise.then(result => { | 
					
						
							|  |  |  |         expect(result).toEqual([12, 34, 56]); | 
					
						
							|  |  |  |         done(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       readdirCb(null, ['12', '34', '56']); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-25 01:40:04 +03:00
										 |  |  |     it('should remove `HIDDEN_DIR_PREFIX` from the filenames', done => { | 
					
						
							|  |  |  |       promise.then(result => { | 
					
						
							|  |  |  |         expect(result).toEqual([12, 34, 56]); | 
					
						
							|  |  |  |         done(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       readdirCb(null, [`${HIDDEN_DIR_PREFIX}12`, '34', `${HIDDEN_DIR_PREFIX}56`]); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |     it('should ignore files with non-numeric (or zero) names', done => { | 
					
						
							|  |  |  |       promise.then(result => { | 
					
						
							|  |  |  |         expect(result).toEqual([12, 34, 56]); | 
					
						
							|  |  |  |         done(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       readdirCb(null, ['12', 'foo', '34', 'bar', '56', '000']); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('getOpenPrNumbers()', () => { | 
					
						
							| 
									
										
										
										
											2017-09-23 15:24:04 +03:00
										 |  |  |     let prDeferred: {resolve: (v: any) => void, reject: (v: any) => void}; | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |     let promise: Promise<number[]>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       spyOn(GithubPullRequests.prototype, 'fetchAll').and.callFake(() => { | 
					
						
							|  |  |  |         return new Promise((resolve, reject) => prDeferred = {resolve, reject}); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       promise = cleaner.getOpenPrNumbers(); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should return a promise', () => { | 
					
						
							|  |  |  |       expect(promise).toEqual(jasmine.any(Promise)); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should fetch open PRs via \'GithubPullRequests\'', () => { | 
					
						
							|  |  |  |       expect(GithubPullRequests.prototype.fetchAll).toHaveBeenCalledWith('open'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should reject if an error occurs while fetching PRs', done => { | 
					
						
							|  |  |  |       promise.catch(err => { | 
					
						
							|  |  |  |         expect(err).toBe('Test'); | 
					
						
							|  |  |  |         done(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       prDeferred.reject('Test'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should resolve with the numbers of the fetched PRs', done => { | 
					
						
							|  |  |  |       promise.then(prNumbers => { | 
					
						
							|  |  |  |         expect(prNumbers).toEqual([1, 2, 3]); | 
					
						
							|  |  |  |         done(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       prDeferred.resolve([{id: 0, number: 1}, {id: 1, number: 2}, {id: 2, number: 3}]); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-15 15:31:39 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |     it('should log the number of open PRs', () => { | 
					
						
							|  |  |  |       promise.then(prNumbers => { | 
					
						
							| 
									
										
										
										
											2018-08-27 18:07:25 +03:00
										 |  |  |         expect(loggerLogSpy).toHaveBeenCalledWith( | 
					
						
							|  |  |  |           ANY_DATE, 'BuildCleaner:        ', `Open pull requests: ${prNumbers}`); | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-09-15 15:31:39 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('getExistingDownloads()', () => { | 
					
						
							|  |  |  |     let fsReaddirSpy: jasmine.Spy; | 
					
						
							|  |  |  |     let readdirCb: (err: any, files?: string[]) => void; | 
					
						
							|  |  |  |     let promise: Promise<string[]>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       fsReaddirSpy = spyOn(fs, 'readdir').and.callFake((_: string, cb: typeof readdirCb) => readdirCb = cb); | 
					
						
							|  |  |  |       promise = cleaner.getExistingDownloads(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should return a promise', () => { | 
					
						
							|  |  |  |       expect(promise).toEqual(jasmine.any(Promise)); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-15 15:31:39 +03:00
										 |  |  |     it('should get the contents of the downloads directory', () => { | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       expect(fsReaddirSpy).toHaveBeenCalled(); | 
					
						
							| 
									
										
										
										
											2018-09-15 15:31:39 +03:00
										 |  |  |       expect(fsReaddirSpy.calls.argsFor(0)[0]).toBe('/downloads'); | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should reject if an error occurs while getting the files', done => { | 
					
						
							|  |  |  |       promise.catch(err => { | 
					
						
							|  |  |  |         expect(err).toBe('Test'); | 
					
						
							|  |  |  |         done(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       readdirCb('Test'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-15 15:31:39 +03:00
										 |  |  |     it('should resolve with the returned file names', done => { | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       promise.then(result => { | 
					
						
							|  |  |  |         expect(result).toEqual(EXISTING_DOWNLOADS); | 
					
						
							|  |  |  |         done(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       readdirCb(null, EXISTING_DOWNLOADS); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should ignore files that do not match the artifactPath', done => { | 
					
						
							|  |  |  |       promise.then(result => { | 
					
						
							|  |  |  |         expect(result).toEqual(['10-ABCDEF-build.zip', '30-FFFFFFF-build.zip']); | 
					
						
							|  |  |  |         done(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       readdirCb(null, ['10-ABCDEF-build.zip', '20-AAAAAAA-otherfile.zip', '30-FFFFFFF-build.zip']); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('removeDir()', () => { | 
					
						
							|  |  |  |     let shellChmodSpy: jasmine.Spy; | 
					
						
							|  |  |  |     let shellRmSpy: jasmine.Spy; | 
					
						
							| 
									
										
										
										
											2017-06-25 01:40:04 +03:00
										 |  |  |     let shellTestSpy: jasmine.Spy; | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       shellChmodSpy = spyOn(shell, 'chmod'); | 
					
						
							|  |  |  |       shellRmSpy = spyOn(shell, 'rm'); | 
					
						
							| 
									
										
										
										
											2017-06-25 01:40:04 +03:00
										 |  |  |       shellTestSpy = spyOn(shell, 'test').and.returnValue(true); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should test if the directory exists (and return if is does not)', () => { | 
					
						
							|  |  |  |       shellTestSpy.and.returnValue(false); | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       cleaner.removeDir('/foo/bar'); | 
					
						
							| 
									
										
										
										
											2017-06-25 01:40:04 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(shellTestSpy).toHaveBeenCalledWith('-d', '/foo/bar'); | 
					
						
							|  |  |  |       expect(shellChmodSpy).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |       expect(shellRmSpy).not.toHaveBeenCalled(); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should remove the specified directory and its content', () => { | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       cleaner.removeDir('/foo/bar'); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |       expect(shellRmSpy).toHaveBeenCalledWith('-rf', '/foo/bar'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should make the directory and its content writable before removing', () => { | 
					
						
							|  |  |  |       shellRmSpy.and.callFake(() => expect(shellChmodSpy).toHaveBeenCalledWith('-R', 'a+w', '/foo/bar')); | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       cleaner.removeDir('/foo/bar'); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(shellRmSpy).toHaveBeenCalled(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should catch errors and log them', () => { | 
					
						
							| 
									
										
										
										
											2017-09-23 15:24:04 +03:00
										 |  |  |       shellRmSpy.and.callFake(() => { | 
					
						
							|  |  |  |         // tslint:disable-next-line: no-string-throw
 | 
					
						
							|  |  |  |         throw 'Test'; | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       cleaner.removeDir('/foo/bar'); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-27 18:07:25 +03:00
										 |  |  |       expect(loggerErrorSpy).toHaveBeenCalledWith('ERROR: Unable to remove \'/foo/bar\' due to:', 'Test'); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('removeUnnecessaryBuilds()', () => { | 
					
						
							|  |  |  |     let cleanerRemoveDirSpy: jasmine.Spy; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       cleanerRemoveDirSpy = spyOn(cleaner, 'removeDir'); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |     it('should log the number of existing builds and builds to be removed', () => { | 
					
						
							|  |  |  |       cleaner.removeUnnecessaryBuilds([1, 2, 3], [3, 4, 5, 6]); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-27 18:07:25 +03:00
										 |  |  |       expect(loggerLogSpy).toHaveBeenCalledWith('Existing builds: 3'); | 
					
						
							|  |  |  |       expect(loggerLogSpy).toHaveBeenCalledWith('Removing 2 build(s): 1, 2'); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should construct full paths to directories (by prepending \'buildsDir\')', () => { | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       cleaner.removeUnnecessaryBuilds([1, 2, 3], []); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       expect(cleanerRemoveDirSpy).toHaveBeenCalledWith(normalize('/foo/bar/1')); | 
					
						
							|  |  |  |       expect(cleanerRemoveDirSpy).toHaveBeenCalledWith(normalize('/foo/bar/2')); | 
					
						
							|  |  |  |       expect(cleanerRemoveDirSpy).toHaveBeenCalledWith(normalize('/foo/bar/3')); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-25 01:40:04 +03:00
										 |  |  |     it('should try removing hidden directories as well', () => { | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       cleaner.removeUnnecessaryBuilds([1, 2, 3], []); | 
					
						
							| 
									
										
										
										
											2017-06-25 01:40:04 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       expect(cleanerRemoveDirSpy).toHaveBeenCalledWith(normalize(`/foo/bar/${HIDDEN_DIR_PREFIX}1`)); | 
					
						
							|  |  |  |       expect(cleanerRemoveDirSpy).toHaveBeenCalledWith(normalize(`/foo/bar/${HIDDEN_DIR_PREFIX}2`)); | 
					
						
							|  |  |  |       expect(cleanerRemoveDirSpy).toHaveBeenCalledWith(normalize(`/foo/bar/${HIDDEN_DIR_PREFIX}3`)); | 
					
						
							| 
									
										
										
										
											2017-06-25 01:40:04 +03:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |     it('should remove the builds that do not correspond to open PRs', () => { | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       cleaner.removeUnnecessaryBuilds([1, 2, 3, 4], [2, 4]); | 
					
						
							| 
									
										
										
										
											2017-06-25 01:40:04 +03:00
										 |  |  |       expect(cleanerRemoveDirSpy).toHaveBeenCalledTimes(4); | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       expect(cleanerRemoveDirSpy).toHaveBeenCalledWith(normalize('/foo/bar/1')); | 
					
						
							|  |  |  |       expect(cleanerRemoveDirSpy).toHaveBeenCalledWith(normalize('/foo/bar/3')); | 
					
						
							|  |  |  |       expect(cleanerRemoveDirSpy).toHaveBeenCalledWith(normalize(`/foo/bar/${HIDDEN_DIR_PREFIX}1`)); | 
					
						
							|  |  |  |       expect(cleanerRemoveDirSpy).toHaveBeenCalledWith(normalize(`/foo/bar/${HIDDEN_DIR_PREFIX}3`)); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |       cleanerRemoveDirSpy.calls.reset(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       cleaner.removeUnnecessaryBuilds([1, 2, 3, 4], [1, 2, 3, 4]); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |       expect(cleanerRemoveDirSpy).toHaveBeenCalledTimes(0); | 
					
						
							|  |  |  |       cleanerRemoveDirSpy.calls.reset(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       (cleaner as any).removeUnnecessaryBuilds([1, 2, 3, 4], []); | 
					
						
							| 
									
										
										
										
											2017-06-25 01:40:04 +03:00
										 |  |  |       expect(cleanerRemoveDirSpy).toHaveBeenCalledTimes(8); | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       expect(cleanerRemoveDirSpy).toHaveBeenCalledWith(normalize('/foo/bar/1')); | 
					
						
							|  |  |  |       expect(cleanerRemoveDirSpy).toHaveBeenCalledWith(normalize('/foo/bar/2')); | 
					
						
							|  |  |  |       expect(cleanerRemoveDirSpy).toHaveBeenCalledWith(normalize('/foo/bar/3')); | 
					
						
							|  |  |  |       expect(cleanerRemoveDirSpy).toHaveBeenCalledWith(normalize('/foo/bar/4')); | 
					
						
							|  |  |  |       expect(cleanerRemoveDirSpy).toHaveBeenCalledWith(normalize(`/foo/bar/${HIDDEN_DIR_PREFIX}1`)); | 
					
						
							|  |  |  |       expect(cleanerRemoveDirSpy).toHaveBeenCalledWith(normalize(`/foo/bar/${HIDDEN_DIR_PREFIX}2`)); | 
					
						
							|  |  |  |       expect(cleanerRemoveDirSpy).toHaveBeenCalledWith(normalize(`/foo/bar/${HIDDEN_DIR_PREFIX}3`)); | 
					
						
							|  |  |  |       expect(cleanerRemoveDirSpy).toHaveBeenCalledWith(normalize(`/foo/bar/${HIDDEN_DIR_PREFIX}4`)); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  |       cleanerRemoveDirSpy.calls.reset(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('removeUnnecessaryDownloads()', () => { | 
					
						
							| 
									
										
										
										
											2018-09-15 15:31:39 +03:00
										 |  |  |     let shellRmSpy: jasmine.Spy; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |     beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2018-09-15 15:31:39 +03:00
										 |  |  |       shellRmSpy = spyOn(shell, 'rm'); | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-15 15:31:39 +03:00
										 |  |  |     it('should log the number of existing downloads and downloads to be removed', () => { | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |       cleaner.removeUnnecessaryDownloads(EXISTING_DOWNLOADS, OPEN_PRS); | 
					
						
							| 
									
										
										
										
											2018-09-15 15:31:39 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(loggerLogSpy).toHaveBeenCalledWith('Existing downloads: 4'); | 
					
						
							|  |  |  |       expect(loggerLogSpy).toHaveBeenCalledWith('Removing 2 download(s): 20-ABCDEF0-build.zip, 20-1234567-build.zip'); | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-15 15:31:39 +03:00
										 |  |  |     it('should construct full paths to directories (by prepending \'downloadsDir\')', () => { | 
					
						
							|  |  |  |       cleaner.removeUnnecessaryDownloads(['dl-1', 'dl-2', 'dl-3'], []); | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-15 15:31:39 +03:00
										 |  |  |       expect(shellRmSpy).toHaveBeenCalledWith(normalize('/downloads/dl-1')); | 
					
						
							|  |  |  |       expect(shellRmSpy).toHaveBeenCalledWith(normalize('/downloads/dl-2')); | 
					
						
							|  |  |  |       expect(shellRmSpy).toHaveBeenCalledWith(normalize('/downloads/dl-3')); | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-09-15 15:31:39 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should remove the downloads that do not correspond to open PRs', () => { | 
					
						
							|  |  |  |       cleaner.removeUnnecessaryDownloads(EXISTING_DOWNLOADS, OPEN_PRS); | 
					
						
							|  |  |  |       expect(shellRmSpy).toHaveBeenCalledTimes(2); | 
					
						
							|  |  |  |       expect(shellRmSpy).toHaveBeenCalledWith(normalize('/downloads/20-ABCDEF0-build.zip')); | 
					
						
							|  |  |  |       expect(shellRmSpy).toHaveBeenCalledWith(normalize('/downloads/20-1234567-build.zip')); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 13:56:07 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2017-02-06 20:40:28 +02:00
										 |  |  | }); |