Repository: streamich/memfs
Pull request #43 (2017-09-26 22:54:33)

Develop


Patch:
diff --git a/demo/rename.ts b/demo/rename.ts
new file mode 100644
index 00000000..476852cd
--- /dev/null
+++ b/demo/rename.ts
@@ -0,0 +1,5 @@
+import {Volume} from "../src/volume";
+
+const vol = Volume.fromJSON({'/foo/foo': 'bar'});
+vol.renameSync('/foo/foo', '/foo/foo2');
+console.log(vol.toJSON());
diff --git a/package.json b/package.json
index a757ac01..342b676a 100644
--- a/package.json
+++ b/package.json
@@ -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",
diff --git a/src/volume.ts b/src/volume.ts
index 4f5dded2..fc52d764 100644
--- a/src/volume.ts
+++ b/src/volume.ts
@@ -1024,7 +1024,7 @@ export class Volume {
     readFile(id: TFileId, callback: TCallback<TDataOut>);
     readFile(id: TFileId, options: IReadFileOptions|string,                 callback: TCallback<TDataOut>);
     readFile(id: TFileId, a: TCallback<TDataOut>|IReadFileOptions|string,   b?: TCallback<TDataOut>) {
-        let options: IReadFileOptions|string = a;
+        let options: IReadFileOptions|string = a as IReadFileOptions|string;
         let callback: TCallback<TData> = b;
 
         if(typeof options === 'function') {
@@ -1400,7 +1400,8 @@ export class Volume {
         }
 
         // Rename should overwrite the new path, if that exists.
-        link.steps = newPathSteps;
+        const name = newPathSteps[newPathSteps.length - 1];
+        link.steps = [...newPathDirLink.steps, name];
         newPathDirLink.setChild(link.getName(), link);
     }
 
@@ -1518,7 +1519,7 @@ export class Volume {
 
     readdir(path: TFilePath, callback: TCallback<TDataOut[]>);
     readdir(path: TFilePath, options: IOptions | string,                        callback: TCallback<TDataOut[]>);
-    readdir(path: TFilePath, a: TCallback<TDataOut[]> | IOptions | string,      b?: TCallback<TDataOut[]>) {
+    readdir(path: TFilePath, a?, b?) {
         let options: IOptions | string = a;
         let callback: TCallback<TDataOut[]> = b;
 


Test patch:
diff --git a/src/__tests__/volume.test.ts b/src/__tests__/volume.test.ts
index dba5dd70..7e8663a2 100644
--- a/src/__tests__/volume.test.ts
+++ b/src/__tests__/volume.test.ts
@@ -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)', () => {
diff --git a/src/__tests__/volume/__snapshots__/renameSync.test.ts.snap b/src/__tests__/volume/__snapshots__/renameSync.test.ts.snap
new file mode 100644
index 00000000..60e16bdf
--- /dev/null
+++ b/src/__tests__/volume/__snapshots__/renameSync.test.ts.snap
@@ -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"`;
diff --git a/src/__tests__/volume/rename.test.ts b/src/__tests__/volume/rename.test.ts
new file mode 100644
index 00000000..3ea8d43f
--- /dev/null
+++ b/src/__tests__/volume/rename.test.ts
@@ -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();
+        });
+    });
+});
\ No newline at end of file
diff --git a/src/__tests__/volume/renameSync.test.ts b/src/__tests__/volume/renameSync.test.ts
new file mode 100644
index 00000000..51d0fe17
--- /dev/null
+++ b/src/__tests__/volume/renameSync.test.ts
@@ -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()
+    });
+});
\ No newline at end of file