Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Feb 7, 2025
1 parent 7b54b58 commit 8aaef44
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
17 changes: 1 addition & 16 deletions src/read-from-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,6 @@ import { parseChunked } from '@discoveryjs/json-ext';
import * as clap from 'clap';

const now = typeof performace !== 'undefined' && typeof performance.now === 'function' ? performance.now : Date.now;
const concatChunks = typeof Buffer === 'function' && typeof Buffer.concat === 'function'
? Buffer.concat
: function bufferCocnatPolyfill(chunks, totalLength) {
// Create a new TypedArray with the combined length
const combinedChunks = new Uint8Array(totalLength);

// Iterate through the input arrays and set their values in the combinedChunks array
let offset = 0;
for (const array of chunks) {
combinedChunks.set(array, offset);
offset += array.length;
}

return combinedChunks;
};

async function readFromStream(stream, totalSize, setStageProgress = async () => {}) {
const CHUNK_SIZE = 10 * 1024 * 1024; // 10MB
Expand Down Expand Up @@ -69,7 +54,7 @@ async function readFromStream(stream, totalSize, setStageProgress = async () =>
}

// Concat chunks
return concatChunks(chunks, size);
return Buffer.concat(chunks, size);
}

async function* streamConsumer(firstChunk, iterator) {
Expand Down
58 changes: 57 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from 'assert';
import path from 'path';
import fs from 'fs';
import tempfile from 'tempfile';
import { spawn } from 'child_process';
import { fileURLToPath } from 'url';
import { parseJsonxl, style } from './helpers.js';
Expand Down Expand Up @@ -50,6 +51,7 @@ function runWithForceColors(...args) {

function runCli(forceColors, cliArgs) {
let assertStdout = () => {};
let assertStderr = null;
let error = '';
const args = [path.join(__dirname, '../bin/jora')].concat(cliArgs);
const child = spawn('node', args, {
Expand All @@ -58,12 +60,13 @@ function runCli(forceColors, cliArgs) {
});
const wrapper = new Promise(function(resolve, reject) {
child.once('exit', (code) => {
if (error || code) {
if ((error && !assertStderr) || code) {
reject(new Error(error || 'Process exit with code' + code));
return;
}

assertStdout();
assertStderr?.();
resolve();
});
});
Expand All @@ -83,6 +86,27 @@ function runCli(forceColors, cliArgs) {

if (typeof expected === 'function') {
expected(data);
} else if (expected instanceof RegExp) {
assert.match(data, expected);
} else {
assert.equal(data, expected);
}
};

return wrapper;
};

wrapper.stderr = function(expected) {
const buffer = [];

child.stderr.on('data', chunk => buffer.push(chunk));
assertStderr = function() {
const data = buffer.join('').trim();

if (typeof expected === 'function') {
expected(data);
} else if (expected instanceof RegExp) {
assert.match(data, expected);
} else {
assert.equal(data, expected);
}
Expand Down Expand Up @@ -140,6 +164,31 @@ it('should read from file', () =>
.output(JSON.stringify(fixture.data.version))
);

it('should write to file', async () => {
const filename = tempfile({ extension: '.json' });

await run('-o', filename, '-q', 'version')
.input(fixture.string)
.output('');

assert(fs.existsSync(filename));
assert.strictEqual(fs.readFileSync(filename, 'utf8'), JSON.stringify(fixture.data.version));
});

it('--verbose', () =>
run('--verbose', 'version')
.input(fixture.string)
.output(JSON.stringify(fixture.data.version))
.stderr(/Input from/)
);

it('--dry-run', () =>
run('--dry-run', 'version')
.input(fixture.string)
.output('')
.stderr(/Input from/)
);

describe('query from a file', () => {
it('as arg', () =>
run(queryFilename)
Expand Down Expand Up @@ -177,6 +226,13 @@ describe('jsonxl', () => {
.input(fixtureJsonxl.string)
.output(fixtureJsonxl.raw)
);

it('--dry-run jsonxl output', () =>
run('--dry-run', 'version', '-e', 'jsonxl')
.input(fixture.string)
.output('')
.stderr(/Encoded jsonxl/)
);
});

describe('pretty print', function() {
Expand Down

0 comments on commit 8aaef44

Please sign in to comment.