-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds the initial set of tests for our utils.
- Loading branch information
Showing
6 changed files
with
158 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
/node_modules | ||
/lib | ||
/dist | ||
/coverage | ||
*.swp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import {computeEnv} from '../env.js'; | ||
import * as assert from 'node:assert/strict'; | ||
import {test} from 'node:test'; | ||
import process from 'node:process'; | ||
|
||
test('computeEnv', async (t) => { | ||
await t.test('adds node binaries to path', () => { | ||
const env = computeEnv(process.cwd()); | ||
const path = env['PATH']!; | ||
|
||
assert.ok(path.includes('node_modules/.bin')); | ||
}); | ||
|
||
await t.test('extends process env', () => { | ||
const env = computeEnv(process.cwd(), { | ||
foo: 'bar' | ||
}); | ||
|
||
for (const key in process.env) { | ||
if (key !== 'PATH') { | ||
assert.equal(env[key], process.env[key]); | ||
} | ||
} | ||
|
||
assert.equal(env.foo, 'bar'); | ||
}); | ||
|
||
await t.test('supports case-insensitive path keys', () => { | ||
const originalPath = process.env['PATH']; | ||
try { | ||
delete process.env['PATH']; | ||
const env = computeEnv(process.cwd(), { | ||
Path: '/' | ||
}); | ||
const keys = [...Object.keys(env)]; | ||
|
||
assert.ok(keys.includes('Path')); | ||
assert.ok(!keys.includes('PATH')); | ||
} finally { | ||
process.env['PATH'] = originalPath; | ||
} | ||
}); | ||
|
||
await t.test('uses default key if empty path found', () => { | ||
const originalPath = process.env['PATH']; | ||
try { | ||
delete process.env['PATH']; | ||
const env = computeEnv(process.cwd(), { | ||
Path: undefined | ||
}); | ||
|
||
assert.ok(typeof env['PATH'] === 'string'); | ||
assert.equal(env['Path'], undefined); | ||
} finally { | ||
process.env['PATH'] = originalPath; | ||
} | ||
}); | ||
|
||
await t.test('uses default key if no path found', () => { | ||
const originalPath = process.env['PATH']; | ||
try { | ||
delete process.env['PATH']; | ||
const env = computeEnv(process.cwd()); | ||
|
||
assert.ok(typeof env['PATH'] === 'string'); | ||
} finally { | ||
process.env['PATH'] = originalPath; | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import {combineStreams, waitForEvent, readStreamAsString} from '../stream.js'; | ||
import * as assert from 'node:assert/strict'; | ||
import {test} from 'node:test'; | ||
import {EventEmitter} from 'node:events'; | ||
import {Readable} from 'node:stream'; | ||
|
||
test('waitForEvent', async (t) => { | ||
await t.test('waits for event to fire', async () => { | ||
const emitter = new EventEmitter(); | ||
const waiter = waitForEvent(emitter, 'foo'); | ||
emitter.emit('foo'); | ||
await waiter; | ||
}); | ||
}); | ||
|
||
test('readStreamAsString', async (t) => { | ||
await t.test('rejects on error', async () => { | ||
const streamError = new Error('fudge'); | ||
const stream = new Readable({ | ||
read() { | ||
this.destroy(streamError); | ||
} | ||
}); | ||
try { | ||
await readStreamAsString(stream); | ||
assert.fail('expected to throw'); | ||
} catch (err) { | ||
assert.equal(err, streamError); | ||
} | ||
}); | ||
|
||
await t.test('resolves to concatenated data', async () => { | ||
const stream = Readable.from(['foo', 'bar']); | ||
const result = await readStreamAsString(stream); | ||
assert.equal(result, 'foobar'); | ||
}); | ||
|
||
await t.test('handles buffer data', async () => { | ||
const stream = new Readable({ | ||
read() { | ||
this.push(Buffer.from('foo')); | ||
this.push(Buffer.from('bar')); | ||
this.push(null); | ||
} | ||
}); | ||
const result = await readStreamAsString(stream); | ||
assert.equal(result, 'foobar'); | ||
}); | ||
}); | ||
|
||
test('combineStreams', async (t) => { | ||
await t.test('works with a single stream', async () => { | ||
const stream = Readable.from(['foo', 'bar']); | ||
const combined = combineStreams([stream]); | ||
const chunks: string[] = []; | ||
combined.on('data', (chunk: Buffer) => { | ||
chunks.push(chunk.toString()); | ||
}); | ||
await waitForEvent(combined, 'end'); | ||
assert.deepEqual(chunks, ['foo', 'bar']); | ||
}); | ||
|
||
await t.test('works with multiple streams', async () => { | ||
const stream0 = Readable.from(['foo']); | ||
const stream1 = Readable.from(['bar', 'baz']); | ||
const combined = combineStreams([stream0, stream1]); | ||
const chunks: string[] = []; | ||
combined.on('data', (chunk: Buffer) => { | ||
chunks.push(chunk.toString()); | ||
}); | ||
await waitForEvent(combined, 'end'); | ||
assert.deepEqual(chunks, ['foo', 'bar', 'baz']); | ||
}); | ||
}); |