Skip to content

Commit 8333354

Browse files
committed
chore: add strict tests
1 parent 4292e4a commit 8333354

File tree

5 files changed

+228
-11
lines changed

5 files changed

+228
-11
lines changed

mongosh.code-workspace

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
{
2+
"folders": [
3+
{
4+
"name": "mongosh",
5+
"path": "."
6+
},
7+
{
8+
"name": "📦 @mongosh/arg-parser",
9+
"path": "packages/arg-parser"
10+
},
11+
{
12+
"name": "📦 @mongosh/async-rewriter2",
13+
"path": "packages/async-rewriter2"
14+
},
15+
{
16+
"name": "📦 @mongosh/autocomplete",
17+
"path": "packages/autocomplete"
18+
},
19+
// {
20+
// "name": "📦 @mongosh/browser-repl",
21+
// "path": "packages/browser-repl"
22+
// },
23+
{
24+
"name": "📦 @mongosh/browser-runtime-core",
25+
"path": "packages/browser-runtime-core"
26+
},
27+
{
28+
"name": "📦 @mongosh/browser-runtime-electron",
29+
"path": "packages/browser-runtime-electron"
30+
},
31+
{
32+
"name": "📦 @mongosh/build",
33+
"path": "packages/build"
34+
},
35+
{
36+
"name": "📦 @mongosh/cli-repl",
37+
"path": "packages/cli-repl"
38+
},
39+
{
40+
"name": "📦 @mongosh/connectivity-tests",
41+
"path": "packages/connectivity-tests"
42+
},
43+
{
44+
"name": "📦 @mongosh/e2e-tests",
45+
"path": "packages/e2e-tests"
46+
},
47+
{
48+
"name": "📦 @mongosh/editor",
49+
"path": "packages/editor"
50+
},
51+
{
52+
"name": "📦 @mongosh/errors",
53+
"path": "packages/errors"
54+
},
55+
{
56+
"name": "📦 @mongosh/history",
57+
"path": "packages/history"
58+
},
59+
{
60+
"name": "📦 @mongosh/i18n",
61+
"path": "packages/i18n"
62+
},
63+
{
64+
"name": "📦 @mongosh/java-shell",
65+
"path": "packages/java-shell"
66+
},
67+
{
68+
"name": "📦 @mongosh/js-multiline-to-singleline",
69+
"path": "packages/js-multiline-to-singleline"
70+
},
71+
{
72+
"name": "📦 @mongosh/logging",
73+
"path": "packages/logging"
74+
},
75+
{
76+
"name": "📦 @mongosh/node-runtime-worker-thread",
77+
"path": "packages/node-runtime-worker-thread"
78+
},
79+
{
80+
"name": "📦 @mongosh/service-provider-core",
81+
"path": "packages/service-provider-core"
82+
},
83+
{
84+
"name": "📦 @mongosh/service-provider-node-driver",
85+
"path": "packages/service-provider-node-driver"
86+
},
87+
{
88+
"name": "📦 @mongosh/shell-api",
89+
"path": "packages/shell-api"
90+
},
91+
{
92+
"name": "📦 @mongosh/shell-bson",
93+
"path": "packages/shell-bson"
94+
},
95+
{
96+
"name": "📦 @mongosh/shell-evaluator",
97+
"path": "packages/shell-evaluator"
98+
},
99+
{
100+
"name": "📦 @mongosh/snippet-manager",
101+
"path": "packages/snippet-manager"
102+
},
103+
{
104+
"name": "📦 @mongosh/testing",
105+
"path": "packages/testing"
106+
},
107+
{
108+
"name": "📦 @mongosh/types",
109+
"path": "packages/types"
110+
}
111+
],
112+
"settings": {
113+
"typescript.tsdk": "node_modules/typescript/lib",
114+
"mochaExplorer.files": "./{src,lib}/**/*.spec.ts",
115+
"mochaExplorer.require": [
116+
"../../scripts/import-expansions.js",
117+
"ts-node/register"
118+
],
119+
"mochaExplorer.timeout": 60000,
120+
"mochaExplorer.ui": "bdd",
121+
"mochaExplorer.esmLoader": false,
122+
"mochaExplorer.monkeyPatch": true,
123+
"mochaExplorer.autoload": true,
124+
"testExplorer.codeLens": true,
125+
"testExplorer.gutterDecoration": true
126+
}
127+
}

package-lock.json

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

