testing.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var path = require("path");
  2. var fs = require("fs");
  3. var promises = require("../lib/promises");
  4. var _ = require("underscore");
  5. exports.testPath = testPath;
  6. exports.testData = testData;
  7. exports.createFakeDocxFile = createFakeDocxFile;
  8. exports.createFakeFiles = createFakeFiles;
  9. function testPath(filename) {
  10. return path.join(__dirname, "test-data", filename);
  11. }
  12. function testData(testDataPath) {
  13. var fullPath = testPath(testDataPath);
  14. return promises.nfcall(fs.readFile, fullPath, "utf-8");
  15. }
  16. function createFakeDocxFile(files) {
  17. function exists(path) {
  18. return !!files[path];
  19. }
  20. return {
  21. read: createRead(files),
  22. exists: exists
  23. };
  24. }
  25. function createFakeFiles(files) {
  26. return {
  27. read: createRead(files)
  28. };
  29. }
  30. function createRead(files) {
  31. function read(path, encoding) {
  32. return promises.when(files[path], function(buffer) {
  33. if (_.isString(buffer)) {
  34. buffer = new Buffer(buffer);
  35. }
  36. if (!Buffer.isBuffer(buffer)) {
  37. return promises.reject(new Error("file was not a buffer"));
  38. } else if (encoding) {
  39. return promises.when(buffer.toString(encoding));
  40. } else {
  41. return promises.when(buffer.buffer);
  42. }
  43. });
  44. }
  45. return read;
  46. }