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

feature: Added support for pyproject.toml. #117

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
25 changes: 17 additions & 8 deletions autoswitch_virtualenv.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function _virtual_env_dir() {

function _python_version() {
local PYTHON_BIN="$1"
if [[ -f "$PYTHON_BIN" ]] then
if [[ -f "$PYTHON_BIN" ]]; then
# For some reason python --version writes to stderr
printf "%s" "$($PYTHON_BIN --version 2>&1)"
else
Expand All @@ -38,7 +38,7 @@ function _get_venv_type() {
local venv_type="${2:-virtualenv}"
if [[ -f "$venv_dir/Pipfile" ]]; then
venv_type="pipenv"
elif [[ -f "$venv_dir/requirements.txt" || -f "$venv_dir/setup.py" ]]; then
elif [[ -f "$venv_dir/requirements.txt" || -f "$venv_dir/setup.py" || -f "$venv_dir/pyproject.toml" ]]; then
venv_type="virtualenv"
fi
printf "%s" "$venv_type"
Expand All @@ -62,15 +62,15 @@ function _get_venv_name() {
function _maybeworkon() {
local venv_dir="$1"
local venv_type="$2"
local venv_name="$(_get_venv_name $venv_dir $venv_type)"
local venv_name="$(_get_venv_name "$venv_dir" "$venv_type")"

local DEFAULT_MESSAGE_FORMAT="Switching %venv_type: ${BOLD}${PURPLE}%venv_name${NORMAL} ${GREEN}[🐍%py_version]${NORMAL}"
if [[ "$LANG" != *".UTF-8" ]]; then
# Remove multibyte characters if the terminal does not support utf-8
DEFAULT_MESSAGE_FORMAT="${DEFAULT_MESSAGE_FORMAT/🐍/}"
fi

if [[ -z "$VIRTUAL_ENV" || "$venv_name" != "$(basename $VIRTUAL_ENV)" ]]; then
if [[ -z "$VIRTUAL_ENV" || "$venv_name" != "$(basename "$VIRTUAL_ENV")" ]]; then

if [[ ! -d "$venv_dir" ]]; then
printf "Unable to find ${PURPLE}$venv_name${NORMAL} virtualenv\n"
Expand All @@ -83,7 +83,7 @@ function _maybeworkon() {
message="${message//\%venv_type/$venv_type}"
message="${message//\%venv_name/$venv_name}"
message="${message//\%py_version/$py_version}"
_autoswitch_message "${message}\n"
_autoswitch_message "${message}"

# If we are using pipenv and activate its virtual environment - turn down its verbosity
# to prevent users seeing " Pipenv found itself running within a virtual environment" warning
Expand All @@ -103,7 +103,7 @@ function _check_venv_path()
local check_dir="$1"

if [[ -f "${check_dir}/$AUTOSWITCH_FILE" ]]; then
printf "${check_dir}/$AUTOSWITCH_FILE"
printf "%s/$AUTOSWITCH_FILE" "${check_dir}"
return
else
# Abort search at file system root or HOME directory (latter is a performance optimisation).
Expand Down Expand Up @@ -179,7 +179,7 @@ function _default_venv()
_maybeworkon "$(_virtual_env_dir "$AUTOSWITCH_DEFAULTENV")" "$venv_type"
elif [[ -n "$VIRTUAL_ENV" ]]; then
local venv_name="$(_get_venv_name "$VIRTUAL_ENV" "$venv_type")"
_autoswitch_message "Deactivating: ${BOLD}${PURPLE}%s${NORMAL}\n" "$venv_name"
_autoswitch_message "Deactivating: ${BOLD}${PURPLE}$venv_name${NORMAL}\n"
deactivate
fi
}
Expand Down Expand Up @@ -228,7 +228,7 @@ function mkvenv()
if [[ -f "$AUTOSWITCH_FILE" ]]; then
printf "$AUTOSWITCH_FILE file already exists. If this is a mistake use the rmvenv command\n"
else
local venv_name="$(basename $PWD)"
local venv_name="$(basename "$PWD")"

printf "Creating ${PURPLE}%s${NONE} virtualenv\n" "$venv_name"

Expand Down Expand Up @@ -279,6 +279,15 @@ function install_requirements() {
fi
fi

if [[ -f "$PWD/pyproject.toml" ]]; then
printf "Found a ${PURPLE}pyproject.toml${NORMAL} file. Install dependencies? [y/N]: "
read ans

if [[ "$ans" = "y" || "$ans" = "Y" ]]; then
pip install .
fi
fi

setopt nullglob
local requirements
for requirements in **/*requirements.txt
Expand Down
10 changes: 10 additions & 0 deletions tests/test_check_venv.zunit
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@
assert "$output" same_as "Python project detected. Run \e[35mmkvenv\e[0m to setup autoswitching"
}

@test 'check_venv - Displays message on project detection (pyproject.toml)' {
PWD="$TARGET"
touch "$TARGET/pyproject.toml"

run check_venv

assert $status equals 0
assert "$output" same_as "Python project detected. Run \e[35mmkvenv\e[0m to setup autoswitching"
}

@test 'check_venv - Displays message on project detection (Pipfile)' {
PWD="$TARGET"
touch "$TARGET/Pipfile"
Expand Down
9 changes: 9 additions & 0 deletions tests/test_get_venv_type.zunit
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@
assert "$output" same_as "virtualenv"
}

@test '_get_venv_type virtualenv (pyproject.toml)' {
touch "$TARGET/pyproject.toml"

run _get_venv_type "$TARGET" "unknown"

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

@test '_get_venv_type virtualenv (default)' {
run _get_venv_type "$TARGET"

Expand Down
31 changes: 31 additions & 0 deletions tests/test_install_requirements.zunit
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,21 @@
assert "$lines[2]" same_as "pip install -e ."
}

@test 'install_requirements - installs pyproject.toml' {
touch "pyproject.toml"

# mock pip to make it testable
function pip {
echo "pip $@"
}

run install_requirements

assert $status equals 0
assert "$lines[1]" same_as "Found a \e[35mpyproject.toml\e[0m file. Install dependencies? [y/N]: y"
assert "$lines[2]" same_as "pip install ."
}

@test 'install_requirements - installs recursive *requirements.txt' {
echo "django" > requirements.txt
mkdir subdir
Expand Down Expand Up @@ -142,3 +157,19 @@
assert "$lines[1]" same_as "Found a \e[35msetup.py\e[0m file. Install dependencies? [y/N]: y"
assert "$lines[2]" same_as "pip install ."
}

@test 'install_requirements - installs pyproject.toml - full' {
touch "pyproject.toml"

# mock pip to make it testable
function pip {
echo "pip $@"
}
local AUTOSWITCH_PIPINSTALL="FULL"

run install_requirements

assert $status equals 0
assert "$lines[1]" same_as "Found a \e[35mpyproject.toml\e[0m file. Install dependencies? [y/N]: y"
assert "$lines[2]" same_as "pip install ."
}