-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support compiled tests with a sourcemap only (#49)
- Loading branch information
Showing
13 changed files
with
151 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
jest.mock('../logger'); | ||
|
||
import path from 'node:path'; | ||
|
||
import { log } from '../logger'; | ||
|
||
import { FileNavigatorCache } from './FileNavigatorCache'; | ||
|
||
const __FIXTURES__ = path.join(__dirname, '__fixtures__'); | ||
|
||
describe('FileNavigatorCache', () => { | ||
test('should handle non-existent sourcemap', async () => { | ||
const cache = new FileNavigatorCache(); | ||
await cache.scanSourcemap(path.join(__FIXTURES__, 'non-existent')); | ||
await expect(cache.resolve(path.join(__FIXTURES__, 'test.ts'))).resolves.toBeUndefined(); | ||
expect(log.error).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('should handle a broken sourcemap', async () => { | ||
const cache = new FileNavigatorCache(); | ||
await cache.scanSourcemap(path.join(__FIXTURES__, 'broken')); | ||
await expect(cache.resolve(path.join(__FIXTURES__, 'test.ts'))).resolves.toBeUndefined(); | ||
expect(log.error).toHaveBeenCalled(); | ||
}); | ||
|
||
test('should handle an empty sourcemap', async () => { | ||
const cache = new FileNavigatorCache(); | ||
await cache.scanSourcemap(path.join(__FIXTURES__, 'empty')); | ||
await expect(cache.resolve(path.join(__FIXTURES__, 'test.ts'))).resolves.toBeUndefined(); | ||
}); | ||
|
||
test('should handle a simple sourcemap', async () => { | ||
const cache = new FileNavigatorCache(); | ||
await cache.scanSourcemap(path.join(__FIXTURES__, 'simple')); | ||
await expect(cache.resolve(path.join(__FIXTURES__, 'test.ts'))).resolves.toBeDefined(); | ||
}); | ||
|
||
test('should handle a sourcemap with a sourceRoot', async () => { | ||
const cache = new FileNavigatorCache(); | ||
await cache.scanSourcemap(path.join(__FIXTURES__, 'with-root')); | ||
await expect(cache.resolve('/home/user/project/test.ts')).resolves.toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import path from 'node:path'; | ||
import fs from 'node:fs/promises'; | ||
import type { SourceMapPayload } from 'node:module'; | ||
|
||
import { log } from '../logger'; | ||
|
||
import { FileNavigator } from './index'; | ||
|
||
export class FileNavigatorCache { | ||
#cache = new Map<string, Promise<FileNavigator | undefined>>(); | ||
|
||
resolve(filePath: string): Promise<FileNavigator | undefined> { | ||
const absolutePath = path.isAbsolute(filePath) ? filePath : path.resolve(filePath); | ||
if (!this.#cache.has(absolutePath)) { | ||
this.#cache.set(absolutePath, this.#createNavigator(absolutePath)); | ||
} | ||
|
||
return this.#cache.get(absolutePath)!; | ||
} | ||
|
||
async scanSourcemap(filePath: string): Promise<void> { | ||
const sourceMapPath = `${filePath}.map`; | ||
|
||
const doesNotExist = await fs.access(sourceMapPath).catch(() => true); | ||
if (doesNotExist) return; | ||
|
||
const sourceMapRaw = await fs.readFile(sourceMapPath, 'utf8').catch((error) => { | ||
log.error(error, `Failed to read sourcemap for: ${filePath}`); | ||
}); | ||
if (sourceMapRaw == null) return; | ||
|
||
let sourceMap: SourceMapPayload | undefined; | ||
try { | ||
sourceMap = JSON.parse(sourceMapRaw); | ||
} catch (error) { | ||
log.error(error, `Failed to parse sourcemap for: ${filePath}`); | ||
} | ||
if (!sourceMap) return; | ||
|
||
const { sourceRoot, sources, sourcesContent } = sourceMap; | ||
if (!sources || !sourcesContent) return; | ||
|
||
const baseDirectory = | ||
sourceRoot && path.isAbsolute(sourceRoot) ? sourceRoot : path.dirname(filePath); | ||
for (const [index, content] of sourcesContent.entries()) { | ||
const source = sources[index]; | ||
if (!content || !source) continue; | ||
|
||
const sourcePath = path.isAbsolute(source) ? source : path.resolve(baseDirectory, source); | ||
if (this.#cache.has(sourcePath)) continue; | ||
|
||
const navigator = new FileNavigator(content); | ||
this.#cache.set(sourcePath, Promise.resolve(navigator)); | ||
} | ||
} | ||
|
||
#createNavigator = async (filePath: string) => { | ||
const sourceCode = await fs.readFile(filePath, 'utf8').catch(() => void 0); | ||
return sourceCode == null ? undefined : new FileNavigator(sourceCode); | ||
}; | ||
|
||
clear() { | ||
this.#cache.clear(); | ||
} | ||
|
||
static readonly instance = new FileNavigatorCache(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"sources": ["test.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"sources": ["test.ts"], | ||
"sourcesContent": ["export default 42;"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"sources": ["test.ts"], | ||
"sourceRoot": "/home/user/project", | ||
"sourcesContent": ["export default 42;"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters