Skip to content

Commit

Permalink
fix: rename() path fix #40
Browse files Browse the repository at this point in the history
  • Loading branch information
teagrinder committed Sep 26, 2017
1 parent 6338421 commit 68439db
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"nyc": "11.1.0",
"watch": "^1.0.0",
"@types/node": "8.0.17",
"@types/jest": "^20.0.8"
"@types/jest": "^21.1.1"
},
"scripts": {
"build": "npm run build-ts && npm run build-js",
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/volume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ describe('volume', () => {
const stats2 = vol.statSync('/mtime.txt');
expect(stats2.mtimeMs).toBeGreaterThan(stats1.mtimeMs);
done();
}, 1);
}, 2);
});
});
describe('.stat(path, callback)', () => {
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/volume/__snapshots__/renameSync.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`renameSync(fromPath, toPath) Throws if path is of wrong type 1`] = `"path must be a string or Buffer"`;

exports[`renameSync(fromPath, toPath) Throws on no params 1`] = `"path must be a string or Buffer"`;

exports[`renameSync(fromPath, toPath) Throws on only one param 1`] = `"path must be a string or Buffer"`;
12 changes: 12 additions & 0 deletions src/__tests__/volume/rename.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {create} from "../util";


describe('renameSync(fromPath, toPath)', () => {
it('Renames a simple case', done => {
const vol = create({'/foo': 'bar'});
vol.rename('/foo', '/foo2', (err, res) => {
expect(vol.toJSON()).toEqual({'/foo2': 'bar'});
done();
});
});
});
49 changes: 49 additions & 0 deletions src/__tests__/volume/renameSync.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {create} from "../util";


describe('renameSync(fromPath, toPath)', () => {
it('Renames a file', () => {
const vol = create({'/foo': 'bar'});
expect(vol.root.getChild('foo').getNode().isFile()).toBe(true);
vol.renameSync('/foo', '/baz');
expect(vol.root.getChild('foo')).toBeUndefined();
expect(vol.root.getChild('baz').getNode().isFile()).toBe(true);
expect(vol.readFileSync('/baz', 'utf8')).toBe('bar');
});
it('Rename file two levels deep', () => {
const vol = create({'/1/2': 'foobar'});
vol.renameSync('/1/2', '/1/3');
expect(vol.toJSON()).toEqual({'/1/3': 'foobar'});
});
it('Rename file three levels deep', () => {
const vol = create({
'/foo1': 'bar',
'/foo2/foo': 'bar',
'/foo3/foo/foo': 'bar',
});
vol.renameSync('/foo3/foo/foo', '/foo3/foo/foo2');
expect(vol.toJSON()).toEqual({
'/foo1': 'bar',
'/foo2/foo': 'bar',
'/foo3/foo/foo2': 'bar',
});
});
it('Throws on no params', () => {
const vol = create();
expect(() => {
(vol as any).renameSync();
}).toThrowErrorMatchingSnapshot()
});
it('Throws on only one param', () => {
const vol = create({'/foo': 'bar'});
expect(() => {
(vol as any).renameSync('/foo');
}).toThrowErrorMatchingSnapshot()
});
it('Throws if path is of wrong type', () => {
const vol = create({'/foo': 'bar'});
expect(() => {
(vol as any).renameSync('/foo', 123);
}).toThrowErrorMatchingSnapshot()
});
});
3 changes: 2 additions & 1 deletion src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1400,7 +1400,8 @@ export class Volume {
}

// Rename should overwrite the new path, if that exists.
link.steps = [...newPathDirLink.steps, newPathSteps[newPathSteps.length - 1]];
const name = newPathSteps[newPathSteps.length - 1];
link.steps = [...newPathDirLink.steps, name];
newPathDirLink.setChild(link.getName(), link);
}

Expand Down

0 comments on commit 68439db

Please sign in to comment.