tests: honour $PYTHON override
[platform/upstream/automake.git] / t / python-virtualenv.sh
1 #! /bin/sh
2 # Copyright (C) 2011-2012 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 <http://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
20 required='cc python virtualenv'
21 . ./defs || exit 1
22
23 # In case the user's config.site defines pythondir or pyexecdir.
24 CONFIG_SITE=/dev/null; export CONFIG_SITE
25
26 py_version_pre=$($PYTHON -V)
27
28 # Skip the test if a proper virtualenv cannot be created.
29 virtualenv -p"$PYTHON" --verbose virtenv && test -f virtenv/bin/activate \
30   || skip_ "couldn't create python virtual environment"
31
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"
37 fi
38
39 py_version_post=$(python -V)
40
41 # Sanity check.
42 test "$py_version_pre" = "$py_version_post"
43
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
47
48 # We need control over the package name.
49 cat > configure.ac << END
50 AC_INIT([am_virtenv], [1.0])
51 AM_INIT_AUTOMAKE
52 AC_CONFIG_FILES([Makefile])
53 AC_SUBST([MY_VIRTENV], ['$cwd/virtenv'])
54 AC_PROG_CC
55 AM_PROG_AR
56 AC_PROG_RANLIB
57 AM_PATH_PYTHON
58 AC_OUTPUT
59 END
60
61 cat > Makefile.am << 'END'
62 python_PYTHON = am_foo.py
63 pkgpython_PYTHON = __init__.py
64 pyexec_LIBRARIES = libquux.a
65 libquux_a_SOURCES = foo.c
66 pkgpyexec_LIBRARIES = libzardoz.a
67 libzardoz_a_SOURCES = foo.c
68
69 py_site = $(MY_VIRTENV)/lib/python$(PYTHON_VERSION)/site-packages
70
71 .PYTHON: debug test-run test-install test-uninstall
72 debug:
73         @echo PYTHON: $(PYTHON)
74         @echo PYTHON_VERSION: $(PYTHON_VERSION)
75         @echo prefix: $(prefix)
76         @echo pythondir: $(pythondir)
77         @echo pkgpythondir: $(pkgpythondir)
78         @echo pyexecdir: $(pyexecdir)
79         @echo pkgpyexecdir: $(pkgpyexecdir)
80 test-run:
81         ## In a virtualenv, the default python must be the custom
82         ## virtualenv python.
83         @: \
84           && py1=`python -c 'import sys; print(sys.executable)'` \
85           && py2=`$(PYTHON) -c 'import sys; print(sys.executable)'` \
86           && echo "py1: $$py1" \
87           && echo "py2: $$py2" \
88           && test -n "$$py1" \
89           && test -n "$$py2" \
90           && test x"$$py1" = x"$$py2"
91         ## Check that modules installed in the virtualenv are readily
92         ## available.
93         python -c 'from am_foo import foo_func; assert (foo_func () == 12345)'
94         python -c 'from am_virtenv import old_am; assert (old_am () == "AutoMake")'
95 test-install:
96         test -f $(py_site)/am_foo.py
97         test -f $(py_site)/am_foo.pyc
98         test -f $(py_site)/am_foo.pyo
99         test -f $(py_site)/am_virtenv/__init__.py
100         test -f $(py_site)/am_virtenv/__init__.pyc
101         test -f $(py_site)/am_virtenv/__init__.pyo
102         test -f $(py_site)/libquux.a
103         test -f $(py_site)/am_virtenv/libzardoz.a
104 test-uninstall:
105         test ! -f $(py_site)/am_foo.py
106         test ! -f $(py_site)/am_foo.pyc
107         test ! -f $(py_site)/am_foo.pyo
108         test ! -f $(py_site)/am_virtenv/__init__.py
109         test ! -f $(py_site)/am_virtenv/__init__.pyc
110         test ! -f $(py_site)/am_virtenv/__init__.pyo
111         test ! -f $(py_site)/libquux.a
112         test ! -f $(py_site)/am_virtenv/libzardoz.a
113 all-local: debug
114 END
115
116 cat > am_foo.py << 'END'
117 def foo_func ():
118     return 12345
119 END
120
121 cat > __init__.py << 'END'
122 def old_am ():
123     return 'AutoMake'
124 END
125
126 cat > foo.c << 'END'
127 int foo (void)
128 {
129   return 0;
130 }
131 END
132
133 $ACLOCAL
134 $AUTOCONF
135 $AUTOMAKE --add-missing
136
137 # Try a VPATH build.
138 mkdir build
139 cd build
140 ../configure --prefix="$VIRTUAL_ENV"
141 $MAKE install
142 $MAKE test-install
143 $MAKE test-run
144 $MAKE uninstall
145 $MAKE test-uninstall
146 cd ..
147
148 # Try an in-tree build.
149 ./configure --prefix="$VIRTUAL_ENV"
150 $MAKE install
151 $MAKE test-install
152 $MAKE test-run
153 $MAKE uninstall
154 $MAKE test-uninstall
155
156 $MAKE distclean
157
158 # Overriding pythondir and pyexecdir with cache variables should work.
159 ./configure am_cv_python_pythondir="$py_site" \
160             am_cv_python_pyexecdir="$py_site"
161 $MAKE install
162 $MAKE test-install
163 $MAKE test-run
164 $MAKE uninstall
165 $MAKE test-uninstall
166
167 $MAKE distclean
168
169 # Overriding pythondir and pyexecdir at make time should be enough.
170 ./configure --prefix="$cwd/bad-prefix"
171 pythondir=$py_site pyexecdir=$py_site
172 export pythondir pyexecdir
173 $MAKE -e install
174 test ! -e bad-prefix
175 $MAKE -e test-install
176 $MAKE test-run
177 $MAKE -e uninstall
178 $MAKE -e test-uninstall
179 unset pythondir pyexecdir
180
181 # Also check that the distribution is self-contained, for completeness.
182 $MAKE distcheck
183
184 # Finally, check that if we disable the virtualenv, we shouldn't be
185 # able to access to the installed modules anymore.
186 cd build
187 $MAKE install
188 python -c 'import am_foo; print(am_foo.__file__)'
189 python -c 'import am_virtenv; print(am_virtenv.__file__)'
190 deactivate "nondestructive"
191 python -c 'import am_foo' && exit 1
192 python -c 'import am_virtenv' && exit 1
193
194 :