fs.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // if running on older node, ensure that es6-shim is loaded first
  2. if (/^v0.10/.test(process.version)) { require('es6-shim'); }
  3. var assert= require('../assert');
  4. var fs = require('../fs');
  5. var path = require('../path');
  6. var TESTFILE = '/tmp/hello';
  7. describe('fs module', function() {
  8. it('write/read/unlink (callbacks)', function(done) {
  9. fs.exists(TESTFILE, function(exists) {
  10. if (exists) {
  11. return done("Pre-existing file "+TESTFILE+"; aborting test.");
  12. }
  13. fs.writeFile(TESTFILE, 'hello', 'utf-8', function(err) {
  14. if (err) { return done(err); }
  15. fs.exists(TESTFILE, function(exists) {
  16. if (!exists) {
  17. return done(TESTFILE+" not found");
  18. }
  19. fs.readFile(TESTFILE, 'utf-8', function(err, contents) {
  20. if (err) { return done(err); }
  21. if (contents !== 'hello') {
  22. return done("File contents are not right");
  23. }
  24. fs.unlink(TESTFILE, function(err) {
  25. if (err) { return done(err); }
  26. fs.exists(TESTFILE, function(exists) {
  27. if (exists) {
  28. return done("unlink didn't work");
  29. }
  30. done(/*success!*/);
  31. });
  32. });
  33. });
  34. });
  35. });
  36. });
  37. });
  38. it('write/read/unlink (promises)', function() {
  39. return fs.exists(TESTFILE).then(function(exists) {
  40. assert.equal(!!exists, false,
  41. "Pre-existing file "+TESTFILE+"; aborting test.");
  42. }).then(function() {
  43. return fs.writeFile(TESTFILE, 'hello', 'utf-8');
  44. }).then(function() {
  45. return fs.exists(TESTFILE);
  46. }).then(function(exists) {
  47. assert.equal(!!exists, true);
  48. return fs.readFile(TESTFILE, 'utf-8');
  49. }).then(function(contents) {
  50. assert.equal(contents, 'hello');
  51. return fs.unlink(TESTFILE);
  52. }).then(function() {
  53. return fs.exists(TESTFILE);
  54. }).then(function(exists) {
  55. assert.equal(!!exists, false);
  56. });
  57. });
  58. });