-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ERR_STREAM_WRITE_AFTER_END when processing more than one log file
- Loading branch information
Showing
5 changed files
with
146 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) 2020, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
export function ensureDirectoryExists(filePath: string): void { | ||
if (fs.existsSync(filePath)) { | ||
return; | ||
} | ||
ensureDirectoryExists(path.dirname(filePath)); | ||
fs.mkdirSync(filePath); | ||
} | ||
|
||
export function ensureFileExists(filePath: string): void { | ||
ensureDirectoryExists(path.dirname(filePath)); | ||
fs.closeSync(fs.openSync(filePath, 'w')); | ||
} | ||
|
||
/** | ||
* Method to save multiple files on disk. | ||
* | ||
* @param fileMap key = filePath, value = file contents | ||
*/ | ||
export function createFiles(fileMap: Map<string, string>): void { | ||
for (const filePath of fileMap.keys()) { | ||
ensureFileExists(filePath); | ||
|
||
const writeStream = fs.createWriteStream(filePath); | ||
writeStream.write(fileMap.get(filePath)); | ||
writeStream.end(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* Copyright (c) 2020, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
export { createFiles } from './fileSystemHandler'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (c) 2020, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
import * as fsUtil from '../../src/utils/fileSystemHandler'; | ||
import { createSandbox, SinonStub } from 'sinon'; | ||
import { expect } from 'chai'; | ||
import { join } from 'path'; | ||
import * as fs from 'fs'; | ||
|
||
const sb = createSandbox(); | ||
|
||
describe('File System Utils', () => { | ||
describe('ensureDirectoryExists', () => { | ||
let mkdirStub: SinonStub; | ||
let existsStub: SinonStub; | ||
|
||
beforeEach(() => { | ||
mkdirStub = sb.stub(fs, 'mkdirSync'); | ||
existsStub = sb.stub(fs, 'existsSync'); | ||
}); | ||
|
||
afterEach(() => { | ||
sb.restore(); | ||
}); | ||
|
||
it('should return immediately if file or directory already exists', () => { | ||
const path = join('path', 'to', 'dir'); | ||
existsStub.withArgs(path).returns(true); | ||
|
||
fsUtil.ensureDirectoryExists(path); | ||
|
||
expect(mkdirStub.notCalled).to.be.true; | ||
}); | ||
|
||
it('should create nested directories as needed', () => { | ||
const path = join('path', 'to'); | ||
const path2 = join(path, 'dir'); | ||
const path3 = join(path2, 'dir2'); | ||
existsStub.returns(false); | ||
existsStub.withArgs(path).returns(true); | ||
|
||
fsUtil.ensureDirectoryExists(path3); | ||
|
||
expect(mkdirStub.firstCall.args[0]).to.equal(path2); | ||
expect(mkdirStub.secondCall.args[0]).to.equal(path3); | ||
}); | ||
}); | ||
|
||
describe('ensureFileExists', () => { | ||
afterEach(() => { | ||
sb.restore(); | ||
}); | ||
|
||
it('should ensure file exists', () => { | ||
const path = join('path', 'to', 'a', 'file.x'); | ||
const closeStub = sb.stub(fs, 'closeSync'); | ||
const openStub = sb.stub(fs, 'openSync'); | ||
openStub.returns(123); | ||
const existsSyncStub = sb.stub(fs, 'existsSync').returns(true); | ||
|
||
fsUtil.ensureFileExists(path); | ||
|
||
expect(existsSyncStub.calledBefore(openStub)).to.be.true; | ||
expect(closeStub.firstCall.args[0]).to.equal(123); | ||
}); | ||
}); | ||
}); |