Repository navigation
Factor out the tempfile-related code. #9455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Eric Snow (ericsnowcurrently)
merged 5 commits into
microsoft:master
from
ericsnowcurrently:fs-temp
Jan 21, 2020
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7462d37
Extract the temp file operations into a separate class.
ericsnowcurrently 5aa1c54
Add missing doc comments.
ericsnowcurrently 2256097
Ensure full coverage on FileSystem.
ericsnowcurrently 59e665b
Add ITempFileSystem.
ericsnowcurrently 6d1bfeb
Add tests for the error cases.
ericsnowcurrently File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,42 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import * as tmp from 'tmp'; | ||
| import { ITempFileSystem, TemporaryFile } from './types'; | ||
|
|
||
| interface IRawTempFS { | ||
| // tslint:disable-next-line:no-any | ||
| file(config: tmp.Options, callback?: (err: any, path: string, fd: number, cleanupCallback: () => void) => void): void; | ||
| } | ||
|
|
||
| // Operations related to temporary files and directories. | ||
| export class TemporaryFileSystem implements ITempFileSystem { | ||
| // prettier-ignore | ||
| constructor( | ||
| private readonly raw: IRawTempFS | ||
| ) { } | ||
| public static withDefaults(): TemporaryFileSystem { | ||
| // prettier-ignore | ||
| return new TemporaryFileSystem( | ||
| tmp | ||
| ); | ||
| } | ||
|
|
||
| // Create a new temp file with the given filename suffix. | ||
| public createFile(suffix: string): Promise<TemporaryFile> { | ||
| const opts = { | ||
| postfix: suffix | ||
| }; | ||
| return new Promise<TemporaryFile>((resolve, reject) => { | ||
| this.raw.file(opts, (err, filename, _fd, cleanUp) => { | ||
| if (err) { | ||
| return reject(err); | ||
| } | ||
| resolve({ | ||
| filePath: filename, | ||
| dispose: cleanUp | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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,85 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| // tslint:disable:max-func-body-length chai-vague-errors | ||
|
|
||
| import { expect } from 'chai'; | ||
| import * as fs from 'fs-extra'; | ||
| import { TemporaryFileSystem } from '../../../client/common/platform/fs-temp'; | ||
| import { TemporaryFile } from '../../../client/common/platform/types'; | ||
| // prettier-ignore | ||
| import { | ||
| assertDoesNotExist, assertExists, FSFixture, WINDOWS | ||
| } from './utils'; | ||
|
|
||
| suite('FileSystem - TemporaryFileSystem', () => { | ||
| let tmpfs: TemporaryFileSystem; | ||
| let fix: FSFixture; | ||
| setup(async () => { | ||
| tmpfs = TemporaryFileSystem.withDefaults(); | ||
| fix = new FSFixture(); | ||
| }); | ||
| teardown(async () => { | ||
| await fix.cleanUp(); | ||
| }); | ||
|
|
||
| suite('createFile', () => { | ||
| async function createFile(suffix: string): Promise<TemporaryFile> { | ||
| const tempfile = await tmpfs.createFile(suffix); | ||
| fix.addFSCleanup(tempfile.filePath, tempfile.dispose); | ||
| return tempfile; | ||
| } | ||
|
|
||
| test('TemporaryFile is created properly', async () => { | ||
| const tempfile = await tmpfs.createFile('.tmp'); | ||
| fix.addFSCleanup(tempfile.filePath, tempfile.dispose); | ||
| await assertExists(tempfile.filePath); | ||
|
|
||
| expect(tempfile.filePath.endsWith('.tmp')).to.equal(true, `bad suffix on ${tempfile.filePath}`); | ||
| }); | ||
|
|
||
| test('TemporaryFile is disposed properly', async () => { | ||
| const tempfile = await createFile('.tmp'); | ||
| await assertExists(tempfile.filePath); | ||
|
|
||
| tempfile.dispose(); | ||
|
|
||
| await assertDoesNotExist(tempfile.filePath); | ||
| }); | ||
|
|
||
| test('Ensure creating a temporary file results in a unique temp file path', async () => { | ||
| const tempFile = await createFile('.tmp'); | ||
| const tempFile2 = await createFile('.tmp'); | ||
|
|
||
| const filename1 = tempFile.filePath; | ||
| const filename2 = tempFile2.filePath; | ||
|
|
||
| expect(filename1).to.not.equal(filename2); | ||
| }); | ||
|
|
||
| test('Ensure writing to a temp file is supported via file stream', async function() { | ||
| if (WINDOWS) { | ||
| // tslint:disable-next-line:no-invalid-this | ||
| this.skip(); | ||
| } | ||
| const tempfile = await createFile('.tmp'); | ||
| const stream = fs.createWriteStream(tempfile.filePath); | ||
| fix.addCleanup(() => stream.destroy()); | ||
| const data = '...'; | ||
|
|
||
| stream.write(data, 'utf8'); | ||
|
|
||
| const actual = await fs.readFile(tempfile.filePath, 'utf8'); | ||
| expect(actual).to.equal(data); | ||
| }); | ||
|
|
||
| test('Ensure chmod works against a temporary file', async () => { | ||
| // Note that on Windows chmod is a noop. | ||
| const tempfile = await createFile('.tmp'); | ||
|
|
||
| const promise = fs.chmod(tempfile.filePath, '7777'); | ||
|
|
||
| await expect(promise).to.not.eventually.be.rejected; | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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,52 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| // tslint:disable:max-func-body-length | ||
|
|
||
| import { expect } from 'chai'; | ||
| import * as TypeMoq from 'typemoq'; | ||
| import { TemporaryFileSystem } from '../../../client/common/platform/fs-temp'; | ||
|
|
||
| interface IDeps { | ||
| // tmp module | ||
| // tslint:disable-next-line:no-any | ||
| file(config: { postfix?: string }, callback?: (err: any, path: string, fd: number, cleanupCallback: () => void) => void): void; | ||
| } | ||
|
|
||
| suite('FileSystem - temp files', () => { | ||
| let deps: TypeMoq.IMock<IDeps>; | ||
| let temp: TemporaryFileSystem; | ||
| setup(() => { | ||
| deps = TypeMoq.Mock.ofType<IDeps>(undefined, TypeMoq.MockBehavior.Strict); | ||
| temp = new TemporaryFileSystem(deps.object); | ||
| }); | ||
| function verifyAll() { | ||
| deps.verifyAll(); | ||
| } | ||
|
|
||
| suite('createFile', () => { | ||
| test(`fails if the raw call fails`, async () => { | ||
| const failure = new Error('oops'); | ||
| // prettier-ignore | ||
| deps.setup(d => d.file({ postfix: '.tmp' }, TypeMoq.It.isAny())) | ||
| .throws(failure); | ||
|
|
||
| const promise = temp.createFile('.tmp'); | ||
|
|
||
| await expect(promise).to.eventually.be.rejected; | ||
| verifyAll(); | ||
| }); | ||
|
|
||
| test(`fails if the raw call "returns" an error`, async () => { | ||
| const failure = new Error('oops'); | ||
| deps.setup(d => d.file({ postfix: '.tmp' }, TypeMoq.It.isAny())) | ||
| // tslint:disable-next-line:no-empty | ||
| .callback((_cfg, cb) => cb(failure, '...', -1, () => {})); | ||
|
|
||
| const promise = temp.createFile('.tmp'); | ||
|
|
||
| await expect(promise).to.eventually.be.rejected; | ||
| verifyAll(); | ||
| }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.