Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements move function #17

Merged
merged 1 commit into from
Jan 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ Copy the `from` file and parse its content as an underscore template where `cont

Optionnally pass a template `settings` object.

### `#move(from, to, [options])`

Move/rename a file from the `from` path to the `to` path.

`#move` internally uses `#copy` and `#delete`, so `from` can be a glob pattern, and you can provide `options.globOptions` with it.

### `#exists(filepath)`

Returns `true` if a file exists. Returns `false` if the file is not found or deleted.
Expand Down
6 changes: 6 additions & 0 deletions actions/move.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = function (from, to, options) {
this.copy(from, to, options);
this.delete(from, options);
};
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ EditionInterface.prototype.delete = require('./actions/delete.js');
EditionInterface.prototype.copy = require('./actions/copy.js').copy;
EditionInterface.prototype._copySingle = require('./actions/copy.js')._copySingle;
EditionInterface.prototype.copyTpl = require('./actions/copy-tpl.js');
EditionInterface.prototype.move = require('./actions/move.js');
EditionInterface.prototype.commit = require('./actions/commit.js');

exports.create = function (store) {
Expand Down
64 changes: 64 additions & 0 deletions test/move.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict';

var assert = require('assert');
var path = require('path');
var sinon = require('sinon');
var editor = require('..');
var memFs = require('mem-fs');

describe('#move()', function () {
beforeEach(function() {
var store = memFs.create();
this.fs = editor.create(store);
});

it('move file', function () {
var filepath = path.join(__dirname, 'fixtures/file-a.txt');
var initialContents = this.fs.read(filepath);
var newpath = '/new/path/file.txt';
this.fs.move(filepath, newpath);
assert.equal(this.fs.read(newpath), initialContents);
assert.throws(this.fs.read.bind(this.fs, filepath));
});

it('move directory', function() {
var filename = 'file.txt';
var dirpath = path.join(__dirname, 'fixtures/nested');
var filepath = path.join(dirpath, filename);
var newdirpath = '/new/path';
var newfilepath = path.join(newdirpath, filename);
this.fs.move(dirpath, newdirpath);
assert.equal(this.fs.store.get(newfilepath).state, 'modified');
assert.throws(this.fs.read.bind(this.fs, filepath));
});

it('move file to an existing `to` path', function() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SBoudrias If you disagree with this behavior, then #14 may make sense.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seem fine, we just overwrite the to file correct?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but the overwrite could be unintentional, especially when it is done without giving any warning.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's to be handled by write filters.

var filepath = path.join(__dirname, 'fixtures/file-a.txt');
var initialContents = this.fs.read(filepath);
var newpath = path.join(__dirname, 'fixtures/nested/file.txt');
this.fs.move(filepath, newpath);
assert.equal(this.fs.read(newpath), initialContents);
assert.throws(this.fs.read.bind(this.fs, filepath));
});

it('move directory to an existing `to` path (as a directory)', function () {
var dirpath = path.join(__dirname, 'fixtures/other');
var filepath = path.join(__dirname, 'another.txt');
var contents = 'another';
var fromdir = path.join(__dirname, 'fixtures/nested');

this.fs.write(filepath, contents);
this.fs.move(fromdir, dirpath);

assert.equal(this.fs.read(path.join(dirpath, 'file.txt')), 'nested\n');
assert.equal(this.fs.read(filepath), contents);
assert.throws(this.fs.read.bind(this.fs, path.join(fromdir, 'file.txt')));
});

it('throws when moving directory to an existing `to` path (as a file)', function() {
var filepath = path.join(__dirname, 'fixtures/file-a.txt');
var frompath = path.join(__dirname, 'fixtures/nested');

assert.throws(this.fs.move.bind(this.fs, frompath, filepath));
});
});