-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store log files as .log @W-7858918@ (#20)
@W-7858918@
- Loading branch information
1 parent
13eaeca
commit 3f272b5
Showing
5 changed files
with
141 additions
and
32 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,35 @@ | ||
/* | ||
* 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 a file on disk. | ||
* | ||
* @param filePath path where to | ||
* @param fileContent file contents | ||
*/ | ||
export function createFile(filePath: string, fileContent: string): void { | ||
ensureFileExists(filePath); | ||
|
||
const writeStream = fs.createWriteStream(filePath); | ||
writeStream.write(fileContent); | ||
} |
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 { createFile } 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); | ||
}); | ||
}); | ||
}); |