Skip to content

Add integration test setup; check for legacy/new scope duplicates #1428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/cursorless-engine/src/cursorlessEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { HatTokenMapImpl } from "./core/HatTokenMapImpl";
import { Snippets } from "./core/Snippets";
import { RangeUpdater } from "./core/updateSelections/RangeUpdater";
import { LanguageDefinitions } from "./languages/LanguageDefinitions";
import { runIntegrationTests } from "./runIntegrationTests";
import { injectIde } from "./singletons/ide.singleton";
import { ensureCommandShape } from "./core/commandVersionUpgrades/ensureCommandShape";
import { runCommand } from "./runCommand";
Expand Down Expand Up @@ -72,6 +73,8 @@ export function createCursorlessEngine(
hatTokenMap,
snippets,
injectIde,
runIntegrationTests: () =>
runIntegrationTests(treeSitter, languageDefinitions),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we instead expose treeSitter and languageDefinitions, then we could avoid runIntegrationTests ending up in the prod bundle, right?

Copy link
Member Author

@pokey pokey Apr 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I don't think that would help. The issue is that the test needs to be defined inside cursorless engine; see PR description for more detail. Also notice that treeSitter actually comes from the caller, so exposing it wouldn't actually do anything

We'd need to expose everything imported in the file that defines the integration tests

But maybe I've misunderstood your proposal?

};
}

Expand All @@ -96,4 +99,5 @@ export interface CursorlessEngine {
hatTokenMap: HatTokenMapImpl;
snippets: Snippets;
injectIde: (ide: IDE | undefined) => void;
runIntegrationTests: () => Promise<void>;
}
51 changes: 27 additions & 24 deletions packages/cursorless-engine/src/languages/LegacyLanguageId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@
* The language IDs that we have full tree-sitter support for using our legacy
* modifiers.
*/
export type LegacyLanguageId =
| "c"
| "clojure"
| "cpp"
| "css"
| "csharp"
| "go"
| "html"
| "java"
| "javascript"
| "javascriptreact"
| "json"
| "jsonc"
| "latex"
| "markdown"
| "php"
| "python"
| "ruby"
| "scala"
| "scss"
| "rust"
| "typescript"
| "typescriptreact"
| "xml";
export const legacyLanguageIds = [
"c",
"clojure",
"cpp",
"css",
"csharp",
"go",
"html",
"java",
"javascript",
"javascriptreact",
"json",
"jsonc",
"latex",
"markdown",
"php",
"python",
"ruby",
"scala",
"scss",
"rust",
"typescript",
"typescriptreact",
"xml",
];

export type LegacyLanguageId = (typeof legacyLanguageIds)[number];
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function getNodeMatcher(
return matcher;
}

const languageMatchers: Record<
export const languageMatchers: Record<
LegacyLanguageId,
Record<SimpleScopeTypeType, NodeMatcher>
> = {
Expand Down
44 changes: 44 additions & 0 deletions packages/cursorless-engine/src/runIntegrationTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import assert = require("assert");
import { languageMatchers } from "./languages/getNodeMatcher";
import { TreeSitter } from "./typings/TreeSitter";
import { legacyLanguageIds } from "./languages/LegacyLanguageId";
import { LanguageDefinitions } from "./languages/LanguageDefinitions";

/**
* Run tests that require multiple components to be instantiated, as well as a
* full context, eg including tree sitter, but that are too closely tied to the
* engine to be defined in cursorless-vscode-e2e
*
* @param treeSitter The tree sitter instance
* @param languageDefinitions The language definitions instance
*/
export async function runIntegrationTests(
treeSitter: TreeSitter,
languageDefinitions: LanguageDefinitions,
) {
await assertNoScopesBothLegacyAndNew(treeSitter, languageDefinitions);
}

async function assertNoScopesBothLegacyAndNew(
treeSitter: TreeSitter,
languageDefinitions: LanguageDefinitions,
) {
const errors: string[] = [];
for (const languageId of legacyLanguageIds) {
await treeSitter.loadLanguage(languageId);

Object.keys(languageMatchers[languageId]).map((scopeTypeType) => {
if (
languageDefinitions.get(languageId)?.getScopeHandler({
type: scopeTypeType,
}) != null
) {
errors.push(
`Scope '${scopeTypeType}' defined as both legacy and new for language ${languageId}`,
);
}
});
}

assert.deepStrictEqual(errors, []);
}
10 changes: 9 additions & 1 deletion packages/cursorless-engine/src/typings/TreeSitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ export interface TreeSitter {
/**
* Gets a language if it is loaded
*
* @param languageId The language id of the language to load
* @param languageId The language id of the language to get
* @returns The language if it is already loaded
*/
getLanguage(languageId: string): Language | undefined;

/**
* Loads a language, returning true if it was successfully loaded
*
* @param languageId The language id of the language to load
* @returns `true` if the language was successfully loaded
*/
loadLanguage(languageId: string): Promise<boolean>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getCursorlessApi } from "@cursorless/vscode-common";
import { endToEndTestSetup } from "../endToEndTestSetup";

suite("Cursorless engine integration", async function () {
endToEndTestSetup(this);

test("integration", async () => {
const cursorlessApi = await getCursorlessApi();
const { runIntegrationTests } = cursorlessApi.testHelpers!;
await runIntegrationTests();
});
});
2 changes: 2 additions & 0 deletions packages/cursorless-vscode/src/constructTestHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function constructTestHelpers(
vscodeIDE: VscodeIDE,
normalizedIde: NormalizedIDE,
injectIde: (ide: IDE) => void,
runIntegrationTests: () => Promise<void>,
): TestHelpers | undefined {
return {
commandServerApi: commandServerApi!,
Expand Down Expand Up @@ -72,5 +73,6 @@ export function constructTestHelpers(
);
},
hatTokenMap,
runIntegrationTests,
};
}
3 changes: 3 additions & 0 deletions packages/cursorless-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export async function activate(
hatTokenMap,
snippets,
injectIde,
runIntegrationTests,
} = createCursorlessEngine(
treeSitter,
normalizedIde ?? vscodeIDE,
Expand Down Expand Up @@ -93,6 +94,7 @@ export async function activate(
vscodeIDE,
normalizedIde!,
injectIde,
runIntegrationTests,
)
: undefined,

Expand Down Expand Up @@ -129,6 +131,7 @@ function createTreeSitter(parseTreeApi: ParseTreeApi): TreeSitter {
return parseTreeApi.getTreeForUri(document.uri);
},

loadLanguage: parseTreeApi.loadLanguage,
getLanguage: parseTreeApi.getLanguage,
};
}
Expand Down
2 changes: 2 additions & 0 deletions packages/vscode-common/src/getExtensionApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export interface TestHelpers {
marks: SerializedMarks | undefined,
forceRealClipboard: boolean,
): Promise<TestCaseSnapshot>;

runIntegrationTests(): Promise<void>;
}

export interface CursorlessApi {
Expand Down