11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci// Test of fs.readFile with different flags. 41cb0ef41Sopenharmony_ciconst common = require('../common'); 51cb0ef41Sopenharmony_ciconst fs = require('fs'); 61cb0ef41Sopenharmony_ciconst assert = require('assert'); 71cb0ef41Sopenharmony_ciconst path = require('path'); 81cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir'); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_citmpdir.refresh(); 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci{ 131cb0ef41Sopenharmony_ci const emptyFile = path.join(tmpdir.path, 'empty.txt'); 141cb0ef41Sopenharmony_ci fs.closeSync(fs.openSync(emptyFile, 'w')); 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci fs.readFile( 171cb0ef41Sopenharmony_ci emptyFile, 181cb0ef41Sopenharmony_ci // With `a+` the file is created if it does not exist 191cb0ef41Sopenharmony_ci common.mustNotMutateObjectDeep({ encoding: 'utf8', flag: 'a+' }), 201cb0ef41Sopenharmony_ci common.mustCall((err, data) => { assert.strictEqual(data, ''); }) 211cb0ef41Sopenharmony_ci ); 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci fs.readFile( 241cb0ef41Sopenharmony_ci emptyFile, 251cb0ef41Sopenharmony_ci // Like `a+` but fails if the path exists. 261cb0ef41Sopenharmony_ci common.mustNotMutateObjectDeep({ encoding: 'utf8', flag: 'ax+' }), 271cb0ef41Sopenharmony_ci common.mustCall((err, data) => { assert.strictEqual(err.code, 'EEXIST'); }) 281cb0ef41Sopenharmony_ci ); 291cb0ef41Sopenharmony_ci} 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci{ 321cb0ef41Sopenharmony_ci const willBeCreated = path.join(tmpdir.path, 'will-be-created'); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci fs.readFile( 351cb0ef41Sopenharmony_ci willBeCreated, 361cb0ef41Sopenharmony_ci // With `a+` the file is created if it does not exist 371cb0ef41Sopenharmony_ci common.mustNotMutateObjectDeep({ encoding: 'utf8', flag: 'a+' }), 381cb0ef41Sopenharmony_ci common.mustCall((err, data) => { assert.strictEqual(data, ''); }) 391cb0ef41Sopenharmony_ci ); 401cb0ef41Sopenharmony_ci} 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci{ 431cb0ef41Sopenharmony_ci const willNotBeCreated = path.join(tmpdir.path, 'will-not-be-created'); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci fs.readFile( 461cb0ef41Sopenharmony_ci willNotBeCreated, 471cb0ef41Sopenharmony_ci // Default flag is `r`. An exception occurs if the file does not exist. 481cb0ef41Sopenharmony_ci common.mustNotMutateObjectDeep({ encoding: 'utf8' }), 491cb0ef41Sopenharmony_ci common.mustCall((err, data) => { assert.strictEqual(err.code, 'ENOENT'); }) 501cb0ef41Sopenharmony_ci ); 511cb0ef41Sopenharmony_ci} 52