Skip to content

fix: unit test tree-sitter loading #377

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
merged 1 commit into from
Jul 19, 2023
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
2 changes: 1 addition & 1 deletion ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"stompjs": "^2.3.3",
"ts-pattern": "^4.0.6",
"typescript": "^4.4.2",
"web-tree-sitter": "0.20.7",
"web-tree-sitter": "^0.20.8",
"web-vitals": "^2.1.0",
"xterm": "^5.0.0",
"xterm-addon-fit": "^0.6.0",
Expand Down
1 change: 0 additions & 1 deletion ui/public/tree-sitter.js

This file was deleted.

Binary file modified ui/public/tree-sitter.wasm
Binary file not shown.
29 changes: 29 additions & 0 deletions ui/src/lib/parser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,35 @@ export async function initParser(prefix = "/", callback = () => {}) {
});
}

/**
* This function is used only in unit test. The difference:
* 1. no locateFile function.
* 2. use ./public/ prefix
* 3. no callback.
*/
export async function initParserForTest() {
if (parser_loading) return false;
if (parser) {
return true;
}
return new Promise((resolve, reject) => {
parser_loading = true;
// Diff 1: no locateFile
Parser.init().then(async () => {
/* the library is ready */
parser = new Parser();
const Python = await Parser.Language.load(
// Diff 2: use ./public/ prefix
`./public/tree-sitter-python.wasm`
);
parser.setLanguage(Python);
parser_loading = false;
// Diff 3: no callback
resolve(true);
});
});
}

export type Annotation = {
name: string;
type: "function" | "callsite" | "vardef" | "varuse" | "bridge";
Expand Down
4 changes: 2 additions & 2 deletions ui/src/tests/parser.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// import { describe, expect, test } from "@jest/globals";
import { analyzeCode, initParser } from "../lib/parser";
import { analyzeCode, initParserForTest } from "../lib/parser";
import * as fs from "fs";

describe("sum module", () => {
Expand All @@ -9,7 +9,7 @@ describe("sum module", () => {

test("initparser", async () => {
// await new Promise((resolve) => setTimeout(resolve, 1000));
let res = await initParser("./public/");
let res = await initParserForTest();
expect(res).toBe(true);
});

Expand Down
8 changes: 4 additions & 4 deletions ui/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12686,10 +12686,10 @@ wbuf@^1.1.0, wbuf@^1.7.3:
dependencies:
minimalistic-assert "^1.0.0"

[email protected].7:
version "0.20.7"
resolved "https://registry.yarnpkg.com/web-tree-sitter/-/web-tree-sitter-0.20.7.tgz#b0ddb78e8244221a3100f432c7e162516cd9cd09"
integrity sha512-flC9JJmTII9uAeeYpWF8hxDJ7bfY+leldQryetll8Nv4WgI+MXc6h7TiyAZASWl9uC9TvmfdgOjZn1DAQecb3A==
web-tree-sitter@^0.20.8:
version "0.20.8"
resolved "https://registry.yarnpkg.com/web-tree-sitter/-/web-tree-sitter-0.20.8.tgz#1e371cb577584789cadd75cb49c7ddfbc99d04c8"
integrity sha512-weOVgZ3aAARgdnb220GqYuh7+rZU0Ka9k9yfKtGAzEYMa6GgiCzW9JjQRJyCJakvibQW+dfjJdihjInKuuCAUQ==

web-vitals@^2.1.0:
version "2.1.4"
Expand Down