| 
									
										
										
										
											2015-05-10 19:50:15 +08:00
										 |  |  | /* globals describe it */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-08 08:17:21 +08:00
										 |  |  | var should = require("should"); | 
					
						
							| 
									
										
										
										
											2013-02-11 07:17:29 +08:00
										 |  |  | var NodeWatchFileSystem = require("../lib/node/NodeWatchFileSystem"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe("NodeWatchFileSystem", function() { | 
					
						
							| 
									
										
										
										
											2017-11-15 21:08:11 +08:00
										 |  |  | 	it("should throw if 'files' argument is not an array", function() { | 
					
						
							| 
									
										
										
										
											2017-01-08 08:17:21 +08:00
										 |  |  | 		should(function() { | 
					
						
							| 
									
										
										
										
											2017-11-15 21:08:11 +08:00
										 |  |  | 			new NodeWatchFileSystem().watch(undefined); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:17:21 +08:00
										 |  |  | 		}).throw("Invalid arguments: 'files'"); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 21:08:11 +08:00
										 |  |  | 	it("should throw if 'dirs' argument is not an array", function() { | 
					
						
							| 
									
										
										
										
											2017-01-08 08:17:21 +08:00
										 |  |  | 		should(function() { | 
					
						
							| 
									
										
										
										
											2017-11-15 21:08:11 +08:00
										 |  |  | 			new NodeWatchFileSystem().watch([], undefined); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:17:21 +08:00
										 |  |  | 		}).throw("Invalid arguments: 'dirs'"); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 21:08:11 +08:00
										 |  |  | 	it("should throw if 'missing' argument is not an array", function() { | 
					
						
							| 
									
										
										
										
											2017-01-08 08:17:21 +08:00
										 |  |  | 		should(function() { | 
					
						
							| 
									
										
										
										
											2017-11-15 21:08:11 +08:00
										 |  |  | 			new NodeWatchFileSystem().watch([], [], undefined); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:17:21 +08:00
										 |  |  | 		}).throw("Invalid arguments: 'missing'"); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 21:08:11 +08:00
										 |  |  | 	it("should throw if 'starttime' argument is missing", function() { | 
					
						
							| 
									
										
										
										
											2017-01-08 08:17:21 +08:00
										 |  |  | 		should(function() { | 
					
						
							| 
									
										
										
										
											2017-11-15 21:08:11 +08:00
										 |  |  | 			new NodeWatchFileSystem().watch([], [], [], "42", {}, function() {}); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:17:21 +08:00
										 |  |  | 		}).throw("Invalid arguments: 'startTime'"); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 21:08:11 +08:00
										 |  |  | 	it("should throw if 'callback' argument is missing", function() { | 
					
						
							| 
									
										
										
										
											2017-01-08 08:17:21 +08:00
										 |  |  | 		should(function() { | 
					
						
							| 
									
										
										
										
											2017-11-15 21:08:11 +08:00
										 |  |  | 			new NodeWatchFileSystem().watch([], [], [], 42, {}, undefined); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:17:21 +08:00
										 |  |  | 		}).throw("Invalid arguments: 'callback'"); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 21:08:11 +08:00
										 |  |  | 	it("should throw if 'options' argument is invalid", function() { | 
					
						
							| 
									
										
										
										
											2017-01-08 08:17:21 +08:00
										 |  |  | 		should(function() { | 
					
						
							| 
									
										
										
										
											2017-11-15 21:08:11 +08:00
										 |  |  | 			new NodeWatchFileSystem().watch([], [], [], 42, "options", function() {}); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:17:21 +08:00
										 |  |  | 		}).throw("Invalid arguments: 'options'"); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 21:08:11 +08:00
										 |  |  | 	it("should throw if 'callbackUndelayed' argument is invalid", function() { | 
					
						
							| 
									
										
										
										
											2017-01-08 08:17:21 +08:00
										 |  |  | 		should(function() { | 
					
						
							| 
									
										
										
										
											2017-11-15 21:08:11 +08:00
										 |  |  | 			new NodeWatchFileSystem().watch([], [], [], 42, {}, function() {}, "undefined"); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:17:21 +08:00
										 |  |  | 		}).throw("Invalid arguments: 'callbackUndelayed'"); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 13:19:38 +08:00
										 |  |  | 	if(process.env.NO_WATCH_TESTS) { | 
					
						
							|  |  |  | 		it("long running tests excluded"); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var path = require("path"); | 
					
						
							|  |  |  | 	var fs = require("fs"); | 
					
						
							|  |  |  | 	var fixtures = path.join(__dirname, "fixtures"); | 
					
						
							|  |  |  | 	var fileDirect = path.join(fixtures, "watched-file.txt"); | 
					
						
							|  |  |  | 	var fileSubdir = path.join(fixtures, "subdir", "watched-file.txt"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	this.timeout(10000); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-11 07:17:29 +08:00
										 |  |  | 	it("should register a file change (change delayed)", function(done) { | 
					
						
							|  |  |  | 		var startTime = new Date().getTime(); | 
					
						
							| 
									
										
										
										
											2015-01-18 04:55:44 +08:00
										 |  |  | 		var wfs = new NodeWatchFileSystem(); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 		var watcher = wfs.watch([fileDirect], [], [], startTime, { | 
					
						
							|  |  |  | 			aggregateTimeout: 1000 | 
					
						
							| 
									
										
										
										
											2017-11-16 16:04:21 +08:00
										 |  |  | 		}, function(err, filesModified, dirsModified, missingCreated, fileTimestamps) { | 
					
						
							| 
									
										
										
										
											2013-02-11 07:17:29 +08:00
										 |  |  | 			if(err) throw err; | 
					
						
							|  |  |  | 			filesModified.should.be.eql([fileDirect]); | 
					
						
							|  |  |  | 			dirsModified.should.be.eql([]); | 
					
						
							| 
									
										
										
										
											2018-01-24 13:55:53 +08:00
										 |  |  | 			(typeof fileTimestamps.get(fileDirect)).should.be.eql("number"); | 
					
						
							| 
									
										
										
										
											2015-01-18 04:55:44 +08:00
										 |  |  | 			watcher.close(); | 
					
						
							| 
									
										
										
										
											2013-02-11 07:17:29 +08:00
										 |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		setTimeout(function() { | 
					
						
							|  |  |  | 			fs.writeFile(fileDirect, "", function() {}); | 
					
						
							|  |  |  | 		}, 500); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	it("should register a file change (watch delayed)", function(done) { | 
					
						
							|  |  |  | 		var startTime = new Date().getTime(); | 
					
						
							|  |  |  | 		setTimeout(function() { | 
					
						
							| 
									
										
										
										
											2015-01-18 04:55:44 +08:00
										 |  |  | 			var wfs = new NodeWatchFileSystem(); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 			var watcher = wfs.watch([fileDirect], [], [], startTime, { | 
					
						
							|  |  |  | 				aggregateTimeout: 1000 | 
					
						
							| 
									
										
										
										
											2017-11-16 16:04:21 +08:00
										 |  |  | 			}, function(err, filesModified, dirsModified, missingCreated, fileTimestamps) { | 
					
						
							| 
									
										
										
										
											2013-02-11 07:17:29 +08:00
										 |  |  | 				if(err) throw err; | 
					
						
							|  |  |  | 				filesModified.should.be.eql([fileDirect]); | 
					
						
							|  |  |  | 				dirsModified.should.be.eql([]); | 
					
						
							| 
									
										
										
										
											2018-01-24 13:55:53 +08:00
										 |  |  | 				(typeof fileTimestamps.get(fileDirect)).should.be.eql("number"); | 
					
						
							| 
									
										
										
										
											2015-01-18 04:55:44 +08:00
										 |  |  | 				watcher.close(); | 
					
						
							| 
									
										
										
										
											2013-02-11 07:17:29 +08:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}, 500); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fs.writeFile(fileDirect, "", function() {}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	it("should register a context change (change delayed)", function(done) { | 
					
						
							|  |  |  | 		var startTime = new Date().getTime(); | 
					
						
							| 
									
										
										
										
											2015-01-18 04:55:44 +08:00
										 |  |  | 		var wfs = new NodeWatchFileSystem(); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 		var watcher = wfs.watch([], [fixtures], [], startTime, { | 
					
						
							|  |  |  | 			aggregateTimeout: 1000 | 
					
						
							|  |  |  | 		}, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) { | 
					
						
							| 
									
										
										
										
											2013-02-11 07:17:29 +08:00
										 |  |  | 			if(err) throw err; | 
					
						
							|  |  |  | 			filesModified.should.be.eql([]); | 
					
						
							|  |  |  | 			dirsModified.should.be.eql([fixtures]); | 
					
						
							| 
									
										
										
										
											2018-01-24 13:55:53 +08:00
										 |  |  | 			(typeof dirTimestamps.get(fixtures)).should.be.eql("number"); | 
					
						
							| 
									
										
										
										
											2015-01-18 04:55:44 +08:00
										 |  |  | 			watcher.close(); | 
					
						
							| 
									
										
										
										
											2013-02-11 07:17:29 +08:00
										 |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		setTimeout(function() { | 
					
						
							|  |  |  | 			fs.writeFile(fileDirect, "", function() {}); | 
					
						
							|  |  |  | 		}, 500); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	it("should register a context change (watch delayed)", function(done) { | 
					
						
							|  |  |  | 		var startTime = new Date().getTime(); | 
					
						
							|  |  |  | 		setTimeout(function() { | 
					
						
							| 
									
										
										
										
											2015-01-18 04:55:44 +08:00
										 |  |  | 			var wfs = new NodeWatchFileSystem(); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 			var watcher = wfs.watch([], [fixtures], [], startTime, { | 
					
						
							|  |  |  | 				aggregateTimeout: 1000 | 
					
						
							|  |  |  | 			}, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) { | 
					
						
							| 
									
										
										
										
											2013-02-11 07:17:29 +08:00
										 |  |  | 				if(err) throw err; | 
					
						
							|  |  |  | 				filesModified.should.be.eql([]); | 
					
						
							|  |  |  | 				dirsModified.should.be.eql([fixtures]); | 
					
						
							| 
									
										
										
										
											2018-01-24 13:55:53 +08:00
										 |  |  | 				(typeof dirTimestamps.get(fixtures)).should.be.eql("number"); | 
					
						
							| 
									
										
										
										
											2015-01-18 04:55:44 +08:00
										 |  |  | 				watcher.close(); | 
					
						
							| 
									
										
										
										
											2013-02-11 07:17:29 +08:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}, 500); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fs.writeFile(fileDirect, "", function() {}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	it("should register a context change (change delayed, subdirectory)", function(done) { | 
					
						
							|  |  |  | 		var startTime = new Date().getTime(); | 
					
						
							| 
									
										
										
										
											2015-01-18 04:55:44 +08:00
										 |  |  | 		var wfs = new NodeWatchFileSystem(); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 		var watcher = wfs.watch([], [fixtures], [], startTime, { | 
					
						
							|  |  |  | 			aggregateTimeout: 1000 | 
					
						
							|  |  |  | 		}, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) { | 
					
						
							| 
									
										
										
										
											2013-02-11 07:17:29 +08:00
										 |  |  | 			if(err) throw err; | 
					
						
							|  |  |  | 			filesModified.should.be.eql([]); | 
					
						
							|  |  |  | 			dirsModified.should.be.eql([fixtures]); | 
					
						
							| 
									
										
										
										
											2018-01-24 13:55:53 +08:00
										 |  |  | 			(typeof dirTimestamps.get(fixtures)).should.be.eql("number"); | 
					
						
							| 
									
										
										
										
											2015-01-18 04:55:44 +08:00
										 |  |  | 			watcher.close(); | 
					
						
							| 
									
										
										
										
											2013-02-11 07:17:29 +08:00
										 |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		setTimeout(function() { | 
					
						
							|  |  |  | 			fs.writeFile(fileSubdir, "", function() {}); | 
					
						
							|  |  |  | 		}, 500); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	it("should register a context change (watch delayed, subdirectory)", function(done) { | 
					
						
							|  |  |  | 		var startTime = new Date().getTime(); | 
					
						
							|  |  |  | 		setTimeout(function() { | 
					
						
							| 
									
										
										
										
											2015-01-18 04:55:44 +08:00
										 |  |  | 			var wfs = new NodeWatchFileSystem(); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 			var watcher = wfs.watch([], [fixtures], [], startTime, { | 
					
						
							|  |  |  | 				aggregateTimeout: 1000 | 
					
						
							|  |  |  | 			}, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) { | 
					
						
							| 
									
										
										
										
											2013-02-11 07:17:29 +08:00
										 |  |  | 				if(err) throw err; | 
					
						
							|  |  |  | 				filesModified.should.be.eql([]); | 
					
						
							|  |  |  | 				dirsModified.should.be.eql([fixtures]); | 
					
						
							| 
									
										
										
										
											2018-01-24 13:55:53 +08:00
										 |  |  | 				(typeof dirTimestamps.get(fixtures)).should.be.eql("number"); | 
					
						
							| 
									
										
										
										
											2015-01-18 04:55:44 +08:00
										 |  |  | 				watcher.close(); | 
					
						
							| 
									
										
										
										
											2013-02-11 07:17:29 +08:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}, 500); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fs.writeFile(fileSubdir, "", function() {}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	it("should allow to combine all", function(done) { | 
					
						
							|  |  |  | 		var startTime = new Date().getTime(); | 
					
						
							|  |  |  | 		setTimeout(function() { | 
					
						
							| 
									
										
										
										
											2015-01-18 04:55:44 +08:00
										 |  |  | 			var wfs = new NodeWatchFileSystem(); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 			var watcher = wfs.watch([fileDirect, fileSubdir], [fixtures], [], startTime, { | 
					
						
							|  |  |  | 				aggregateTimeout: 1000 | 
					
						
							|  |  |  | 			}, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) { | 
					
						
							| 
									
										
										
										
											2013-02-11 07:17:29 +08:00
										 |  |  | 				if(err) throw err; | 
					
						
							|  |  |  | 				filesModified.should.be.eql([fileSubdir, fileDirect]); | 
					
						
							|  |  |  | 				dirsModified.should.be.eql([fixtures]); | 
					
						
							| 
									
										
										
										
											2018-01-24 13:55:53 +08:00
										 |  |  | 				(typeof fileTimestamps.get(fileDirect)).should.be.eql("number"); | 
					
						
							|  |  |  | 				(typeof fileTimestamps.get(fileSubdir)).should.be.eql("number"); | 
					
						
							|  |  |  | 				(typeof dirTimestamps.get(fixtures)).should.be.eql("number"); | 
					
						
							| 
									
										
										
										
											2015-01-18 04:55:44 +08:00
										 |  |  | 				watcher.close(); | 
					
						
							| 
									
										
										
										
											2013-02-11 07:17:29 +08:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}, 500); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fs.writeFile(fileDirect, "", function() {}); | 
					
						
							|  |  |  | 		fs.writeFile(fileSubdir, "", function() {}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	it("should sum up multiple changes", function(done) { | 
					
						
							|  |  |  | 		var startTime = new Date().getTime(); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 		var wfs = new NodeWatchFileSystem(); | 
					
						
							|  |  |  | 		var watcher = wfs.watch([fileDirect, fileSubdir], [fixtures], [], startTime, { | 
					
						
							|  |  |  | 			aggregateTimeout: 1000 | 
					
						
							|  |  |  | 		}, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) { | 
					
						
							|  |  |  | 			if(err) throw err; | 
					
						
							|  |  |  | 			filesModified.should.be.eql([fileSubdir, fileDirect]); | 
					
						
							|  |  |  | 			dirsModified.should.be.eql([fixtures]); | 
					
						
							| 
									
										
										
										
											2018-01-24 13:55:53 +08:00
										 |  |  | 			(typeof fileTimestamps.get(fileDirect)).should.be.eql("number"); | 
					
						
							|  |  |  | 			(typeof fileTimestamps.get(fileSubdir)).should.be.eql("number"); | 
					
						
							|  |  |  | 			(typeof dirTimestamps.get(fixtures)).should.be.eql("number"); | 
					
						
							| 
									
										
										
										
											2015-08-09 18:42:43 +08:00
										 |  |  | 			watcher.close(); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-02-11 07:17:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		setTimeout(function() { | 
					
						
							|  |  |  | 			fs.writeFile(fileDirect, "", function() {}); | 
					
						
							|  |  |  | 			setTimeout(function() { | 
					
						
							|  |  |  | 				fs.writeFile(fileDirect, "", function() {}); | 
					
						
							|  |  |  | 				setTimeout(function() { | 
					
						
							|  |  |  | 					fs.writeFile(fileDirect, "", function() {}); | 
					
						
							|  |  |  | 					setTimeout(function() { | 
					
						
							|  |  |  | 						fs.writeFile(fileSubdir, "", function() {}); | 
					
						
							|  |  |  | 					}, 500); | 
					
						
							|  |  |  | 				}, 500); | 
					
						
							|  |  |  | 			}, 500); | 
					
						
							|  |  |  | 		}, 500); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2015-01-18 04:55:44 +08:00
										 |  |  | }); |