Skip to content

Commit 387ada3

Browse files
Make bash kernel ID file path determination more robust
- Replace brittle string replacement with robust path construction using os.path.dirname() - Add bash_kernel_id_file property to Config class for centralized path management - Update tests to use new robust approach - Add test coverage for custom kernel file path scenarios - Ensures compatibility with any custom CODERUNNER_KERNEL_ID_FILE configuration
1 parent 41e16a4 commit 387ada3

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ def __init__(self, **kwargs):
4444
if self.kernel_id_file is None:
4545
self.kernel_id_file = str(self.shared_dir / "python_kernel_id.txt")
4646

47+
@property
48+
def bash_kernel_id_file(self) -> str:
49+
"""Get the path for the bash kernel ID file"""
50+
return os.path.join(os.path.dirname(self.kernel_id_file), "bash_kernel_id.txt")
51+
4752
@property
4853
def jupyter_ws_base_url(self) -> str:
4954
"""Get the base WebSocket URL for Jupyter"""

jupyter_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def _load_kernel_ids(self) -> None:
3838
"""Load kernel IDs from files"""
3939
kernel_files = {
4040
"python": config.kernel_id_file,
41-
"bash": config.kernel_id_file.replace("python_kernel_id.txt", "bash_kernel_id.txt")
41+
"bash": config.bash_kernel_id_file
4242
}
4343

4444
for kernel_type, kernel_file in kernel_files.items():

tests/test_config.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,20 @@ def test_config_kernel_id_file_path():
7373
shared_dir = pathlib.Path(tmp_dir) / "uploads"
7474
config = Config(shared_dir=shared_dir)
7575

76-
expected_path = str(shared_dir / "python_kernel_id.txt")
77-
assert config.kernel_id_file == expected_path
76+
expected_python_path = str(shared_dir / "python_kernel_id.txt")
77+
expected_bash_path = str(shared_dir / "bash_kernel_id.txt")
78+
79+
assert config.kernel_id_file == expected_python_path
80+
assert config.bash_kernel_id_file == expected_bash_path
81+
82+
83+
def test_config_bash_kernel_id_file_with_custom_path():
84+
"""Test that bash kernel ID file path is robust to custom python kernel path"""
85+
with tempfile.TemporaryDirectory() as tmp_dir:
86+
custom_python_path = os.path.join(tmp_dir, "custom", "my_python_kernel.txt")
87+
os.makedirs(os.path.dirname(custom_python_path), exist_ok=True)
88+
89+
config = Config(kernel_id_file=custom_python_path)
90+
91+
expected_bash_path = os.path.join(tmp_dir, "custom", "bash_kernel_id.txt")
92+
assert config.bash_kernel_id_file == expected_bash_path

tests/test_jupyter_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def test_init_with_bash_kernel(self, mock_config):
7777
with open(mock_config.kernel_id_file, 'w') as f:
7878
f.write(python_kernel_id)
7979

80-
bash_kernel_file = mock_config.kernel_id_file.replace("python_kernel_id.txt", "bash_kernel_id.txt")
80+
bash_kernel_file = mock_config.bash_kernel_id_file
8181
with open(bash_kernel_file, 'w') as f:
8282
f.write(bash_kernel_id)
8383

@@ -238,7 +238,7 @@ async def test_execute_code_connection_error(self, jupyter_client_with_kernel):
238238
async def test_execute_bash_code_success(self, mock_config):
239239
"""Test successful bash code execution"""
240240
# Create bash kernel file
241-
bash_kernel_file = mock_config.kernel_id_file.replace("python_kernel_id.txt", "bash_kernel_id.txt")
241+
bash_kernel_file = mock_config.bash_kernel_id_file
242242
with open(bash_kernel_file, 'w') as f:
243243
f.write("bash-kernel-789")
244244

@@ -311,7 +311,7 @@ def test_reload_kernel_ids(self, mock_config):
311311
with open(mock_config.kernel_id_file, 'w') as f:
312312
f.write("new-python-kernel-456")
313313

314-
bash_kernel_file = mock_config.kernel_id_file.replace("python_kernel_id.txt", "bash_kernel_id.txt")
314+
bash_kernel_file = mock_config.bash_kernel_id_file
315315
with open(bash_kernel_file, 'w') as f:
316316
f.write("new-bash-kernel-789")
317317

0 commit comments

Comments
 (0)