From db3cf94d602b186ed716706a5b8e5f5be1209ac0 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 3 Oct 2025 15:22:58 +0200 Subject: [PATCH 1/2] fix(java-shell): make tests run and pass again These have been broken since 684002ce00, silently failing in CI. This commit re-enables failing on a broken test and fixes the tests that have started to fail since then. --- packages/java-shell/src/main/js/all.js | 8 +++++++- packages/java-shell/src/test/js/run-tests.ts | 3 +++ .../aggregateWithNegativeBatchSize.expected.4.txt | 1 + .../src/test/resources/db/help.expected.txt | 4 ++-- .../src/test/resources/literal/ISODate.js | 2 +- .../src/test/resources/literal/long.expected.txt | 4 ++-- .../java-shell/src/test/resources/literal/long.js | 2 +- packages/shell-api/src/collection.ts | 7 ++++++- packages/shell-api/src/shell-instance-state.ts | 13 ++++++++++--- 9 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 packages/java-shell/src/test/resources/collection/aggregateWithNegativeBatchSize.expected.4.txt diff --git a/packages/java-shell/src/main/js/all.js b/packages/java-shell/src/main/js/all.js index 9a3bb50095..242e2da72a 100644 --- a/packages/java-shell/src/main/js/all.js +++ b/packages/java-shell/src/main/js/all.js @@ -24,9 +24,15 @@ require('../../../../service-provider-core'); // Ensure TextEncoder polyfill is const ShellApi = require('../../../../shell-api/'); const ShellEvaluator = require('../../../../shell-evaluator/').default; +class ShellInstanceState extends ShellApi.ShellInstanceState { + constructor(sp, bus, cliOptions) { + super(sp, bus, cliOptions, require('bson')); + } +} + /** temporal object that is used to access symbols from closures generated by browserify */ _global = { - ShellInstanceState: ShellApi.ShellInstanceState, + ShellInstanceState, ShellEvaluator: ShellEvaluator, toShellResult: ShellApi.toShellResult, getShellApiType: ShellApi.getShellApiType, diff --git a/packages/java-shell/src/test/js/run-tests.ts b/packages/java-shell/src/test/js/run-tests.ts index f4e14bd14a..ccbfa02a03 100644 --- a/packages/java-shell/src/test/js/run-tests.ts +++ b/packages/java-shell/src/test/js/run-tests.ts @@ -33,6 +33,9 @@ describe('java-shell tests', function() { proc = spawn('.\\gradlew.bat test --info', [], opts); } await once(proc, 'exit'); + if (proc.exitCode !== 0) { + throw new Error(`java-shell tests failed (exit code ${proc.exitCode}, signal ${proc.signalCode})`); + } }); }); diff --git a/packages/java-shell/src/test/resources/collection/aggregateWithNegativeBatchSize.expected.4.txt b/packages/java-shell/src/test/resources/collection/aggregateWithNegativeBatchSize.expected.4.txt new file mode 100644 index 0000000000..89c534fae5 --- /dev/null +++ b/packages/java-shell/src/test/resources/collection/aggregateWithNegativeBatchSize.expected.4.txt @@ -0,0 +1 @@ +com.mongodb.MongoCommandException: Command failed with error 2 (BadValue): 'BSON field 'batchSize' value must be >= 0, actual value '-1'' on server %mongohostport%. The full response is {"ok": 0.0, "errmsg": "BSON field 'batchSize' value must be >= 0, actual value '-1'", "code": 2, "codeName": "BadValue"} diff --git a/packages/java-shell/src/test/resources/db/help.expected.txt b/packages/java-shell/src/test/resources/db/help.expected.txt index fe42d8bdfe..7958090eeb 100644 --- a/packages/java-shell/src/test/resources/db/help.expected.txt +++ b/packages/java-shell/src/test/resources/db/help.expected.txt @@ -1,4 +1,4 @@ ArrayResult ArrayResult -"use" -"use" \ No newline at end of file +"log" +"log" diff --git a/packages/java-shell/src/test/resources/literal/ISODate.js b/packages/java-shell/src/test/resources/literal/ISODate.js index 2a60a1b379..4d57f58ec0 100644 --- a/packages/java-shell/src/test/resources/literal/ISODate.js +++ b/packages/java-shell/src/test/resources/literal/ISODate.js @@ -31,4 +31,4 @@ ISODate('2012-12-19T140505') // command checkResultClass db.coll.find().toArray()[0].v; // clear -db.coll.drop(); \ No newline at end of file +db.coll.drop(); diff --git a/packages/java-shell/src/test/resources/literal/long.expected.txt b/packages/java-shell/src/test/resources/literal/long.expected.txt index 7394ef79f2..417ccd87b2 100644 --- a/packages/java-shell/src/test/resources/literal/long.expected.txt +++ b/packages/java-shell/src/test/resources/literal/long.expected.txt @@ -1,3 +1,3 @@ LongResult: 2147483649 -DoubleResult: 1281104301025214460 -LongResult: 1281104301025214467 \ No newline at end of file +DoubleResult: 1281104301025214500 +LongResult: 1281104301025214467 diff --git a/packages/java-shell/src/test/resources/literal/long.js b/packages/java-shell/src/test/resources/literal/long.js index bc32bd71e2..e152cd565c 100644 --- a/packages/java-shell/src/test/resources/literal/long.js +++ b/packages/java-shell/src/test/resources/literal/long.js @@ -3,4 +3,4 @@ // command checkResultClass 1281104301025214467 // command checkResultClass -new NumberLong("1281104301025214467") \ No newline at end of file +new NumberLong("1281104301025214467") diff --git a/packages/shell-api/src/collection.ts b/packages/shell-api/src/collection.ts index dc574843f5..3836231685 100644 --- a/packages/shell-api/src/collection.ts +++ b/packages/shell-api/src/collection.ts @@ -525,7 +525,12 @@ export class Collection< CommonErrors.InvalidArgument ); } - reducedOptions.projection ??= reducedOptions.fields; + if ( + reducedOptions.projection === undefined && + reducedOptions.fields !== undefined + ) { + reducedOptions.projection ??= reducedOptions.fields; + } delete (reducedOptions as any).query; delete (reducedOptions as any).update; delete (reducedOptions as any).fields; diff --git a/packages/shell-api/src/shell-instance-state.ts b/packages/shell-api/src/shell-instance-state.ts index 22f65c160d..f2f402f4a1 100644 --- a/packages/shell-api/src/shell-instance-state.ts +++ b/packages/shell-api/src/shell-instance-state.ts @@ -39,7 +39,11 @@ import { import { InterruptFlag } from './interruptor'; import { TransformMongoErrorPlugin } from './mongo-errors'; import NoDatabase from './no-db'; -import { type ShellBson, constructShellBson } from '@mongosh/shell-bson'; +import { + type ShellBson, + constructShellBson, + type BSON as BSONLibrary, +} from '@mongosh/shell-bson'; import { Streams } from './streams'; import { ShellLog } from './shell-log'; @@ -158,6 +162,7 @@ export class ShellInstanceState { public currentDb: DatabaseWithSchema; public messageBus: MongoshBus; public initialServiceProvider: ServiceProvider; // the initial service provider + private bsonLibrary: BSONLibrary; private connectionInfoCache: { // Caching/lazy-loading functionality for the ServiceProvider's getConnectionInfo() // return value. We store the ServiceProvider instance for which we are @@ -195,9 +200,11 @@ export class ShellInstanceState { constructor( initialServiceProvider: ServiceProvider, messageBus: any = new EventEmitter(), - cliOptions: ShellCliOptions = {} + cliOptions: ShellCliOptions = {}, + bsonLibrary: BSONLibrary = initialServiceProvider.bsonLibrary ) { this.initialServiceProvider = initialServiceProvider; + this.bsonLibrary = bsonLibrary; this.messageBus = messageBus; this.shellApi = new ShellApi(this); this.shellLog = new ShellLog(this); @@ -230,7 +237,7 @@ export class ShellInstanceState { private constructShellBson(): ShellBson { return constructShellBson({ - bsonLibrary: this.initialServiceProvider.bsonLibrary, + bsonLibrary: this.bsonLibrary, printWarning: (msg: string) => { void this.shellApi.print(`Warning: ${msg}`); }, From 2d3f8125f9246a542743fc34678f309832378265 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 3 Oct 2025 15:45:01 +0200 Subject: [PATCH 2/2] fixup: account for `long` variation across Java (?) versions --- .../java-shell/src/test/resources/literal/long.expected.1.txt | 3 +++ .../java-shell/src/test/resources/literal/long.expected.txt | 4 ++-- packages/java-shell/src/test/resources/literal/long.js | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 packages/java-shell/src/test/resources/literal/long.expected.1.txt diff --git a/packages/java-shell/src/test/resources/literal/long.expected.1.txt b/packages/java-shell/src/test/resources/literal/long.expected.1.txt new file mode 100644 index 0000000000..417ccd87b2 --- /dev/null +++ b/packages/java-shell/src/test/resources/literal/long.expected.1.txt @@ -0,0 +1,3 @@ +LongResult: 2147483649 +DoubleResult: 1281104301025214500 +LongResult: 1281104301025214467 diff --git a/packages/java-shell/src/test/resources/literal/long.expected.txt b/packages/java-shell/src/test/resources/literal/long.expected.txt index 417ccd87b2..7394ef79f2 100644 --- a/packages/java-shell/src/test/resources/literal/long.expected.txt +++ b/packages/java-shell/src/test/resources/literal/long.expected.txt @@ -1,3 +1,3 @@ LongResult: 2147483649 -DoubleResult: 1281104301025214500 -LongResult: 1281104301025214467 +DoubleResult: 1281104301025214460 +LongResult: 1281104301025214467 \ No newline at end of file diff --git a/packages/java-shell/src/test/resources/literal/long.js b/packages/java-shell/src/test/resources/literal/long.js index e152cd565c..bc32bd71e2 100644 --- a/packages/java-shell/src/test/resources/literal/long.js +++ b/packages/java-shell/src/test/resources/literal/long.js @@ -3,4 +3,4 @@ // command checkResultClass 1281104301025214467 // command checkResultClass -new NumberLong("1281104301025214467") +new NumberLong("1281104301025214467") \ No newline at end of file