Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
315 changes: 253 additions & 62 deletions packages/java-shell/package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion packages/java-shell/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
},
"scripts": {
"browserify-shell-api": "browserify src/main/js/all.js -o src/main/resources/js/all-standalone.js -dv",
"test-ci": "npm run test"
"test": "mocha -r \"../../scripts/import-expansions.js\" --timeout 60000 --colors -r ts-node/register \"./src/test/js/run-tests.ts\"",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do i need locally to run this? Should we add some docs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s the same as the other packages – it’s part of npm test on the root module. Whether that’s good or not is another question, it requires Java, but then again npm test already requires stuff like headless chrome, so … 🤷‍♀️

"test-ci": "node ../../scripts/run-if-platform.js linux npm run test"
},
"devDependencies": {
"browserify": "latest"
Expand Down
62 changes: 62 additions & 0 deletions packages/java-shell/src/test/js/run-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict';
import child_process from 'child_process';
import fs from 'fs';
import path from 'path';
import { startTestServer } from '../../../../../testing/integration-testing-hooks';

const port = '27019';
const authlessUri = `mongodb://localhost:${port}`;
const uri = `mongodb://admin:admin@localhost:${port}`;

describe('java-shell tests', function() {
this.timeout(300_000);
const connectionString = startTestServer();
const uriFile = path.resolve(__dirname, '..', 'resources', 'URI.txt');
const packageRoot = path.resolve(__dirname, '..', '..', '..') + '/';
let origUriFileContent;

before((done) => {
// We can probably turn this into execSync once
// https://jira.mongodb.org/browse/MONGOSH-401 is fixed
const mongosh = child_process.spawn(
process.execPath,
[ path.resolve(packageRoot, '..', 'cli-repl', 'bin', 'mongosh.js'), connectionString ],
{ stdio: [ 'pipe', 'pipe', 'inherit' ], env: { ...process.env, NO_COLOR: '1' } });
let out = '';
let wroteCreateUser = false;
let isDone = false;
mongosh.stdout.setEncoding('utf8').on('data', (chunk) => {
out += chunk;
process.stderr.write(chunk);
if (out.includes('> ') && !wroteCreateUser) {
wroteCreateUser = true;
mongosh.stdin.write(`
use admin;
db.createUser({ user: "admin", pwd: "admin", roles: ["root"]});
`);
}
if ((out.includes('{ ok: 1 }') ||
out.includes('User "admin@admin" already exists')) && !isDone) {
isDone = true;
mongosh.kill();
origUriFileContent = fs.readFileSync(uriFile);
fs.writeFileSync(uriFile,
connectionString.replace('mongodb://', 'mongodb://admin:admin@'));
done();
}
});
});

after(() => {
fs.writeFileSync(uriFile, origUriFileContent);
});

it('passes the JavaShell tests', () => {
if (process.platform !== 'win32') {
child_process.execSync('./gradlew test --info', { stdio: 'inherit', cwd: packageRoot });
} else {
child_process.execSync('.\\gradlew.bat test --info', { stdio: 'inherit', cwd: packageRoot });
}
});
});

10 changes: 9 additions & 1 deletion packages/java-shell/src/test/kotlin/com/mongodb/mongosh/util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,20 @@ private fun withDb(shell: MongoShell, name: String?, block: () -> Unit) {

@Throws(IOException::class)
private fun compare(testDataPath: String, name: String, actual: String) {
val expectedFile = File("$testDataPath/$name.expected.txt")
var expectedFile = File("$testDataPath/$name.expected.txt")
if (!expectedFile.exists()) {
assertTrue(expectedFile.createNewFile())
expectedFile.writeText(actual.trim())
fail("Created output file $expectedFile")
} else {
for (counter in 1..10) {
if (expectedFile.readText().trim() == actual.trim()) break

val alternativeFile = File("$testDataPath/$name.expected.$counter.txt")
if (alternativeFile.exists()) {
expectedFile = alternativeFile
}
}
assertEquals(expectedFile.readText().trim(), actual.trim())
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.mongodb.MongoCommandException: Command failed with error 2 (BadValue): 'cursor.batchSize must not be negative' on server localhost:27018. The full response is {"ok": 0.0, "errmsg": "cursor.batchSize must not be negative", "code": 2, "codeName": "BadValue"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[ "category_1" ]
[ { "v": 2, "key": { "_id": 1 }, "name": "_id_" }, { "v": 2, "key": { "category": 1 }, "name": "category_1", "collation": { "locale": "fr", "caseLevel": false, "caseFirst": "off", "strength": 3, "numericOrdering": false, "alternate": "non-ignorable", "maxVariable": "punct", "normalization": false, "backwards": false, "version": "57.1" } } ]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ { "v": 2, "key": { "_id": 1 }, "name": "_id_" } ]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ { "v": 2, "key": { "_id": 1 }, "name": "_id_" } ]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ { "v": 2, "key": { "_id": 1 }, "name": "_id_" } ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[ "category_1" ]
com.mongodb.MongoBulkWriteException: Bulk write operation error on server localhost:27018. Write errors: [BulkWriteError{index=0, code=11000, message='E11000 duplicate key error collection: admin.coll index: category_1 dup key: { category: "cat1" }', details={}}].
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.mongodb.MongoBulkWriteException: Bulk write operation error on server localhost:27018. Write errors: [BulkWriteError{index=0, code=121, message='Document failed validation', details={}}].
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ { "name": "testCollection1", "type": "collection", "options": { }, "info": { "readOnly": false, "uuid": <UUID> }, "idIndex": { "v": 2, "key": { "_id": 1 }, "name": "_id_" } } ]
3 changes: 3 additions & 0 deletions packages/java-shell/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../config/tsconfig.base.json"
}