open.js 897 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. var test = require('tap').test
  2. var fs = require('../graceful-fs.js')
  3. test('graceful fs is monkeypatched fs', function (t) {
  4. t.equal(fs, require('../fs.js'))
  5. t.end()
  6. })
  7. test('open an existing file works', function (t) {
  8. var fd = fs.openSync(__filename, 'r')
  9. fs.closeSync(fd)
  10. fs.open(__filename, 'r', function (er, fd) {
  11. if (er) throw er
  12. fs.close(fd, function (er) {
  13. if (er) throw er
  14. t.pass('works')
  15. t.end()
  16. })
  17. })
  18. })
  19. test('open a non-existing file throws', function (t) {
  20. var er
  21. try {
  22. var fd = fs.openSync('this file does not exist', 'r')
  23. } catch (x) {
  24. er = x
  25. }
  26. t.ok(er, 'should throw')
  27. t.notOk(fd, 'should not get an fd')
  28. t.equal(er.code, 'ENOENT')
  29. fs.open('neither does this file', 'r', function (er, fd) {
  30. t.ok(er, 'should throw')
  31. t.notOk(fd, 'should not get an fd')
  32. t.equal(er.code, 'ENOENT')
  33. t.end()
  34. })
  35. })