-
Notifications
You must be signed in to change notification settings - Fork 92
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
R: Add snapshot tests for indentation #2577
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4c31d97
Set up indent testing for R extension
lionel- ca7941d
Document how to run tests
lionel- c3ff622
Add snapshot tests for indentation
lionel- 16fe7eb
Convert spaces to tabs consistently
lionel- 7f5e40e
Remove trailing whitespace in fences
lionel- 46f05ec
Use `EXTENSION_ROOT_DIR`
lionel- 5032416
Include original case in snapshot
lionel- File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Launch tests by running this from the repository root: | ||
|
||
```sh | ||
yarn test-extension -l positron-r | ||
``` |
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,96 @@ | ||
// From testUtils.ts in the typescript-language-feature extension | ||
// https://github.com/posit-dev/positron/blob/main/extensions/typescript-language-features/src/test/testUtils.ts | ||
|
||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as fs from 'fs'; | ||
import * as os from 'os'; | ||
import { join } from 'path'; | ||
import * as vscode from 'vscode'; | ||
|
||
export function rndName() { | ||
let name = ''; | ||
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
for (let i = 0; i < 10; i++) { | ||
name += possible.charAt(Math.floor(Math.random() * possible.length)); | ||
} | ||
return name; | ||
} | ||
|
||
export function createRandomFile(contents = '', fileExtension = 'txt'): Thenable<vscode.Uri> { | ||
return new Promise((resolve, reject) => { | ||
const tmpFile = join(os.tmpdir(), rndName() + '.' + fileExtension); | ||
fs.writeFile(tmpFile, contents, (error) => { | ||
if (error) { | ||
return reject(error); | ||
} | ||
|
||
resolve(vscode.Uri.file(tmpFile)); | ||
}); | ||
}); | ||
} | ||
|
||
export function deleteFile(file: vscode.Uri): Thenable<boolean> { | ||
return new Promise((resolve, reject) => { | ||
fs.unlink(file.fsPath, (err) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(true); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
export const CURSOR = '"<>"'; | ||
|
||
export async function withFileEditor( | ||
contents: string, | ||
fileExtension: string, | ||
run: (editor: vscode.TextEditor, doc: vscode.TextDocument) => Promise<void> | ||
): Promise<void> { | ||
const cursorIndex = contents.indexOf(CURSOR); | ||
const rawContents = contents.replace(CURSOR, ''); | ||
|
||
const file = await createRandomFile(rawContents, fileExtension); | ||
|
||
try { | ||
const doc = await vscode.workspace.openTextDocument(file); | ||
const editor = await vscode.window.showTextDocument(doc); | ||
|
||
if (cursorIndex >= 0) { | ||
const pos = doc.positionAt(cursorIndex); | ||
editor.selection = new vscode.Selection(pos, pos); | ||
} | ||
|
||
await run(editor, doc); | ||
|
||
if (doc.isDirty) { | ||
await doc.save(); | ||
} | ||
} finally { | ||
deleteFile(file); | ||
} | ||
} | ||
|
||
export const onDocumentChange = (doc: vscode.TextDocument): Promise<vscode.TextDocument> => { | ||
return new Promise<vscode.TextDocument>(resolve => { | ||
const sub = vscode.workspace.onDidChangeTextDocument(e => { | ||
if (e.document !== doc) { | ||
return; | ||
} | ||
sub.dispose(); | ||
resolve(e.document); | ||
}); | ||
}); | ||
}; | ||
|
||
export const type = async (document: vscode.TextDocument, text: string): Promise<vscode.TextDocument> => { | ||
const onChange = onDocumentChange(document); | ||
await vscode.commands.executeCommand('type', { text }); | ||
await onChange; | ||
return document; | ||
}; |
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,66 @@ | ||
import * as vscode from 'vscode'; | ||
import * as assert from 'assert'; | ||
import * as fs from 'fs'; | ||
import { CURSOR, type, withFileEditor } from './editor-utils'; | ||
import { EXTENSION_ROOT_DIR } from '../constants'; | ||
import { removeLeadingLines } from '../util'; | ||
|
||
const snapshotsFolder = `${EXTENSION_ROOT_DIR}/src/test/snapshots`; | ||
const snippetsPath = `${snapshotsFolder}/indentation-cases.R`; | ||
const snapshotsPath = `${snapshotsFolder}/indentation-snapshots.R`; | ||
|
||
// FIXME: This should normally be run as part of tests setup in `before()` but | ||
// it's somehow not defined | ||
async function init() { | ||
// Open workspace with custom configuration for snapshots. If you need | ||
// custom settings set them there via `config.update()`. | ||
const uri = vscode.Uri.file(snapshotsFolder); | ||
await vscode.commands.executeCommand('vscode.openFolder', uri, false); | ||
const config = vscode.workspace.getConfiguration(); | ||
|
||
// Prevents `ENOENT: no such file or directory` errors caused by us | ||
// deleting temporary editor files befor Code had the opportunity to | ||
// save the user history of these files. | ||
config.update('workbench.localHistory.enabled', false, vscode.ConfigurationTarget.Workspace); | ||
} | ||
|
||
suite('Indentation', () => { | ||
// This regenerates snapshots in place. If the snapshots differ from last | ||
// run, a failure is emitted. You can either commit the new output or discard | ||
// it if that's a bug to fix. | ||
test('Regenerate and check', async () => { | ||
await init(); | ||
const expected = fs.readFileSync(snapshotsPath, 'utf8'); | ||
const current = await regenerateIndentSnapshots(); | ||
|
||
// Update snapshot file | ||
fs.writeFileSync(snapshotsPath, current, 'utf8'); | ||
|
||
// Notify if snapshots were outdated | ||
assert.strictEqual(expected, current); | ||
}); | ||
}); | ||
|
||
async function regenerateIndentSnapshots() { | ||
const snippets = fs. | ||
readFileSync(snippetsPath, 'utf8'). | ||
split('# ---\n'); | ||
|
||
// Remove documentation snippet | ||
snippets.splice(0, 1); | ||
|
||
const snapshots: string[] = ['# File generated from `indentation-cases.R`.\n\n']; | ||
|
||
for (const snippet of snippets) { | ||
const bareSnippet = snippet.split('\n').slice(0, -1).join('\n'); | ||
|
||
await withFileEditor(snippet, 'R', async (_editor, doc) => { | ||
// Type one newline character to trigger indentation | ||
await type(doc, `\n${CURSOR}`); | ||
const snapshot = removeLeadingLines(doc.getText(), /^$|^#/); | ||
snapshots.push(bareSnippet + '\n# ->\n' + snapshot); | ||
}); | ||
} | ||
|
||
return snapshots.join('# ---\n'); | ||
} |
3 changes: 3 additions & 0 deletions
3
extensions/positron-r/src/test/snapshots/.vscode/settings.json
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 @@ | ||
{ | ||
"workbench.localHistory.enabled": false | ||
} |
82 changes: 82 additions & 0 deletions
82
extensions/positron-r/src/test/snapshots/indentation-cases.R
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,82 @@ | ||
# Indentation snapshots. Edit cases in `indentation-cases.R` and observe results | ||
# in `indentation-snapshots.R`. | ||
# | ||
# The cursor position is represented by a string containing angular brackets. A | ||
# newline is typed at that position, triggering indentation rules. The result is | ||
# saved in the snapshot file. | ||
# | ||
# Snippets are separated by `# ---`. This makes it possible to extract them and | ||
# process them separately to prevent interferences between test cases. | ||
|
||
# --- | ||
1 +"<>" | ||
|
||
# --- | ||
1 + | ||
2 +"<>" | ||
|
||
# --- | ||
data |>"<>" | ||
|
||
# --- | ||
data |> | ||
fn()"<>" | ||
|
||
# --- | ||
# https://github.com/posit-dev/positron/issues/1727 | ||
# FIXME | ||
data |> | ||
fn() | ||
"<>" | ||
|
||
# --- | ||
# https://github.com/posit-dev/positron/issues/1316 | ||
data |> | ||
fn() |>"<>" | ||
|
||
# --- | ||
# With trailing whitespace | ||
# https://github.com/posit-dev/positron/pull/1655#issuecomment-1780093395 | ||
data |> | ||
fn() |> "<>" | ||
|
||
# --- | ||
data |> | ||
fn1() |> | ||
fn2() |>"<>" | ||
|
||
# --- | ||
# FIXME | ||
data |> | ||
fn1() |> | ||
fn2( | ||
"arg" | ||
)"<>" | ||
|
||
# --- | ||
# https://github.com/posit-dev/positron-beta/discussions/46 | ||
# FIXME | ||
data |> | ||
fn("<>") | ||
|
||
# --- | ||
# FIXME | ||
{ | ||
fn(function() {}"<>") | ||
} | ||
|
||
# --- | ||
# FIXME | ||
{ | ||
fn(function() { | ||
# | ||
}"<>") | ||
} | ||
|
||
# --- | ||
for (i in NA) NULL"<>" | ||
|
||
# --- | ||
# https://github.com/posit-dev/positron/issues/1880 | ||
# FIXME | ||
for (i in 1) fn()"<>" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I like having a README