diff --git a/lib/ansible/modules/packaging/language/pip.py b/lib/ansible/modules/packaging/language/pip.py index 12e90cd0bf..85bdb403bb 100644 --- a/lib/ansible/modules/packaging/language/pip.py +++ b/lib/ansible/modules/packaging/language/pip.py @@ -457,7 +457,7 @@ def main(): env = module.params['virtualenv'] venv_created = False - if chdir: + if env and chdir: env = os.path.join(chdir, env) if umask and not isinstance(umask, int): diff --git a/test/integration/targets/pip/files/ansible_test_pip_chdir/__init__.py b/test/integration/targets/pip/files/ansible_test_pip_chdir/__init__.py new file mode 100644 index 0000000000..c8a79430f9 --- /dev/null +++ b/test/integration/targets/pip/files/ansible_test_pip_chdir/__init__.py @@ -0,0 +1,2 @@ +def main(): + print("success") diff --git a/test/integration/targets/pip/files/setup.py b/test/integration/targets/pip/files/setup.py new file mode 100755 index 0000000000..094064b7c0 --- /dev/null +++ b/test/integration/targets/pip/files/setup.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python + +from setuptools import setup, find_packages + +setup( + name="ansible_test_pip_chdir", + version="0", + packages=find_packages(), + entry_points={ + 'console_scripts': [ + 'ansible_test_pip_chdir = ansible_test_pip_chdir:main' + ] + } +) diff --git a/test/integration/targets/pip/tasks/pip.yml b/test/integration/targets/pip/tasks/pip.yml index 11716f2bdb..00293316ef 100644 --- a/test/integration/targets/pip/tasks/pip.yml +++ b/test/integration/targets/pip/tasks/pip.yml @@ -230,6 +230,48 @@ that: - "pip_install_venv is changed" +# https://github.com/ansible/ansible/issues/37912 +# support chdir without virtualenv +- name: create chdir test directories + file: + state: directory + name: "{{ output_dir }}/{{ item }}" + loop: + - pip_module + - pip_root + - pip_module/ansible_test_pip_chdir + +- name: copy test module + copy: + src: "{{ item }}" + dest: "{{ output_dir }}/pip_module/{{ item }}" + loop: + - setup.py + - ansible_test_pip_chdir/__init__.py + +- name: install test module + pip: + name: . + chdir: "{{ output_dir }}/pip_module" + extra_args: --user --upgrade --root {{ output_dir }}/pip_root + +- name: register python_site_lib + command: 'python -c "import site; print(site.USER_SITE)"' + register: pip_python_site_lib + +- name: register python_user_base + command: 'python -c "import site; print(site.USER_BASE)"' + register: pip_python_user_base + +- name: run test module + shell: "PYTHONPATH=$(echo {{ output_dir }}/pip_root{{ pip_python_site_lib.stdout }}) {{ output_dir }}/pip_root{{ pip_python_user_base.stdout }}/bin/ansible_test_pip_chdir" + register: pip_chdir_command + +- name: make sure command ran + assert: + that: + - pip_chdir_command.stdout == "success" + # https://github.com/ansible/ansible/issues/25122 - name: ensure is a fresh virtualenv file: