Skip to content

Commit e737b47

Browse files
committed
recursive rename/mv directories. Fixes #9.
1 parent 2084998 commit e737b47

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,13 @@ function bashEmulator (initialState) {
183183
return Promise.reject(source + ': No such file or directory')
184184
}
185185
return parentExists(destinationPath).then(function () {
186-
state.fileSystem[destinationPath] = state.fileSystem[sourcePath]
187-
delete state.fileSystem[sourcePath]
186+
Object.keys(state.fileSystem).forEach(function (key) {
187+
if (key.startsWith(sourcePath)) {
188+
var destKey = key.replace(sourcePath, destinationPath)
189+
state.fileSystem[destKey] = state.fileSystem[key]
190+
delete state.fileSystem[key]
191+
}
192+
})
188193
})
189194
},
190195

test/commands/mv.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,21 @@ function emulator () {
2424
type: 'file',
2525
modified: Date.now(),
2626
content: 'some err'
27+
},
28+
'/somedir': {
29+
type: 'dir',
30+
modified: Date.now()
31+
},
32+
'/somedir/subdir': {
33+
type: 'dir',
34+
modified: Date.now()
2735
}
2836
}
2937
})
3038
}
3139

3240
test('mv', function (t) {
33-
t.plan(28)
41+
t.plan(33)
3442

3543
emulator().run('mv').then(null, function (output) {
3644
t.equal(output, 'mv: missing file operand', 'fail without args')
@@ -157,4 +165,28 @@ test('mv', function (t) {
157165
}).then(function (output) {
158166
t.equal(output, 'read this first', 'create new file')
159167
})
168+
169+
var mul8 = emulator()
170+
mul8.run('mv somedir othername')
171+
.then(function (output) {
172+
t.equal(output, '', 'no output on success')
173+
return mul8.stat('somedir')
174+
})
175+
.then(null, function () {
176+
t.ok(true, 'old directory is gone')
177+
return mul8.stat('somedir/subdir')
178+
})
179+
.then(null, function () {
180+
t.ok(true, 'old sub-directory is gone')
181+
return mul8.stat('othername')
182+
})
183+
.then(function () {
184+
t.ok(true, 'directory is at new location')
185+
return mul8.stat('othername/subdir')
186+
}, function () {
187+
console.log(arguments)
188+
})
189+
.then(function () {
190+
t.ok(true, 'sub-directory has been moved too')
191+
})
160192
})

0 commit comments

Comments
 (0)