-
-
Notifications
You must be signed in to change notification settings - Fork 0
test: add unit tests for getUriParts utility #36
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| export const env = { | ||
| uriScheme: 'vscode', | ||
| appName: 'Code', | ||
| language: 'en-US' | ||
| }; | ||
|
|
||
| export const Uri = { | ||
| parse: (value: string) => ({ | ||
| toString: () => value, | ||
| path: value, | ||
| scheme: 'file' | ||
| }), | ||
| file: (path: string) => ({ | ||
| toString: () => `file://${path}`, | ||
| path, | ||
| scheme: 'file' | ||
| }) | ||
|
Comment on lines
+13
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This mock for While the current tests only use POSIX-style paths, making the mock cross-platform compatible would make it more robust for future tests. file: (path: string) => {
// Handle Windows paths to create valid file URIs.
const posixPath = path.replace(/\\/g, '/');
const uriPath = /^[a-zA-Z]:\//.test(posixPath) ? `/${posixPath}` : posixPath;
return {
toString: () => `file://${uriPath}`,
path: uriPath,
scheme: 'file'
};
} |
||
| }; | ||
|
|
||
| export enum ColorThemeKind { | ||
| Light = 1, | ||
| Dark = 2, | ||
| HighContrast = 3, | ||
| HighContrastLight = 4 | ||
| } | ||
|
|
||
| export enum UIKind { | ||
| Desktop = 1, | ||
| Web = 2 | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { describe, it } from 'node:test'; | ||
| import assert from 'node:assert'; | ||
| import { getUriParts } from '../../src/helpers'; | ||
| import * as vsc from 'vscode'; | ||
|
|
||
| describe('getUriParts', () => { | ||
| it('should parse simple file path string', () => { | ||
| const parts = getUriParts('/home/user/test.txt'); | ||
| assert.strictEqual(parts.dirname, '/home/user/'); | ||
| assert.strictEqual(parts.filename, 'test.txt'); | ||
| assert.strictEqual(parts.basename, 'test'); | ||
| assert.strictEqual(parts.extname, '.txt'); | ||
| }); | ||
|
|
||
| it('should parse simple file path URI', () => { | ||
| const uri = vsc.Uri.file('/home/user/test.txt'); | ||
| const parts = getUriParts(uri); | ||
| assert.strictEqual(parts.dirname, 'file:///home/user/'); // Regex matches from uri.toString() which returns file://... | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test reveals a correctness issue with the To fix this, Here is a suggested change for export function getUriParts(uri: string | vsc.Uri): { /* ... */ } {
const pathString = typeof uri === 'string' ? uri : uri.path;
const match = pathString.match(uriPathPattern);
// ... the rest of the function
}With this change in |
||
| assert.strictEqual(parts.filename, 'test.txt'); | ||
| assert.strictEqual(parts.basename, 'test'); | ||
| assert.strictEqual(parts.extname, '.txt'); | ||
| }); | ||
|
|
||
| it('should parse file without extension', () => { | ||
| const parts = getUriParts('/home/user/makefile'); | ||
| assert.strictEqual(parts.dirname, '/home/user/'); | ||
| assert.strictEqual(parts.filename, 'makefile'); | ||
| assert.strictEqual(parts.basename, 'makefile'); | ||
| assert.strictEqual(parts.extname, ''); | ||
| }); | ||
|
|
||
| it('should parse file in root', () => { | ||
| const parts = getUriParts('/config.json'); | ||
| assert.strictEqual(parts.dirname, '/'); | ||
| assert.strictEqual(parts.filename, 'config.json'); | ||
| assert.strictEqual(parts.basename, 'config'); | ||
| assert.strictEqual(parts.extname, '.json'); | ||
| }); | ||
|
|
||
| it('should parse just filename', () => { | ||
| const parts = getUriParts('notes.md'); | ||
| assert.strictEqual(parts.dirname, ''); | ||
| assert.strictEqual(parts.filename, 'notes.md'); | ||
| assert.strictEqual(parts.basename, 'notes'); | ||
| assert.strictEqual(parts.extname, '.md'); | ||
| }); | ||
|
|
||
| it('should handle special characters and decoding', () => { | ||
| // We use string input to test decodeURIComponent logic independently of vsc.Uri mock | ||
| const parts = getUriParts('/path/to/file%20name.txt'); | ||
| assert.strictEqual(parts.dirname, '/path/to/'); | ||
| assert.strictEqual(parts.filename, 'file name.txt'); | ||
| assert.strictEqual(parts.basename, 'file name'); | ||
| assert.strictEqual(parts.extname, '.txt'); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "extends": "./tsconfig.json", | ||
| "compilerOptions": { | ||
| "baseUrl": ".", | ||
| "paths": { | ||
| "vscode": ["tests/mocks/vscode.ts"] | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
pathproperty in this mock forUri.parseis incorrect. It returns the entire URI string, whereas it should return only the path component. For example, for a URIfile:///foo/bar.txt,pathshould be/foo/bar.txt.This is inconsistent with the
file()mock and could cause issues in future tests that rely onuri.path. This becomes more important ifgetUriPartsis changed to useuri.pathas suggested in another comment.Consider making the mock more accurate to prevent future issues.