1#! /bin/sh
2# Copyright (C) 2011-2021 Free Software Foundation, Inc.
3#
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.
8#
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.
13#
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/>.
16
17# Check that python support can work well with virtualenvs.
18# This test also works as a mild stress-test on the python support.
19
20required='cc python virtualenv'
21. test-init.sh
22
23# In case the user's config.site defines pythondir or pyexecdir.
24CONFIG_SITE=/dev/null; export CONFIG_SITE
25
26py_version_pre=$($PYTHON -V)
27
28# Skip the test if a proper virtualenv cannot be created.
29virtualenv -p"$PYTHON" --verbose virtenv && py_installed virtenv/bin/activate \
30  || skip_ "couldn't create python virtual environment"
31
32# Activate the virtualenv.
33. ./virtenv/bin/activate
34# Sanity check.
35if test -z "$VIRTUAL_ENV"; then
36  framework_failure_ "can't activate python virtual environment"
37fi
38
39py_version_post=$(python -V)
40
41# Sanity check.
42test "$py_version_pre" = "$py_version_post" \
43  || skip_ "virtualenv $py_version_post != $py_version_pre"
44
45cwd=$(pwd) || fatal_ "getting current working directory"
46py_version=$(python -c 'import sys; print("%u.%u" % tuple(sys.version_info[:2]))')
47py_site=$VIRTUAL_ENV/lib/python$py_version/site-packages
48
49# We need to do this early, just to set some cache variables properly,
50# since because we're going to unset $PYTHON next.
51if python_has_pep3147; then
52  : PEP 3147 will be used in installation of ".pyc" files
53fi
54# We don't want our original python to be picked up by configure
55# invocations.
56unset PYTHON
57
58# We need control over the package name.
59cat > configure.ac << END
60AC_INIT([am_virtenv], [1.0])
61AM_INIT_AUTOMAKE
62AC_CONFIG_FILES([Makefile])
63AC_PROG_CC
64AM_PROG_AR
65AC_PROG_RANLIB
66AM_PATH_PYTHON
67AC_OUTPUT
68END
69
70cat > Makefile.am << 'END'
71python_PYTHON = am_foo.py
72pkgpython_PYTHON = __init__.py
73pyexec_LIBRARIES = libquux.a
74libquux_a_SOURCES = foo.c
75pkgpyexec_LIBRARIES = libzardoz.a
76libzardoz_a_SOURCES = foo.c
77
78.PYTHON: debug test-run
79debug:
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)
87test-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")'
102all-local: debug
103END
104
105cat > am_foo.py << 'END'
106def foo_func ():
107    return 12345
108END
109
110cat > __init__.py << 'END'
111def old_am ():
112    return 'AutoMake'
113END
114
115cat > foo.c << 'END'
116int foo (void)
117{
118  return 0;
119}
120END
121
122check_install ()
123{
124  $MAKE install ${1+"$@"}
125
126  test -f      "$py_site"/am_foo.py
127  py_installed "$py_site"/am_foo.pyc
128  py_installed "$py_site"/am_virtenv/__init__.py
129  py_installed "$py_site"/am_virtenv/__init__.pyc
130  test -f      "$py_site"/libquux.a
131  test -f      "$py_site"/am_virtenv/libzardoz.a
132}
133
134check_uninstall ()
135{
136  $MAKE uninstall ${1+"$@"}
137
138  test ! -e          "$py_site"/am_foo.py
139  py_installed --not "$py_site"/am_foo.pyc
140  test ! -e          "$py_site"/am_virtenv/__init__.py
141  py_installed --not "$py_site"/am_virtenv/__init__.pyc
142  test ! -e          "$py_site"/libquux.a
143  test ! -e          "$py_site"/am_virtenv/libzardoz.a
144}
145
146$ACLOCAL
147$AUTOCONF
148$AUTOMAKE --add-missing
149
150# Try a VPATH build.
151mkdir build
152cd build
153../configure --prefix="$VIRTUAL_ENV"
154check_install
155$MAKE test-run
156check_uninstall
157cd ..
158
159# Try an in-tree build.
160./configure --prefix="$VIRTUAL_ENV"
161check_install
162$MAKE test-run
163check_uninstall
164
165$MAKE distclean
166
167# Overriding pythondir and pyexecdir with cache variables should work.
168./configure am_cv_python_pythondir="$py_site" \
169            am_cv_python_pyexecdir="$py_site"
170check_install
171$MAKE test-run
172check_uninstall
173
174$MAKE distclean
175
176# Overriding pythondir and pyexecdir at make time should be enough.
177./configure --prefix="$cwd/bad-prefix"
178check_install pythondir="$py_site" pyexecdir="$py_site" \
179              AM_MAKEFLAGS="pythondir='$py_site' pyexecdir='$py_site'"
180test ! -e bad-prefix
181$MAKE test-run
182check_uninstall pythondir="$py_site" pyexecdir="$py_site" \
183                AM_MAKEFLAGS="pythondir='$py_site' pyexecdir='$py_site'"
184
185# Also check that the distribution is self-contained, for completeness.
186$MAKE distcheck
187
188# Finally, check that if we disable the virtualenv, we shouldn't be
189# able to access to the installed modules anymore.
190cd build
191$MAKE install
192python -c 'import am_foo; print(am_foo.__file__)'
193python -c 'import am_virtenv; print(am_virtenv.__file__)'
194deactivate "nondestructive"
195python -c 'import am_foo' && exit 1
196python -c 'import am_virtenv' && exit 1
197
198:
199