Skip to content

Commit f3d642c

Browse files
authored
feat(shell-api): disable line breaks in sh.status chunks MONGOSH-802 (#965)
This only works for environments using the Node.js `util.inspect()`, but most likely those environments are also the main issue here.
1 parent 6791bb8 commit f3d642c

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

packages/shell-api/src/helpers.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { AutoEncryptionOptions } from 'mongodb';
2323
import { shellApiType } from './enums';
2424
import type { AbstractCursor } from './abstract-cursor';
2525
import type ChangeStreamCursor from './change-stream-cursor';
26+
import { inspect } from 'util';
2627

2728
/**
2829
* Helper method to adapt aggregation pipeline options.
@@ -458,6 +459,17 @@ export async function getPrintableShardStatus(db: Database, verbose: boolean): P
458459
'on shard': chunk.shard,
459460
'last modified': chunk.lastmod
460461
} as any;
462+
// Displaying a full, multi-line output for each chunk is a bit verbose,
463+
// even if there are only a few chunks. Where supported, we use a custom
464+
// inspection function to inspect a copy of this object with an unlimited
465+
// line break length (i.e. all objects on a single line).
466+
Object.defineProperty(c, Symbol.for('nodejs.util.inspect.custom'), {
467+
value: function(depth: number, options: any): string {
468+
return inspect({ ...this }, { ...options, breakLength: Infinity });
469+
},
470+
writable: true,
471+
configurable: true
472+
});
461473
if (chunk.jumbo) c.jumbo = 'yes';
462474
chunksRes.push(c);
463475
});

packages/shell-api/src/shard.spec.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { UpdateResult } from './result';
1111
import { CliServiceProvider } from '../../service-provider-server';
1212
import { startTestCluster, skipIfServerVersion, skipIfApiStrict } from '../../../testing/integration-testing-hooks';
1313
import Database from './database';
14-
import { ObjectId } from 'mongodb';
14+
import { inspect } from 'util';
1515

1616
describe('Shard', () => {
1717
skipIfApiStrict();
@@ -685,7 +685,7 @@ describe('Shard', () => {
685685

686686
it('returns whatever serviceProvider.updateOne returns', async() => {
687687
serviceProvider.runCommandWithCheck.resolves({ ok: 1, msg: 'isdbgrid' });
688-
const oid = new ObjectId();
688+
const oid = new bson.ObjectId();
689689
const expectedResult = {
690690
matchedCount: 1,
691691
modifiedCount: 1,
@@ -744,7 +744,7 @@ describe('Shard', () => {
744744

745745
it('returns whatever serviceProvider.updateOne returns', async() => {
746746
serviceProvider.runCommandWithCheck.resolves({ ok: 1, msg: 'isdbgrid' });
747-
const oid = new ObjectId();
747+
const oid = new bson.ObjectId();
748748
const expectedResult = {
749749
matchedCount: 1,
750750
modifiedCount: 1,
@@ -919,7 +919,7 @@ describe('Shard', () => {
919919

920920
it('returns whatever serviceProvider.updateOne returns', async() => {
921921
serviceProvider.runCommandWithCheck.resolves({ ok: 1, msg: 'isdbgrid' });
922-
const oid = new ObjectId();
922+
const oid = new bson.ObjectId();
923923
const expectedResult = {
924924
matchedCount: 1,
925925
modifiedCount: 1,
@@ -977,7 +977,7 @@ describe('Shard', () => {
977977

978978
it('returns whatever serviceProvider.updateOne returns', async() => {
979979
serviceProvider.runCommandWithCheck.resolves({ ok: 1, msg: 'isdbgrid' });
980-
const oid = new ObjectId();
980+
const oid = new bson.ObjectId();
981981
const expectedResult = {
982982
matchedCount: 1,
983983
modifiedCount: 1,
@@ -1285,7 +1285,14 @@ describe('Shard', () => {
12851285
expect(original.key).to.equal('A');
12861286
expect(original.value).to.equal(10);
12871287

1288-
expect((await sh.status()).value.databases[1].collections[ns].chunkMetadata).to.have.lengthOf(1);
1288+
const collectionInfo = (await sh.status()).value.databases[1].collections[ns];
1289+
expect(collectionInfo.chunkMetadata).to.have.lengthOf(1);
1290+
const inspectedCollectionInfo = inspect(collectionInfo);
1291+
// Make sure that each individual chunk in the output is on a single line
1292+
expect(inspectedCollectionInfo).to.include('chunks: [\n' +
1293+
' { min: { key: MinKey() }, max: { key: MaxKey() }, ' +
1294+
`'on shard': '${collectionInfo.chunks[0]['on shard']}', 'last modified': Timestamp(0, 1) }\n` +
1295+
' ],\n');
12891296
});
12901297
});
12911298
describe('autosplit', () => {

0 commit comments

Comments
 (0)