file_spec.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. var assert = require("assert");
  2. var util = require("util");
  3. var mocha = require("mocha");
  4. var file = require("../lib/file");
  5. var fs = require("fs");
  6. var path = require("path");
  7. var madeDirs = [];
  8. fs.mkdir = function (dir, mode, callback) {
  9. madeDirs.push(dir);
  10. callback();
  11. };
  12. fs.mkdirSync = function (dir, mode) {
  13. madeDirs.push(dir);
  14. };
  15. global.fs = fs;
  16. describe("file#mkdirs", function () {
  17. beforeEach(function (done) {
  18. madeDirs = [];
  19. done();
  20. });
  21. it("should make all the directories in the tree", function (done) {
  22. file.mkdirs("/test/test/test/test", 0755, function(err) {
  23. if (err) throw new Error(err);
  24. assert.equal(madeDirs[0], "/test");
  25. assert.equal(madeDirs[1], "/test/test");
  26. assert.equal(madeDirs[2], "/test/test/test");
  27. assert.equal(madeDirs[3], "/test/test/test/test");
  28. done();
  29. });
  30. });
  31. });
  32. describe("file#mkdirsSync", function () {
  33. beforeEach(function (done) {
  34. madeDirs = [];
  35. done();
  36. });
  37. it("should make all the directories in the tree", function (done) {
  38. file.mkdirsSync("/test/test/test/test", 0755, function(err) {
  39. if (err) throw new Error(err);
  40. });
  41. assert.equal(madeDirs[0], "/test");
  42. assert.equal(madeDirs[1], "/test/test");
  43. assert.equal(madeDirs[2], "/test/test/test");
  44. assert.equal(madeDirs[3], "/test/test/test/test");
  45. done();
  46. });
  47. });
  48. // TODO: File walk tests are obviously not really working
  49. describe("file#walk", function () {
  50. it("should call \"callback\" for ever file in the tree", function (done) {
  51. file.walk("./tests", function(start, dirs, names) {});
  52. done();
  53. });
  54. });
  55. describe("file#walkSync", function () {
  56. it("should call \"callback\" for ever file in the tree", function (done) {
  57. file.walkSync("./tests", function(start, dirs, names) {});
  58. done();
  59. });
  60. });
  61. describe("file.path#abspath", function () {
  62. it("should convert . to the current directory", function (done) {
  63. assert.equal(file.path.abspath("."), process.cwd());
  64. assert.equal(file.path.abspath("./test/dir"), file.path.join(process.cwd(), "test/dir"));
  65. done();
  66. });
  67. it("should convert .. to the parrent directory", function (done) {
  68. assert.equal(file.path.abspath(".."), path.dirname(process.cwd()));
  69. assert.equal(file.path.abspath("../test/dir"), file.path.join(path.dirname(process.cwd()), "test/dir"));
  70. done();
  71. });
  72. it("should convert ~ to the home directory", function (done) {
  73. assert.equal(file.path.abspath("~"), file.path.join(process.env.HOME, ""));
  74. assert.equal(file.path.abspath("~/test/dir"), file.path.join(process.env.HOME, "test/dir"));
  75. done();
  76. });
  77. it("should not convert paths begining with /", function (done) {
  78. assert.equal(file.path.abspath("/x/y/z"), "/x/y/z");
  79. done();
  80. });
  81. });
  82. describe("file.path#relativePath", function () {
  83. it("should return the relative path", function (done) {
  84. var rel = file.path.relativePath("/", "/test.js");
  85. assert.equal(rel, "test.js");
  86. var rel = file.path.relativePath("/test/loc", "/test/loc/test.js");
  87. assert.equal(rel, "test.js");
  88. done();
  89. });
  90. it("should take two equal paths and return \"\"", function (done) {
  91. var rel = file.path.relativePath("/test.js", "/test.js");
  92. assert.equal(rel, "");
  93. done();
  94. });
  95. });