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
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Features
Bug Fixes
---------
* Let interactive changes to the prompt format respect dynamically-computed values.
* Better handle arguments to `system cd`.


1.56.0 (2026/02/23)
Expand Down
23 changes: 17 additions & 6 deletions mycli/packages/special/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import logging
import os
import subprocess
import shlex

import click
import pymysql
from pymysql.cursors import Cursor

from mycli.compat import WIN

logger = logging.getLogger(__name__)

CACHED_SSL_VERSION: dict[tuple, str | None] = {}
Expand All @@ -13,13 +16,21 @@
def handle_cd_command(arg: str) -> tuple[bool, str | None]:
"""Handles a `cd` shell command by calling python's os.chdir."""
CD_CMD = "cd"
tokens = arg.split(CD_CMD + " ")
directory = tokens[-1] if len(tokens) > 1 else None
if not directory:
return False, "No folder name was provided."
tokens: list[str] = []
try:
tokens = shlex.split(arg, posix=not WIN)
except ValueError:
return False, 'Cannot parse cd command.'
if not tokens:
return False, 'Not a cd command.'
if not tokens[0].lower() == CD_CMD:
return False, 'Not a cd command.'
if len(tokens) != 2:
return False, 'Exactly one directory name must be provided.'
directory = tokens[1]
try:
os.chdir(directory)
subprocess.call(["pwd"])
click.echo(os.getcwd(), err=True)
return True, None
except OSError as e:
return False, e.strerror
Expand Down
27 changes: 26 additions & 1 deletion test/test_sqlexecute.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,32 @@ def test_special_command(executor):
@dbtest
def test_cd_command_without_a_folder_name(executor):
results = run(executor, "system cd")
assert_result_equal(results, status="No folder name was provided.")
assert_result_equal(results, status="Exactly one directory name must be provided.")


@dbtest
def test_cd_command_with_one_nonexistent_folder_name(executor):
results = run(executor, 'system cd nonexistent_folder_name')
assert_result_equal(results, status='No such file or directory')


@dbtest
def test_cd_command_with_one_real_folder_name(executor):
results = run(executor, 'system cd screenshots')
# todo would be better to capture stderr but there was a problem with capsys
assert results[0]['status'] == ''


@dbtest
def test_cd_command_with_two_folder_names(executor):
results = run(executor, "system cd one two")
assert_result_equal(results, status='Exactly one directory name must be provided.')


@dbtest
def test_cd_command_unbalanced(executor):
results = run(executor, "system cd 'one")
assert_result_equal(results, status='Cannot parse cd command.')


@dbtest
Expand Down