Skip to content

Commit

Permalink
Progress removing pty
Browse files Browse the repository at this point in the history
  • Loading branch information
eternauta1337 committed Apr 3, 2024
1 parent 4c61254 commit 3c51725
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .mocharc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"timeout": 60000,
"require": ["test/setup.js"],
"recursive": true,
"bail": true,
"bail": false,
"ignore": ["../../fixture-projects/**/*"],
"spec": ["../../**/*.test.js"]
}
35 changes: 17 additions & 18 deletions packages/ethernaut-common/src/test/terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,6 @@ class Terminal {
this.output = ''
}

onData = (data) => {
console.log('DATA', data.toString())
const txt = data.toString().replace(ansiEscapeCodesPattern, '')
this.history += txt
this.output += txt
debug.log(`Terminal output: ${txt}`, 'terminal')
}

onError = (data) => {
throw new Error(`Terminal error, ${data}`)
}

async run(command, delay = 10000, killAfter = false) {
this.output = ''

Expand All @@ -44,10 +32,20 @@ class Terminal {
debug.log(`Running command: ${f} ${args.join(' ')}`, 'terminal')
this.process = spawn(f, args, { shell: true, stdio: 'pipe' })

if (this.process.stdout.listeners('data').length === 0)
this.process.stdout.on('data', this.onData)
if (this.process.stderr.listeners('error').length === 0)
this.process.stderr.on('error', this.onError)
const onData = (data) => {
const txt = data.toString().replace(ansiEscapeCodesPattern, '')
this.history += txt
this.output += txt
debug.log(`Terminal output: ${txt}`, 'terminal')
}

const onError = (data) => {
throw new Error(`Terminal error, ${data}`)
}

this.process.stdout.on('data', onData)
this.process.stderr.on('data', onData)
this.process.stderr.on('error', onError)

const completion = this._waitForCompletion().then(() => ({
type: 'completion',
Expand All @@ -61,8 +59,9 @@ class Terminal {
}

kill() {
this.process.stdout.removeListener('data', this.onData)
this.process.stderr.removeListener('data', this.onError)
this.process.stdout.removeAllListeners('data')
this.process.stderr.removeAllListeners('data')
this.process.stderr.removeAllListeners('error')
kill(this.process.pid, 'SIGKILL', (err) => {
if (err) debug.log(`Unable to kill process: ${err}`, 'terminal')
else debug.log('Killed process', 'terminal')
Expand Down
4 changes: 1 addition & 3 deletions packages/ethernaut-interact/test/tasks/contract.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const getBalance = require('../../src/internal/get-balance')
const { Terminal } = require('ethernaut-common/src/test/terminal')
const path = require('path')

describe.only('contract', function () {
describe('contract', function () {
const terminal = new Terminal()

describe('when interacting with a sample contract', function () {
Expand Down Expand Up @@ -104,8 +104,6 @@ describe.only('contract', function () {
await terminal.run(
`hardhat interact contract --address ${await sample.getAddress()} --no-confirm --abi ${abiPath} --fn incrementCounter --params 42`,
)
console.log(terminal.output)
process.exit(0)
})

itPrintsPreliminaryInfo()
Expand Down

0 comments on commit 3c51725

Please sign in to comment.