Skip to content

Commit

Permalink
fix(@angular-devkit/core): Rename to a non-existing dir
Browse files Browse the repository at this point in the history
Added unit test and requested changes.
Fixes #16484
  • Loading branch information
sacgrover authored and dgp1130 committed Feb 7, 2020
1 parent 1beb582 commit 54b79ad
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/angular_devkit/core/node/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
16 changes: 16 additions & 0 deletions packages/angular_devkit/core/node/host_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

});

0 comments on commit 54b79ad

Please sign in to comment.