packages/cli-repl/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@
9393
"semver": "^7.5.4",
9494
"strip-ansi": "^6.0.0",
9595
"text-table": "^0.2.0",
96-
"glibc-version": "^1.0.0",
97-
"yargs-parser": "^20.2.4"
96+
"yargs-parser": "^20.2.4",
97+
"zod": "^3.25.76 || ^4.1.8"
9898
},
9999
"devDependencies": {
100100
"@mongodb-js/eslint-config-mongosh": "^1.0.0",
@@ -107,7 +107,7 @@
107107
"@types/node": "^22.15.30",
108108
"@types/numeral": "^2.0.2",
109109
"@types/text-table": "^0.2.1",
110-
"@types/yargs-parser": "^15.0.0",
110+
"@types/yargs-parser": "^21.0.3",
111111
"chai-as-promised": "^8.0.2",
112112
"depcheck": "^1.4.7",
113113
"eslint": "^7.25.0",

packages/cli-repl/src/arg-parser.spec.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,4 +943,67 @@ describe('arg-parser', function () {
943943
}
944944
});
945945
});
946+
947+
describe('union type fields', function () {
948+
for (const { argument, values, strict } of [
949+
{ argument: 'json', values: ['relaxed', 'canonical'] },
950+
{ argument: 'oidcDumpTokens', values: ['redacted', 'include-secrets'] },
951+
{ argument: 'browser', values: ['test'], strict: false },
952+
] as const) {
953+
const baseArgv = ['node', 'mongosh', 'mongodb://domain.com:20000'];
954+
describe(`with ${argument}`, function () {
955+
context('with boolean', function () {
956+
it(`get set to true with --${argument}`, function () {
957+
expect(
958+
parseCliArgs([...baseArgv, `--${argument}`])[argument]
959+
).to.equal(true);
960+
});
961+
962+
it(`sets to true with --${argument}=true`, function () {
963+
expect(
964+
parseCliArgs([...baseArgv, `--${argument}=true`])[argument]
965+
).to.equal(true);
966+
});
967+
968+
it(`sets to false with --${argument}=false`, function () {
969+
expect(
970+
parseCliArgs([...baseArgv, `--${argument}=false`])[argument]
971+
).to.equal(false);
972+
});
973+
});
974+
975+
for (const value of values) {
976+
context('with string value', function () {
977+
// This matches the legacy behavior pre-Zod schema migration.
978+
it(`does not work with "--${argument} ${value}"`, function () {
979+
expect(
980+
parseCliArgs([...baseArgv, `--${argument} ${value}`])[argument]
981+
).to.be.undefined;
982+
});
983+
984+
it(`works "--${argument}=${value}"`, function () {
985+
expect(
986+
parseCliArgs([...baseArgv, `--${argument}=${value}`])[argument]
987+
).to.equal(value);
988+
});
989+
});
990+
}
991+
992+
if (strict) {
993+
it('throws an error with invalid value', function () {
994+
try {
995+
parseCliArgs([...baseArgv, `--${argument}`, 'invalid']);
996+
} catch (e: any) {
997+
expect(e).to.be.instanceOf(MongoshUnimplementedError);
998+
expect(e.message).to.include(
999+
`--${argument} can only have the values ${values.join(', ')}`
1000+
);
1001+
return;
1002+
}
1003+
expect.fail('Expected error');
1004+
});
1005+
}
1006+
});
1007+
}
1008+
});
9461009
});

packages/cli-repl/src/arg-parser.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { CommonErrors, MongoshUnimplementedError } from '@mongosh/errors';
22
import i18n from '@mongosh/i18n';
33
import type { CliOptions } from '@mongosh/arg-parser';
4-
import parser from 'yargs-parser';
4+
import parser, { type Options } from 'yargs-parser';
55
import { colorizeForStderr as clr } from './clr';
66
import { USAGE } from './constants';
77

@@ -10,10 +10,23 @@ import { USAGE } from './constants';
1010
*/
1111
const UNKNOWN = 'cli-repl.arg-parser.unknown-option';
1212

13+
const coerceIfBoolean = (value: unknown) => {
14+
if (typeof value === 'string') {
15+
if (value === 'true') {
16+
return true;
17+
}
18+
if (value === 'false') {
19+
return false;
20+
}
21+
return value;
22+
}
23+
return value;
24+
};
25+
1326
/**
1427
* The yargs-parser options configuration.
1528
*/
16-
const OPTIONS = {
29+
const OPTIONS: Options = {
1730
string: [
1831
'apiVersion',
1932
'authenticationDatabase',
@@ -23,7 +36,6 @@ const OPTIONS = {
2336
'awsSecretAccessKey',
2437
'awsSessionToken',
2538
'awsIamSessionToken',
26-
'browser',
2739
'csfleLibraryPath',
2840
'cryptSharedLibPath',
2941
'db',
@@ -84,6 +96,11 @@ const OPTIONS = {
8496
'verbose',
8597
'version',
8698
],
99+
coerce: {
100+
browser: coerceIfBoolean,
101+
json: coerceIfBoolean,
102+
oidcDumpTokens: coerceIfBoolean,
103+
},
87104
array: ['eval', 'file'],
88105
alias: {
89106
h: 'help',

0 commit comments

Comments
 (0)