Skip to content

Commit 3472ba5

Browse files
authored
fix(java-shell): make tests run and pass again MONGOSH-2711 (#2545)
These have been broken since 684002c, silently failing in CI. This commit re-enables failing on a broken test and fixes the tests that have started to fail since then.
1 parent 7f15b1f commit 3472ba5

File tree

8 files changed

+33
-8
lines changed

8 files changed

+33
-8
lines changed

packages/java-shell/src/main/js/all.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,15 @@ require('../../../../service-provider-core'); // Ensure TextEncoder polyfill is
2424
const ShellApi = require('../../../../shell-api/');
2525
const ShellEvaluator = require('../../../../shell-evaluator/').default;
2626

27+
class ShellInstanceState extends ShellApi.ShellInstanceState {
28+
constructor(sp, bus, cliOptions) {
29+
super(sp, bus, cliOptions, require('bson'));
30+
}
31+
}
32+
2733
/** temporal object that is used to access symbols from closures generated by browserify */
2834
_global = {
29-
ShellInstanceState: ShellApi.ShellInstanceState,
35+
ShellInstanceState,
3036
ShellEvaluator: ShellEvaluator,
3137
toShellResult: ShellApi.toShellResult,
3238
getShellApiType: ShellApi.getShellApiType,

packages/java-shell/src/test/js/run-tests.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ describe('java-shell tests', function() {
3333
proc = spawn('.\\gradlew.bat test --info', [], opts);
3434
}
3535
await once(proc, 'exit');
36+
if (proc.exitCode !== 0) {
37+
throw new Error(`java-shell tests failed (exit code ${proc.exitCode}, signal ${proc.signalCode})`);
38+
}
3639
});
3740
});
3841

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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"}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
ArrayResult
22
ArrayResult
3-
"use"
4-
"use"
3+
"log"
4+
"log"

packages/java-shell/src/test/resources/literal/ISODate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ ISODate('2012-12-19T140505')
3131
// command checkResultClass
3232
db.coll.find().toArray()[0].v;
3333
// clear
34-
db.coll.drop();
34+
db.coll.drop();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
LongResult: 2147483649
2+
DoubleResult: 1281104301025214500
3+
LongResult: 1281104301025214467

packages/shell-api/src/collection.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,12 @@ export class Collection<
525525
CommonErrors.InvalidArgument
526526
);
527527
}
528-
reducedOptions.projection ??= reducedOptions.fields;
528+
if (
529+
reducedOptions.projection === undefined &&
530+
reducedOptions.fields !== undefined
531+
) {
532+
reducedOptions.projection ??= reducedOptions.fields;
533+
}
529534
delete (reducedOptions as any).query;
530535
delete (reducedOptions as any).update;
531536
delete (reducedOptions as any).fields;

packages/shell-api/src/shell-instance-state.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ import {
3939
import { InterruptFlag } from './interruptor';
4040
import { TransformMongoErrorPlugin } from './mongo-errors';
4141
import NoDatabase from './no-db';
42-
import { type ShellBson, constructShellBson } from '@mongosh/shell-bson';
42+
import {
43+
type ShellBson,
44+
constructShellBson,
45+
type BSON as BSONLibrary,
46+
} from '@mongosh/shell-bson';
4347
import { Streams } from './streams';
4448
import { ShellLog } from './shell-log';
4549

@@ -158,6 +162,7 @@ export class ShellInstanceState {
158162
public currentDb: DatabaseWithSchema;
159163
public messageBus: MongoshBus;
160164
public initialServiceProvider: ServiceProvider; // the initial service provider
165+
private bsonLibrary: BSONLibrary;
161166
private connectionInfoCache: {
162167
// Caching/lazy-loading functionality for the ServiceProvider's getConnectionInfo()
163168
// return value. We store the ServiceProvider instance for which we are
@@ -195,9 +200,11 @@ export class ShellInstanceState {
195200
constructor(
196201
initialServiceProvider: ServiceProvider,
197202
messageBus: any = new EventEmitter(),
198-
cliOptions: ShellCliOptions = {}
203+
cliOptions: ShellCliOptions = {},
204+
bsonLibrary: BSONLibrary = initialServiceProvider.bsonLibrary
199205
) {
200206
this.initialServiceProvider = initialServiceProvider;
207+
this.bsonLibrary = bsonLibrary;
201208
this.messageBus = messageBus;
202209
this.shellApi = new ShellApi(this);
203210
this.shellLog = new ShellLog(this);
@@ -230,7 +237,7 @@ export class ShellInstanceState {
230237

231238
private constructShellBson(): ShellBson {
232239
return constructShellBson({
233-
bsonLibrary: this.initialServiceProvider.bsonLibrary,
240+
bsonLibrary: this.bsonLibrary,
234241
printWarning: (msg: string) => {
235242
void this.shellApi.print(`Warning: ${msg}`);
236243
},

0 commit comments

Comments
 (0)