226 lines
5.9 KiB
JavaScript
226 lines
5.9 KiB
JavaScript
/**
|
|
* CI smoke checks for awk/sed sources (POSIX-oriented coverage is partial by design).
|
|
*/
|
|
import { readFile } from 'node:fs/promises'
|
|
import { dirname, join } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
import test from 'brittle'
|
|
import { bareAwkRun } from '../lib/engines/awk-engine.js'
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
const srcDir = join(__dirname, '../src')
|
|
const fixtureDir = join(__dirname, 'fixtures')
|
|
|
|
test('sed.js exists and documents line-oriented operation', async (t) => {
|
|
const s = await readFile(join(srcDir, 'sed.js'), 'utf8')
|
|
t.ok(s.includes('async function run'))
|
|
t.ok(s.length > 200)
|
|
})
|
|
|
|
test('awk.js exists and wires field parsing', async (t) => {
|
|
const s = await readFile(join(srcDir, 'awk.js'), 'utf8')
|
|
t.ok(s.includes('async function run'))
|
|
t.ok(s.includes('field-separator') || s.includes('split'))
|
|
})
|
|
|
|
test('awk-engine supports delete arr[key] statement', async (t) => {
|
|
const s = await readFile(join(__dirname, '../lib/engines/awk-engine.js'), 'utf8')
|
|
t.ok(s.includes("id === 'delete'"))
|
|
t.ok(s.includes("case 'delete':"))
|
|
})
|
|
|
|
test('printf FORMAT octal escape helper is defined', async (t) => {
|
|
const s = await readFile(join(srcDir, 'printf.js'), 'utf8')
|
|
t.ok(s.includes('barePrintfUnescapeFormat'))
|
|
})
|
|
|
|
test('awk nextfile skips remainder of first file', async (t) => {
|
|
/** @type {string[]} */
|
|
const lines = []
|
|
const enc = new TextEncoder()
|
|
const io = {
|
|
stdinLines: [],
|
|
bytesToString(buf) {
|
|
return buf ? new TextDecoder().decode(buf) : ''
|
|
},
|
|
print(s) {
|
|
lines.push(String(s).replace(/\n$/, ''))
|
|
},
|
|
async writeFile() {},
|
|
readFile(p) {
|
|
if (p === 'a') return enc.encode('A1\nA2\nA3\n')
|
|
if (p === 'b') return enc.encode('B1\nB2\n')
|
|
return null
|
|
}
|
|
}
|
|
await bareAwkRun(
|
|
'FNR==2 && FILENAME == "a" { nextfile } { print $0 }',
|
|
{ fs: ' ', argc: 3, argv: ['awk', 'a', 'b'], environ: {} },
|
|
io
|
|
)
|
|
t.is(lines.join('\n'), 'A1\nB1\nB2')
|
|
})
|
|
|
|
test('awk getline from file reads lines', async (t) => {
|
|
/** @type {string[]} */
|
|
const lines = []
|
|
const enc = new TextEncoder()
|
|
const io = {
|
|
stdinLines: [],
|
|
bytesToString(buf) {
|
|
return buf ? new TextDecoder().decode(buf) : ''
|
|
},
|
|
print(s) {
|
|
lines.push(String(s).replace(/\n$/, ''))
|
|
},
|
|
async writeFile() {},
|
|
readFile(p) {
|
|
if (p === 'data.txt') return enc.encode('one\ntwo\n')
|
|
return null
|
|
}
|
|
}
|
|
await bareAwkRun(
|
|
'BEGIN { while ((getline x < "data.txt") > 0) print x }',
|
|
{ fs: ' ', argc: 1, argv: ['awk'], environ: {} },
|
|
io
|
|
)
|
|
t.is(lines.join('\n'), 'one\ntwo')
|
|
})
|
|
|
|
test('awk getline missing file returns 0 and does not throw', async (t) => {
|
|
/** @type {string[]} */
|
|
const lines = []
|
|
const io = {
|
|
stdinLines: [],
|
|
bytesToString(buf) {
|
|
return buf ? new TextDecoder().decode(buf) : ''
|
|
},
|
|
print(s) {
|
|
lines.push(String(s).replace(/\n$/, ''))
|
|
},
|
|
async writeFile() {},
|
|
readFile() {
|
|
return null
|
|
}
|
|
}
|
|
await bareAwkRun(
|
|
'BEGIN { print (getline x < "missing.txt"); print x }',
|
|
{ fs: ' ', argc: 1, argv: ['awk'], environ: {} },
|
|
io
|
|
)
|
|
t.is(lines[0], '0')
|
|
t.is(lines[1], '')
|
|
})
|
|
|
|
test('awk getline repeated reads preserve per-file cursor', async (t) => {
|
|
/** @type {string[]} */
|
|
const lines = []
|
|
const enc = new TextEncoder()
|
|
const io = {
|
|
stdinLines: [],
|
|
bytesToString(buf) {
|
|
return buf ? new TextDecoder().decode(buf) : ''
|
|
},
|
|
print(s) {
|
|
lines.push(String(s).replace(/\n$/, ''))
|
|
},
|
|
async writeFile() {},
|
|
readFile(p) {
|
|
if (p === 'data.txt') return enc.encode('one\ntwo\nthree\n')
|
|
return null
|
|
}
|
|
}
|
|
await bareAwkRun(
|
|
'BEGIN { getline a < "data.txt"; getline b < "data.txt"; print a; print b }',
|
|
{ fs: ' ', argc: 1, argv: ['awk'], environ: {} },
|
|
io
|
|
)
|
|
t.is(lines[0], 'one')
|
|
t.is(lines[1], 'two')
|
|
})
|
|
|
|
test('awk getline handles larger VFS input boundaries', async (t) => {
|
|
/** @type {string[]} */
|
|
const lines = []
|
|
const big = 'x'.repeat(8192)
|
|
const enc = new TextEncoder()
|
|
const io = {
|
|
stdinLines: [],
|
|
bytesToString(buf) {
|
|
return buf ? new TextDecoder().decode(buf) : ''
|
|
},
|
|
print(s) {
|
|
lines.push(String(s).replace(/\n$/, ''))
|
|
},
|
|
async writeFile() {},
|
|
readFile(p) {
|
|
if (p === 'big.txt') return enc.encode(`${big}\nend\n`)
|
|
return null
|
|
}
|
|
}
|
|
await bareAwkRun(
|
|
'BEGIN { getline a < "big.txt"; getline b < "big.txt"; print length(a); print b }',
|
|
{ fs: ' ', argc: 1, argv: ['awk'], environ: {} },
|
|
io
|
|
)
|
|
t.is(lines[0], '8192')
|
|
t.is(lines[1], 'end')
|
|
})
|
|
|
|
test('sed Open Group Issue 7 golden fixture is present', async (t) => {
|
|
const raw = await readFile(join(fixtureDir, 'sed-open-group-issue7.json'), 'utf8')
|
|
const parsed = JSON.parse(raw)
|
|
t.ok(Array.isArray(parsed.examples))
|
|
t.ok(parsed.examples.length >= 3)
|
|
t.ok(parsed.examples.some((e) => e.script === 's/[0-9][0-9]*/NUM/g'))
|
|
})
|
|
|
|
test('awk atan2 and unary minus', async (t) => {
|
|
/** @type {string[]} */
|
|
const lines = []
|
|
const io = {
|
|
stdinLines: [''],
|
|
bytesToString() {
|
|
return ''
|
|
},
|
|
print(s) {
|
|
lines.push(String(s).replace(/\n$/, ''))
|
|
},
|
|
async writeFile() {},
|
|
async readFile() {
|
|
return null
|
|
}
|
|
}
|
|
await bareAwkRun(
|
|
'BEGIN { print int(atan2(0,-1)*1000); print -3+1 }',
|
|
{ fs: ' ', argc: 1, argv: ['awk'], environ: {} },
|
|
io
|
|
)
|
|
t.ok(lines[0].includes('3141') || lines[0] === '3141' || Number(lines[0]) > 3000)
|
|
t.is(lines[1], '-2')
|
|
})
|
|
|
|
test('awk substr two-arg is suffix from start', async (t) => {
|
|
/** @type {string[]} */
|
|
const lines = []
|
|
const io = {
|
|
stdinLines: [''],
|
|
bytesToString() {
|
|
return ''
|
|
},
|
|
print(s) {
|
|
lines.push(String(s).replace(/\n$/, ''))
|
|
},
|
|
async writeFile() {},
|
|
async readFile() {
|
|
return null
|
|
}
|
|
}
|
|
await bareAwkRun(
|
|
'BEGIN { print substr("abcdef", 3) }',
|
|
{ fs: ' ', argc: 1, argv: ['awk'], environ: {} },
|
|
io
|
|
)
|
|
t.is(lines[0], 'cdef')
|
|
})
|