Skip to content

Commit

Permalink
Feature: Add delete
Browse files Browse the repository at this point in the history
  • Loading branch information
nick spragg committed Jan 17, 2017
1 parent d488bb8 commit bb8b480
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ class File {
isExecutable() {
return this._access(fs.X_OK);
}

delete() {
return fsp.unlinkAsync(this._pathname);
}
}

module.exports.create = (filename) => {
Expand Down
29 changes: 26 additions & 3 deletions test/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ function deleteFile(fname) {
return fs.unlinkSync(fname);
}

function deleteFileIfExists(fname) {
try {
fs.unlinkSync(fname);
deleteFile(fname);
} catch (e) {}
}

describe('File', () => {
describe('.isDirectorySync', () => {
it('returns true when a pathname is a directory', () => {
Expand Down Expand Up @@ -454,12 +461,28 @@ describe('File', () => {
});

describe('.delete', () => {
const file = File.create(getFixturePath('justFiles/a.json'));
const fileToDelete = getFixturePath('delete/a.txt');
beforeEach(() => {
createFile(fileToDelete, {
duration: 1,
modifier: 'hours'
});
});

});
afterEach(() => {
deleteFileIfExists(fileToDelete);
});

describe('.createNewFile', () => {
it('deletes a file that exists', () => {
const file = File.create(getFixturePath('delete/a.txt'));

return file.delete()
.then(() => {
assert.throws(() => {
fs.statSync(getFixturePath('delete/a.txt'));
}, /ENOENT/);
});
});
});

describe('.getFixturePath', () => {
Expand Down

0 comments on commit bb8b480

Please sign in to comment.