Skip to content

Commit

Permalink
ci: run tests for GB locale (#107)
Browse files Browse the repository at this point in the history
* ci: run tests for GB locale

Signed-off-by: Miroslav Bajtoš <[email protected]>

* store timestamps as iso strings

* output human readable strings

* `isHumanReadable` -> `pretty`

* undo change

* `date` -> `timestamp` everywhere

* refactor

* de-trick-ify

---------

Signed-off-by: Miroslav Bajtoš <[email protected]>
Co-authored-by: Julian Gruber <[email protected]>
  • Loading branch information
bajtos and juliangruber authored May 2, 2023
1 parent 9df5a64 commit 4e56d9f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 20 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ jobs:
if: matrix.os != 'windows-latest'
- run: npm run test:unit

test-locales:
runs-on: ubuntu-latest
strategy:
matrix:
lang:
- en_GB.UTF-8
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- run: npm ci
- run: npm run test:unit
env:
LANG: ${{ matrix.lang }}

docker:
name: Build and test Docker
runs-on: ubuntu-latest
Expand Down
15 changes: 12 additions & 3 deletions commands/logs.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { followLogs, getLatestLogs } from '../lib/log.js'
import { followLogs, getLatestLogs, parseLog, formatLog } from '../lib/log.js'

export const logs = async ({ module, follow }) => {
if (follow) {
for await (const line of followLogs(module)) {
console.log(line)
const { text, timestamp } = parseLog(line)
process.stdout.write(formatLog(text, { timestamp, pretty: true }))
}
} else {
process.stdout.write(await getLatestLogs(module))
const lines = (await getLatestLogs(module))
.toString()
.trim()
.split('\n')
.filter(line => line !== '')
for (const line of lines) {
const { text, timestamp } = parseLog(line)
process.stdout.write(formatLog(text, { timestamp, pretty: true }))
}
}
}
12 changes: 6 additions & 6 deletions lib/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export const createActivityStream = source => {
}

const activityLogLineToObject = line => {
const { date, text } = parseLog(line)
const { timestamp, text } = parseLog(line)
const { type, message, source, id } = JSON.parse(text)
return { timestamp: date.toJSON(), type, source, message, id }
return { timestamp, type, source, message, id }
}

export const followActivity = async function * ({ signal, nLines = 10 } = {}) {
Expand All @@ -54,8 +54,8 @@ export const getActivity = async () => {
export const maybeCreateActivityFile = () => maybeCreateFile(paths.activity)

export const formatActivityObject = ({ type, message, timestamp }) => {
return formatLog(
`${type.toUpperCase().padEnd(5)} ${message}`,
new Date(timestamp)
)
return formatLog(`${type.toUpperCase().padEnd(5)} ${message}`, {
timestamp,
pretty: true
})
}
17 changes: 12 additions & 5 deletions lib/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@ import { Tail } from 'tail'
import { platform } from 'node:os'
import { on } from 'node:events'

export const formatLog = (text, date = new Date()) =>
text
export const formatLog = (
text,
{ timestamp = new Date(), pretty = false } = {}
) => {
const timestampFormatted = pretty
? timestamp.toLocaleString()
: timestamp.toISOString()
return text
.trimEnd()
.split(/\n/g)
.map(line => `[${date.toLocaleString()}] ${line}`)
.map(line => `[${timestampFormatted}] ${line}`)
.join('\n') + '\n'
}

export const parseLog = line => {
const [, date, text] = line.split(/\[([^\]]+)\] /)
return { date: new Date(date), text }
const [, timestamp, text] = /\[([^\]]+)\] (.+)/.exec(line)
return { timestamp: new Date(timestamp), text }
}

const createSingleLogStream = path => {
Expand Down
4 changes: 2 additions & 2 deletions test/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ describe('Activity', () => {
)
await fs.writeFile(
getPaths(CACHE_ROOT, STATE_ROOT).activity,
'[3/14/2023, 10:38:14 AM] {"source":"Saturn","type":"info","message":"beep boop"}\n'
'[2023-04-26T12:26:40.489Z] {"source":"Saturn","type":"info","message":"beep boop"}\n'
)
const { stdout } = await execa(
station,
['activity'],
{ env: { CACHE_ROOT, STATE_ROOT } }
)
assert.match(stdout, /3\/14\/2023/)
assert.match(stdout, /2023/)
assert.match(stdout, /beep boop/)
})
it('outputs activity json', async () => {
Expand Down
10 changes: 6 additions & 4 deletions test/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ describe('Logs', () => {
)
await fs.writeFile(
getPaths(CACHE_ROOT, STATE_ROOT).allLogs,
'[date] beep boop\n'
'[2023-04-26T12:42:23.562Z] beep boop\n'
)
const { stdout } = await execa(
station,
['logs'],
{ env: { CACHE_ROOT, STATE_ROOT } }
)
assert.strictEqual(stdout, '[date] beep boop')
assert.match(stdout, /2023/)
assert.match(stdout, /beep boop/)
})

describe('Follow', () => {
Expand All @@ -57,10 +58,11 @@ describe('Logs', () => {
once(ps.stdout, 'data'),
fs.writeFile(
getPaths(CACHE_ROOT, STATE_ROOT).allLogs,
'[date] beep boop\n'
'[2023-04-26T12:42:23.562Z] beep boop\n'
)
])
assert.strictEqual(data.toString(), '[date] beep boop\n')
assert.match(data.toString(), /2023/)
assert.match(data.toString(), /beep boop/)
ps.kill()
})
}
Expand Down

0 comments on commit 4e56d9f

Please sign in to comment.