2014-05-23 20:53:50 +00:00
var util = require ( '../lib/test-utils.js' ) ;
var expect = require ( 'chai' ) . expect ;
2018-12-29 18:10:58 +00:00
var { FIRST _DESCRIPTOR } = require ( '../../src/constants.js' ) ;
2019-04-10 19:34:05 +00:00
var Constants = require ( '../../src/constants.js' ) ;
2014-05-23 20:53:50 +00:00
describe ( 'fs.open' , function ( ) {
beforeEach ( util . setup ) ;
afterEach ( util . cleanup ) ;
it ( 'should be a function' , function ( ) {
var fs = util . fs ( ) ;
expect ( fs . open ) . to . be . a ( 'function' ) ;
} ) ;
it ( 'should return an error if the parent path does not exist' , function ( done ) {
var fs = util . fs ( ) ;
fs . open ( '/tmp/myfile' , 'w+' , function ( error , result ) {
expect ( error ) . to . exist ;
2018-07-15 17:25:35 +00:00
expect ( error . code ) . to . equal ( 'ENOENT' ) ;
2014-05-23 20:53:50 +00:00
expect ( result ) . not . to . exist ;
done ( ) ;
} ) ;
} ) ;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
it ( 'should return an error when flagged for read and the path does not exist' , function ( done ) {
var fs = util . fs ( ) ;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs . open ( '/myfile' , 'r+' , function ( error , result ) {
expect ( error ) . to . exist ;
2018-07-15 17:25:35 +00:00
expect ( error . code ) . to . equal ( 'ENOENT' ) ;
2014-05-23 20:53:50 +00:00
expect ( result ) . not . to . exist ;
done ( ) ;
2013-11-27 17:18:09 +00:00
} ) ;
2014-05-23 20:53:50 +00:00
} ) ;
2013-11-27 17:18:09 +00:00
2019-04-10 19:34:05 +00:00
it ( 'should create a file when passed the flag as a bitwise number' , function ( done ) {
var fs = util . fs ( ) ;
fs . open ( '/myfile' , Constants . fsConstants . O _CREAT , function ( error , fd ) {
expect ( error ) . not . to . exist ;
expect ( fd ) . to . exist ;
fs . close ( fd ) ;
done ( ) ;
} ) ;
} ) ;
2014-05-23 20:53:50 +00:00
it ( 'should return an error when flagged for write and the path is a directory' , function ( done ) {
var fs = util . fs ( ) ;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs . mkdir ( '/tmp' , function ( error ) {
if ( error ) throw error ;
fs . open ( '/tmp' , 'w' , function ( error , result ) {
2014-01-21 21:25:09 +00:00
expect ( error ) . to . exist ;
2018-07-15 17:25:35 +00:00
expect ( error . code ) . to . equal ( 'EISDIR' ) ;
2014-01-21 21:25:09 +00:00
expect ( result ) . not . to . exist ;
done ( ) ;
2013-11-27 17:18:09 +00:00
} ) ;
} ) ;
2014-05-23 20:53:50 +00:00
} ) ;
2013-11-27 17:18:09 +00:00
2019-02-27 16:56:11 +00:00
it ( 'should return an error when flagged for write and the path exists' , function ( done ) {
var fs = util . fs ( ) ;
fs . mkdir ( '/tmp' , function ( error ) {
if ( error ) throw error ;
fs . writeFile ( '/tmp/file' , 'data' , function ( error ) {
if ( error ) throw error ;
fs . open ( '/tmp/file' , 'wx' , function ( error , result ) {
expect ( error ) . to . exist ;
expect ( error . code ) . to . equal ( 'EEXIST' ) ;
expect ( result ) . not . to . exist ;
done ( ) ;
} ) ;
} ) ;
} ) ;
} ) ;
2014-05-23 20:53:50 +00:00
it ( 'should return an error when flagged for append and the path is a directory' , function ( done ) {
var fs = util . fs ( ) ;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs . mkdir ( '/tmp' , function ( error ) {
if ( error ) throw error ;
fs . open ( '/tmp' , 'a' , function ( error , result ) {
2014-01-21 21:25:09 +00:00
expect ( error ) . to . exist ;
2018-07-15 17:25:35 +00:00
expect ( error . code ) . to . equal ( 'EISDIR' ) ;
2014-01-21 21:25:09 +00:00
expect ( result ) . not . to . exist ;
done ( ) ;
2013-11-27 17:18:09 +00:00
} ) ;
} ) ;
2014-05-23 20:53:50 +00:00
} ) ;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
it ( 'should return a unique file descriptor' , function ( done ) {
var fs = util . fs ( ) ;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs . open ( '/file1' , 'w+' , function ( error , fd ) {
if ( error ) throw error ;
expect ( error ) . not . to . exist ;
expect ( fd ) . to . be . a ( 'number' ) ;
2013-11-27 17:18:09 +00:00
2018-12-29 18:10:58 +00:00
fs . open ( '/file2' , 'w+' , function ( error , fd1 ) {
2013-11-27 17:18:09 +00:00
if ( error ) throw error ;
2014-01-21 21:25:09 +00:00
expect ( error ) . not . to . exist ;
2018-12-29 18:10:58 +00:00
expect ( fd1 ) . to . be . a ( 'number' ) ;
expect ( fd1 ) . not . to . equal ( fd ) ;
fs . close ( fd , function ( error ) {
if ( error ) throw error ;
fs . close ( fd1 , done ) ;
} ) ;
2013-11-27 17:18:09 +00:00
} ) ;
} ) ;
2014-05-23 20:53:50 +00:00
} ) ;
2013-11-27 17:18:09 +00:00
2018-12-29 18:10:58 +00:00
it ( 'should return the argument value of the file descriptor index greater than or equal to the value set by the first useable file descriptor constant' , function ( done ) {
2014-05-23 20:53:50 +00:00
var fs = util . fs ( ) ;
2014-05-14 20:02:46 +00:00
2014-05-23 20:53:50 +00:00
fs . open ( '/file1' , 'w+' , function ( error , fd ) {
if ( error ) throw error ;
2018-12-29 18:10:58 +00:00
expect ( fd ) . to . equal ( FIRST _DESCRIPTOR ) ;
fs . close ( fd , done ) ;
} ) ;
} ) ;
it ( 'should reuse file descriptors after closing' , function ( done ) {
var fs = util . fs ( ) ;
fs . open ( '/file1' , 'w+' , function ( error , fd ) {
if ( error ) throw error ;
expect ( fd ) . to . equal ( FIRST _DESCRIPTOR ) ;
fs . close ( fd , function ( error ) {
if ( error ) throw error ;
fs . open ( '/file1' , 'w+' , function ( error , fd ) {
if ( error ) throw error ;
expect ( fd ) . to . equal ( FIRST _DESCRIPTOR ) ;
fs . close ( fd , done ) ;
} ) ;
} ) ;
2014-05-14 20:02:46 +00:00
} ) ;
2014-05-23 20:53:50 +00:00
} ) ;
2014-05-14 20:02:46 +00:00
2014-05-23 20:53:50 +00:00
it ( 'should create a new file when flagged for write' , function ( done ) {
var fs = util . fs ( ) ;
2013-11-27 17:18:09 +00:00
2018-12-29 18:10:58 +00:00
fs . open ( '/myfile' , 'w' , function ( error , fd ) {
2014-05-23 20:53:50 +00:00
if ( error ) throw error ;
2018-07-27 14:38:44 +00:00
2014-05-23 20:53:50 +00:00
fs . stat ( '/myfile' , function ( error , result ) {
expect ( error ) . not . to . exist ;
expect ( result ) . to . exist ;
2018-11-28 18:54:20 +00:00
expect ( result . isFile ( ) ) . to . be . true ;
2018-12-29 18:10:58 +00:00
fs . close ( fd , done ) ;
2013-11-27 17:18:09 +00:00
} ) ;
} ) ;
} ) ;
2014-10-17 17:16:09 +00:00
2018-09-25 03:28:56 +00:00
it ( 'should create a new file, when flagged for write, and set the mode to the passed value' , function ( done ) {
var fs = util . fs ( ) ;
2018-12-29 18:10:58 +00:00
fs . open ( '/myfile' , 'w' , 0o777 , function ( error , fd ) {
2018-09-25 03:28:56 +00:00
if ( error ) throw error ;
fs . stat ( '/myfile' , function ( error , result ) {
expect ( error ) . not . to . exist ;
expect ( result ) . to . exist ;
expect ( result . mode ) . to . exist ;
expect ( result . mode & 0o777 ) . to . equal ( 0o777 ) ;
2018-12-29 18:10:58 +00:00
fs . close ( fd , done ) ;
2018-09-25 03:28:56 +00:00
} ) ;
} ) ;
} ) ;
2018-10-10 19:10:34 +00:00
it ( 'should create a new file, but no mode is passed, so the default value of 644 should be seen' , function ( done ) {
var fs = util . fs ( ) ;
2018-12-29 18:10:58 +00:00
fs . open ( '/myfile' , 'w' , function ( error , fd ) {
2018-10-10 19:10:34 +00:00
if ( error ) throw error ;
fs . stat ( '/myfile' , function ( error , result ) {
expect ( error ) . not . to . exist ;
expect ( result ) . to . exist ;
expect ( result . mode ) . to . exist ;
expect ( result . mode & 0o644 ) . to . equal ( 0o644 ) ;
2018-12-29 18:10:58 +00:00
fs . close ( fd , done ) ;
2018-10-10 19:10:34 +00:00
} ) ;
} ) ;
} ) ;
2014-10-17 17:34:29 +00:00
/ * *
* This test is currently correct per our code , but incorrect according to the spec .
* When we fix https : //github.com/filerjs/filer/issues/314 we'll have to update this.
* /
2014-10-17 17:16:09 +00:00
it ( 'should error if an ofd\'s node goes away while open' , function ( done ) {
var fs = util . fs ( ) ;
fs . writeFile ( '/myfile' , 'data' , function ( error ) {
if ( error ) throw error ;
fs . open ( '/myfile' , 'r' , function ( error , fd ) {
if ( error ) throw error ;
// Delete the file while it's still open
fs . unlink ( '/myfile' , function ( error ) {
if ( error ) throw error ;
// This should fail now, since fd points to a bad node
fs . fstat ( fd , function ( error , result ) {
expect ( error ) . to . exist ;
expect ( error . code ) . to . equal ( 'EBADF' ) ;
expect ( result ) . not . to . exist ;
2018-12-29 18:10:58 +00:00
fs . close ( fd , done ) ;
2014-10-17 17:16:09 +00:00
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;
2018-09-24 22:18:31 +00:00
2018-12-13 03:02:08 +00:00
it ( 'should error when flag is invalid' , function ( done ) {
2018-09-24 22:18:31 +00:00
var fs = util . fs ( ) ;
2018-12-13 03:02:08 +00:00
fs . open ( '/myfile' , 'abcd' , function ( err ) {
expect ( err ) . to . exist ;
expect ( err . code ) . to . equal ( 'EINVAL' ) ;
2018-09-24 22:18:31 +00:00
done ( ) ;
2018-12-13 03:02:08 +00:00
} ) ;
2018-09-24 22:18:31 +00:00
} ) ;
2014-01-21 21:25:09 +00:00
} ) ;