Skip to content
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

feat(sourcemaps): Add tests for SourceMap.ts, generate test source maps (internal and external) and verify line translations. #242

Merged
merged 5 commits into from
Oct 22, 2024
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
5 changes: 2 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
],
"rules": {
"@typescript-eslint/naming-convention": "warn",
"@typescript-eslint/semi": "warn",
"@typescript-eslint/semi": ["warn", "always"],
"curly": "warn",
"eqeqeq": "warn",
"no-throw-literal": "warn",
"semi": "off"
"no-throw-literal": "warn"
},
"ignorePatterns": [
"**/*.d.ts"
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
node-version: 20
- run: npm run ci:all
- run: npm run build:all
- run: npm run test

- run: npm run release:dry-run
env:
Expand Down
14 changes: 13 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,16 @@ Some examples of this are:
- `feat(diagnostics): Added client-side statistics to the diagnostics view`
- `chore: Added CONTRIBUTING.md`

To see the current list of supported types, see [CI.yml](./.github/workflows/ci.yml#L13)'s 'Lint PR Title' task.
To see the current list of supported types, see [CI.yml](./.github/workflows/ci.yml#L13)'s 'Lint PR Title' task.

# Tests

Create tests using vitest.
Follow the convention `sourcefilename.test.ts` when adding new files.

To run tests use the command:
```
npm run test
```

Optionally install [vitest extension](https://marketplace.visualstudio.com/items?itemName=vitest.explorer).
8,509 changes: 5,435 additions & 3,074 deletions package-lock.json

Large diffs are not rendered by default.

30 changes: 13 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@
"build:all": "npm run build:webview && npm run compile",
"vscode:prepublish": "npm run package:all",
"pretest": "npm run compile && npm run lint",
"lint": "eslint src --ext ts",
"lint": "eslint src --ext .ts",
"test": "vitest",
"release": "npx semantic-release",
"release:dry-run": "npx semantic-release --dry-run",
"compile": "npm run check-types && node esbuild.mjs",
Expand All @@ -192,32 +193,27 @@
"watch:tsc": "tsc --noEmit --watch --project tsconfig.json",
"package:all": "npm run build:webview && npm run check-types && node esbuild.mjs --production"
},
"dependencies": {
"@vscode/debugadapter": "^1.67.0",
"@vscode/webview-ui-toolkit": "^1.4.0",
"source-map": "^0.7.4",
"stream-parser": "^0.3.1"
},
"devDependencies": {
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/git": "^10.0.1",
"@types/glob": "^7.1.3",
"@types/mocha": "^10.0.8",
"@types/node": "^22.7.3",
"@types/react": "^18.3.9",
"@types/vscode": "^1.93.0",
"@typescript-eslint/eslint-plugin": "^8.8.0",
"@typescript-eslint/parser": "^8.7.0",
"@typescript-eslint/eslint-plugin": "^4.14.1",
"@typescript-eslint/parser": "^4.14.1",
"@vscode/debugadapter": "^1.67.0",
"@vscode/debugprotocol": "1.68.0",
"@vscode/vsce": "^3.1.1",
"@vscode/webview-ui-toolkit": "^1.4.0",
"esbuild": "^0.24.0",
"esbuild-plugin-copy": "^2.1.1",
"eslint": "^9.11.1",
"glob": "^7.1.6",
"mocha": "^10.7.3",
"path-browserify": "^1.0.1",
"eslint": "^7.19.0",
"semantic-release": "^24.1.1",
"semantic-release-vsce": "github:JakeShirley/semantic-release-vsce",
"source-map": "^0.7.4",
"stream-parser": "^0.3.1",
"ts-loader": "^9.5.1",
"typescript": "^5.5.4"
"typescript": "^5.5.4",
"vitest": "^2.1.3"
}
}
}
64 changes: 64 additions & 0 deletions src/SourceMaps.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { describe, it, expect } from 'vitest';
import { SourceMaps } from './SourceMaps';
import path from 'path';

describe('SourceMaps', () => {

const linesToVerify = [
17, 28, 35, 52, 67, 81, 86, 94, 103, 109, 115, 119
];

const verifyLines = async (sourceMaps: SourceMaps, originalLocalAbsolutePath: string, generatedRemoteLocalPath: string) => {
for (const sourceLine of linesToVerify) {
const generatedPosition = await sourceMaps.getGeneratedPositionFor({
source: originalLocalAbsolutePath,
column: 0,
line: sourceLine,
});
const originalPosition = await sourceMaps.getOriginalPositionFor({
source: generatedRemoteLocalPath,
column: 0,
line: generatedPosition.line || 0,
});
expect(sourceLine).toBe(originalPosition.line);
}
};

//
// No source maps
//
it('should leave line number unchanged between generated and original if no source maps', async () => {
const localRoot = path.resolve('./test-source-maps/scripts-and-source-maps');
const sourceMaps = new SourceMaps(localRoot);
const originalLocalAbsolutePath = path.join(localRoot, 'main.js');
const generatedRemoteLocalPath = await sourceMaps.getGeneratedRemoteRelativePath(originalLocalAbsolutePath);
expect(generatedRemoteLocalPath).toBe('main.js');
verifyLines(sourceMaps, originalLocalAbsolutePath, generatedRemoteLocalPath);
});

//
// External source maps
//
it('should use main.ts source map (main.js.map) to generate translated line numbers to script main.js and back', async () => {
const localRoot = path.resolve('./test-source-maps/src');
const sourceMapRoot = path.resolve('./test-source-maps/external-source-maps/scripts');
const sourceMaps = new SourceMaps(localRoot, sourceMapRoot);
const originalLocalAbsolutePath = path.join(localRoot, 'main.ts');
const generatedRemoteLocalPath = await sourceMaps.getGeneratedRemoteRelativePath(originalLocalAbsolutePath);
expect(generatedRemoteLocalPath).toBe('main.js');
verifyLines(sourceMaps, originalLocalAbsolutePath, generatedRemoteLocalPath);
});

//
// Inline source maps
//
it('should use inline source map to generate translated line numbers to script main.js and back', async () => {
const localRoot = path.resolve('./test-source-maps/src');
const sourceMapRoot = path.resolve('./test-source-maps/inline-source-maps/scripts');
const sourceMaps = new SourceMaps('', sourceMapRoot, undefined, true);
const originalLocalAbsolutePath = path.join(localRoot, 'main.ts');
const generatedRemoteLocalPath = await sourceMaps.getGeneratedRemoteRelativePath(originalLocalAbsolutePath);
expect(generatedRemoteLocalPath).toBe('main.js');
verifyLines(sourceMaps, originalLocalAbsolutePath, generatedRemoteLocalPath);
});
});
44 changes: 44 additions & 0 deletions src/Utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

// Copyright (C) Microsoft Corporation. All rights reserved.

import * as os from 'os';
import { describe, it, expect } from 'vitest';
import { normalizePath, normalizePathForRemote, isUUID } from './Utils';

describe('Utils', () => {
describe('normalizePath', () => {
it('should normalize Windows paths correctly with drive letter', () => {
expect(normalizePath('C:\\path\\to\\file')).toBe('C:\\path\\to\\file');
if (os.type() === 'Windows_NT') {
expect(normalizePath('c:\\path\\to\\file')).toBe('C:\\path\\to\\file');
}
});

it('should normalize non-Windows paths correctly', () => {
if (os.type() === 'Windows_NT') {
expect(normalizePath('/path/to/file')).toBe('\\path\\to\\file');
} else {
expect(normalizePath('/path/to/file')).toBe('/path/to/file');
}
});
});

describe('normalizePathForRemote', () => {
it('should replace backslashes with forward slashes', () => {
expect(normalizePathForRemote('C:\\path\\to\\file')).toBe('C:/path/to/file');
expect(normalizePathForRemote('C:/path/to/file')).toBe('C:/path/to/file');
});
});

describe('isUUID', () => {
it('should return true for valid UUIDs', () => {
expect(isUUID('123e4567-e89b-12d3-a456-426614174000')).toBe(true);
});

it('should return false for invalid UUIDs', () => {
expect(isUUID('invalid-uuid')).toBe(false);
expect(isUUID('123e4567-e89b-12d3-a456-42661417400')).toBe(false); // One character short
expect(isUUID('123e4567-e89b-12d3-a456-4266141740000')).toBe(false); // One character too long
});
});
});
8 changes: 8 additions & 0 deletions test-source-maps/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# `test-source-maps` Directory

Project to generate test source maps consumed by SourceMap.tests.ts.

```
npm run generate
```

90 changes: 90 additions & 0 deletions test-source-maps/external-source-maps/scripts/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test-source-maps/external-source-maps/scripts/main.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading