Skip to content
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

[115] tests keep running and running tests number increases #130

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Config } from '@jest/types'
export default async (): Promise<Config.InitialOptions> => {
return {
bail: 1,
forceExit: true,
modulePathIgnorePatterns: ['dist', '.vscode-test'],
transform: {
'^.+\\.tsx?$': [
Expand Down
27 changes: 14 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
"command": "pester.toggleAutoRunOnSave",
"title": "Pester: Toggle Auto Run on Save",
"category": "PowerShell"
},
{
"command": "pester.stopPowerShell",
"title": "Pester: Stop PowerShell background process",
"category": "PowerShell"
}
],
"menus": {
Expand Down Expand Up @@ -128,7 +133,7 @@
"ts-node": "^10.7.0",
"typescript": "^4.6.2",
"utility-types": "^3.10.0",
"vsce": "^2.7.0",
"vsce": "^2.15.0",
"vscode-dts": "^0.3.3"
},
"dependencies": {
Expand Down
17 changes: 16 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,24 @@ export async function activate(context: ExtensionContext) {
})
}

const controller = new PesterTestController(powershellExtension, context)
const stopPowerShellCommand = commands.registerCommand(
'pester.stopPowershell',
() => {
if (controller.stopPowerShell()) {
window.showInformationMessage('PowerShell background process stopped.')
} else {
window.showWarningMessage(
'No PowerShell background process was running !'
)
}
}
)

context.subscriptions.push(
new PesterTestController(powershellExtension, context),
controller,
toggleAutoRunOnSaveCommand,
stopPowerShellCommand,
autoRunStatusBarItem,
autoRunStatusBarVisibleEvent,
updateAutoRunStatusBarOnConfigChange
Expand Down
11 changes: 11 additions & 0 deletions src/pesterTestController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ export class PesterTestController implements Disposable {
log.warn(
`Detected PowerShell Session change from ${this.ps.exePath} to ${exePath}. Restarting Pester Runner.`
)
this.ps.reset()
}
const exePathDir = exePath
? dirname(exePath)
Expand All @@ -520,6 +521,9 @@ export class PesterTestController implements Disposable {
// Objects from the run will return to the success stream, which we then send to the return handler
const psOutput = new PSOutput()
psOutput.success.on('data', returnHandler)
psOutput.success.on('close', () => {
testRun?.end()
})

if (usePSIC) {
log.debug('Running Script in PSIC:', scriptPath, scriptArgs)
Expand Down Expand Up @@ -712,6 +716,13 @@ export class PesterTestController implements Disposable {
return testItems
}

stopPowerShell(): boolean {
if (this.ps !== undefined) {
return this.ps.reset()
}
return false
}

dispose() {
this.testController.dispose()
this.returnServer.dispose()
Expand Down
13 changes: 9 additions & 4 deletions src/powershell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { execSync } from 'child_process'
import ReadlineTransform from 'readline-transform'
import { Readable } from 'stream'
import { pipeline } from 'stream/promises'
import { createJsonParseTransform, PowerShell, PSOutput } from './powershell'
import {
createJsonParseTransform,
PowerShell,
PSOutput,
defaultPowershellExePath
} from './powershell'

// jest.setTimeout(30000)

Expand Down Expand Up @@ -79,8 +84,8 @@ describe('run', () => {

it('mixed', async () => {
expect.assertions(3)
const successResult = []
const infoResult = []
const successResult: any[] = []
const infoResult: any[] = []
const streams = new PSOutput()
streams.success
.on('data', data => {
Expand Down Expand Up @@ -152,7 +157,7 @@ describe('exec', () => {
})

it('pwsh baseline', () => {
const result = execSync('pwsh -nop -c "echo hello"')
const result = execSync(`${defaultPowershellExePath} -nop -c "echo hello"`)
expect(result.toString()).toMatch('hello')
})

Expand Down
22 changes: 16 additions & 6 deletions src/powershell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ export function createSplitPSOutputStream(streams: IPSOutput) {
})
}

export const defaultPowershellExePath =
process.platform === 'win32'
? 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe'
: 'pwsh'

/** Represents an instance of a PowerShell process. By default this will use pwsh if installed, and will fall back to PowerShell on Windows,
* unless the exepath parameter is specified. Use the exePath parameter to specify specific powershell executables
* such as pwsh-preview or a pwsh executable not located in the PATH
Expand All @@ -165,7 +170,8 @@ export class PowerShell {
if (path !== undefined) {
this.resolvedExePath = path
} else if (process.platform === 'win32') {
this.resolvedExePath = 'powershell'
this.resolvedExePath =
'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe'
} else {
throw new Error(
'pwsh not found in your path and you are not on Windows so PowerShell 5.1 is not an option. Did you install PowerShell first?'
Expand Down Expand Up @@ -299,7 +305,10 @@ export class PowerShell {
async exec(script: string, cancelExisting?: boolean) {
const psOutput = new PSOutputUnified()
await this.run(script, psOutput, undefined, cancelExisting)
await finished(psOutput.success)

if (!psOutput.success.destroyed) {
await finished(psOutput.success)
}
const result: Record<string, unknown>[] = []
for (;;) {
const output = psOutput.success.read() as Record<string, unknown>
Expand All @@ -322,18 +331,19 @@ export class PowerShell {
}

/** Kill any existing invocations and reset the state */
reset() {
reset(): boolean {
let result = false
if (this.psProcess !== undefined) {
// We use SIGKILL to keep the behavior consistent between Windows and Linux (die immediately)
this.psProcess.kill('SIGKILL')
result = true
}
// Initialize will reinstate the process upon next call
this.psProcess = undefined
return result
}

dispose() {
if (this.psProcess !== undefined) {
this.psProcess.kill()
}
this.reset()
}
}