@theia/variable-resolver 1.53.0-next.55 → 1.53.0-next.64
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +114 -114
- package/package.json +3 -3
- package/src/browser/common-variable-contribution.ts +151 -151
- package/src/browser/index.ts +19 -19
- package/src/browser/variable-input-schema.ts +131 -131
- package/src/browser/variable-input.ts +47 -47
- package/src/browser/variable-quick-open-service.ts +62 -62
- package/src/browser/variable-resolver-frontend-contribution.spec.ts +98 -98
- package/src/browser/variable-resolver-frontend-contribution.ts +50 -50
- package/src/browser/variable-resolver-frontend-module.ts +40 -40
- package/src/browser/variable-resolver-service.spec.ts +83 -83
- package/src/browser/variable-resolver-service.ts +185 -185
- package/src/browser/variable.spec.ts +106 -106
- package/src/browser/variable.ts +111 -111
- package/src/common/variable-types.ts +23 -23
|
@@ -1,185 +1,185 @@
|
|
|
1
|
-
// *****************************************************************************
|
|
2
|
-
// Copyright (C) 2018 Red Hat, Inc. and others.
|
|
3
|
-
//
|
|
4
|
-
// This program and the accompanying materials are made available under the
|
|
5
|
-
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
-
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
-
//
|
|
8
|
-
// This Source Code may also be made available under the following Secondary
|
|
9
|
-
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
-
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
-
// with the GNU Classpath Exception which is available at
|
|
12
|
-
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
-
//
|
|
14
|
-
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
-
// *****************************************************************************
|
|
16
|
-
|
|
17
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
18
|
-
|
|
19
|
-
import { injectable, inject } from '@theia/core/shared/inversify';
|
|
20
|
-
import { VariableRegistry } from './variable';
|
|
21
|
-
import URI from '@theia/core/lib/common/uri';
|
|
22
|
-
import { CommandIdVariables } from '../common/variable-types';
|
|
23
|
-
import { isCancelled } from '@theia/core';
|
|
24
|
-
|
|
25
|
-
export interface VariableResolveOptions {
|
|
26
|
-
context?: URI;
|
|
27
|
-
/**
|
|
28
|
-
* Used for resolving inputs, see https://code.visualstudio.com/docs/editor/variables-reference#_input-variables
|
|
29
|
-
*/
|
|
30
|
-
configurationSection?: string;
|
|
31
|
-
commandIdVariables?: CommandIdVariables;
|
|
32
|
-
configuration?: unknown;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* The variable resolver service should be used to resolve variables in strings.
|
|
37
|
-
*/
|
|
38
|
-
@injectable()
|
|
39
|
-
export class VariableResolverService {
|
|
40
|
-
|
|
41
|
-
protected static VAR_REGEXP = /\$\{(.*?)\}/g;
|
|
42
|
-
|
|
43
|
-
@inject(VariableRegistry) protected readonly variableRegistry: VariableRegistry;
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Resolve the variables in the given string array.
|
|
47
|
-
* @param value The array of data to resolve variables in.
|
|
48
|
-
* @param options Options of the variable resolution.
|
|
49
|
-
* @returns Promise to array with variables resolved. Never rejects.
|
|
50
|
-
*
|
|
51
|
-
* @deprecated since 1.28.0 use {@link resolve} instead.
|
|
52
|
-
*/
|
|
53
|
-
resolveArray(value: string[], options: VariableResolveOptions = {}): Promise<string[] | undefined> {
|
|
54
|
-
return this.resolve(value, options);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Resolve the variables for all strings found in the object and nested objects.
|
|
59
|
-
* @param value Data to resolve variables in.
|
|
60
|
-
* @param options Options of the variable resolution
|
|
61
|
-
* @returns Promise to object with variables resolved. Returns `undefined` if a variable resolution was cancelled.
|
|
62
|
-
*/
|
|
63
|
-
async resolve<T>(value: T, options: VariableResolveOptions = {}): Promise<T | undefined> {
|
|
64
|
-
const context = new VariableResolverService.Context(this.variableRegistry, options);
|
|
65
|
-
try {
|
|
66
|
-
return await this.doResolve(value, context);
|
|
67
|
-
} catch (error) {
|
|
68
|
-
if (isCancelled(error)) {
|
|
69
|
-
return undefined;
|
|
70
|
-
}
|
|
71
|
-
throw error;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
protected async doResolve(value: any, context: VariableResolverService.Context): Promise<any> {
|
|
76
|
-
// eslint-disable-next-line no-null/no-null
|
|
77
|
-
if (value === undefined || value === null) {
|
|
78
|
-
return value;
|
|
79
|
-
}
|
|
80
|
-
if (typeof value === 'string') {
|
|
81
|
-
return this.doResolveString(value, context);
|
|
82
|
-
}
|
|
83
|
-
if (Array.isArray(value)) {
|
|
84
|
-
return this.doResolveArray(value, context);
|
|
85
|
-
}
|
|
86
|
-
if (typeof value === 'object') {
|
|
87
|
-
return this.doResolveObject(value, context);
|
|
88
|
-
}
|
|
89
|
-
return value;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
protected async doResolveObject(obj: object, context: VariableResolverService.Context): Promise<object> {
|
|
93
|
-
const result: {
|
|
94
|
-
[prop: string]: Object | undefined
|
|
95
|
-
} = {};
|
|
96
|
-
for (const name of Object.keys(obj)) {
|
|
97
|
-
const value = (obj as any)[name];
|
|
98
|
-
const resolved = await this.doResolve(value, context);
|
|
99
|
-
result[name] = resolved;
|
|
100
|
-
}
|
|
101
|
-
return result;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
protected async doResolveArray(values: Array<Object | undefined>, context: VariableResolverService.Context): Promise<Array<Object | undefined>> {
|
|
105
|
-
const result: (Object | undefined)[] = [];
|
|
106
|
-
for (const value of values) {
|
|
107
|
-
const resolved = await this.doResolve(value, context);
|
|
108
|
-
result.push(resolved);
|
|
109
|
-
}
|
|
110
|
-
return result;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
protected async doResolveString(value: string, context: VariableResolverService.Context): Promise<string> {
|
|
114
|
-
await this.resolveVariables(value, context);
|
|
115
|
-
return value.replace(VariableResolverService.VAR_REGEXP, (match: string, varName: string) => {
|
|
116
|
-
const varValue = context.get(varName);
|
|
117
|
-
return varValue !== undefined ? varValue : match;
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
protected async resolveVariables(value: string, context: VariableResolverService.Context): Promise<void> {
|
|
122
|
-
const variableRegExp = new RegExp(VariableResolverService.VAR_REGEXP);
|
|
123
|
-
let match;
|
|
124
|
-
// eslint-disable-next-line no-null/no-null
|
|
125
|
-
while ((match = variableRegExp.exec(value)) !== null) {
|
|
126
|
-
const variableName = match[1];
|
|
127
|
-
await context.resolve(variableName);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
export namespace VariableResolverService {
|
|
132
|
-
|
|
133
|
-
export class Context {
|
|
134
|
-
|
|
135
|
-
protected readonly resolved = new Map<string, string | undefined>();
|
|
136
|
-
|
|
137
|
-
constructor(
|
|
138
|
-
protected readonly variableRegistry: VariableRegistry,
|
|
139
|
-
protected readonly options: VariableResolveOptions
|
|
140
|
-
) { }
|
|
141
|
-
|
|
142
|
-
get(name: string): string | undefined {
|
|
143
|
-
return this.resolved.get(name);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
async resolve(name: string): Promise<void> {
|
|
147
|
-
if (this.resolved.has(name)) {
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
try {
|
|
151
|
-
let variableName = name;
|
|
152
|
-
let argument: string | undefined;
|
|
153
|
-
const parts = name.split(':', 2);
|
|
154
|
-
if (parts.length > 1) {
|
|
155
|
-
variableName = parts[0];
|
|
156
|
-
argument = parts[1];
|
|
157
|
-
}
|
|
158
|
-
const variable = this.variableRegistry.getVariable(variableName);
|
|
159
|
-
const resolved = await variable?.resolve(
|
|
160
|
-
this.options.context,
|
|
161
|
-
argument,
|
|
162
|
-
this.options.configurationSection,
|
|
163
|
-
this.options.commandIdVariables,
|
|
164
|
-
this.options.configuration
|
|
165
|
-
);
|
|
166
|
-
if (
|
|
167
|
-
typeof resolved === 'bigint' ||
|
|
168
|
-
typeof resolved === 'boolean' ||
|
|
169
|
-
typeof resolved === 'number' ||
|
|
170
|
-
typeof resolved === 'string'
|
|
171
|
-
) {
|
|
172
|
-
this.resolved.set(name, `${resolved}`);
|
|
173
|
-
} else {
|
|
174
|
-
this.resolved.set(name, undefined);
|
|
175
|
-
}
|
|
176
|
-
} catch (e) {
|
|
177
|
-
if (isCancelled(e)) {
|
|
178
|
-
throw e;
|
|
179
|
-
}
|
|
180
|
-
this.resolved.set(name, undefined);
|
|
181
|
-
console.error(`Failed to resolve '${name}' variable:`, e);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
}
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2018 Red Hat, Inc. and others.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
18
|
+
|
|
19
|
+
import { injectable, inject } from '@theia/core/shared/inversify';
|
|
20
|
+
import { VariableRegistry } from './variable';
|
|
21
|
+
import URI from '@theia/core/lib/common/uri';
|
|
22
|
+
import { CommandIdVariables } from '../common/variable-types';
|
|
23
|
+
import { isCancelled } from '@theia/core';
|
|
24
|
+
|
|
25
|
+
export interface VariableResolveOptions {
|
|
26
|
+
context?: URI;
|
|
27
|
+
/**
|
|
28
|
+
* Used for resolving inputs, see https://code.visualstudio.com/docs/editor/variables-reference#_input-variables
|
|
29
|
+
*/
|
|
30
|
+
configurationSection?: string;
|
|
31
|
+
commandIdVariables?: CommandIdVariables;
|
|
32
|
+
configuration?: unknown;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The variable resolver service should be used to resolve variables in strings.
|
|
37
|
+
*/
|
|
38
|
+
@injectable()
|
|
39
|
+
export class VariableResolverService {
|
|
40
|
+
|
|
41
|
+
protected static VAR_REGEXP = /\$\{(.*?)\}/g;
|
|
42
|
+
|
|
43
|
+
@inject(VariableRegistry) protected readonly variableRegistry: VariableRegistry;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Resolve the variables in the given string array.
|
|
47
|
+
* @param value The array of data to resolve variables in.
|
|
48
|
+
* @param options Options of the variable resolution.
|
|
49
|
+
* @returns Promise to array with variables resolved. Never rejects.
|
|
50
|
+
*
|
|
51
|
+
* @deprecated since 1.28.0 use {@link resolve} instead.
|
|
52
|
+
*/
|
|
53
|
+
resolveArray(value: string[], options: VariableResolveOptions = {}): Promise<string[] | undefined> {
|
|
54
|
+
return this.resolve(value, options);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Resolve the variables for all strings found in the object and nested objects.
|
|
59
|
+
* @param value Data to resolve variables in.
|
|
60
|
+
* @param options Options of the variable resolution
|
|
61
|
+
* @returns Promise to object with variables resolved. Returns `undefined` if a variable resolution was cancelled.
|
|
62
|
+
*/
|
|
63
|
+
async resolve<T>(value: T, options: VariableResolveOptions = {}): Promise<T | undefined> {
|
|
64
|
+
const context = new VariableResolverService.Context(this.variableRegistry, options);
|
|
65
|
+
try {
|
|
66
|
+
return await this.doResolve(value, context);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
if (isCancelled(error)) {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
protected async doResolve(value: any, context: VariableResolverService.Context): Promise<any> {
|
|
76
|
+
// eslint-disable-next-line no-null/no-null
|
|
77
|
+
if (value === undefined || value === null) {
|
|
78
|
+
return value;
|
|
79
|
+
}
|
|
80
|
+
if (typeof value === 'string') {
|
|
81
|
+
return this.doResolveString(value, context);
|
|
82
|
+
}
|
|
83
|
+
if (Array.isArray(value)) {
|
|
84
|
+
return this.doResolveArray(value, context);
|
|
85
|
+
}
|
|
86
|
+
if (typeof value === 'object') {
|
|
87
|
+
return this.doResolveObject(value, context);
|
|
88
|
+
}
|
|
89
|
+
return value;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
protected async doResolveObject(obj: object, context: VariableResolverService.Context): Promise<object> {
|
|
93
|
+
const result: {
|
|
94
|
+
[prop: string]: Object | undefined
|
|
95
|
+
} = {};
|
|
96
|
+
for (const name of Object.keys(obj)) {
|
|
97
|
+
const value = (obj as any)[name];
|
|
98
|
+
const resolved = await this.doResolve(value, context);
|
|
99
|
+
result[name] = resolved;
|
|
100
|
+
}
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
protected async doResolveArray(values: Array<Object | undefined>, context: VariableResolverService.Context): Promise<Array<Object | undefined>> {
|
|
105
|
+
const result: (Object | undefined)[] = [];
|
|
106
|
+
for (const value of values) {
|
|
107
|
+
const resolved = await this.doResolve(value, context);
|
|
108
|
+
result.push(resolved);
|
|
109
|
+
}
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
protected async doResolveString(value: string, context: VariableResolverService.Context): Promise<string> {
|
|
114
|
+
await this.resolveVariables(value, context);
|
|
115
|
+
return value.replace(VariableResolverService.VAR_REGEXP, (match: string, varName: string) => {
|
|
116
|
+
const varValue = context.get(varName);
|
|
117
|
+
return varValue !== undefined ? varValue : match;
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
protected async resolveVariables(value: string, context: VariableResolverService.Context): Promise<void> {
|
|
122
|
+
const variableRegExp = new RegExp(VariableResolverService.VAR_REGEXP);
|
|
123
|
+
let match;
|
|
124
|
+
// eslint-disable-next-line no-null/no-null
|
|
125
|
+
while ((match = variableRegExp.exec(value)) !== null) {
|
|
126
|
+
const variableName = match[1];
|
|
127
|
+
await context.resolve(variableName);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
export namespace VariableResolverService {
|
|
132
|
+
|
|
133
|
+
export class Context {
|
|
134
|
+
|
|
135
|
+
protected readonly resolved = new Map<string, string | undefined>();
|
|
136
|
+
|
|
137
|
+
constructor(
|
|
138
|
+
protected readonly variableRegistry: VariableRegistry,
|
|
139
|
+
protected readonly options: VariableResolveOptions
|
|
140
|
+
) { }
|
|
141
|
+
|
|
142
|
+
get(name: string): string | undefined {
|
|
143
|
+
return this.resolved.get(name);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async resolve(name: string): Promise<void> {
|
|
147
|
+
if (this.resolved.has(name)) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
try {
|
|
151
|
+
let variableName = name;
|
|
152
|
+
let argument: string | undefined;
|
|
153
|
+
const parts = name.split(':', 2);
|
|
154
|
+
if (parts.length > 1) {
|
|
155
|
+
variableName = parts[0];
|
|
156
|
+
argument = parts[1];
|
|
157
|
+
}
|
|
158
|
+
const variable = this.variableRegistry.getVariable(variableName);
|
|
159
|
+
const resolved = await variable?.resolve(
|
|
160
|
+
this.options.context,
|
|
161
|
+
argument,
|
|
162
|
+
this.options.configurationSection,
|
|
163
|
+
this.options.commandIdVariables,
|
|
164
|
+
this.options.configuration
|
|
165
|
+
);
|
|
166
|
+
if (
|
|
167
|
+
typeof resolved === 'bigint' ||
|
|
168
|
+
typeof resolved === 'boolean' ||
|
|
169
|
+
typeof resolved === 'number' ||
|
|
170
|
+
typeof resolved === 'string'
|
|
171
|
+
) {
|
|
172
|
+
this.resolved.set(name, `${resolved}`);
|
|
173
|
+
} else {
|
|
174
|
+
this.resolved.set(name, undefined);
|
|
175
|
+
}
|
|
176
|
+
} catch (e) {
|
|
177
|
+
if (isCancelled(e)) {
|
|
178
|
+
throw e;
|
|
179
|
+
}
|
|
180
|
+
this.resolved.set(name, undefined);
|
|
181
|
+
console.error(`Failed to resolve '${name}' variable:`, e);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
@@ -1,106 +1,106 @@
|
|
|
1
|
-
// *****************************************************************************
|
|
2
|
-
// Copyright (C) 2018 Red Hat, Inc. and others.
|
|
3
|
-
//
|
|
4
|
-
// This program and the accompanying materials are made available under the
|
|
5
|
-
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
-
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
-
//
|
|
8
|
-
// This Source Code may also be made available under the following Secondary
|
|
9
|
-
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
-
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
-
// with the GNU Classpath Exception which is available at
|
|
12
|
-
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
-
//
|
|
14
|
-
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
-
// *****************************************************************************
|
|
16
|
-
|
|
17
|
-
import * as chai from 'chai';
|
|
18
|
-
import { Container, ContainerModule } from '@theia/core/shared/inversify';
|
|
19
|
-
import { ILogger, Disposable } from '@theia/core/lib/common';
|
|
20
|
-
import { MockLogger } from '@theia/core/lib/common/test/mock-logger';
|
|
21
|
-
import { Variable, VariableRegistry } from './variable';
|
|
22
|
-
|
|
23
|
-
const expect = chai.expect;
|
|
24
|
-
let variableRegistry: VariableRegistry;
|
|
25
|
-
|
|
26
|
-
before(() => {
|
|
27
|
-
chai.config.showDiff = true;
|
|
28
|
-
chai.config.includeStack = true;
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
describe('variable api', () => {
|
|
32
|
-
let testContainer: Container;
|
|
33
|
-
|
|
34
|
-
before(() => {
|
|
35
|
-
testContainer = new Container();
|
|
36
|
-
const module = new ContainerModule((bind, unbind, isBound, rebind) => {
|
|
37
|
-
bind(ILogger).to(MockLogger);
|
|
38
|
-
bind(VariableRegistry).toSelf();
|
|
39
|
-
});
|
|
40
|
-
testContainer.load(module);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
beforeEach(() => {
|
|
44
|
-
variableRegistry = testContainer.get<VariableRegistry>(VariableRegistry);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('should register and return variable', () => {
|
|
48
|
-
registerTestVariable();
|
|
49
|
-
|
|
50
|
-
const variable = variableRegistry.getVariable(TEST_VARIABLE.name);
|
|
51
|
-
expect(variable).is.not.undefined;
|
|
52
|
-
if (variable) {
|
|
53
|
-
expect(variable.name).is.equal(TEST_VARIABLE.name);
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('should not register a variable for already existed name', () => {
|
|
58
|
-
const variables: Variable[] = [
|
|
59
|
-
{
|
|
60
|
-
name: 'workspaceRoot',
|
|
61
|
-
description: 'workspace root URI',
|
|
62
|
-
resolve: () => Promise.resolve('')
|
|
63
|
-
},
|
|
64
|
-
{
|
|
65
|
-
name: 'workspaceRoot',
|
|
66
|
-
description: 'workspace root URI 2',
|
|
67
|
-
resolve: () => Promise.resolve('')
|
|
68
|
-
}
|
|
69
|
-
];
|
|
70
|
-
variables.forEach(v => variableRegistry.registerVariable(v));
|
|
71
|
-
|
|
72
|
-
const registeredVariables = variableRegistry.getVariables();
|
|
73
|
-
expect(registeredVariables.length).to.be.equal(1);
|
|
74
|
-
expect(registeredVariables[0].name).to.be.equal('workspaceRoot');
|
|
75
|
-
expect(registeredVariables[0].description).to.be.equal('workspace root URI');
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it('should dispose variable', () => {
|
|
79
|
-
const disposable = registerTestVariable();
|
|
80
|
-
disposable.dispose();
|
|
81
|
-
|
|
82
|
-
const variable = variableRegistry.getVariable(TEST_VARIABLE.name);
|
|
83
|
-
expect(variable).is.undefined;
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it('should unregister variables on dispose', () => {
|
|
87
|
-
registerTestVariable();
|
|
88
|
-
|
|
89
|
-
let variables = variableRegistry.getVariables();
|
|
90
|
-
expect(variables.length).to.be.equal(1);
|
|
91
|
-
|
|
92
|
-
variableRegistry.dispose();
|
|
93
|
-
|
|
94
|
-
variables = variableRegistry.getVariables();
|
|
95
|
-
expect(variables.length).to.be.equal(0);
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
const TEST_VARIABLE: Variable = {
|
|
100
|
-
name: 'workspaceRoot',
|
|
101
|
-
resolve: () => Promise.resolve('')
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
function registerTestVariable(): Disposable {
|
|
105
|
-
return variableRegistry.registerVariable(TEST_VARIABLE);
|
|
106
|
-
}
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2018 Red Hat, Inc. and others.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
import * as chai from 'chai';
|
|
18
|
+
import { Container, ContainerModule } from '@theia/core/shared/inversify';
|
|
19
|
+
import { ILogger, Disposable } from '@theia/core/lib/common';
|
|
20
|
+
import { MockLogger } from '@theia/core/lib/common/test/mock-logger';
|
|
21
|
+
import { Variable, VariableRegistry } from './variable';
|
|
22
|
+
|
|
23
|
+
const expect = chai.expect;
|
|
24
|
+
let variableRegistry: VariableRegistry;
|
|
25
|
+
|
|
26
|
+
before(() => {
|
|
27
|
+
chai.config.showDiff = true;
|
|
28
|
+
chai.config.includeStack = true;
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('variable api', () => {
|
|
32
|
+
let testContainer: Container;
|
|
33
|
+
|
|
34
|
+
before(() => {
|
|
35
|
+
testContainer = new Container();
|
|
36
|
+
const module = new ContainerModule((bind, unbind, isBound, rebind) => {
|
|
37
|
+
bind(ILogger).to(MockLogger);
|
|
38
|
+
bind(VariableRegistry).toSelf();
|
|
39
|
+
});
|
|
40
|
+
testContainer.load(module);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
beforeEach(() => {
|
|
44
|
+
variableRegistry = testContainer.get<VariableRegistry>(VariableRegistry);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('should register and return variable', () => {
|
|
48
|
+
registerTestVariable();
|
|
49
|
+
|
|
50
|
+
const variable = variableRegistry.getVariable(TEST_VARIABLE.name);
|
|
51
|
+
expect(variable).is.not.undefined;
|
|
52
|
+
if (variable) {
|
|
53
|
+
expect(variable.name).is.equal(TEST_VARIABLE.name);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should not register a variable for already existed name', () => {
|
|
58
|
+
const variables: Variable[] = [
|
|
59
|
+
{
|
|
60
|
+
name: 'workspaceRoot',
|
|
61
|
+
description: 'workspace root URI',
|
|
62
|
+
resolve: () => Promise.resolve('')
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
name: 'workspaceRoot',
|
|
66
|
+
description: 'workspace root URI 2',
|
|
67
|
+
resolve: () => Promise.resolve('')
|
|
68
|
+
}
|
|
69
|
+
];
|
|
70
|
+
variables.forEach(v => variableRegistry.registerVariable(v));
|
|
71
|
+
|
|
72
|
+
const registeredVariables = variableRegistry.getVariables();
|
|
73
|
+
expect(registeredVariables.length).to.be.equal(1);
|
|
74
|
+
expect(registeredVariables[0].name).to.be.equal('workspaceRoot');
|
|
75
|
+
expect(registeredVariables[0].description).to.be.equal('workspace root URI');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('should dispose variable', () => {
|
|
79
|
+
const disposable = registerTestVariable();
|
|
80
|
+
disposable.dispose();
|
|
81
|
+
|
|
82
|
+
const variable = variableRegistry.getVariable(TEST_VARIABLE.name);
|
|
83
|
+
expect(variable).is.undefined;
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('should unregister variables on dispose', () => {
|
|
87
|
+
registerTestVariable();
|
|
88
|
+
|
|
89
|
+
let variables = variableRegistry.getVariables();
|
|
90
|
+
expect(variables.length).to.be.equal(1);
|
|
91
|
+
|
|
92
|
+
variableRegistry.dispose();
|
|
93
|
+
|
|
94
|
+
variables = variableRegistry.getVariables();
|
|
95
|
+
expect(variables.length).to.be.equal(0);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const TEST_VARIABLE: Variable = {
|
|
100
|
+
name: 'workspaceRoot',
|
|
101
|
+
resolve: () => Promise.resolve('')
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
function registerTestVariable(): Disposable {
|
|
105
|
+
return variableRegistry.registerVariable(TEST_VARIABLE);
|
|
106
|
+
}
|