Skip to content

Commit 1429190

Browse files
Added CURSORLESS_MODE environment variable (#2493)
Fixes #2408 ## Checklist - [/] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [/] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [/] I have not broken the cheatsheet --------- Co-authored-by: Pokey Rule <[email protected]>
1 parent d582717 commit 1429190

21 files changed

+104
-76
lines changed

.vscode/launch.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"type": "extensionHost",
1111
"request": "launch",
1212
"env": {
13+
"CURSORLESS_MODE": "development",
1314
"CURSORLESS_REPO_ROOT": "${workspaceFolder}"
1415
},
1516
"args": [
@@ -28,7 +29,7 @@
2829
"type": "extensionHost",
2930
"request": "launch",
3031
"env": {
31-
"CURSORLESS_TEST": "true",
32+
"CURSORLESS_MODE": "test",
3233
"CURSORLESS_REPO_ROOT": "${workspaceFolder}"
3334
},
3435
"args": [
@@ -48,7 +49,7 @@
4849
"type": "extensionHost",
4950
"request": "launch",
5051
"env": {
51-
"CURSORLESS_TEST": "true",
52+
"CURSORLESS_MODE": "test",
5253
"CURSORLESS_RUN_TEST_SUBSET": "true",
5354
"CURSORLESS_REPO_ROOT": "${workspaceFolder}"
5455
},
@@ -70,7 +71,7 @@
7071
"request": "launch",
7172
"program": "${workspaceFolder}/packages/test-harness/dist/runTalonTests.cjs",
7273
"env": {
73-
"CURSORLESS_TEST": "true",
74+
"CURSORLESS_MODE": "test",
7475
"CURSORLESS_REPO_ROOT": "${workspaceFolder}"
7576
},
7677
"outFiles": ["${workspaceFolder}/**/out/**/*.js"],
@@ -86,7 +87,7 @@
8687
"request": "launch",
8788
"program": "${workspaceFolder}/packages/test-harness/dist/runTalonTests.cjs",
8889
"env": {
89-
"CURSORLESS_TEST": "true",
90+
"CURSORLESS_MODE": "test",
9091
"CURSORLESS_RUN_TEST_SUBSET": "true",
9192
"CURSORLESS_REPO_ROOT": "${workspaceFolder}"
9293
},
@@ -103,7 +104,7 @@
103104
"request": "launch",
104105
"program": "${workspaceFolder}/packages/test-harness/dist/runUnitTestsOnly.cjs",
105106
"env": {
106-
"CURSORLESS_TEST": "true",
107+
"CURSORLESS_MODE": "test",
107108
"CURSORLESS_REPO_ROOT": "${workspaceFolder}"
108109
},
109110
"outFiles": ["${workspaceFolder}/**/out/**/*.js"],
@@ -118,7 +119,7 @@
118119
"type": "extensionHost",
119120
"request": "launch",
120121
"env": {
121-
"CURSORLESS_TEST": "true",
122+
"CURSORLESS_MODE": "test",
122123
"CURSORLESS_TEST_UPDATE_FIXTURES": "true",
123124
"CURSORLESS_REPO_ROOT": "${workspaceFolder}"
124125
},
@@ -139,7 +140,7 @@
139140
"type": "extensionHost",
140141
"request": "launch",
141142
"env": {
142-
"CURSORLESS_TEST": "true",
143+
"CURSORLESS_MODE": "test",
143144
"CURSORLESS_TEST_UPDATE_FIXTURES": "true",
144145
"CURSORLESS_RUN_TEST_SUBSET": "true",
145146
"CURSORLESS_REPO_ROOT": "${workspaceFolder}"
@@ -162,7 +163,7 @@
162163
"request": "launch",
163164
"program": "${workspaceFolder}/packages/test-harness/dist/runUnitTestsOnly.cjs",
164165
"env": {
165-
"CURSORLESS_TEST": "true",
166+
"CURSORLESS_MODE": "test",
166167
"CURSORLESS_TEST_UPDATE_FIXTURES": "true",
167168
"CURSORLESS_REPO_ROOT": "${workspaceFolder}"
168169
},

packages/common/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ export * from "./types/RangeOffsets";
6262
export * from "./util/omitByDeep";
6363
export * from "./util/range";
6464
export * from "./util/uniqWithHash";
65-
export * from "./testUtil/isTesting";
6665
export * from "./testUtil/testConstants";
6766
export * from "./testUtil/getFixturePaths";
6867
export * from "./testUtil/getCursorlessRepoRoot";

packages/common/src/testUtil/isTesting.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/cursorless-engine/src/actions/GenerateSnippet/GenerateSnippet.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FlashStyle, isTesting, Range } from "@cursorless/common";
1+
import { FlashStyle, Range } from "@cursorless/common";
22
import type { Snippets } from "../../core/Snippets";
33
import { Offsets } from "../../processTargets/modifiers/surroundingPair/types";
44
import { ide } from "../../singletons/ide.singleton";
@@ -220,7 +220,7 @@ export default class GenerateSnippet {
220220

221221
const editableEditor = ide().getEditableTextEditor(editor);
222222

223-
if (isTesting()) {
223+
if (ide().runMode === "test") {
224224
// If we're testing, we just overwrite the current document
225225
await editableEditor.setSelections([
226226
editor.document.range.toSelection(false),

packages/cursorless-engine/src/languages/LanguageDefinitions.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
Notifier,
44
Range,
55
TextDocument,
6-
isTesting,
76
showError,
87
type IDE,
98
type RawTreeSitterQueryProvider,
@@ -117,7 +116,7 @@ export class LanguageDefinitionsImpl
117116
"Failed to load language definitions",
118117
toString(err),
119118
);
120-
if (isTesting()) {
119+
if (this.ide.runMode === "test") {
121120
throw err;
122121
}
123122
}

packages/cursorless-vscode/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@
12421242
"@cursorless/common": "workspace:*",
12431243
"@cursorless/cursorless-cheatsheet": "workspace:*",
12441244
"@cursorless/cursorless-engine": "workspace:*",
1245-
"@cursorless/file-system-common": "workspace:*",
1245+
"@cursorless/node-common": "workspace:*",
12461246
"@cursorless/vscode-common": "workspace:*",
12471247
"immer": "^10.0.4",
12481248
"itertools": "^2.2.5",

packages/cursorless-vscode/src/extension.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
FakeCommandServerApi,
44
FakeIDE,
55
IDE,
6-
isTesting,
76
NormalizedIDE,
87
Range,
98
ScopeProvider,
@@ -21,7 +20,7 @@ import {
2120
FileSystemCommandHistoryStorage,
2221
FileSystemRawTreeSitterQueryProvider,
2322
FileSystemTalonSpokenForms,
24-
} from "@cursorless/file-system-common";
23+
} from "@cursorless/node-common";
2524
import {
2625
CursorlessApi,
2726
getCommandServerApi,
@@ -83,9 +82,10 @@ export async function activate(
8382
);
8483

8584
const fakeCommandServerApi = new FakeCommandServerApi();
86-
const commandServerApi = isTesting()
87-
? fakeCommandServerApi
88-
: await getCommandServerApi();
85+
const commandServerApi =
86+
normalizedIde.runMode === "test"
87+
? fakeCommandServerApi
88+
: await getCommandServerApi();
8989

9090
const treeSitter = createTreeSitter(parseTreeApi);
9191
const talonSpokenForms = new FileSystemTalonSpokenForms(fileSystem);
@@ -173,19 +173,20 @@ export async function activate(
173173
new ReleaseNotes(vscodeApi, context, normalizedIde.messages).maybeShow();
174174

175175
return {
176-
testHelpers: isTesting()
177-
? constructTestHelpers(
178-
fakeCommandServerApi,
179-
storedTargets,
180-
hatTokenMap,
181-
vscodeIDE,
182-
normalizedIde as NormalizedIDE,
183-
fileSystem,
184-
scopeProvider,
185-
injectIde,
186-
runIntegrationTests,
187-
)
188-
: undefined,
176+
testHelpers:
177+
normalizedIde.runMode === "test"
178+
? constructTestHelpers(
179+
fakeCommandServerApi,
180+
storedTargets,
181+
hatTokenMap,
182+
vscodeIDE,
183+
normalizedIde as NormalizedIDE,
184+
fileSystem,
185+
scopeProvider,
186+
injectIde,
187+
runIntegrationTests,
188+
)
189+
: undefined,
189190

190191
experimental: {
191192
registerThirdPartySnippets: snippets.registerThirdPartySnippets,
@@ -210,9 +211,10 @@ async function createVscodeIde(context: vscode.ExtensionContext) {
210211
// extension initialization, probably by returning a function from extension
211212
// init that has parameters consisting of test configuration, and have that
212213
// function do the actual initialization.
213-
const cursorlessDir = isTesting()
214-
? path.join(os.tmpdir(), crypto.randomBytes(16).toString("hex"))
215-
: path.join(os.homedir(), ".cursorless");
214+
const cursorlessDir =
215+
vscodeIDE.runMode === "test"
216+
? path.join(os.tmpdir(), crypto.randomBytes(16).toString("hex"))
217+
: path.join(os.homedir(), ".cursorless");
216218

217219
const fileSystem = new VscodeFileSystem(
218220
context,

packages/cursorless-vscode/src/ide/vscode/VscodeEnabledHatStyleManager.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { isTesting } from "@cursorless/common";
21
import { pickBy } from "lodash-es";
32
import * as vscode from "vscode";
43
import { HatStyleInfo, HatStyleMap } from "@cursorless/common";
@@ -33,7 +32,7 @@ export default class VscodeEnabledHatStyleManager {
3332
hatStyleMap!: ExtendedHatStyleMap;
3433
private notifier: Notifier<[HatStyleMap]> = new Notifier();
3534

36-
constructor(extensionContext: vscode.ExtensionContext) {
35+
constructor(private extensionContext: vscode.ExtensionContext) {
3736
this.recomputeEnabledHatStyles = this.recomputeEnabledHatStyles.bind(this);
3837

3938
extensionContext.subscriptions.push(
@@ -76,9 +75,10 @@ export default class VscodeEnabledHatStyleManager {
7675
colorPenalties.default = 0;
7776

7877
// So that unit tests don't fail locally if you have some colors disabled
79-
const activeHatColors = isTesting()
80-
? HAT_COLORS.filter((color) => !color.startsWith("user"))
81-
: HAT_COLORS.filter((color) => colorEnablement[color]);
78+
const activeHatColors =
79+
this.extensionContext.extensionMode === vscode.ExtensionMode.Test
80+
? HAT_COLORS.filter((color) => !color.startsWith("user"))
81+
: HAT_COLORS.filter((color) => colorEnablement[color]);
8282
const activeNonDefaultHatShapes = HAT_NON_DEFAULT_SHAPES.filter(
8383
(shape) => shapeEnablement[shape],
8484
);
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ExtensionContext, ExtensionMode } from "vscode";
22
import type { RunMode } from "@cursorless/common";
3+
import { nodeGetRunMode } from "@cursorless/node-common";
34

45
const EXTENSION_MODE_MAP: Record<ExtensionMode, RunMode> = {
56
[ExtensionMode.Development]: "development",
@@ -8,5 +9,14 @@ const EXTENSION_MODE_MAP: Record<ExtensionMode, RunMode> = {
89
};
910

1011
export function vscodeRunMode(extensionContext: ExtensionContext): RunMode {
11-
return EXTENSION_MODE_MAP[extensionContext.extensionMode];
12+
const envMode = nodeGetRunMode();
13+
const extensionMode = EXTENSION_MODE_MAP[extensionContext.extensionMode];
14+
15+
if (envMode !== extensionMode) {
16+
throw new Error(
17+
`Extension mode '${extensionMode}' doesn't match environment variable mode '${envMode}'`,
18+
);
19+
}
20+
21+
return extensionMode;
1222
}

packages/cursorless-vscode/src/registerCommands.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
CURSORLESS_COMMAND_ID,
33
CursorlessCommandId,
4-
isTesting,
54
type CommandHistoryStorage,
65
} from "@cursorless/common";
76
import {
@@ -39,7 +38,7 @@ export function registerCommands(
3938
try {
4039
return await run();
4140
} catch (e) {
42-
if (!isTesting()) {
41+
if (vscodeIde.runMode !== "test") {
4342
const err = e as Error;
4443
console.error(err.stack);
4544
vscodeIde.handleCommandError(err);

0 commit comments

Comments
 (0)