fix(@angular-devkit/core): Rename to a non-existing dir

Added unit test and requested changes.
Fixes #16484
This commit is contained in:
Sachin Grover 2020-02-05 02:27:56 +05:30 committed by Douglas Parker
parent 1beb582050
commit 54b79ade65
2 changed files with 21 additions and 0 deletions

View File

@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import * as fs from 'fs';
import * as path from 'path';
import { Observable, concat, from as observableFrom, of, throwError } from 'rxjs';
import {
concatMap,
@ -316,6 +317,10 @@ export class NodeJsSyncHost implements virtualFs.Host<fs.Stats> {
// TODO: remove this try+catch when issue https://github.com/ReactiveX/rxjs/issues/3740 is
// fixed.
try {
const toSystemPath = getSystemPath(to);
if (!fs.existsSync(path.dirname(toSystemPath))) {
fs.mkdirSync(path.dirname(toSystemPath), { recursive: true });
}
fs.renameSync(getSystemPath(from), getSystemPath(to));
obs.next();
obs.complete();

View File

@ -108,4 +108,20 @@ describe('NodeJsSyncHost', () => {
.then(done, done.fail);
}, 30000);
linuxOnlyIt('rename to a non-existing dir', done => {
Promise.resolve()
.then(() => fs.mkdirSync(root + '/rename'))
.then(() => fs.writeFileSync(root + '/rename/a.txt', 'hello world'))
.then(() => {
host.rename(normalize('/rename/a.txt'), normalize('/rename/b/c/d/a.txt'));
if (fs.existsSync(root + '/rename/b/c/d/a.txt')) {
const resContent = host.read(normalize('/rename/b/c/d/a.txt'));
const content = virtualFs.fileBufferToString(resContent);
expect(content).toEqual('hello world');
}
})
.then(done, done.fail);
}, 30000);
});