Skip to content

Commit

Permalink
ensure user dir exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Tianhao-Gu committed Sep 8, 2024
1 parent 7e65823 commit e107274
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/jupyterhub_config/custom_spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ def _ensure_user_jupyter_directory(self, user_dir: Path):
environment variables for Jupyter to use these directories.
"""

if not user_dir.exists():
raise ValueError(f'User directory {user_dir} does not exist')

jupyter_dir = user_dir / '.jupyter'
jupyter_runtime_dir = jupyter_dir / 'runtime'
juputer_data_dir = jupyter_dir / 'data'
Expand Down
23 changes: 20 additions & 3 deletions test/src/jupyterhub_config/custom_spawner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,13 @@ def test_ensure_user_directory_reuse_existing(mock_exists, mock_mkdir, mock_chow


@patch('pathlib.Path.mkdir')
def test_ensure_user_jupyter_directory(mock_mkdir):
@patch('pathlib.Path.exists')
def test_ensure_user_jupyter_directory(mock_exists, mock_mkdir):
user_dir = Path('/home/testuser')

# Mock directory existence to simulate the user directory existing
mock_exists.return_value = True

spawner = VirtualEnvSpawner()
spawner._ensure_user_jupyter_directory(user_dir)

Expand All @@ -201,9 +205,22 @@ def test_ensure_user_jupyter_directory(mock_mkdir):
# Expected directories
jupyter_dir = user_dir / '.jupyter'
jupyter_runtime_dir = jupyter_dir / 'runtime'
juputer_data_dir = jupyter_dir / 'data'
jupyter_data_dir = jupyter_dir / 'data'

# Assert the JUPYTER environment variables are set correctly
assert spawner.environment['JUPYTER_CONFIG_DIR'] == str(jupyter_dir)
assert spawner.environment['JUPYTER_RUNTIME_DIR'] == str(jupyter_runtime_dir)
assert spawner.environment['JUPYTER_DATA_DIR'] == str(juputer_data_dir)
assert spawner.environment['JUPYTER_DATA_DIR'] == str(jupyter_data_dir)


@patch('pathlib.Path.exists')
def test_ensure_user_jupyter_directory_user_dir_does_not_exist(mock_exists):
user_dir = Path('/home/nonexistentuser')

# Mock directory existence to simulate the user directory not existing
mock_exists.return_value = False

spawner = VirtualEnvSpawner()

with pytest.raises(ValueError, match=f'User directory {user_dir} does not exist'):
spawner._ensure_user_jupyter_directory(user_dir)

0 comments on commit e107274

Please sign in to comment.