File size: 5,413 Bytes
6e7eaf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
import { spawnSync } from 'child_process';
import { existsSync, promises as fs } from 'fs';
import { tmpdir } from 'os';
import { dirname, join } from 'path';
import { afterAll, afterEach, beforeAll, describe, expect, test } from 'vitest';
import { downloadAndUnzipVSCode, fetchInsiderVersions, fetchStableVersions, fetchTargetInferredVersion, } from './download.js';
import { SilentReporter } from './progress.js';
import { resolveCliPathFromVSCodeExecutablePath, systemDefaultPlatform } from './util.js';
const platforms = [
    'darwin',
    'darwin-arm64',
    'win32-x64-archive',
    'win32-arm64-archive',
    'linux-x64',
    'linux-arm64',
    'linux-armhf',
];
describe('sane downloads', () => {
    const testTempDir = join(tmpdir(), 'vscode-test-download');
    beforeAll(async () => {
        await fs.mkdir(testTempDir, { recursive: true });
    });
    for (const quality of ['insiders', 'stable']) {
        for (const platform of platforms) {
            test.concurrent(`${quality}/${platform}`, async () => {
                const location = await downloadAndUnzipVSCode({
                    platform,
                    version: quality,
                    cachePath: testTempDir,
                    reporter: new SilentReporter(),
                });
                if (!existsSync(location)) {
                    throw new Error(`expected ${location} to exist for ${platform}`);
                }
                const exePath = resolveCliPathFromVSCodeExecutablePath(location, platform);
                if (!existsSync(exePath)) {
                    throw new Error(`expected ${exePath} to from ${location}`);
                }
                if (platform === systemDefaultPlatform) {
                    const shell = process.platform === 'win32';
                    const version = spawnSync(shell ? `"${exePath}"` : exePath, ['--version'], { shell });
                    expect(version.status).to.equal(0);
                    expect(version.stdout.toString().trim()).to.not.be.empty;
                }
            });
        }
    }
    afterAll(async () => {
        try {
            await fs.rmdir(testTempDir, { recursive: true });
        }
        catch {
            // ignored
        }
    });
});
describe('fetchTargetInferredVersion', () => {
    let stable;
    let insiders;
    const extensionsDevelopmentPath = join(tmpdir(), 'vscode-test-tmp-workspace');
    beforeAll(async () => {
        [stable, insiders] = await Promise.all([fetchStableVersions(true, 5000), fetchInsiderVersions(true, 5000)]);
    });
    afterEach(async () => {
        await fs.rm(extensionsDevelopmentPath, { recursive: true, force: true });
    });
    const writeJSON = async (path, contents) => {
        const target = join(extensionsDevelopmentPath, path);
        await fs.mkdir(dirname(target), { recursive: true });
        await fs.writeFile(target, JSON.stringify(contents));
    };
    const doFetch = (paths = ['./']) => fetchTargetInferredVersion({
        cachePath: join(extensionsDevelopmentPath, '.cache'),
        platform: 'win32-x64-archive',
        timeout: 5000,
        extensionsDevelopmentPath: paths.map((p) => join(extensionsDevelopmentPath, p)),
    });
    test('matches stable if no workspace', async () => {
        const version = await doFetch();
        expect(version.id).to.equal(stable[0]);
    });
    test('matches stable by default', async () => {
        await writeJSON('package.json', {});
        const version = await doFetch();
        expect(version.id).to.equal(stable[0]);
    });
    test('matches if stable is defined', async () => {
        await writeJSON('package.json', { engines: { vscode: '^1.50.0' } });
        const version = await doFetch();
        expect(version.id).to.equal(stable[0]);
    });
    test('matches best', async () => {
        await writeJSON('package.json', { engines: { vscode: '<=1.60.5' } });
        const version = await doFetch();
        expect(version.id).to.equal('1.60.2');
    });
    test('matches multiple workspaces', async () => {
        await writeJSON('a/package.json', { engines: { vscode: '<=1.60.5' } });
        await writeJSON('b/package.json', { engines: { vscode: '<=1.55.5' } });
        const version = await doFetch(['a', 'b']);
        expect(version.id).to.equal('1.55.2');
    });
    test('matches insiders to better stable if there is one', async () => {
        await writeJSON('package.json', { engines: { vscode: '^1.60.0-insider' } });
        const version = await doFetch();
        expect(version.id).to.equal(stable[0]);
    });
    test('matches current insiders', async () => {
        await writeJSON('package.json', { engines: { vscode: `^${insiders[0]}` } });
        const version = await doFetch();
        expect(version.id).to.equal(insiders[0]);
    });
    test('matches insiders to exact', async () => {
        await writeJSON('package.json', { engines: { vscode: '1.60.0-insider' } });
        const version = await doFetch();
        expect(version.id).to.equal('1.60.0-insider');
    });
});