-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Return whole lines when looking up source code
- Loading branch information
1 parent
ec341a8
commit 3182837
Showing
4 changed files
with
72 additions
and
11 deletions.
There are no files selected for viewing
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
35 changes: 35 additions & 0 deletions
35
packages/cli/tests/unit/rpc/explain/lookupSourceCode.spec.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,35 @@ | ||
import path from 'node:path'; | ||
|
||
import { fs, vol } from 'memfs'; | ||
|
||
import lookupSourceCode from '../../../../src/rpc/explain/lookupSourceCode'; | ||
|
||
describe('lookupSourceCode', () => { | ||
const directory = '/project'; | ||
const fileName = 'example.js'; | ||
const sourceCodeSnippet = 'line 1\nline 2\nline 3\nline 4\nline 5'; | ||
const filePath = path.join(directory, fileName); | ||
|
||
it('returns the correct snippet object', async () => { | ||
const snippet = await lookupSourceCode(directory, `${fileName}:2`); | ||
expect(snippet).toEqual({ | ||
content: sourceCodeSnippet, | ||
location: 'example.js:1-5', | ||
}); | ||
}); | ||
|
||
it('returns undefined for non-existent line', async () => { | ||
const snippet = await lookupSourceCode(directory, `${fileName}:10`); | ||
expect(snippet).toBeUndefined(); | ||
}); | ||
|
||
beforeEach(() => { | ||
vol.mkdirSync(directory, { recursive: true }); | ||
vol.writeFileSync(filePath, sourceCodeSnippet); | ||
}); | ||
|
||
afterEach(() => vol.reset()); | ||
}); | ||
|
||
jest.mock('fs', () => fs); | ||
jest.mock('fs/promises', () => fs.promises); |