Skip to content

Commit

Permalink
feat(cli-repl): print index update violations
Browse files Browse the repository at this point in the history
  • Loading branch information
gribnoysup committed Nov 17, 2022
1 parent 1dbc89b commit fa3b62a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
13 changes: 13 additions & 0 deletions packages/cli-repl/src/format-output.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ for (const colors of [ false, true ]) {
expect(output).to.equal(
'\rError: Something went wrong.\nResult: { nInserted: 0 }');
});

it('provides violation info if present', () => {
const err = Object.assign(new Error('Something went wrong.'), {
violations: [{ ids: [1] }]
});
const output = stripAnsiColors(format({
value: err,
type: 'Error'
}));

expect(output).to.equal(
'\rError: Something went wrong.\nViolations: [ { ids: [ 1 ] } ]');
});
});

context('when the result is ShowDatabasesResult', () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/cli-repl/src/format-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ function formatListCommands(output: Record<string, any>, options: FormatOptions)
return tableEntries.join('\n\n');
}

// eslint-disable-next-line complexity
export function formatError(error: Error, options: FormatOptions): string {
let result = '';
if (error.name) result += `\r${clr(error.name, 'mongosh:error', options)}: `;
Expand Down Expand Up @@ -231,6 +232,10 @@ export function formatError(error: Error, options: FormatOptions): string {
result += `\n${clr(i18n.__('cli-repl.cli-repl.additionalErrorResult'), 'mongosh:additional-error-info', options)}: `;
result += inspect((error as any).result, options);
}
if ((error as any).violations) {
result += `\n${clr(i18n.__('cli-repl.cli-repl.additionalErrorViolations'), 'mongosh:additional-error-info', options)}: `;
result += inspect((error as any).violations, options);
}

return result;
}
Expand Down
39 changes: 39 additions & 0 deletions packages/cli-repl/test/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,45 @@ describe('e2e', function() {
expect(shell.output).to.match(
/multi update (only works with \$ operators|is not supported for replacement-style update)/);
});
context('when creating unique index', () => {
skipIfServerVersion(testServer, '< 5.3');

afterEach(async() => {
await shell.executeLine('db.apples.drop()');
});

it('prints out violations for unique index creation', async() => {
await shell.executeLine(`db.apples.insertMany([
{ type: 'Delicious', quantity: 12 },
{ type: 'Macintosh', quantity: 13 },
{ type: 'Delicious', quantity: 13 },
{ type: 'Fuji', quantity: 15 },
{ type: 'Washington', quantity: 10 }
]);`);

await shell.executeLine('db.apples.createIndex({ type: 1 });');

await shell.executeLine(`db.runCommand({
collMod: 'apples',
index: {
keyPattern: { type: 1 },
prepareUnique: true
}
});`);

const result = await shell.executeLine(`db.runCommand({
collMod: 'apples',
index: {
keyPattern: { type: 1 },
unique: true
}
});`);

expect(result).to.match(/Violations\:/);
// Two duplicated ids
expect(result).to.match(/ids: \[\s+ObjectId.+?\s+?ObjectId.+?\s+\]/m);
});
});
});
it('throws multiline input with a single line string', async() => {
// this is an unterminated string constant and should throw, since it does
Expand Down
3 changes: 2 additions & 1 deletion packages/i18n/src/locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ const translations: Catalog = {
link: 'https://docs.mongodb.com/mongodb-shell/'
},
additionalErrorInfo: 'Additional information',
additionalErrorResult: 'Result'
additionalErrorResult: 'Result',
additionalErrorViolations: 'Violations'
},
'uri-generator': {
'no-host-port': 'If a full URI is provided, you cannot also specify --host or --port',
Expand Down

0 comments on commit fa3b62a

Please sign in to comment.