Skip to content

Commit 8aa513a

Browse files
authored
fix: History file does not exist (#1784)
* fix: Shell adapter throwing an error because history file doesn't exist * chore: Fix a test that is failing but not showing up as failed * chore: Add doc comments on scripts
1 parent b03aed1 commit 8aa513a

8 files changed

Lines changed: 80 additions & 11 deletions

File tree

.github/workflows/pipeline.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ jobs:
3636
- name: Verify the integrity of provenance attestations and registry signatures for installed dependencies
3737
run: npm audit signatures
3838
test:
39-
name: Fast Tests
39+
name: Tests
4040
runs-on: ubuntu-latest
4141
needs: build
4242
strategy:
4343
matrix:
4444
node-version:
45-
- 23.x
45+
- 24.x
4646
steps:
4747
- name: Checkout
4848
uses: actions/checkout@v4
@@ -54,7 +54,7 @@ jobs:
5454
node-version: ${{ matrix.node-version }}
5555
cache: 'npm'
5656
- run: npm ci
57-
- run: npm test --experimental-strip-types
57+
- run: npm test -- --experimental-strip-types
5858
e2etest:
5959
name: E2E Test
6060
needs:

configuration/Config.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
// Description:
2+
// Configuration
3+
//
4+
// Commands:
5+
//
6+
// Notes:
7+
// This is a test script.
8+
//
9+
110
export default async robot => {
211
robot.config = {}
312
}

src/adapters/Shell.mjs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
import fs from 'node:fs'
4+
import { stat, writeFile, unlink } from 'node:fs/promises'
45
import readline from 'node:readline'
56
import Adapter from '../Adapter.mjs'
67
import { TextMessage } from '../Message.mjs'
@@ -44,12 +45,15 @@ class Shell extends Adapter {
4445
}
4546

4647
async run () {
47-
if (!fs.existsSync(historyPath)) {
48-
fs.writeFileSync(historyPath, '')
49-
}
50-
const stats = fs.statSync(historyPath)
51-
if (stats.size > historySize) {
52-
fs.unlinkSync(historyPath)
48+
try {
49+
const stats = await stat(historyPath)
50+
if (stats.size > historySize) {
51+
await unlink(historyPath)
52+
await writeFile(historyPath, '')
53+
}
54+
} catch (error) {
55+
console.log(error)
56+
await writeFile(historyPath, '')
5357
}
5458

5559
this.#rl = readline.createInterface({

test/Robot_test.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,17 +247,21 @@ describe('Robot', () => {
247247

248248
it('stops processing if a listener marks the message as done', async () => {
249249
const testMessage = new TextMessage(user, 'message123')
250+
let spyCalled = false
250251

251252
const matchingListener = async response => {
252-
response.message.done = true
253-
assert.deepEqual(response.message, testMessage)
253+
response.message.finish()
254+
assert.equal(response.message.text, testMessage.text)
255+
assert.equal(response.message.user.id, testMessage.user.id)
254256
}
255257
const listenerSpy = async message => {
258+
spyCalled = true
256259
assert.fail('Should not have triggered listener')
257260
}
258261
robot.listen(() => true, null, matchingListener)
259262
robot.listen(() => true, null, listenerSpy)
260263
await robot.receive(testMessage)
264+
assert.equal(spyCalled, false)
261265
})
262266

263267
it('gracefully handles listener uncaughtExceptions (move on to next listener)', async () => {

test/Shell_test.mjs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,30 @@ import { describe, it, beforeEach, afterEach } from 'node:test'
44
import assert from 'node:assert/strict'
55
import { Robot, TextMessage, User } from '../index.mjs'
66
import stream from 'node:stream'
7+
import fs from 'node:fs'
8+
import { writeFile } from 'node:fs/promises'
9+
10+
describe('Shell history file test', () => {
11+
it('History file is > 1024 bytes when running does not throw an error', async () => {
12+
const robot = new Robot('Shell', false, 'TestHubot')
13+
robot.stdin = new stream.Readable()
14+
robot.stdin._read = () => {}
15+
const __dirname = new URL('.', import.meta.url).pathname
16+
const historyPath = `${__dirname}/../.hubot_history`
17+
await writeFile(historyPath, 'a'.repeat(1025))
18+
await robot.loadAdapter()
19+
await robot.run()
20+
assert.ok(fs.existsSync(historyPath), 'History file should exist')
21+
try {
22+
const stats = fs.statSync(historyPath)
23+
assert.ok(stats.size <= 1024, 'History file should be less than or equal to 1024 bytes after running the robot')
24+
} catch (error) {
25+
assert.fail('Should not throw an error when reading history file')
26+
} finally {
27+
robot.shutdown()
28+
}
29+
})
30+
})
731

832
describe('Shell Adapter Integration Test', () => {
933
let robot = null
@@ -51,6 +75,7 @@ describe('Shell Adapter Integration Test', () => {
5175
assert.deepEqual(wasCalled, false)
5276
})
5377
})
78+
5479
describe('Shell Adapter', () => {
5580
let robot = null
5681
beforeEach(async () => {

test/ordered-scripts/01-PFirst.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
// Description:
2+
// First one
3+
//
4+
// Commands:
5+
//
6+
// Notes:
7+
// This is a test script.
8+
//
9+
110
export default async robot => {
211
robot.loadedScripts = []
312
robot.loadedScripts.push('01-First')
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
// Description:
2+
// Setup bot configuration
3+
//
4+
// Commands:
5+
//
6+
// Notes:
7+
// This is a test script.
8+
//
9+
110
export default async robot => {
211
robot.loadedScripts.push('02-Second')
312
}

test/ordered-scripts/WebSetup.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
// Description:
2+
// Third one
3+
//
4+
// Commands:
5+
//
6+
// Notes:
7+
// This is a test script.
8+
//
9+
110
export default async robot => {
211
robot.loadedScripts.push('03-Third')
312
}

0 commit comments

Comments
 (0)