Skip to content

Commit

Permalink
test(fs): add test for fs.writeFile
Browse files Browse the repository at this point in the history
  • Loading branch information
sgtcoolguy committed Jul 18, 2019
1 parent 1087925 commit 8f94f70
Showing 1 changed file with 87 additions and 2 deletions.
89 changes: 87 additions & 2 deletions tests/Resources/fs.addontest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* Please see the LICENSE included with this distribution for details.
*/
/* eslint-env mocha */
/* global Ti */
/* eslint no-unused-expressions: "off" */
'use strict';
const should = require('./utilities/assertions');
Expand Down Expand Up @@ -849,7 +848,93 @@ describe('fs', function () {
});
});

// TODO: #writeFile()
describe('#writeFile()', () => {
it('is a function', () => {
should(fs.writeFile).be.a.Function;
});

it('writes a string to a non-existent file', finish => {
const filename = path.join(Ti.Filesystem.tempDirectory, `writeFile${Date.now()}.txt`);
// ensure parent dir exists
should(fs.existsSync(Ti.Filesystem.tempDirectory)).eql(true);
// ensure file does not
should(fs.existsSync(filename)).eql(false);
const contents = 'Hello World!';
fs.writeFile(filename, contents, err => {
try {
should(err).not.exist;
// file should now exist
should(fs.existsSync(filename)).eql(true);
// contents should match what we wrote
should(fs.readFileSync(filename, 'utf-8')).eql(contents);
} catch (e) {
return finish(e);
}
finish();
});
});

it('writes a string to existing file, replaces it', finish => {
const filename = path.join(Ti.Filesystem.tempDirectory, `writeFile${Date.now()}.txt`);
// ensure parent dir exists
should(fs.existsSync(Ti.Filesystem.tempDirectory)).eql(true);
// ensure file does not
should(fs.existsSync(filename)).eql(false);
const contents = 'Hello World!';
fs.writeFile(filename, contents, err => {
try {
should(err).not.exist;

// file should now exist
should(fs.existsSync(filename)).eql(true); // FIXME: fails on Android
// contents should match what we wrote
should(fs.readFileSync(filename, 'utf-8')).eql(contents);

// Now replace it's contents by writing again
const contents2 = 'I replaced you!';
fs.writeFile(filename, contents2, err2 => {
try {
should(err2).not.exist;
// contents should match what we wrote
should(fs.readFileSync(filename, 'utf-8')).eql(contents2);
} catch (e) {
return finish(e);
}
finish();
});
} catch (e) {
return finish(e);
}
});
});

it('throws if trying to write to path of an existing directory', finish => {
const dirname = path.join(Ti.Filesystem.tempDirectory, `writeFile_d_${Date.now()}`);
fs.mkdirSync(dirname);
// ensure dir exists
should(fs.existsSync(dirname)).eql(true);

fs.writeFile(dirname, 'Hello World!', error => {
try {
should(error).exist;
// verify error
error.name.should.eql('Error');
error.message.should.eql(`EISDIR: illegal operation on a directory, open '${dirname}'`);
error.errno.should.eql(-21);
error.syscall.should.eql('open');
error.code.should.eql('EISDIR');
error.path.should.eql(dirname);
} catch (e) {
return finish(e);
}
finish();
});
});

// TODO: What if parent dir does not exist?
// TODO: what if target path exists but is a directory?
// TODO: What if data is a Buffer?
});

describe('#writeFileSync()', () => {
it('is a function', () => {
Expand Down

0 comments on commit 8f94f70

Please sign in to comment.