Skip to content

Commit

Permalink
Fix test configuration and add basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saoudrizwan committed Dec 25, 2024
1 parent 6ef15cf commit d8080d7
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .vscode-test.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { defineConfig } from "@vscode/test-cli"

export default defineConfig({
files: "out/test/**/*.test.js",
files: "out/**/*.test.js",
mocha: {
timeout: 20000, // Maximum time (in ms) that a test can run before failing
},
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
"watch:esbuild": "node esbuild.js --watch",
"watch:tsc": "tsc --noEmit --watch --project tsconfig.json",
"package": "npm run build:webview && npm run check-types && npm run lint && node esbuild.js --production",
"compile-tests": "tsc -p . --outDir out",
"compile-tests": "tsc -p ./tsconfig.test.json --outDir out",
"watch-tests": "tsc -p . -w --outDir out",
"pretest": "npm run compile-tests && npm run compile && npm run lint",
"check-types": "tsc --noEmit",
Expand Down
22 changes: 18 additions & 4 deletions src/test/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,25 @@ import * as assert from "assert"
import * as vscode from "vscode"
// import * as myExtension from '../../extension';

import { readFile } from "fs/promises"
import path from "path"

const packagePath = path.join(__dirname, "..", "..", "..", "package.json")

suite("Extension Test Suite", () => {
vscode.window.showInformationMessage("Start all tests.")
suiteTeardown(() => {
vscode.window.showInformationMessage("All tests done!")
})

test("Sanity check", async () => {
const packageJSON = JSON.parse(await readFile(packagePath, "utf8"))
const id = packageJSON.publisher + "." + packageJSON.name
const clineExtensionApi = vscode.extensions.getExtension(id)
assert.equal(clineExtensionApi?.id, id)
})

test("Sample test", () => {
assert.strictEqual(-1, [1, 2, 3].indexOf(5))
assert.strictEqual(-1, [1, 2, 3].indexOf(0))
test("Ensure that the extension is correctly loaded by running a command", async () => {
await new Promise((resolve) => setTimeout(resolve, 400))
await vscode.commands.executeCommand("cline.plusButtonClicked")
})
})
48 changes: 48 additions & 0 deletions src/utils/path.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as assert from "assert"
import * as path from "path"
import * as os from "os"
import { arePathsEqual, getReadablePath } from "./path"

suite("Path Utils", () => {
test("arePathsEqual handles undefined paths", () => {
assert.strictEqual(arePathsEqual(undefined, undefined), true)
assert.strictEqual(arePathsEqual("foo", undefined), false)
assert.strictEqual(arePathsEqual(undefined, "foo"), false)
})

test("arePathsEqual handles case sensitivity based on platform", () => {
if (process.platform === "win32") {
assert.strictEqual(arePathsEqual("FOO/BAR", "foo/bar"), true)
} else {
assert.strictEqual(arePathsEqual("FOO/BAR", "foo/bar"), false)
}
})

test("arePathsEqual handles normalized paths", () => {
assert.strictEqual(arePathsEqual("/tmp/./dir", "/tmp/../tmp/dir"), true)
assert.strictEqual(arePathsEqual("/tmp/./dir", "/tmp/../dir"), false)
})

test("getReadablePath handles desktop path", () => {
const desktop = path.join(os.homedir(), "Desktop")
const testPath = path.join(desktop, "test.txt")
assert.strictEqual(getReadablePath(desktop, "test.txt"), testPath.replace(/\\/g, "/"))
})

test("getReadablePath shows relative paths within cwd", () => {
const cwd = "/home/user/project"
const filePath = "/home/user/project/src/file.txt"
assert.strictEqual(getReadablePath(cwd, filePath), "src/file.txt")
})

test("getReadablePath shows basename when path equals cwd", () => {
const cwd = "/home/user/project"
assert.strictEqual(getReadablePath(cwd, cwd), "project")
})

test("getReadablePath shows absolute path when outside cwd", () => {
const cwd = "/home/user/project"
const filePath = "/home/user/other/file.txt"
assert.strictEqual(getReadablePath(cwd, filePath), filePath)
})
})
13 changes: 13 additions & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
// This separate tsconfig is necessary because VS Code's test runner requires CommonJS modules,
// while our main project uses ES Modules (ESM). This config inherits most settings from the base
// tsconfig.json but overrides the module system for test files only. This doesn't affect how
// tests interact with the main codebase - it only changes how the test files themselves are
// compiled to make them compatible with VS Code's test runner.
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node"
},
"include": ["src/**/*.test.ts"]
}

0 comments on commit d8080d7

Please sign in to comment.