Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to detect uv based projects #205

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions autoswitch_virtualenv.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ function _get_venv_type() {
venv_type="poetry"
elif [[ -f "$venv_dir/requirements.txt" || -f "$venv_dir/setup.py" ]]; then
venv_type="virtualenv"
elif [[ -f "$venv_dir/uv.lock" ]]; then
venv_type="uv"
elif [[ -f "$venv_dir/pyproject.toml" && $(grep -c '[tool.uv]' "$venv_dir/pyproject.toml") -gt 0 ]]; then
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this line be turned into a function?

$(grep -c '[tool.uv]' "$venv_dir/pyproject.toml") -gt 0

Something like

function _check_pyproject_type {
  local result
  if [[ $(grep -c '[tool.uv]' "$venv_dir/pyproject.toml") -gt 0 ]]; then
     result = "uv"
  elif [[ $(grep -c '[tool.poetry]' "$venv_dir/pyproject.toml") -gt 0 ]]; then
    result = "poetry"
  ....

  printf "$result"
}
elif [[ -f "$venv_dir/pyproject.toml" ]]; then
    venv_type="$(_check_pyproject_type)"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm. With uv 0.4.27 bringing support for dependency groups as described by PEP 735, and the replacement of

[tool.uv]
dev-dependencies = ["some-dependencies"]

by

[dependency-groups]
dev = ["some-dependencies"]

I don't really know now how to detect that a pyproject.toml file can and is supposed to be handled by uv.
I guess we can't just assume all pyproject.toml are handled by uv.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MichaelAquilina gentle reminder for reviewing this ticket if you can spare the time 🙏

Copy link

@hanslemm hanslemm Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@afeblot I would suggest to use the lock file to determine if it is a poetry or uv managed project. If uv.lock or poetry.lock, handle accordingly.

venv_type="uv"
fi
printf "%s" "$venv_type"
}
Expand Down Expand Up @@ -160,6 +164,12 @@ function _activate_pipenv() {
}


function _activate_uv() {
_maybeworkon ".venv" "virtualenv"
return 0
}


# Automatically switch virtualenv when $AUTOSWITCH_FILE file detected
function check_venv()
{
Expand Down Expand Up @@ -318,6 +328,14 @@ function mkvenv()
poetry install $params
_activate_poetry
return
elif [[ "$venv_type" == "uv" ]]; then
if ! type "uv" > /dev/null; then
_missing_error_message uv
return
fi
uv sync $params
_activate_uv
return
else
if ! type "virtualenv" > /dev/null; then
_missing_error_message virtualenv
Expand Down
27 changes: 27 additions & 0 deletions tests/test_get_venv_type.zunit
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,33 @@
assert "$output" same_as "poetry"
}

@test '_get_venv_type uv not found' {
touch "$TARGET/pyproject.toml"

run _get_venv_type "$TARGET"

assert $state equals 0
assert "$output" same_as "virtualenv"
}

@test '_get_venv_type uv found from pyproject.toml' {
echo "[uv.tool]" > "$TARGET/pyproject.toml"

run _get_venv_type "$TARGET"

assert $state equals 0
assert "$output" same_as "uv"
}

@test '_get_venv_type uv found from uv.lock' {
touch "$TARGET/uv.lock"

run _get_venv_type "$TARGET"

assert $state equals 0
assert "$output" same_as "uv"
}

@test '_get_venv_type virtualenv (requirements.txt)' {
touch "$TARGET/requirements.txt"

Expand Down
42 changes: 42 additions & 0 deletions tests/test_mkvenv.zunit
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
function _activate_pipenv {
echo "activating pipenv"
}

function uv {
echo "uv" $@
}

function _activate_uv {
echo "activating uv"
}
}

@teardown {
Expand Down Expand Up @@ -81,6 +89,18 @@
assert "$lines[2]" same_as "activating pipenv"
}

@test 'mkvenv - (uv project) runs correct command' {
mkdir myproject
cd myproject
touch "uv.lock"

run mkvenv

assert $status equals 0
assert "$lines[1]" same_as "uv sync"
assert "$lines[2]" same_as "activating uv"
}

@test 'mkvenv - uses default python if set and not specified' {
mkdir myproject
cd myproject
Expand Down Expand Up @@ -189,3 +209,25 @@
assert "$lines[5]" is_empty
assert ${#lines} equals 4
}

@test 'prints help message and disables plugin if uv not setup' {
touch "uv.lock"

# Mock type to fail
function type() {
if [[ "$1" == "uv" ]]; then
return 1
fi
return 0
}

run mkvenv

assert $status equals 0
assert "$lines[1]" contains "zsh-autoswitch-virtualenv requires 'uv' to install this project!"
assert "$lines[2]" is_empty
assert "$lines[3]" contains "If this is already installed but you are still seeing this message,"
assert "$lines[4]" contains "then make sure the \e[1muv\e[0m command is in your PATH.\n"
assert "$lines[5]" is_empty
assert ${#lines} equals 4
}