diff --git a/README.md b/README.md index 593c3ff..b1baaa6 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,42 @@ Use or download a specific version of the Database Tools: $ m tools 100.9.4 ``` +### Downloading MongoDB Shell + +The MongoDB Shell (mongosh) is released separately from the server. You can use `m` to manage your MongoDB Shell version independently of your MongoDB server and tools versions. + +List available MongoDB Shell versions: + +```bash +$ m mongosh ls +``` + +List installed MongoDB Shell versions: + +```bash +$ m mongosh installed +``` + +Use or download the latest stable release of the MongoDB Shell: + +```bash +$ m mongosh stable +``` + +Use or download a specific version of the MongoDB Shell: + +```bash +$ m mongosh 2.3.7 +``` + +Remove previously installed MongoDB Shell versions: + +```bash +$ m mongosh rm 2.3.0 2.3.5 +``` + +**Note:** The `m shell [args...]` command will first try to use the installed mongosh version, and fall back to the legacy `mongo` shell if mongosh is not installed. + ### Removing Binaries Remove some previously installed versions: @@ -258,12 +294,18 @@ Output from `m --help`: m tools stable Install or activate the latest stable Database Tools release m tools X.Y.Z Install or activate the Database Tools X.Y.Z m tools ls Output the versions of the Database Tools available - m tools installed [--json] Output installed versions of the Database Tools available + m tools rm Remove the given Database Tools versions + m tools installed [--json] Output the installed versions of the Database Tools + m mongosh stable Install or activate the latest stable MongoDB Shell release + m mongosh X.Y.Z Install or activate the MongoDB Shell X.Y.Z + m mongosh ls Output the versions of the MongoDB Shell available + m mongosh rm Remove the given MongoDB Shell versions + m mongosh installed [--json] Output the installed versions of the MongoDB Shell Events: - change Occurs when switching MongoDB versions - install Occurs when installing a previously uninstalled MongoDB version + change Occurs when switching MongoDB server versions + install Occurs when installing a previously uninstalled MongoDB server version Options: diff --git a/bin/m b/bin/m index b4fb5d3..9ace4eb 100755 --- a/bin/m +++ b/bin/m @@ -20,6 +20,9 @@ VERSIONS_DIR=$M_DIR/versions # Directory for MongoDB Tools version binaries TOOLS_DIR=$M_DIR/tools/versions +# Directory for MongoDB Shell version binaries +SHELL_DIR=$M_DIR/shell/versions + # Working directory for unpacking MongoDB tarballs BUILD_DIR_BASE=$M_DIR/mongo- @@ -81,6 +84,14 @@ abort_not_installed() { exit 1 } +abort_shell_not_installed() { + printf "Error: The requested MongoDB shell version is not installed.\n" + if [[ ! -z "$version" ]]; then + printf "Try 'm mongosh $version' to download and activate.\n" + fi + exit 1 +} + # # Check file age (in seconds) # @@ -153,12 +164,18 @@ display_help() { m tools stable Install or activate the latest stable Database Tools release m tools X.Y.Z Install or activate the Database Tools X.Y.Z m tools ls Output the versions of the Database Tools available - m tools installed [--json] Output installed versions of the Database Tools available + m tools rm Remove the given Database Tools versions + m tools installed [--json] Output the installed versions of the Database Tools + m mongosh stable Install or activate the latest stable MongoDB Shell release + m mongosh X.Y.Z Install or activate the MongoDB Shell X.Y.Z + m mongosh ls Output the versions of the MongoDB Shell available + m mongosh rm Remove the given MongoDB Shell versions + m mongosh installed [--json] Output the installed versions of the MongoDB Shell Events: - change Occurs when switching MongoDB versions - install Occurs when installing a previously uninstalled MongoDB version + change Occurs when switching MongoDB server versions + install Occurs when installing a previously uninstalled MongoDB server version Options: @@ -272,7 +289,7 @@ display_versions() { fi else if [ "$version" = "$active" ]; then - printf " * $version $config\n" + printf " ✔ $version $config\n" else printf " $version $config\n" fi @@ -367,19 +384,18 @@ install_tools_bin() { log "installing binary" ext="tgz" - get_distro_and_arch + get_distro_and_arch 'tools' if [[ $os = "linux" ]]; then # for linux fetch the rhel7 tarball by default local dist=rhel70 # shadowing earlier $distro for distro in $distros; do - if good "https://fastdl.mongodb.org/tools/db/mongodb-database-tools-$distro-$arch-$version.tgz"; then + if good "https://fastdl.mongodb.org/tools/db/mongodb-database-tools-$distro-$arch-$version.$ext"; then dist=$distro break fi done - fi - if [[ $os = "osx" ]]; then + elif [[ $os = "osx" ]]; then dist="macos" if vergte $version "100.1.0"; then ext="zip" @@ -401,7 +417,7 @@ install_tools_bin() { $GET https://fastdl.mongodb.org/tools/db/mongodb-database-tools-$dist-$arch-$version.$ext -o $M_DIR/tools-$version.$ext if test $? -gt 0; then printf "Error: tools installation failed\n" - printf " Tarball fetch for MongoDB tools version $version failed.\n" + printf " Fetch for MongoDB tools version $version failed.\n" printf " Try a different version or view $logpath to view\n" printf " error details.\n" exit 1 @@ -482,7 +498,7 @@ list_tools_versions() { for v in $versions; do if test "$active_tools" = "$v"; then - printf " $v\n" + printf " ✔ $v\n" else if test -d $VERSIONS_DIR/$v || test -d $TOOLS_DIR/$v; then printf " * $v\n" @@ -564,7 +580,162 @@ display_tools_versions() { fi else if [ "$version" = "$active_tools" ]; then - printf " $version $config\n" + printf " ✔ $version $config\n" + else + printf " $version $config\n" + fi + fi + done + + if $json; then + printf "\n]\n" + fi +} + +remove_tools_versions() { + test -z $1 && abort "version(s) required" + check_current_tools_version + + while test $# -ne 0; do + local version=${1#v} + local rmpath="$TOOLS_DIR/$version" + debug "=> Path: $rmpath" + + if verlte $version "4.3.2"; then + printf "Error: Cannot remove tools version $version since it is included with the\n" + printf "MongoDB server package. To remove this version, remove the server instead:\n" + printf " m rm $version\n" + shift + continue + fi + + if test "$version" = "$active_tools"; then + printf "WARNING: $version is the active version!\n" + if [[ "$CONFIRM" == 1 ]]; then + read -p "Are you sure you want to remove this? [y/N] " yn + case $yn in + [Yy]* ) ;; + * ) printf "SKIPPING $version "; exit;; + esac + fi + fi + + if [ -d $rmpath ]; then + rm -rf $rmpath + echo "Removed MongoDB tools version $version" + active_tools="" + else + echo "MongoDB tools version $version is not installed" + fi + + shift + done +} + +# +# Find the current installed version of shell. +# + +check_current_shell_version() { + which $M_BIN_DIR/mongosh &> /dev/null + if test $? -eq 0; then + active_shell=`$M_BIN_DIR/mongosh --version | sed -nE "s/^([0-9]+\.[0-9]+\.[0-9]+)$/\1/p" | head -1` + fi +} + +# +# Find all available versions of the MongoDB Shell. +# + +get_all_shell_versions() { + local rc_regex="" + if [[ $1 == "--rc" ]]; then + rc_regex="(-rc[0-9]+)?" + fi + + curl_shell_versions=(curl -sSf -H "Accept: application/vnd.github.v3+json") + if [ -n "${GH_TOKEN:-}" ]; then + debug "Using GitHub token from environment for API requests" + curl_shell_versions+=(-H "authorization: bearer ${GH_TOKEN:?}") + fi + curl_shell_versions+=("https://api.github.com/repos/mongodb-js/mongosh/git/refs/tags") + + shell_versions="$("${curl_shell_versions[@]}" \ + | sed -nE "s/^.*\"ref\"\: \"refs\/tags\/v([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$rc_regex)\",$/\1/p" \ + | sort -V)" + + versions="$shell_versions" +} + +# +# Output all MongoDB Shell versions +# + +list_shell_versions() { + check_current_shell_version + + if [[ $2 == "--rc" ]]; then + local rc="--rc" + fi + + get_all_shell_versions $rc + + for v in $versions; do + if test "$active_shell" = "$v"; then + printf " ✔ $v\n" + else + if test -d $SHELL_DIR/$v; then + printf " * $v\n" + else + printf " $v\n" + fi + fi + done +} + +# +# Output all installed shell versions. +# Pass "--json" to output in JSON format. +# + +display_shell_versions() { + local option=$1; shift + local json=false + if test "$option" = "--json"; then + json=true + fi + + declare -a versions + if [ -e $SHELL_DIR ]; then + versions=(`ls -1 $SHELL_DIR | sort -t. -k 1,1n -k 2,2n -k 3,3n`) + fi + + if test -z "$versions"; then + if $json; then + echo "[]" + else + echo "No installed versions" + fi + return + fi + local last=${versions[${#versions[@]}-1]} + + if $json; then + printf "[" + fi + + check_current_shell_version + for version in ${versions[@]}; do + local dir="$SHELL_DIR/$version" + local config=`test -f "$dir"/.config && cat "$dir"/.config` + if $json; then + printf "\n {\n \"name\" : \"$version\",\n \"path\" : \"$dir/bin/\" \n }" + if [ "$version" != "$last" ]; then + printf "," + fi + else + if [ "$version" = "$active_shell" ]; then + printf " ✔ $version $config\n" else printf " $version $config\n" fi @@ -576,12 +747,161 @@ display_tools_versions() { fi } +remove_shell_versions() { + test -z $1 && abort "version(s) required" + check_current_shell_version + + while test $# -ne 0; do + local version=${1#v} + local rmpath="$SHELL_DIR/$version" + debug "=> Path: $rmpath" + if test "$version" = "$active_shell"; then + printf "WARNING: $version is the active version!\n" + if [[ "$CONFIRM" == 1 ]]; then + read -p "Are you sure you want to remove this? [y/N] " yn + case $yn in + [Yy]* ) ;; + * ) printf "SKIPPING $version "; exit;; + esac + fi + fi + + if [ -d $rmpath ]; then + rm -rf $rmpath + echo "Removed MongoDB shell version $version" + active_shell="" + else + echo "MongoDB shell version $version is not installed" + fi + + shift + done +} + +# +# Install MongoDB Shell binaries for provided version +# +# install_shell_bin +# + +install_shell_bin() { + local version=$1 + + log "installing mongosh $version" + + ext="tgz" + get_distro_and_arch 'shell' + + if [[ $os = "linux" ]]; then + dist="linux" + fi + + if [[ $os = "osx" ]]; then + dist="darwin" + ext="zip" + fi + + if [[ $arch = "x86_64" ]]; then + # For mongosh, use x64 naming, following this doc: + # https://s3.amazonaws.com/info-mongodb-com/com-download-center/mongosh.multiversion.json + arch="x64" + fi + + link="https://downloads.mongodb.com/compass/mongosh-$version-$dist-$arch.$ext" + + log "downloading $link" + $GET "$link" -o $M_DIR/mongosh-$version.$ext + + if test $? -gt 0; then + printf "Error: mongosh installation failed\n" + printf " Fetch for MongoDB Shell version $version failed.\n" + printf " Try a different version or view $logpath to view\n" + printf " error details.\n" + printf "\n" + printf "You might also consider installing the MongoDB shell from the MongoDB Download Center:\n" + printf " https://www.mongodb.com/try/download/shell\n" + exit 1 + fi + + mkdir -p $SHELL_DIR/$version + + log "unpacking source" + if [[ $ext = "zip" ]]; then + unzip -q $M_DIR/mongosh-$version.zip -d $SHELL_DIR/$version + else + tar -xzf $M_DIR/mongosh-$version.tgz -C $SHELL_DIR/$version + fi + + + log "moving files" + mv $SHELL_DIR/$version/mongosh-$version-$dist-$arch/* $SHELL_DIR/$version + + log "cleaning up" + rm $M_DIR/mongosh-$version.$ext + rm -r $SHELL_DIR/$version/mongosh-$version-$dist-$arch +} + +# +# Install and activate MongoDB Shell. +# +# install_shell +# + +install_shell() { + local version=$1 + + check_current_shell_version + check_current_version + + if [[ $version == $active_shell ]]; then + printf "Already Active: MongoDB Server $active, MongoDB Shell $active_shell\n" + exit 0 + fi + + if [[ $version =~ "rc" ]]; then + local rc="--rc" + fi + + if [[ $version == "stable" ]]; then + # Need to fetch versions to find the latest + get_all_shell_versions $rc + if [[ -z "$versions" ]]; then + printf "Could not fetch available versions of MongoDB Shell\n" + exit 1 + fi + # Set version to the latest available version + version=`echo $versions | awk 'NF>1{print $NF}'` + elif [[ $version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + # Specific version provided, try to install it directly + # (only validate if we successfully fetched versions) + get_all_shell_versions $rc + if [[ ! -z "$versions" ]] && [[ ! $versions =~ $version ]]; then + printf "Could not find any releases for the requested version "$version" of MongoDB Shell\n" + exit 1 + fi + else + printf "Invalid version format: $version\n" + exit 1 + fi + + local dir=$SHELL_DIR/$version + if test ! -d $dir; then + prompt_install "MongoDB Shell version $version is not installed." + install_shell_bin $version + fi + + log "activating MongoDB Shell $version" + debug "Shell path: $dir" + ln -fs $dir/bin/* $M_BIN_DIR + + printf "Installation complete: MongoDB Shell $version activated!\n" +} + # # Install MongoDB [config ...] # install_mongo() { - local version=$1; shift local config=$@ local rc="" @@ -684,29 +1004,19 @@ install_mongo() { if vergte $version "6.0.0"; then if ! command -v mongosh &> /dev/null; then echo "" - echo "NOTE: the legacy mongo shell is not included in MongoDB 6.0+ distributions" + echo "NOTE: the mongo shell is not included in MongoDB 6.0+ distributions." echo "" - echo "Download the new MongoDB Shell (mongosh):" + echo "Install and use the MongoDB Shell (mongosh) separately via:" + echo " m shell stable" + echo "" + echo "Or download it directly from the MongoDB website:" echo " https://www.mongodb.com/try/download/shell" echo "" - - # Convenience for macOS users - if [[ "$(uname)" == "Darwin" ]]; then - if command -v brew &> /dev/null; then - prompt_install "Install "mongosh" via brew?" - brew install mongosh --quiet - else - echo "Install via brew:" - echo " brew install mongosh" - fi - echo "" - fi fi fi } -# # Prompt installation # # prompt_install "About to install something" @@ -771,7 +1081,7 @@ install_bin() { fi # determine url based on os and arch - get_distro_and_arch + get_distro_and_arch 'server' # determine the download url if [[ "$community" == 1 ]]; then @@ -848,6 +1158,16 @@ install_bin() { # get_distro_and_arch() { + local type= + case "$1" in + tools|server|shell) + type="$1" + ;; + *) + abort "Invalid type: '$1' (expected: tools, server, or shell)" + ;; + esac + arch=`uname -m` local OS=`uname` os=`echo $OS | tr '[:upper:]' '[:lower:]'` @@ -857,7 +1177,7 @@ get_distro_and_arch() { os=linux ;; darwin* ) os=osx - if [ "$arch" = "arm64" ]; then + if [[ "$type" == "server" ]] && [ "$arch" = "arm64" ]; then if [ $(numeric_version $version) -lt $(numeric_version "6.0.0") ]; then echo "" echo "NOTE: Apple Silicon is not natively supported for MongoDB server before 6.0" @@ -1248,15 +1568,24 @@ execute_shard_with_version() { execute_shell_with_version() { test -z $1 && abort "version required" - local version=$(get_latest_installed_version ${1#v}) - local bin=$VERSIONS_DIR/$version/bin/mongo + + # first see if this version of mongosh is installed. fallback to legacy. + local bin=$SHELL_DIR/${1#v}/bin/mongosh + + if ! test -f $bin; then + log "mongosh version ${1#v} not found, falling back to legacy mongo shell" + local version=$(get_latest_installed_version ${1#v}) + local bin=$VERSIONS_DIR/$version/bin/mongo + fi + + debug "Shell bin: $bin" shift # remove version if test -f $bin; then $bin $@ else - abort_not_installed $version + abort_shell_not_installed ${1#v} fi } @@ -1376,7 +1705,7 @@ list_versions() { for v in $versions; do if test "$active" = "$v"; then - printf " ο $v\n" + printf " ✔ $v\n" else if test -d $VERSIONS_DIR/$v; then printf " * $v\n" @@ -1533,12 +1862,33 @@ list_posts() { # Handle tools commands handle_tools() { - case $1 in - ls|list|available|avail) list_tools_versions $@; exit;; - lls|installed) display_tools_versions $2; exit ;; - stable) install_tools $@; exit ;; - *) install_tools $@; exit ;; - esac + if test $# -eq 0; then + display_tools_versions + else + case $1 in + ls|list|available|avail) list_tools_versions $@; exit;; + lls|installed) display_tools_versions $2; exit ;; + stable) install_tools $@; exit ;; + rm) shift; remove_tools_versions $@; exit ;; + *) install_tools $@; exit ;; + esac + fi +} + +# Handle mongosh commands + +handle_mongosh() { + if test $# -eq 0; then + display_shell_versions + else + case $1 in + ls|list|available|avail) list_shell_versions $@; exit;; + lls|installed) display_shell_versions $2; exit ;; + stable) install_shell $@; exit ;; + rm) shift; remove_shell_versions $@; exit ;; + *) install_shell $@; exit ;; + esac + fi } # Handle arguments @@ -1567,6 +1917,7 @@ else install) shift; install_mongo $@; exit ;; reinstall) shift; CONFIRM=0; remove_versions $@; install_mongo $@; exit;; tools) shift; handle_tools $@; exit;; + mongosh) shift; handle_mongosh $@; exit;; *) install_mongo $@; exit ;; esac shift diff --git a/test/suite.mjs b/test/suite.mjs index c026181..fe0ae88 100755 --- a/test/suite.mjs +++ b/test/suite.mjs @@ -102,6 +102,7 @@ describe('m - MongoDB Version Management', { concurrency: 5 }, () => { assert.match(result.stdout, /Usage: m \[options\] \[COMMAND\] \[config\]/); assert.match(result.stdout, /Commands:/); assert.match(result.stdout, /Options:/); + assert.match(result.stdout, /Aliases:/); }); test('should display help with --help flag', async () => { @@ -213,7 +214,20 @@ describe('m - MongoDB Version Management', { concurrency: 5 }, () => { assert.match(result.stdout, new RegExp(`Activating: MongoDB Server ${version}`, 'i')); }); - test('should uninstall the version ', async () => { + test('installed version should be marked with checkmark in ls output', async () => { + const result = await run(['ls']); + assert.equal(result.exitCode, 0); + // The active version should have a checkmark + assert.match(result.stdout, new RegExp(`✔ ${version}`)); + }); + + test('installed version should be marked with checkmark in installed output', async () => { + const result = await run(['installed']); + assert.equal(result.exitCode, 0); + assert.match(result.stdout, new RegExp(`✔ ${version}`)); + }); + + test('should uninstall the version', async () => { const result = await run(['rm', version]); assert.equal(result.exitCode, 0); assert.match(result.stdout, new RegExp(`Removed MongoDB version ${version}`, 'i')); @@ -286,6 +300,105 @@ describe('m - MongoDB Version Management', { concurrency: 5 }, () => { assert.equal(result.exitCode, 0); assert.doesNotThrow(() => JSON.parse(result.stdout)); }); + + test('should show installed tools when called without arguments', async () => { + const result = await run(['tools']); + assert.equal(result.exitCode, 0); + assert.match(result.stdout, /(No installed versions|^\s*$)/); + }); + + test('should remove database tools version', async () => { + const result = await run(['tools', 'rm', '100.9.4']); + assert.equal(result.exitCode, 0); + // Should succeed or indicate version not installed + assert.doesNotMatch(result.stdout, /line \d+:/); + }); + }); + + describe('MongoDB Shell (mongosh) Commands', { concurrency: 1 }, () => { + test('should list mongosh versions with ls command', async () => { + const result = await run(['mongosh', 'ls']); + assert.equal(result.exitCode, 0); + // Should contain version numbers + assert.match(result.stdout, /\d+\.\d+\.\d+/); + }); + + // intentionally not testing ls aliases to avoid github rate limits + + test('should show installed mongosh versions (initially empty)', async () => { + const result = await run(['mongosh', 'installed']); + assert.equal(result.exitCode, 0); + assert.match(result.stdout, /(No installed versions|^\s*$)/); + }); + + test('should show installed mongosh versions with lls alias', async () => { + const result = await run(['mongosh', 'lls']); + assert.equal(result.exitCode, 0); + assert.match(result.stdout, /(No installed versions|^\s*$)/); + }); + + test('should show installed mongosh versions in JSON format', async () => { + const result = await run(['mongosh', 'installed', '--json']); + assert.equal(result.exitCode, 0); + assert.doesNotThrow(() => JSON.parse(result.stdout)); + const parsed = JSON.parse(result.stdout); + assert.ok(Array.isArray(parsed)); + }); + + test('should show installed mongosh when called without arguments', async () => { + const result = await run(['mongosh']); + assert.equal(result.exitCode, 0); + assert.match(result.stdout, /(No installed versions|^\s*$)/); + }); + + test('should remove mongosh version', async () => { + const result = await run(['mongosh', 'rm', '2.3.7']); + assert.equal(result.exitCode, 0); + // Should succeed or indicate version not installed + assert.doesNotMatch(result.stdout, /line \d+:/); + }); + + test('should handle removing multiple mongosh versions', async () => { + const result = await run(['mongosh', 'rm', '2.3.0', '2.3.1']); + assert.equal(result.exitCode, 0, `${result.stdout} ${result.stderr}`); + assert.doesNotMatch(result.stdout, /line \d+:/); + }); + + test('should install latest stable mongosh with stable command', async () => { + const result = await run(['mongosh', 'stable'], { timeout: 120_000 }); + assert.equal(result.exitCode, 0, `${result.stdout} ${result.stderr}`); + assert.match(result.stdout, /Installation complete: MongoDB Shell \d+\.\d+\.\d+/); + }); + + test('installed mongosh should appear in installed list', async () => { + await run(['mongosh', '2.5.6'], { timeout: 120_000 }); + const result = await run(['mongosh', 'installed']); + assert.equal(result.exitCode, 0, `${result.stdout} ${result.stderr}`); + assert.match(result.stdout, /2\.5\.6/); + }); + + test('should reactivate already installed mongosh version', async () => { + const installedResult = await run(['mongosh', 'installed', '--json']); + const installed = JSON.parse(installedResult.stdout); + assert.ok(installed.length > 0, 'Should have at least one installed mongosh version'); + const version = installed[0].name; // JSON uses 'name' field + const result = await run(['mongosh', version]); + assert.equal(result.exitCode, 0); + assert.match(result.stdout, /already active|Installation complete/i); + }); + + test('should clean up installed mongosh after tests', async () => { + const installedResult = await run(['mongosh', 'installed', '--json']); + const installed = JSON.parse(installedResult.stdout); + if (installed.length > 0) { + const versions = installed.map(v => v.name); // JSON uses 'name' field + const result = await run(['mongosh', 'rm', ...versions]); + assert.equal(result.exitCode, 0); + } + const installedResult2 = await run(['mongosh', 'installed', '--json']); + const installed2 = JSON.parse(installedResult2.stdout); + assert.ok(installed2.length === 0, 'Should have no installed mongosh versions'); + }); }); describe('Hook Management Commands', () => { @@ -342,49 +455,84 @@ describe('m - MongoDB Version Management', { concurrency: 5 }, () => { describe('Version Management Commands (Error Cases)', () => { test('should fail to execute mongod for non-installed version', async () => { const result = await run(['use', '7.0.0', '--version']); - assert.notEqual(result.exitCode, 0); + assert.notEqual(result.exitCode, 0, `${result.stdout} ${result.stderr}`); assert.match(result.stdout, /not installed/); }); test('should fail to execute mongos for non-installed version', async () => { const result = await run(['shard', '7.0.0', '--version']); - assert.notEqual(result.exitCode, 0); + assert.notEqual(result.exitCode, 0, `${result.stdout} ${result.stderr}`); assert.match(result.stdout, /not installed/); }); test('should fail to execute mongo shell for non-installed version', async () => { const result = await run(['shell', '7.0.0', '--version']); - assert.notEqual(result.exitCode, 0); + assert.notEqual(result.exitCode, 0, `${result.stdout} ${result.stderr}`); + assert.match(result.stdout, /not installed/); + }); + + test('should fail to execute mongo shell with s alias for non-installed version', async () => { + const result = await run(['s', '7.0.0', '--version']); + assert.notEqual(result.exitCode, 0, `${result.stdout} ${result.stderr}`); assert.match(result.stdout, /not installed/); }); + test('should fail to execute mongo shell with mongo alias for non-installed version', async () => { + const result = await run(['mongo', '7.0.0', '--version']); + assert.notEqual(result.exitCode, 0, `${result.stdout} ${result.stderr}`); + assert.match(result.stdout, /not installed/); + }); + + test('shell command should fallback to legacy mongo', async () => { + const result1 = await run(['4.4.29']); + assert.equal(result1.exitCode, 0, `${result1.stdout} ${result1.stderr}`); + + const result2 = await run(['s', '4.4.29', '--version']); + if (result2.exitCode === 0) { + // Legacy mongo shell may not be installable in all CI environments + assert.match(result2.stdout, /version v4.4.29/); + } + }); + test('should succeed when removing non-installed version', async () => { const result = await run(['rm', '7.0.0']); - assert.equal(result.exitCode, 0); + assert.equal(result.exitCode, 0, `${result.stdout} ${result.stderr}`); assert.match(result.stdout, /not installed/); }); test('should fail when no version provided to use command', async () => { const result = await run(['use']); - assert.notEqual(result.exitCode, 0); + assert.notEqual(result.exitCode, 0, `${result.stdout} ${result.stderr}`); assert.match(result.stdout, /version required/); }); test('should fail when no version provided to shard command', async () => { const result = await run(['shard']); - assert.notEqual(result.exitCode, 0); + assert.notEqual(result.exitCode, 0, `${result.stdout} ${result.stderr}`); assert.match(result.stdout, /version required/); }); test('should fail when no version provided to shell command', async () => { const result = await run(['shell']); - assert.notEqual(result.exitCode, 0); + assert.notEqual(result.exitCode, 0, `${result.stdout} ${result.stderr}`); + assert.match(result.stdout, /version required/); + }); + + test('should fail when no version provided to s alias', async () => { + const result = await run(['s']); + assert.notEqual(result.exitCode, 0, `${result.stdout} ${result.stderr}`); + assert.match(result.stdout, /version required/); + }); + + test('should fail when no version provided to mongo alias', async () => { + const result = await run(['mongo']); + assert.notEqual(result.exitCode, 0, `${result.stdout} ${result.stderr}`); assert.match(result.stdout, /version required/); }); test('should fail when no version provided to rm command', async () => { const result = await run(['rm']); - assert.equal(result.exitCode, 1); + assert.equal(result.exitCode, 1, `${result.stdout} ${result.stderr}`); assert.match(result.stdout, /version.*required/); }); });