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