Skip to content

Commit 3927161

Browse files
authored
MONGOSH-248 - Run java-shell tests as part of the test suite (#388)
1 parent 892c65b commit 3927161

13 files changed

+339
-64
lines changed

packages/java-shell/package-lock.json

Lines changed: 253 additions & 62 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/java-shell/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
},
1313
"scripts": {
1414
"browserify-shell-api": "browserify src/main/js/all.js -o src/main/resources/js/all-standalone.js -dv",
15-
"test-ci": "npm run test"
15+
"test": "mocha -r \"../../scripts/import-expansions.js\" --timeout 60000 --colors -r ts-node/register \"./src/test/js/run-tests.ts\"",
16+
"test-ci": "node ../../scripts/run-if-platform.js linux npm run test"
1617
},
1718
"devDependencies": {
1819
"browserify": "latest"
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
import child_process from 'child_process';
3+
import fs from 'fs';
4+
import path from 'path';
5+
import { startTestServer } from '../../../../../testing/integration-testing-hooks';
6+
7+
const port = '27019';
8+
const authlessUri = `mongodb://localhost:${port}`;
9+
const uri = `mongodb://admin:admin@localhost:${port}`;
10+
11+
describe('java-shell tests', function() {
12+
this.timeout(300_000);
13+
const connectionString = startTestServer();
14+
const uriFile = path.resolve(__dirname, '..', 'resources', 'URI.txt');
15+
const packageRoot = path.resolve(__dirname, '..', '..', '..') + '/';
16+
let origUriFileContent;
17+
18+
before((done) => {
19+
// We can probably turn this into execSync once
20+
// https://jira.mongodb.org/browse/MONGOSH-401 is fixed
21+
const mongosh = child_process.spawn(
22+
process.execPath,
23+
[ path.resolve(packageRoot, '..', 'cli-repl', 'bin', 'mongosh.js'), connectionString ],
24+
{ stdio: [ 'pipe', 'pipe', 'inherit' ], env: { ...process.env, NO_COLOR: '1' } });
25+
let out = '';
26+
let wroteCreateUser = false;
27+
let isDone = false;
28+
mongosh.stdout.setEncoding('utf8').on('data', (chunk) => {
29+
out += chunk;
30+
process.stderr.write(chunk);
31+
if (out.includes('> ') && !wroteCreateUser) {
32+
wroteCreateUser = true;
33+
mongosh.stdin.write(`
34+
use admin;
35+
db.createUser({ user: "admin", pwd: "admin", roles: ["root"]});
36+
`);
37+
}
38+
if ((out.includes('{ ok: 1 }') ||
39+
out.includes('User "admin@admin" already exists')) && !isDone) {
40+
isDone = true;
41+
mongosh.kill();
42+
origUriFileContent = fs.readFileSync(uriFile);
43+
fs.writeFileSync(uriFile,
44+
connectionString.replace('mongodb://', 'mongodb://admin:admin@'));
45+
done();
46+
}
47+
});
48+
});
49+
50+
after(() => {
51+
fs.writeFileSync(uriFile, origUriFileContent);
52+
});
53+
54+
it('passes the JavaShell tests', () => {
55+
if (process.platform !== 'win32') {
56+
child_process.execSync('./gradlew test --info', { stdio: 'inherit', cwd: packageRoot });
57+
} else {
58+
child_process.execSync('.\\gradlew.bat test --info', { stdio: 'inherit', cwd: packageRoot });
59+
}
60+
});
61+
});
62+

packages/java-shell/src/test/kotlin/com/mongodb/mongosh/util.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,20 @@ private fun withDb(shell: MongoShell, name: String?, block: () -> Unit) {
143143

144144
@Throws(IOException::class)
145145
private fun compare(testDataPath: String, name: String, actual: String) {
146-
val expectedFile = File("$testDataPath/$name.expected.txt")
146+
var expectedFile = File("$testDataPath/$name.expected.txt")
147147
if (!expectedFile.exists()) {
148148
assertTrue(expectedFile.createNewFile())
149149
expectedFile.writeText(actual.trim())
150150
fail("Created output file $expectedFile")
151151
} else {
152+
for (counter in 1..10) {
153+
if (expectedFile.readText().trim() == actual.trim()) break
154+
155+
val alternativeFile = File("$testDataPath/$name.expected.$counter.txt")
156+
if (alternativeFile.exists()) {
157+
expectedFile = alternativeFile
158+
}
159+
}
152160
assertEquals(expectedFile.readText().trim(), actual.trim())
153161
}
154162
}
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): '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"}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[ "category_1" ]
2+
[ { "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" } } ]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[ { "v": 2, "key": { "_id": 1 }, "name": "_id_" } ]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[ { "v": 2, "key": { "_id": 1 }, "name": "_id_" } ]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[ { "v": 2, "key": { "_id": 1 }, "name": "_id_" } ]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[ "category_1" ]
2+
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={}}].

0 commit comments

Comments
 (0)