open.js 1.1 KB

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