Created
November 9, 2011 00:51
-
-
Save isaacs/1349916 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a dir with a bunch of files in it, then read them. | |
var fs = require("fs") | |
var path = require("path") | |
var util = require("util") | |
var fileCount = 1024 | |
var x = "x".charCodeAt(0) | |
var testFolder = path.resolve("test-many-files-"+process.pid) | |
var eol = process.platform === "win32" ? "\r\n" : "\n" | |
console.error("many file test") | |
console.error(testFolder) | |
function numName (num) { | |
return "test-file-" + | |
new Buffer([Math.floor(num / 256), num % 256]).toString("hex") + | |
"-" + num + | |
// make the length of files variable | |
new Array((num % 10) + 1).join(".x") | |
} | |
console.error("making files") | |
console.error(testFolder) | |
fs.mkdirSync(testFolder, 0777) | |
var test = [] | |
for (var i = 0; i < fileCount; i ++) { | |
test[i] = numName(i) | |
var data = new Buffer(i) | |
for (var d = 0; d < i; d ++) { | |
data[d] = x | |
} | |
fs.writeFileSync(path.resolve(testFolder, test[i]), data) | |
} | |
test.sort(alphasort) | |
console.error("test files written, reading directory") | |
fs.readdir(testFolder, function (er, files) { | |
if (er) throw er | |
files = files.filter(function (f) { | |
return f !== "." && f !== ".." | |
}).sort(alphasort) | |
fs.writeFileSync(testFolder + ".txt", | |
util.inspect(files).split("\n").join(eol)) | |
if (files.length !== fileCount) { | |
throw new Error("wrong file count") | |
} | |
for (var i = 0; i < files.length; i ++) { | |
if (files[i] !== test[i]) throw new Error("invalid data at file #"+i) | |
} | |
console.log("ok") | |
}) | |
// sort alphabetically. | |
// fs.readdir results are arbitrarily sorted, so we need to put them in | |
// order to compare them properly. | |
function alphasort (a, b) { | |
return a === b ? 0 : a > b ? 1 : -1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment