Skip to content

Commit

Permalink
Added CURSORLESS_MODE environment variable (#2493)
Browse files Browse the repository at this point in the history
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 <755842+pokey@users.noreply.github.com>
  • Loading branch information
AndreasArvidsson and pokey committed Jul 12, 2024
1 parent d582717 commit 1429190
Show file tree
Hide file tree
Showing 21 changed files with 104 additions and 76 deletions.
17 changes: 9 additions & 8 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"type": "extensionHost",
"request": "launch",
"env": {
"CURSORLESS_MODE": "development",
"CURSORLESS_REPO_ROOT": "${workspaceFolder}"
},
"args": [
Expand All @@ -28,7 +29,7 @@
"type": "extensionHost",
"request": "launch",
"env": {
"CURSORLESS_TEST": "true",
"CURSORLESS_MODE": "test",
"CURSORLESS_REPO_ROOT": "${workspaceFolder}"
},
"args": [
Expand All @@ -48,7 +49,7 @@
"type": "extensionHost",
"request": "launch",
"env": {
"CURSORLESS_TEST": "true",
"CURSORLESS_MODE": "test",
"CURSORLESS_RUN_TEST_SUBSET": "true",
"CURSORLESS_REPO_ROOT": "${workspaceFolder}"
},
Expand All @@ -70,7 +71,7 @@
"request": "launch",
"program": "${workspaceFolder}/packages/test-harness/dist/runTalonTests.cjs",
"env": {
"CURSORLESS_TEST": "true",
"CURSORLESS_MODE": "test",
"CURSORLESS_REPO_ROOT": "${workspaceFolder}"
},
"outFiles": ["${workspaceFolder}/**/out/**/*.js"],
Expand All @@ -86,7 +87,7 @@
"request": "launch",
"program": "${workspaceFolder}/packages/test-harness/dist/runTalonTests.cjs",
"env": {
"CURSORLESS_TEST": "true",
"CURSORLESS_MODE": "test",
"CURSORLESS_RUN_TEST_SUBSET": "true",
"CURSORLESS_REPO_ROOT": "${workspaceFolder}"
},
Expand All @@ -103,7 +104,7 @@
"request": "launch",
"program": "${workspaceFolder}/packages/test-harness/dist/runUnitTestsOnly.cjs",
"env": {
"CURSORLESS_TEST": "true",
"CURSORLESS_MODE": "test",
"CURSORLESS_REPO_ROOT": "${workspaceFolder}"
},
"outFiles": ["${workspaceFolder}/**/out/**/*.js"],
Expand All @@ -118,7 +119,7 @@
"type": "extensionHost",
"request": "launch",
"env": {
"CURSORLESS_TEST": "true",
"CURSORLESS_MODE": "test",
"CURSORLESS_TEST_UPDATE_FIXTURES": "true",
"CURSORLESS_REPO_ROOT": "${workspaceFolder}"
},
Expand All @@ -139,7 +140,7 @@
"type": "extensionHost",
"request": "launch",
"env": {
"CURSORLESS_TEST": "true",
"CURSORLESS_MODE": "test",
"CURSORLESS_TEST_UPDATE_FIXTURES": "true",
"CURSORLESS_RUN_TEST_SUBSET": "true",
"CURSORLESS_REPO_ROOT": "${workspaceFolder}"
Expand All @@ -162,7 +163,7 @@
"request": "launch",
"program": "${workspaceFolder}/packages/test-harness/dist/runUnitTestsOnly.cjs",
"env": {
"CURSORLESS_TEST": "true",
"CURSORLESS_MODE": "test",
"CURSORLESS_TEST_UPDATE_FIXTURES": "true",
"CURSORLESS_REPO_ROOT": "${workspaceFolder}"
},
Expand Down
1 change: 0 additions & 1 deletion packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export * from "./types/RangeOffsets";
export * from "./util/omitByDeep";
export * from "./util/range";
export * from "./util/uniqWithHash";
export * from "./testUtil/isTesting";
export * from "./testUtil/testConstants";
export * from "./testUtil/getFixturePaths";
export * from "./testUtil/getCursorlessRepoRoot";
Expand Down
1 change: 0 additions & 1 deletion packages/common/src/testUtil/isTesting.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FlashStyle, isTesting, Range } from "@cursorless/common";
import { FlashStyle, Range } from "@cursorless/common";
import type { Snippets } from "../../core/Snippets";
import { Offsets } from "../../processTargets/modifiers/surroundingPair/types";
import { ide } from "../../singletons/ide.singleton";
Expand Down Expand Up @@ -220,7 +220,7 @@ export default class GenerateSnippet {

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

if (isTesting()) {
if (ide().runMode === "test") {
// If we're testing, we just overwrite the current document
await editableEditor.setSelections([
editor.document.range.toSelection(false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
Notifier,
Range,
TextDocument,
isTesting,
showError,
type IDE,
type RawTreeSitterQueryProvider,
Expand Down Expand Up @@ -117,7 +116,7 @@ export class LanguageDefinitionsImpl
"Failed to load language definitions",
toString(err),
);
if (isTesting()) {
if (this.ide.runMode === "test") {
throw err;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cursorless-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,7 @@
"@cursorless/common": "workspace:*",
"@cursorless/cursorless-cheatsheet": "workspace:*",
"@cursorless/cursorless-engine": "workspace:*",
"@cursorless/file-system-common": "workspace:*",
"@cursorless/node-common": "workspace:*",
"@cursorless/vscode-common": "workspace:*",
"immer": "^10.0.4",
"itertools": "^2.2.5",
Expand Down
44 changes: 23 additions & 21 deletions packages/cursorless-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
FakeCommandServerApi,
FakeIDE,
IDE,
isTesting,
NormalizedIDE,
Range,
ScopeProvider,
Expand All @@ -21,7 +20,7 @@ import {
FileSystemCommandHistoryStorage,
FileSystemRawTreeSitterQueryProvider,
FileSystemTalonSpokenForms,
} from "@cursorless/file-system-common";
} from "@cursorless/node-common";
import {
CursorlessApi,
getCommandServerApi,
Expand Down Expand Up @@ -83,9 +82,10 @@ export async function activate(
);

const fakeCommandServerApi = new FakeCommandServerApi();
const commandServerApi = isTesting()
? fakeCommandServerApi
: await getCommandServerApi();
const commandServerApi =
normalizedIde.runMode === "test"
? fakeCommandServerApi
: await getCommandServerApi();

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

return {
testHelpers: isTesting()
? constructTestHelpers(
fakeCommandServerApi,
storedTargets,
hatTokenMap,
vscodeIDE,
normalizedIde as NormalizedIDE,
fileSystem,
scopeProvider,
injectIde,
runIntegrationTests,
)
: undefined,
testHelpers:
normalizedIde.runMode === "test"
? constructTestHelpers(
fakeCommandServerApi,
storedTargets,
hatTokenMap,
vscodeIDE,
normalizedIde as NormalizedIDE,
fileSystem,
scopeProvider,
injectIde,
runIntegrationTests,
)
: undefined,

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

const fileSystem = new VscodeFileSystem(
context,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isTesting } from "@cursorless/common";
import { pickBy } from "lodash-es";
import * as vscode from "vscode";
import { HatStyleInfo, HatStyleMap } from "@cursorless/common";
Expand Down Expand Up @@ -33,7 +32,7 @@ export default class VscodeEnabledHatStyleManager {
hatStyleMap!: ExtendedHatStyleMap;
private notifier: Notifier<[HatStyleMap]> = new Notifier();

constructor(extensionContext: vscode.ExtensionContext) {
constructor(private extensionContext: vscode.ExtensionContext) {
this.recomputeEnabledHatStyles = this.recomputeEnabledHatStyles.bind(this);

extensionContext.subscriptions.push(
Expand Down Expand Up @@ -76,9 +75,10 @@ export default class VscodeEnabledHatStyleManager {
colorPenalties.default = 0;

// So that unit tests don't fail locally if you have some colors disabled
const activeHatColors = isTesting()
? HAT_COLORS.filter((color) => !color.startsWith("user"))
: HAT_COLORS.filter((color) => colorEnablement[color]);
const activeHatColors =
this.extensionContext.extensionMode === vscode.ExtensionMode.Test
? HAT_COLORS.filter((color) => !color.startsWith("user"))
: HAT_COLORS.filter((color) => colorEnablement[color]);
const activeNonDefaultHatShapes = HAT_NON_DEFAULT_SHAPES.filter(
(shape) => shapeEnablement[shape],
);
Expand Down
12 changes: 11 additions & 1 deletion packages/cursorless-vscode/src/ide/vscode/VscodeRunMode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ExtensionContext, ExtensionMode } from "vscode";
import type { RunMode } from "@cursorless/common";
import { nodeGetRunMode } from "@cursorless/node-common";

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

export function vscodeRunMode(extensionContext: ExtensionContext): RunMode {
return EXTENSION_MODE_MAP[extensionContext.extensionMode];
const envMode = nodeGetRunMode();
const extensionMode = EXTENSION_MODE_MAP[extensionContext.extensionMode];

if (envMode !== extensionMode) {
throw new Error(
`Extension mode '${extensionMode}' doesn't match environment variable mode '${envMode}'`,
);
}

return extensionMode;
}
3 changes: 1 addition & 2 deletions packages/cursorless-vscode/src/registerCommands.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
CURSORLESS_COMMAND_ID,
CursorlessCommandId,
isTesting,
type CommandHistoryStorage,
} from "@cursorless/common";
import {
Expand Down Expand Up @@ -39,7 +38,7 @@ export function registerCommands(
try {
return await run();
} catch (e) {
if (!isTesting()) {
if (vscodeIde.runMode !== "test") {
const err = e as Error;
console.error(err.stack);
vscodeIde.handleCommandError(err);
Expand Down
2 changes: 1 addition & 1 deletion packages/cursorless-vscode/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"path": "../cursorless-engine"
},
{
"path": "../file-system-common"
"path": "../node-common"
},
{
"path": "../vscode-common"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "@cursorless/file-system-common",
"name": "@cursorless/node-common",
"version": "1.0.0",
"description": "Package containing common file system utilities",
"description": "Package containing node utilities",
"main": "./out/index.js",
"scripts": {
"compile:tsc": "tsc --build",
"compile:esbuild": "esbuild ./src/index.ts --sourcemap --format=esm --bundle --packages=external --outfile=./out/index.js",
"compile": "pnpm compile:tsc && pnpm compile:esbuild",
"watch:tsc": "pnpm compile:tsc --watch",
"watch:esbuild": "pnpm compile:esbuild --watch",
"watch": "pnpm run --filter @cursorless/file-system-common --parallel '/^watch:.*/'",
"watch": "pnpm run --filter @cursorless/node-common --parallel '/^watch:.*/'",
"clean": "rm -rf ./out tsconfig.tsbuildinfo ./dist ./build"
},
"keywords": [],
Expand All @@ -29,7 +29,5 @@
"glob": "^10.3.10",
"pathe": "^1.1.2"
},
"devDependencies": {
"@types/glob": "^8.1.0"
}
"devDependencies": {}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./FileSystemTalonSpokenForms";
export * from "./FileSystemCommandHistoryStorage";
export * from "./FileSystemLanguageDefinitionsProvider";
export * from "./FileSystemTalonSpokenForms";
export * from "./nodeGetRunMode";
24 changes: 24 additions & 0 deletions packages/node-common/src/nodeGetRunMode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { RunMode } from "@cursorless/common";

/**
* Get the current run mode of the application as defined by the CURSORLESS_MODE
* environment variable.
*
* @returns The current run mode of the application
*/
export function nodeGetRunMode(): RunMode {
const mode = process.env.CURSORLESS_MODE;
switch (mode) {
case undefined:
case "production":
return "production";
case "development":
return "development";
case "test":
return "test";
default:
throw new Error(
`Unexpected value for environment variable CURSORLESS_MODE: ${mode}`,
);
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/test-harness/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"private": true,
"main": "./out/index.js",
"scripts": {
"test": "env CURSORLESS_TEST=true my-ts-node src/scripts/runTestsCI.ts",
"test": "env CURSORLESS_MODE=test my-ts-node src/scripts/runTestsCI.ts",
"build:base": "esbuild --sourcemap --conditions=cursorless:bundler --bundle --external:vscode --format=cjs --platform=node",
"build": "pnpm run build:runner && pnpm run build:tests && pnpm run build:unit && pnpm run build:talon",
"build:runner": "pnpm run build:base ./src/runners/extensionTestsVscode.ts --outfile=dist/extensionTestsVscode.cjs",
Expand Down
Loading

0 comments on commit 1429190

Please sign in to comment.