8000 reconfigure test runner · crabbedbushel/coderoad-vscode@284e1f7 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 284e1f7

Browse files
committed
reconfigure test runner
1 parent a92f866 commit 284e1f7

File tree

13 files changed

+170
-55
lines changed

13 files changed

+170
-55
lines changed

package-lock.json

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"watch": "tsc -watch -p ./"
3131
},
3232
"dependencies": {
33-
"jsdom": "^15.2.1"
33+
"jsdom": "^15.2.1",
34+
"tap-parser": "^10.0.1"
3435
},
3536
"devDependencies": {
3637
"@types/assert": "^1.4.3",

src/actions/runTest/index.ts

Whitespace-only changes.

src/actions/tutorialConfig.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as G from 'typings/graphql'
22
import * as vscode from 'vscode'
33
import * as git from '../services/git'
4-
import langaugeMap from '../editor/languageMap'
4+
import languageMap from '../editor/languageMap'
55

66
interface TutorialConfigParams {
77
config: G.TutorialConfig,
@@ -19,10 +19,27 @@ const tutorialConfig = async ({config, alreadyConfigured, }: TutorialConfigParam
1919
await git.setupRemote(config.repo.uri)
2020
}
2121

22+
vscode.commands.executeCommand('coderoad.config_test_runner', config.testRunner)
23+
24+
const fileFormats = config.testRunner.fileFormats
25+
26+
// verify if file test should run based on document saved
27+
const shouldRun = (document: vscode.TextDocument): boolean => {
28+
if (document.uri.scheme !== 'file') {
29+
return false
30+
}
31+
if (fileFormats && fileFormats.length) {
32+
const fileFormat: G.FileFormat = languageMap[document.languageId]
33+
if (!fileFormats.includes(fileFormat)) {
34+
return false
35+
}
36+
}
37+
return true
38+
}
39+
2240
// setup onSave hook
2341
vscode.workspace.onDidSaveTextDocument((document: vscode.TextDocument) => {
24-
const fileFormat: G.FileFormat = langaugeMap[document.languageId]
25-
if (document.uri.scheme === 'file' && config.fileFormats.includes(fileFormat)) {
42+
if (shouldRun(document)) {
2643
vscode.commands.executeCommand('coderoad.run_test')
2744
}
2845
})

src/editor/ReactWebView.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ class ReactWebView {
161161
}
162162
}
163163

164-
165164
// set CSP (content security policy) to grant permission to local files
166165
const cspMeta: HTMLMetaElement = document.createElement('meta')
167166
cspMeta.httpEquiv = 'Content-Security-Policy'

src/editor/commands.ts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import * as G from 'typings/graphql'
12
import * as vscode from 'vscode'
23
import ReactWebView from './ReactWebView'
3-
import runTest from '../actions/runTest'
4+
import createTestRunner, {Payload} from '../services/testRunner'
45

56
const COMMANDS = {
67
START: 'coderoad.start',
78
OPEN_WEBVIEW: 'coderoad.open_webview',
9+
CONFIG_TEST_RUNNER: 'coderoad.config_test_runner',
810
RUN_TEST: 'coderoad.run_test',
911
SET_CURRENT_STEP: 'coderoad.set_current_step',
1012
}
@@ -19,6 +21,7 @@ export const createCommands = ({extensionPath, workspaceState, workspaceRoot}: C
1921
// React panel webview
2022
let webview: any
2123
let currentStepId = ''
24+
let testRunner: any
2225

2326
return {
2427
// initialize
@@ -49,37 +52,38 @@ export const createCommands = ({extensionPath, workspaceState, workspaceRoot}: C
4952
// setup 1x1 horizontal layout
5053
webview.createOrShow()
5154
},
52-
[COMMANDS.SET_CURRENT_STEP]: ({stepId}: {stepId: string}) => {
53-
// NOTE: as async, may sometimes be inaccurate
54-
// set from last setup stepAction
55-
currentStepId = stepId
56-
},
57-
[COMMANDS.RUN_TEST]: (current: {stepId: string} | undefined, onSuccess: () => void) => {
58-
console.log('-------- command.run_test ------ ')
59-
// use stepId from client, or last set stepId
60-
const payload = {stepId: current ? current.stepId : currentStepId}
61-
runTest({
62-
onSuccess: () => {
55+
[COMMANDS.CONFIG_TEST_RUNNER]: (config: G.TutorialTestRunner) => {
56+
testRunner = createTestRunner(config, {
57+
onSuccess: (payload: Payload) => {
6358
// send test pass message back to client
64-
webview.send({type: 'TEST_PASS', payload})
65-
onSuccess()
6659
vscode.window.showInformationMessage('PASS')
60+
webview.send({type: 'TEST_PASS', payload})
6761
},
68-
onFail: () => {
62+
onFail: (payload: Payload) => {
6963
// send test fail message back to client
70-
webview.send({type: 'TEST_FAIL', payload})
7164
vscode.window.showWarningMessage('FAIL')
65+
webview.send({type: 'TEST_FAIL', payload})
7266
},
73-
onError: () => {
74-
console.log('COMMAND TEST_ERROR')
67+
onError: (payload: Payload) => {
7568
// send test error message back to client
7669
webview.send({type: 'TEST_ERROR', payload})
7770
},
78-
onRun: () => {
71+
onRun: (payload: Payload) => {
7972
// send test run message back to client
8073
webview.send({type: 'TEST_RUNNING', payload})
8174
}
8275
})
8376
},
77+
[COMMANDS.SET_CURRENT_STEP]: ({stepId}: Payload) => {
78+
// NOTE: as async, may sometimes be inaccurate
79+
// set from last setup stepAction
80+
currentStepId = stepId
81+
},
82+
[COMMANDS.RUN_TEST]: (current: Payload | undefined, onSuccess: () => void) => {
83+
console.log('-------- command.run_test ------ ')
84+
// use stepId from client, or last set stepId
85+
const payload: Payload = {stepId: current ? current.stepId : currentStepId}
86+
testRunner(payload, onSuccess)
87+
},
8488
}
8589
}

src/editor/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class Editor {
2929
}
3030

3131
private activateCommands = (): void => {
32-
3332
// set workspace root for node executions
3433
const workspaceRoots: vscode.WorkspaceFolder[] | undefined = vscode.workspace.workspaceFolders
3534
if (!workspaceRoots || !workspaceRoots.length) {
File renamed without changes.
Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
import node from '../../services/node'
2-
import {getOutputChannel} from './channel'
2+
import {getOutputChannel} from '../../editor/outputChannel'
3+
import parser from './parser'
34
import {setLatestProcess, isLatestProcess} from './throttle'
45

5-
// TODO: use tap parser to make it easier to support other test runners
6-
// TODO: how to load test runner parser
7-
// TODO: where to instantiate test runner
8-
6+
export interface Payload {
7+
stepId: string
8+
}
99

1010
interface Callbacks {
11-
onSuccess(): void
12-
onFail(): void
13-
onRun(): void
14-
onError(): void
11+
onSuccess(payload: Payload): void
12+
onFail(payload: Payload): void
13+
onRun(payload: Payload): void
14+
onError(payload: Payload): void
1515
}
1616

1717
interface TestRunnerConfig {
1818
command: string
19-
parser(output: string): Error | null
2019
}
2120

22-
export const createTestRunner = (config: TestRunnerConfig, callbacks: Callbacks) => {
21+
const createTestRunner = (config: TestRunnerConfig, callbacks: Callbacks) => {
2322

2423
const outputChannelName = 'TEST_OUTPUT'
2524

26-
return async () => {
25+
return async (payload: Payload, onSuccess?: () => void) => {
2726
console.log('------------------- run test ------------------')
2827

2928
// track processId to prevent multiple
3029
const processId = setLatestProcess()
3130
if (!isLatestProcess(processId)) {return }
3231

3332
// flag as running
34-
callbacks.onRun()
33+
callbacks.onRun(payload)
3534

3635
let result: {stdout: string | undefined, stderr: string | undefined}
3736
try {
@@ -45,7 +44,7 @@ export const createTestRunner = (config: TestRunnerConfig, callbacks: Callbacks)
4544
if (!stdout || !isLatestProcess(processId)) {return }
4645

4746
if (stderr) {
48-
callbacks.onError()
47+
callbacks.onError(payload)
4948

5049
// open terminal with error string
5150
const channel = getOutputChannel(outputChannelName)
@@ -55,15 +54,19 @@ export const createTestRunner = (config: TestRunnerConfig, callbacks: Callbacks)
5554
}
5655

5756
// pass or fail?
58-
const testsFailed = config.parser(stdout)
59-
if (testsFailed === null) {
60-
callbacks.onSuccess()
57+
const {ok} = parser(stdout)
58+
if (ok) {
59+
callbacks.onSuccess(payload)
60+
if (onSuccess) {onSuccess()}
6161
} else {
62+
// TODO: parse failure message
6263
// open terminal with failed test string
63-
const channel = getOutputChannel(outputChannelName)
64-
channel.show(false)
65-
channel.appendLine(testsFailed.message)
66-
callbacks.onFail()
64+
// const channel = getOutputChannel(outputChannelName)
65+
// channel.show(false)
66+
// channel.appendLine(testsFailed.message)
67+
callbacks.onFail(payload)
6768
}
6869
}
6970
}
71+
72+
export default createTestRunner

src/services/testRunner/parser.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import TapParser from 'tap-parser'
2+
3+
// https://github.com/tapjs/tap-parser#var-p--new-parseroptions-cb
4+
const options = {
5+
bail: true,
6+
}
7+
8+
const parser = new TapParser(options)
9+
10+
export default parser

0 commit comments

Comments
 (0)
0