Skip to content

Commit 4c25214

Browse files
committed
[lldb-dap] add test case
1 parent c62a353 commit 4c25214

File tree

5 files changed

+94
-14
lines changed

5 files changed

+94
-14
lines changed

lldb/tools/lldb-dap/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@
3131
"chokidar": "^4.0.3"
3232
},
3333
"devDependencies": {
34+
"@types/mocha": "^10.0.1",
3435
"@types/node": "^18.19.41",
3536
"@types/tabulator-tables": "^6.2.10",
3637
"@types/vscode": "1.75.0",
3738
"@types/vscode-webview": "^1.57.5",
3839
"@vscode/debugprotocol": "^1.68.0",
3940
"@vscode/vsce": "^3.2.2",
4041
"esbuild": "^0.25.9",
42+
"mocha": "^10.2.0",
4143
"prettier": "^3.4.2",
4244
"prettier-plugin-curly": "^0.3.1",
4345
"tabulator-tables": "^6.3.1",
@@ -53,9 +55,10 @@
5355
"bundle-symbols-table-view": "npx tsc -p src-ts/webview --noEmit && npx esbuild src-ts/webview/symbols-table-view.ts --bundle --format=iife --outdir=./out/webview",
5456
"bundle-tabulator": "cp node_modules/tabulator-tables/dist/js/tabulator.min.js ./out/webview/ && cp node_modules/tabulator-tables/dist/css/tabulator_midnight.min.css ./out/webview/ && cp node_modules/tabulator-tables/dist/css/tabulator_simple.min.css ./out/webview/",
5557
"bundle-webview": "npm run bundle-symbols-table-view && npm run bundle-tabulator",
58+
"check-unit": "npx tsc -p ./ && mocha ./out/unittests-ts",
5659
"vscode:prepublish": "npm run bundle-webview && npm run bundle-extension",
5760
"watch": "npm run bundle-webview && tsc -watch -p ./",
58-
"format": "npx prettier './src-ts/' --write",
61+
"format": "npx prettier './src-ts/' './unittests-ts' --write",
5962
"package": "rm -rf ./out && vsce package --out ./out/lldb-dap.vsix",
6063
"publish": "vsce publish",
6164
"vscode-uninstall": "code --uninstall-extension llvm-vs-code-extensions.lldb-dap",

lldb/tools/lldb-dap/src-ts/debug-adapter-factory.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as os from "os";
21
import * as path from "path";
32
import * as util from "util";
43
import * as vscode from "vscode";
@@ -7,19 +6,10 @@ import * as fs from "node:fs/promises";
76
import { ConfigureButton, OpenSettingsButton } from "./ui/show-error-message";
87
import { ErrorWithNotification } from "./ui/error-with-notification";
98
import { LogFilePathProvider, LogType } from "./logging";
9+
import { expandUser } from "./utils";
1010

1111
const exec = util.promisify(child_process.execFile);
1212

13-
/**
14-
* Expands the character `~` to the user's home directory
15-
*/
16-
function expandUser(file_path: string): string {
17-
if (file_path.startsWith("~")) {
18-
return os.homedir() + file_path.slice(1);
19-
}
20-
return file_path;
21-
}
22-
2313
async function isExecutable(path: string): Promise<Boolean> {
2414
try {
2515
await fs.access(path, fs.constants.X_OK);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import * as os from "os";
2+
import * as path from "path";
3+
4+
/**
5+
* Expands the character `~` to the user's home directory
6+
*/
7+
export function expandUser(file_path: string): string {
8+
if (os.platform() == "win32") {
9+
return file_path;
10+
}
11+
12+
if (!file_path) {
13+
return "";
14+
}
15+
16+
if (!file_path.startsWith("~")) {
17+
return file_path;
18+
}
19+
20+
const path_len = file_path.length;
21+
if (path_len == 1) {
22+
return os.homedir();
23+
}
24+
25+
if (file_path.charAt(1) == path.sep) {
26+
return path.join(os.homedir(), file_path.substring(1));
27+
}
28+
29+
const sep_index = file_path.indexOf(path.sep);
30+
const user_name_end = sep_index == -1 ? file_path.length : sep_index;
31+
const user_name = file_path.substring(1, user_name_end);
32+
try {
33+
if (user_name == os.userInfo().username) {
34+
return path.join(os.homedir(), file_path.substring(user_name_end));
35+
}
36+
} catch (err) {
37+
return file_path;
38+
}
39+
40+
return file_path;
41+
}

lldb/tools/lldb-dap/tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
"moduleResolution": "node",
44
"module": "commonjs",
55
"outDir": "out",
6-
"rootDir": "src-ts",
6+
"rootDirs": ["src-ts", "unittests-ts"],
77
"sourceMap": true,
88
"strict": true,
99
"target": "es6"
1010
},
1111
"include": [
12-
"src-ts"
12+
"src-ts",
13+
"unittests-ts"
1314
],
1415
"exclude": [
1516
"node_modules",
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { suite, test, suiteSetup, suiteTeardown } from "mocha";
2+
import * as assert from "assert";
3+
import * as os from "os";
4+
import * as process from "process";
5+
import { expandUser } from "../src-ts/utils";
6+
7+
suite("expandUser Test", function () {
8+
let home_env: { [key: string]: string | undefined } = {};
9+
const local_username = os.userInfo().username;
10+
11+
suiteSetup(function () {
12+
if (os.platform() == "win32") {
13+
this.skip();
14+
}
15+
home_env.HOME = process.env.HOME;
16+
process.env.HOME = "/home/buildbot";
17+
});
18+
19+
suiteTeardown(function () {
20+
process.env.HOME = home_env.HOME;
21+
});
22+
23+
test("tilde ", function () {
24+
assert.equal(expandUser("~"), "/home/buildbot");
25+
assert.equal(expandUser("~/"), "/home/buildbot/");
26+
assert.equal(expandUser("~/worker"), "/home/buildbot/worker");
27+
});
28+
29+
test("tilde with username", function () {
30+
assert.equal(expandUser(`~${local_username}`), "/home/buildbot");
31+
assert.equal(expandUser(`~${local_username}/`), "/home/buildbot/");
32+
assert.equal(expandUser(`~${local_username}/dev`), "/home/buildbot/dev");
33+
34+
// test unknown user
35+
assert.notEqual(expandUser("~not_a_user"), "/home/build/bot");
36+
});
37+
38+
test("empty", function () {
39+
assert.equal(expandUser(""), "");
40+
});
41+
42+
test("no tilde", function () {
43+
assert.equal(expandUser("/home/buildbot/worker"), "/home/buildbot/worker");
44+
});
45+
});

0 commit comments

Comments
 (0)