microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
7b48f26eb8caae7ed9cc1a09249195dfed76fa76

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

test/extension/checkEnvironment.test.ts

158lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import assert = require("assert");
5import { promises as fs } from "fs";
6import * as os from "os";
7import * as path from "path";
8import proxyquire = require("proxyquire");
9
10suite("checkEnvironment", function () {
11 suite("basicCheck", function () {
12 const commandExistsStub = { sync: () => true };
13 const fakeUtil = proxyquire("../../src/extension/services/validationService/util", {
14 "command-exists": commandExistsStub,
15 }) as typeof import("../../src/extension/services/validationService/util");
16
17 test("command should exist", async () => {
18 commandExistsStub.sync = () => true;
19 assert.deepStrictEqual(await fakeUtil.basicCheck({ command: "whatever" }), {
20 exists: true,
21 });
22 });
23
24 test("command should not exist", async () => {
25 commandExistsStub.sync = () => false;
26 assert.deepStrictEqual(await fakeUtil.basicCheck({ command: "whatever" }), {
27 exists: false,
28 });
29 });
30
31 test("command should check version", async () => {
32 commandExistsStub.sync = () => true;
33
34 let wasExecuted = false;
35
36 assert.deepStrictEqual(
37 await fakeUtil.basicCheck({
38 command: "whatever",
39 getVersion: async () => ((wasExecuted = true), "0.0.1"),
40 }),
41 {
42 exists: true,
43 versionCompare: 0,
44 },
45 );
46
47 assert(wasExecuted);
48 });
49
50 test("command should compare version lt", async () => {
51 commandExistsStub.sync = () => true;
52
53 assert.deepStrictEqual(
54 await fakeUtil.basicCheck({
55 command: "whatever",
56 getVersion: async () => "0.0.1",
57 versionRange: ">0.0.1",
58 }),
59 {
60 exists: true,
61 versionCompare: -1,
62 },
63 );
64 });
65
66 test("command should compare version gt", async () => {
67 commandExistsStub.sync = () => true;
68
69 assert.deepStrictEqual(
70 await fakeUtil.basicCheck({
71 command: "whatever",
72 getVersion: async () => "0.0.1",
73 versionRange: "<0.0.1",
74 }),
75 {
76 exists: true,
77 versionCompare: 1,
78 },
79 );
80 });
81
82 test("command should compare version eq", async () => {
83 commandExistsStub.sync = () => true;
84
85 assert.deepStrictEqual(
86 await fakeUtil.basicCheck({
87 command: "whatever",
88 getVersion: async () => "0.0.1",
89 versionRange: "=0.0.1",
90 }),
91 {
92 exists: true,
93 versionCompare: 0,
94 },
95 );
96 });
97 });
98
99 suite("envTest", async function () {
100 const envTest = await import(
101 "../../src/extension/services/validationService/checks/env"
102 ).then(it => it.androidHome.exec);
103
104 const envVars = {
105 ANDROID_HOME: process.env.ANDROID_HOME,
106 ANDROID_SDK_ROOT: process.env.ANDROID_SDK_ROOT,
107 };
108
109 const setEnv = (arg: string) => {
110 Object.keys(envVars).forEach(it => {
111 process.env[it] = arg;
112 });
113 };
114 const restoreEnv = () => {
115 Object.keys(envVars).forEach(it => {
116 process.env[it] = (envVars as any)[it];
117 });
118 };
119
120 test("should succeed on correct env", async () => {
121 const tempdir = await fs.mkdtemp(path.join(os.tmpdir(), "foo"));
122 setEnv(tempdir);
123
124 const result = await envTest();
125
126 assert(result.status === "success");
127
128 restoreEnv();
129 });
130
131 test("should fail on non existant path", async () => {
132 setEnv("/non-existant-path-abcd");
133
134 const result = await envTest();
135
136 assert(result.status === "failure");
137
138 restoreEnv();
139 });
140
141 test("should succeed on path with env values", async () => {
142 const varName = "some-weired-variable-abcd";
143 const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "foo"));
144 await fs.mkdir(path.join(tempDir, "bar"));
145
146 process.env[varName] = "bar";
147
148 setEnv(`${tempDir}/%${varName}%`);
149
150 const result = await envTest();
151
152 assert(result.status === "partial-success");
153
154 restoreEnv();
155 delete process.env[varName];
156 });
157 });
158});
159