maint: Update HACKING
[automake.git] / t / python-virtualenv.sh
blob5ec32f553549335869e069562c04dc700ba3dd0b
1 #! /bin/sh
2 # Copyright (C) 2011-2017 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"
44 cwd=$(pwd) || fatal_ "getting current working directory"
45 py_version=$(python -c 'import sys; print("%u.%u" % tuple(sys.version_info[:2]))')
46 py_site=$VIRTUAL_ENV/lib/python$py_version/site-packages
48 # We need to do do this early, just to set some cache variables properly,
49 # since because we're going to unset $PYTHON next.
50 if python_has_pep3147; then
51 : PEP 3147 will be used in installation of ".pyc" files
53 # We don't want our original python to be picked up by configure
54 # invocations.
55 unset PYTHON
57 # We need control over the package name.
58 cat > configure.ac << END
59 AC_INIT([am_virtenv], [1.0])
60 AM_INIT_AUTOMAKE
61 AC_CONFIG_FILES([Makefile])
62 AC_PROG_CC
63 AM_PROG_AR
64 AC_PROG_RANLIB
65 AM_PATH_PYTHON
66 AC_OUTPUT
67 END
69 cat > Makefile.am << 'END'
70 python_PYTHON = am_foo.py
71 pkgpython_PYTHON = __init__.py
72 pyexec_LIBRARIES = libquux.a
73 libquux_a_SOURCES = foo.c
74 pkgpyexec_LIBRARIES = libzardoz.a
75 libzardoz_a_SOURCES = foo.c
77 .PYTHON: debug test-run
78 debug:
79 @echo PYTHON: $(PYTHON)
80 @echo PYTHON_VERSION: $(PYTHON_VERSION)
81 @echo prefix: $(prefix)
82 @echo pythondir: $(pythondir)
83 @echo pkgpythondir: $(pkgpythondir)
84 @echo pyexecdir: $(pyexecdir)
85 @echo pkgpyexecdir: $(pkgpyexecdir)
86 test-run:
87 ## In a virtualenv, the default python must be the custom
88 ## virtualenv python.
89 @: \
90 && py1=`python -c 'import sys; print(sys.executable)'` \
91 && py2=`$(PYTHON) -c 'import sys; print(sys.executable)'` \
92 && echo "py1: $$py1" \
93 && echo "py2: $$py2" \
94 && test -n "$$py1" \
95 && test -n "$$py2" \
96 && test x"$$py1" = x"$$py2"
97 ## Check that modules installed in the virtualenv are readily
98 ## available.
99 python -c 'from am_foo import foo_func; assert (foo_func () == 12345)'
100 python -c 'from am_virtenv import old_am; assert (old_am () == "AutoMake")'
101 all-local: debug
104 cat > am_foo.py << 'END'
105 def foo_func ():
106 return 12345
109 cat > __init__.py << 'END'
110 def old_am ():
111 return 'AutoMake'
114 cat > foo.c << 'END'
115 int foo (void)
117 return 0;
121 check_install ()
123 $MAKE install ${1+"$@"}
125 test -f "$py_site"/am_foo.py
126 py_installed "$py_site"/am_foo.pyc
127 py_installed "$py_site"/am_foo.pyo
128 py_installed "$py_site"/am_virtenv/__init__.py
129 py_installed "$py_site"/am_virtenv/__init__.pyc
130 py_installed "$py_site"/am_virtenv/__init__.pyo
131 test -f "$py_site"/libquux.a
132 test -f "$py_site"/am_virtenv/libzardoz.a
135 check_uninstall ()
137 $MAKE uninstall ${1+"$@"}
139 test ! -e "$py_site"/am_foo.py
140 py_installed --not "$py_site"/am_foo.pyc
141 py_installed --not "$py_site"/am_foo.pyo
142 test ! -e "$py_site"/am_virtenv/__init__.py
143 py_installed --not "$py_site"/am_virtenv/__init__.pyc
144 py_installed --not "$py_site"/am_virtenv/__init__.pyo
145 test ! -e "$py_site"/libquux.a
146 test ! -e "$py_site"/am_virtenv/libzardoz.a
149 $ACLOCAL
150 $AUTOCONF
151 $AUTOMAKE --add-missing
153 # Try a VPATH build.
154 mkdir build
155 cd build
156 ../configure --prefix="$VIRTUAL_ENV"
157 check_install
158 $MAKE test-run
159 check_uninstall
160 cd ..
162 # Try an in-tree build.
163 ./configure --prefix="$VIRTUAL_ENV"
164 check_install
165 $MAKE test-run
166 check_uninstall
168 $MAKE distclean
170 # Overriding pythondir and pyexecdir with cache variables should work.
171 ./configure am_cv_python_pythondir="$py_site" \
172 am_cv_python_pyexecdir="$py_site"
173 check_install
174 $MAKE test-run
175 check_uninstall
177 $MAKE distclean
179 # Overriding pythondir and pyexecdir at make time should be enough.
180 ./configure --prefix="$cwd/bad-prefix"
181 check_install pythondir="$py_site" pyexecdir="$py_site" \
182 AM_MAKEFLAGS="pythondir='$py_site' pyexecdir='$py_site'"
183 test ! -e bad-prefix
184 $MAKE test-run
185 check_uninstall pythondir="$py_site" pyexecdir="$py_site" \
186 AM_MAKEFLAGS="pythondir='$py_site' pyexecdir='$py_site'"
188 # Also check that the distribution is self-contained, for completeness.
189 $MAKE distcheck
191 # Finally, check that if we disable the virtualenv, we shouldn't be
192 # able to access to the installed modules anymore.
193 cd build
194 $MAKE install
195 python -c 'import am_foo; print(am_foo.__file__)'
196 python -c 'import am_virtenv; print(am_virtenv.__file__)'
197 deactivate "nondestructive"
198 python -c 'import am_foo' && exit 1
199 python -c 'import am_virtenv' && exit 1