doc: typos in test file.
[automake.git] / t / python-virtualenv.sh
blobc5764aff359ea09cbea8b6d2e07baca24e35b8b0
1 #! /bin/sh
2 # Copyright (C) 2011-2024 Free Software Foundation, Inc.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
7 # any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17 # Check that python support can work well with virtualenvs.
18 # This test also works as a mild stress-test on the python support.
20 required='cc python virtualenv'
21 . test-init.sh
23 # In case the user's config.site defines pythondir or pyexecdir.
24 CONFIG_SITE=/dev/null; export CONFIG_SITE
26 py_version_pre=$($PYTHON -V)
28 # Skip the test if a proper virtualenv cannot be created.
29 virtualenv -p"$PYTHON" --verbose virtenv && py_installed virtenv/bin/activate \
30 || skip_ "couldn't create python virtual environment"
32 # Activate the virtualenv.
33 . ./virtenv/bin/activate
34 # Sanity check.
35 if test -z "$VIRTUAL_ENV"; then
36 framework_failure_ "can't activate python virtual environment"
39 py_version_post=$(python -V)
41 # Sanity check.
42 test "$py_version_pre" = "$py_version_post" \
43 || skip_ "virtualenv $py_version_post != $py_version_pre"
45 cwd=$(pwd) || fatal_ "getting current working directory"
46 py_version=$(python -c 'import sys; print("%u.%u" % tuple(sys.version_info[:2]))')
47 py_site=$VIRTUAL_ENV/lib/python$py_version/site-packages
49 # We need to do this early, just to set some cache variables properly,
50 # since because we're going to unset $PYTHON next.
51 if python_has_pep3147; then
52 : PEP 3147 will be used in installation of ".pyc" files
54 # We don't want our original python to be picked up by configure
55 # invocations.
56 unset PYTHON
58 # We need control over the package name.
59 cat > configure.ac << END
60 AC_INIT([am_virtenv], [1.0])
61 AM_INIT_AUTOMAKE
62 AC_CONFIG_FILES([Makefile])
63 AC_PROG_CC
64 AM_PROG_AR
65 AC_PROG_RANLIB
66 AM_PATH_PYTHON
67 AC_OUTPUT
68 END
70 cat > Makefile.am << 'END'
71 python_PYTHON = am_foo.py
72 pkgpython_PYTHON = __init__.py
73 pyexec_LIBRARIES = libquux.a
74 libquux_a_SOURCES = foo.c
75 pkgpyexec_LIBRARIES = libzardoz.a
76 libzardoz_a_SOURCES = foo.c
78 .PYTHON: debug test-run
79 debug:
80 @echo PYTHON: $(PYTHON)
81 @echo PYTHON_VERSION: $(PYTHON_VERSION)
82 @echo prefix: $(prefix)
83 @echo pythondir: $(pythondir)
84 @echo pkgpythondir: $(pkgpythondir)
85 @echo pyexecdir: $(pyexecdir)
86 @echo pkgpyexecdir: $(pkgpyexecdir)
87 test-run:
88 ## In a virtualenv, the default python must be the custom
89 ## virtualenv python.
90 @: \
91 && py1=`python -c 'import sys; print(sys.executable)'` \
92 && py2=`$(PYTHON) -c 'import sys; print(sys.executable)'` \
93 && echo "py1: $$py1" \
94 && echo "py2: $$py2" \
95 && test -n "$$py1" \
96 && test -n "$$py2" \
97 && test x"$$py1" = x"$$py2"
98 ## Check that modules installed in the virtualenv are readily
99 ## available.
100 python -c 'from am_foo import foo_func; assert (foo_func () == 12345)'
101 python -c 'from am_virtenv import old_am; assert (old_am () == "AutoMake")'
102 all-local: debug
103 get-pyexecdir:
104 @echo $(pyexecdir)
107 cat > am_foo.py << 'END'
108 def foo_func ():
109 return 12345
112 cat > __init__.py << 'END'
113 def old_am ():
114 return 'AutoMake'
117 cat > foo.c << 'END'
118 int foo (void)
120 return 0;
124 check_install ()
126 $MAKE install ${1+"$@"}
128 test -f "$py_site"/am_foo.py
129 py_installed "$py_site"/am_foo.pyc
130 py_installed "$py_site"/am_virtenv/__init__.py
131 py_installed "$py_site"/am_virtenv/__init__.pyc
132 test -f "$($MAKE get-pyexecdir ${1+"$@"})"/libquux.a
133 test -f "$($MAKE get-pyexecdir ${1+"$@"})"/am_virtenv/libzardoz.a
136 check_uninstall ()
138 $MAKE uninstall ${1+"$@"}
140 test ! -e "$py_site"/am_foo.py
141 py_installed --not "$py_site"/am_foo.pyc
142 test ! -e "$py_site"/am_virtenv/__init__.py
143 py_installed --not "$py_site"/am_virtenv/__init__.pyc
144 test ! -e "$($MAKE get-pyexecdir ${1+"$@"})"/libquux.a
145 test ! -e "$($MAKE get-pyexecdir ${1+"$@"})"/am_virtenv/libzardoz.a
148 $ACLOCAL
149 $AUTOCONF
150 $AUTOMAKE --add-missing
152 # Try a VPATH build.
153 mkdir build
154 cd build
155 ../configure --prefix="$VIRTUAL_ENV"
156 check_install
157 $MAKE test-run
158 check_uninstall
159 cd ..
161 # Try an in-tree build.
162 ./configure --prefix="$VIRTUAL_ENV"
163 check_install
164 $MAKE test-run
165 check_uninstall
167 $MAKE distclean
169 # Overriding pythondir and pyexecdir with cache variables should work.
170 ./configure am_cv_python_pythondir="$py_site" \
171 am_cv_python_pyexecdir="$py_site"
172 check_install
173 $MAKE test-run
174 check_uninstall
176 $MAKE distclean
178 # Overriding pythondir and pyexecdir at make time should be enough.
179 ./configure --prefix="$cwd/bad-prefix"
180 check_install pythondir="$py_site" pyexecdir="$py_site" \
181 AM_MAKEFLAGS="pythondir='$py_site' pyexecdir='$py_site'"
182 test ! -e bad-prefix
183 $MAKE test-run
184 check_uninstall pythondir="$py_site" pyexecdir="$py_site" \
185 AM_MAKEFLAGS="pythondir='$py_site' pyexecdir='$py_site'"
187 # Also check that the distribution is self-contained, for completeness.
188 $MAKE distcheck
190 # Finally, check that if we disable the virtualenv, we shouldn't be
191 # able to access to the installed modules anymore.
192 cd build
193 $MAKE install
194 python -c 'import am_foo; print(am_foo.__file__)'
195 python -c 'import am_virtenv; print(am_virtenv.__file__)'
196 deactivate "nondestructive"
197 python -c 'import am_foo' && exit 1
198 python -c 'import am_virtenv' && exit 1