Skip to content

Commit a5e35ca

Browse files
committed
Modify the tests
1 parent 83c10c8 commit a5e35ca

File tree

5 files changed

+33
-45
lines changed

5 files changed

+33
-45
lines changed

src/omnisharp/OmnisharpDownloader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class OmnisharpDownloader {
4141
}
4242

4343
public async GetLatestVersion(serverUrl: string, latestVersionFileServerPath: string): Promise<string> {
44-
let description = "Latest Omnisharp Version Information";
44+
let description = "Latest OmniSharp Version Information";
4545
let url = `${serverUrl}/${latestVersionFileServerPath}`;
4646
try {
4747
this.eventStream.post(new LatestBuildDownloadStart());

src/packageManager/FileDownloader.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import { parse as parseUrl } from 'url';
1212
import { getProxyAgent } from './proxy';
1313
import { NetworkSettingsProvider } from '../NetworkSettings';
1414

15-
export async function DownloadFile(description: string, eventStream: EventStream, networkSettingsProvider: NetworkSettingsProvider, url: string, fallbackUrl?: string){
15+
export async function DownloadFile(description: string, eventStream: EventStream, networkSettingsProvider: NetworkSettingsProvider, url: string, fallbackUrl?: string) {
1616
eventStream.post(new DownloadStart(description));
17-
17+
1818
try {
1919
let buffer = await downloadFile(description, url, eventStream, networkSettingsProvider);
2020
eventStream.post(new DownloadSuccess(` Done!`));
@@ -96,9 +96,6 @@ async function downloadFile(description: string, urlString: string, eventStream:
9696
response.on('error', err => {
9797
reject(new NestedError(`Reponse error: ${err.message || 'NONE'}`, err));
9898
});
99-
100-
// Begin piping data from the response to the package file
101-
// response.pipe(tmpFile, { end: false });
10299
});
103100

104101
request.on('error', err => {

test/unitTests/Packages/FileDownloader.test.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55

66
import * as fs from 'async-file';
77
import * as chai from 'chai';
8-
import * as util from '../../../src/common';
98
import { EventStream } from '../../../src/EventStream';
109
import { DownloadFile } from '../../../src/packageManager/FileDownloader';
1110
import NetworkSettings from '../../../src/NetworkSettings';
12-
import { TmpAsset, CreateTmpFile } from '../../../src/CreateTmpAsset';
1311
import { BaseEvent, DownloadStart, DownloadSizeObtained, DownloadProgress, DownloadSuccess, DownloadFallBack, DownloadFailure } from '../../../src/omnisharp/loggingEvents';
1412
import { getRequestHandler } from '../testAssets/MockHttpServerRequestHandler';
1513

@@ -48,7 +46,6 @@ suite("FileDownloader", () => {
4846

4947
let server: any;
5048
let httpsServerUrl: string;
51-
let tmpFile: TmpAsset;
5249

5350
suiteSetup(async () => {
5451
let port = await getPort();
@@ -66,8 +63,6 @@ suite("FileDownloader", () => {
6663

6764
setup(async () => {
6865
await new Promise(resolve => server.start(resolve));
69-
tmpFile = await CreateTmpFile();
70-
util.setExtensionPath(tmpFile.name);
7166
eventBus = [];
7267
server.on(getRequestHandler('GET', correctUrlPath, 200, { "content-type": "text/plain" }, "Test content"));
7368
server.on(getRequestHandler('GET', errorUrlPath, 404));
@@ -92,15 +87,13 @@ suite("FileDownloader", () => {
9287
].forEach((elem) => {
9388
suite(elem.description, () => {
9489
test('File is downloaded', async () => {
95-
await DownloadFile(tmpFile.fd, fileDescription, eventStream, networkSettingsProvider, getURL(elem.urlPath), getURL(elem.fallBackUrlPath));
96-
const stats = await fs.stat(tmpFile.name);
97-
expect(stats.size).to.not.equal(0);
98-
let text = await fs.readFile(tmpFile.name, 'utf8');
90+
let buffer = await DownloadFile(fileDescription, eventStream, networkSettingsProvider, getURL(elem.urlPath), getURL(elem.fallBackUrlPath));
91+
let text = buffer.toString('utf8');
9992
expect(text).to.be.equal("Test content");
10093
});
10194

10295
test('Events are created in the correct order', async () => {
103-
await DownloadFile(tmpFile.fd, fileDescription, eventStream, networkSettingsProvider, getURL(elem.urlPath), getURL(elem.fallBackUrlPath));
96+
await DownloadFile(fileDescription, eventStream, networkSettingsProvider, getURL(elem.urlPath), getURL(elem.fallBackUrlPath));
10497
expect(eventBus).to.be.deep.equal(elem.getEventSequence());
10598
});
10699
});
@@ -109,17 +102,15 @@ suite("FileDownloader", () => {
109102

110103
suite('If the response status Code is 301, redirect occurs and the download succeeds', () => {
111104
test('File is downloaded from the redirect url', async () => {
112-
await DownloadFile(tmpFile.fd, fileDescription, eventStream, networkSettingsProvider, getURL(redirectUrlPath));
113-
const stats = await fs.stat(tmpFile.name);
114-
expect(stats.size).to.not.equal(0);
115-
let text = await fs.readFile(tmpFile.name, "utf8");
105+
let buffer = await DownloadFile(fileDescription, eventStream, networkSettingsProvider, getURL(redirectUrlPath));
106+
let text = buffer.toString('utf8');
116107
expect(text).to.be.equal("Test content");
117108
});
118109
});
119110

120111
suite('If the response status code is not 301, 302 or 200 then the download fails', () => {
121112
test('Error is thrown', async () => {
122-
expect(DownloadFile(tmpFile.fd, fileDescription, eventStream, networkSettingsProvider, getURL(errorUrlPath))).be.rejected;
113+
expect(DownloadFile(fileDescription, eventStream, networkSettingsProvider, getURL(errorUrlPath))).be.rejected;
123114
});
124115

125116
test('Download Start and Download Failure events are created', async () => {
@@ -128,24 +119,16 @@ suite("FileDownloader", () => {
128119
new DownloadFailure("failed (error code '404')")
129120
];
130121
try {
131-
await DownloadFile(tmpFile.fd, fileDescription, eventStream, networkSettingsProvider, getURL(errorUrlPath));
122+
await DownloadFile(fileDescription, eventStream, networkSettingsProvider, getURL(errorUrlPath));
132123
}
133124
catch (error) {
134125
expect(eventBus).to.be.deep.equal(eventsSequence);
135126
}
136127
});
137128
});
138129

139-
test('Error is thrown on invalid input file', async () => {
140-
//fd=0 means there is no file
141-
expect(DownloadFile(0, fileDescription, eventStream, networkSettingsProvider, getURL(errorUrlPath))).to.be.rejected;
142-
});
143-
144130
teardown(async () => {
145131
await new Promise((resolve, reject) => server.stop(resolve));
146-
if (tmpFile) {
147-
tmpFile.dispose();
148-
}
149132
});
150133

151134
function getURL(urlPath: string) {

test/unitTests/Packages/ZipInstaller.test.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import * as fs from 'async-file';
77
import * as path from 'path';
88
import * as chai from 'chai';
99
import * as util from '../../../src/common';
10-
import { CreateTmpDir, TmpAsset, CreateTmpFile } from '../../../src/CreateTmpAsset';
10+
import { CreateTmpDir, TmpAsset } from '../../../src/CreateTmpAsset';
1111
import { InstallZip } from '../../../src/packageManager/ZipInstaller';
1212
import { EventStream } from '../../../src/EventStream';
1313
import { PlatformInformation } from '../../../src/platform';
1414
import { BaseEvent, InstallationStart } from '../../../src/omnisharp/loggingEvents';
1515
import { Files, Binaries, createTestZipAsync } from '../testAssets/CreateTestZip';
16+
import ReadFileIntoBuffer from '../testAssets/ReadFileIntoBuffer';
1617

1718
chai.use(require("chai-as-promised"));
1819
let expect = chai.expect;
@@ -21,9 +22,8 @@ suite('ZipInstaller', () => {
2122
let tmpSourceDir: TmpAsset;
2223
let tmpInstallDir: TmpAsset;
2324
let testDirPath: string;
24-
let zipFileDescriptor: number;
25-
let txtFile: TmpAsset;
2625
let installationPath: string;
26+
let testBuffer: Buffer;
2727

2828
const fileDescription = "somefile";
2929
const eventStream = new EventStream();
@@ -36,24 +36,23 @@ suite('ZipInstaller', () => {
3636
tmpSourceDir = await CreateTmpDir(true);
3737
tmpInstallDir = await CreateTmpDir(true);
3838
installationPath = tmpInstallDir.name;
39-
txtFile = await CreateTmpFile();
4039
allFiles = [...Files, ...Binaries];
4140
testDirPath = tmpSourceDir.name + "/test.zip";
4241
await createTestZipAsync(testDirPath, allFiles);
43-
zipFileDescriptor = await fs.open(path.resolve(testDirPath), 'r');
42+
testBuffer = await ReadFileIntoBuffer(testDirPath);
4443
util.setExtensionPath(tmpInstallDir.name);
4544
});
4645

4746
test('The folder is unzipped and all the files are present at the expected paths', async () => {
48-
await InstallZip(zipFileDescriptor, fileDescription, installationPath, [], eventStream);
47+
await InstallZip(testBuffer, fileDescription, installationPath, [], eventStream);
4948
for (let elem of allFiles) {
5049
let filePath = path.join(installationPath, elem.path);
5150
expect(await util.fileExists(filePath)).to.be.true;
5251
}
5352
});
5453

5554
test('The folder is unzipped and all the expected events are created', async () => {
56-
await InstallZip(zipFileDescriptor, fileDescription, installationPath, [], eventStream);
55+
await InstallZip(testBuffer, fileDescription, installationPath, [], eventStream);
5756
let eventSequence: BaseEvent[] = [
5857
new InstallationStart(fileDescription)
5958
];
@@ -63,7 +62,7 @@ suite('ZipInstaller', () => {
6362
test('The folder is unzipped and the binaries have the expected permissions(except on Windows)', async () => {
6463
if (!((await PlatformInformation.GetCurrent()).isWindows())) {
6564
let resolvedBinaryPaths = Binaries.map(binary => path.join(installationPath, binary.path));
66-
await InstallZip(zipFileDescriptor, fileDescription, installationPath, resolvedBinaryPaths, eventStream);
65+
await InstallZip(testBuffer, fileDescription, installationPath, resolvedBinaryPaths, eventStream);
6766
for (let binaryPath of resolvedBinaryPaths) {
6867
expect(await util.fileExists(binaryPath)).to.be.true;
6968
let mode = (await fs.stat(binaryPath)).mode;
@@ -73,16 +72,10 @@ suite('ZipInstaller', () => {
7372
});
7473

7574
test('Error is thrown when the file is not a zip', async () => {
76-
expect(InstallZip(txtFile.fd, "Text File", installationPath, [], eventStream)).to.be.rejected;
77-
});
78-
79-
test('Error is thrown on invalid input file', async () => {
80-
//fd=0 means there is no file
81-
expect(InstallZip(0, fileDescription, "someRandomPath", [], eventStream)).to.be.rejected;
75+
expect(InstallZip(new Buffer("My file", "utf8"), "Text File", installationPath, [], eventStream)).to.be.rejected;
8276
});
8377

8478
teardown(async () => {
85-
await fs.close(zipFileDescriptor);
8679
tmpSourceDir.dispose();
8780
tmpInstallDir.dispose();
8881
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import * as fs from 'fs';
7+
8+
export default async function ReadFileIntoBuffer(filepath: string): Promise<Buffer> {
9+
let buffers: any[] = [];
10+
return new Promise<Buffer>(resolve => {
11+
let readStream = fs.createReadStream(filepath);
12+
readStream.on('data', data => buffers.push(data));
13+
readStream.on('end', () => resolve(Buffer.concat(buffers)));
14+
});
15+
}

0 commit comments

Comments
 (0)