Skip to content

Commit

Permalink
Merge pull request #79 from Code-Inspect/12-add-tests-for-all-existin…
Browse files Browse the repository at this point in the history
…g-actions

 Add tests for all existing actions
  • Loading branch information
EagleoutIce authored May 8, 2024
2 parents 70e1648 + 737521a commit 37999c1
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 35 deletions.
7 changes: 7 additions & 0 deletions .vscode-test.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { defineConfig } from '@vscode/test-cli';

export default defineConfig({
version: '1.60.0',
files: 'out/test/**/*.test.js',
workspaceFolder: './test-workspace',
extensionDevelopmentPath: './',
mocha: {
timeout: 300000,
slow: 1100
}
});
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@
"icon": "$(graph)",
"category": "flowR"
},
{
"command": "vscode-flowr.session.internal",
"title": "Start Local flowR Shell",
"icon": "$(terminal)",
"category": "flowR"
},
{
"command": "vscode-flowr.session.connect",
"title": "Connect to flowR Server",
Expand Down
2 changes: 1 addition & 1 deletion src/diagram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function registerDiagramCommands(context: vscode.ExtensionContext) {
if(activeEditor) {
const mermaid = await (await getFlowrSession()).retrieveDataflowMermaid(activeEditor.document)
if(mermaid) {
createWebview('flowr-dataflow', 'Dataflow Graph', mermaid)
return { mermaid, webview: createWebview('flowr-dataflow', 'Dataflow Graph', mermaid) }
}
}
}))
Expand Down
25 changes: 13 additions & 12 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ export async function activate(context: vscode.ExtensionContext) {
registerDiagramCommands(context)
registerSliceCommands(context)

context.subscriptions.push(
vscode.commands.registerCommand('vscode-flowr.session.connect', async() => {
await establishServerSession()
})
)
context.subscriptions.push(
vscode.commands.registerCommand('vscode-flowr.session.disconnect', () => {
if(flowrSession instanceof FlowrServerSession) {
destroySession()
}
})
)
context.subscriptions.push(vscode.commands.registerCommand('vscode-flowr.session.internal', async() => {
await establishInternalSession()
return flowrSession
}))
context.subscriptions.push(vscode.commands.registerCommand('vscode-flowr.session.connect', async() => {
await establishServerSession()
return flowrSession
}))
context.subscriptions.push(vscode.commands.registerCommand('vscode-flowr.session.disconnect', () => {
if(flowrSession instanceof FlowrServerSession) {
destroySession()
}
}))

context.subscriptions.push(
vscode.commands.registerCommand('vscode-flowr.report', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/flowr/internal-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class FlowrInternalSession implements FlowrSession {
// wait at most 1 second for the version, since the R shell doesn't let us know if the path
// we provided doesn't actually lead anywhere, or doesn't contain an R executable, etc.
let handle: NodeJS.Timeout
const timeout = new Promise<null>(resolve => handle = setTimeout(() => resolve(null), 1000))
const timeout = new Promise<null>(resolve => handle = setTimeout(() => resolve(null), 5000))
await Promise.race([this.shell.usedRVersion(), timeout]).then(version => {
clearTimeout(handle)
if(!version){
Expand Down
9 changes: 5 additions & 4 deletions src/selection-slicer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ class SelectionSlicer {
}

// Slice once at the current cursor position
async sliceSelectionOnce(): Promise<void> {
await this.update()
async sliceSelectionOnce(): Promise<string> {
return await this.update()
}

// Stop following the cursor and clear the selection slice output
Expand Down Expand Up @@ -111,10 +111,10 @@ class SelectionSlicer {
this.decos = undefined
}

protected async update(): Promise<void> {
protected async update(): Promise<string> {
const ret = await getSelectionSlice()
if(ret === undefined){
return
return ''
}
const provider = getReconstructionContentProvider()
const uri = this.makeUri()
Expand All @@ -132,6 +132,7 @@ class SelectionSlicer {
this.decos ||= makeSliceDecorationTypes()
await displaySlice(ret.editor, ret.sliceElements, this.decos)
this.decoratedEditors.push(ret.editor)
return ret.code
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { disposeActivePositionSlicer, getActivePositionSlicer, addCurrentPositio

export function registerSliceCommands(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.commands.registerCommand('vscode-flowr.slice.cursor', async() => {
await getSelectionSlicer().sliceSelectionOnce()
return await getSelectionSlicer().sliceSelectionOnce()
}))
context.subscriptions.push(vscode.commands.registerCommand('vscode-flowr.slice.clear', () => {
clearSliceOutput()
Expand All @@ -19,7 +19,7 @@ export function registerSliceCommands(context: vscode.ExtensionContext) {
}))

context.subscriptions.push(vscode.commands.registerCommand('vscode-flowr.slice.show.in.editor', async() => {
await showReconstructionInEditor()
return await showReconstructionInEditor()
}))

context.subscriptions.push(vscode.commands.registerCommand('vscode-flowr.slice.position', async() => {
Expand Down
20 changes: 20 additions & 0 deletions src/test/diagram.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as vscode from 'vscode'
import * as assert from 'assert'

import { activateExtension, openTestFile } from './test-util'

suite('diagram', () => {
suiteSetup(async() => {
await activateExtension()
})

test('dataflow', async() => {
await openTestFile('simple-example.R')
const result: {webview: vscode.WebviewPanel, mermaid: string} | undefined =
await vscode.commands.executeCommand('vscode-flowr.dataflow')
assert.ok(result)
assert.equal(result.webview.title, 'Dataflow Graph')
assert.ok(result.mermaid.startsWith('flowchart TD'))
})
})

15 changes: 0 additions & 15 deletions src/test/extension.test.ts

This file was deleted.

33 changes: 33 additions & 0 deletions src/test/slice.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as vscode from 'vscode'
import * as assert from 'assert'
import { activateExtension, openTestFile } from './test-util'

suite('slice', () => {
suiteSetup(async() => {
await activateExtension()
})

test('slice cursor', async() => {
await openTestFile('example.R', new vscode.Selection(7, 6, 7, 6))
const slice: string | undefined = await vscode.commands.executeCommand('vscode-flowr.slice.cursor')
assert.ok(slice)
assert.equal(slice, `
product <- 1
n <- 10
for(i in 1:(n - 1)) product <- product * i
`.trim())
})

test('reconstruct cursor', async() => {
await openTestFile('example.R', new vscode.Selection(7, 6, 7, 6))
const newEditor: vscode.TextEditor | undefined = await vscode.commands.executeCommand('vscode-flowr.slice.show.in.editor')
assert.ok(newEditor)
assert.equal(vscode.window.activeTextEditor, newEditor)
assert.ok(newEditor.document.fileName.endsWith('Selection Slice'))
assert.equal(newEditor.document.getText(), `
product <- 1
n <- 10
for(i in 1:(n - 1)) product <- product * i
`.trim())
})
})
30 changes: 30 additions & 0 deletions src/test/test-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// also see https://github.com/microsoft/vscode-extension-samples/blob/main/lsp-sample/client/src/test/helper.ts for various test helper samples

import * as vscode from 'vscode'
import * as assert from 'assert'
import * as path from 'path'
import type { FlowrInternalSession } from '../flowr/internal-session'

export async function activateExtension(): Promise<void> {
const ext = vscode.extensions.getExtension('code-Inspect.vscode-flowr')

assert.notEqual(ext, undefined, 'extension not found')

await assert.doesNotReject(async() => {
await ext?.activate()
}, 'extension activation failed')

// force start a local shell and wait, since there seem to be some async issues with commands
const session: FlowrInternalSession = await vscode.commands.executeCommand('vscode-flowr.session.internal')
assert.equal(session.state, 'active')
}

export async function openTestFile(name: string, selection?: vscode.Selection): Promise<vscode.TextEditor> {
const file = path.resolve(__dirname, '..', '..', 'test-workspace', name)
const doc = await vscode.workspace.openTextDocument(file)
const editor = await vscode.window.showTextDocument(doc)
if(selection) {
editor.selection = selection
}
return editor
}
3 changes: 3 additions & 0 deletions test-workspace/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"vscode-flowr.verboseLog": true
}
12 changes: 12 additions & 0 deletions test-workspace/example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
sum <- 0
product <- 1
w <- 7
n <- 10

for (i in 1:(n - 1)) {
sum <- sum + i + w
product <- product * i
}

cat("Sum:", sum, "\n")
cat("Product:", product, "\n")
2 changes: 2 additions & 0 deletions test-workspace/simple-example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
X <- 2
Y <- X + 1

0 comments on commit 37999c1

Please sign in to comment.