test_envwrapper.py 7.42 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env python
# encoding: utf-8
# Andre Anjos <andre.dos.anjos@gmail.com>
# Fri 21 Mar 2014 11:50:06 CET

'''Tests for our environment wrapper class'''

import os
import logging
import nose.tools
André Anjos's avatar
André Anjos committed
11
import platform
12
13
14

from .envwrapper import EnvironmentWrapper

André Anjos's avatar
André Anjos committed
15
16
17
18
19
20
21
22
23
def cleanup():
  '''Removes weird variables from the user environment just for the tests'''

  remove = ['CFLAGS', 'CXXFLAGS', 'LDFLAGS', 'BOB_PREFIX_PATH',
      'PKG_CONFIG_PATH', 'CMAKE_PREFIX_PATH', 'MACOSX_DEPLOYMENT_TARGET']
  for key in remove:
    if key in os.environ: del os.environ[key]

@nose.with_setup(cleanup)
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def test_default():

  e = EnvironmentWrapper(logging.getLogger())

  before = dict(os.environ)

  e.set()
  for key in before:
    assert key in os.environ, "key `%s' from before is not on os.environ" % (key,)
  for key in os.environ:
    assert key in before, "key `%s' was not on os.environ before" % (key,)
  nose.tools.eq_(before, os.environ)

  e.unset()
  for key in before:
    assert key in os.environ, "key `%s' from before is not on os.environ" % (key,)
  for key in os.environ:
    assert key in before, "key `%s' was not on os.environ before" % (key,)
  nose.tools.eq_(before, os.environ)

@nose.with_setup(cleanup)
def test_set_debug_true():

  # a few checks before we start
  assert 'CFLAGS' not in os.environ
  assert 'CXXFLAGS' not in os.environ

  e = EnvironmentWrapper(logging.getLogger(), debug=True)

  before = dict(os.environ)

  e.set()
André Anjos's avatar
André Anjos committed
56

57
  nose.tools.eq_(len(os.environ) - len(before), 2)
André Anjos's avatar
André Anjos committed
58

59
  assert 'CFLAGS' in os.environ
André Anjos's avatar
André Anjos committed
60
61
  assert os.environ['CFLAGS'].find(EnvironmentWrapper.DEBUG_CFLAGS) >= 0
  assert os.environ['CFLAGS'].find(EnvironmentWrapper.RELEASE_CFLAGS) < 0
62
  assert 'CXXFLAGS' in os.environ
André Anjos's avatar
André Anjos committed
63
64
65
  assert os.environ['CXXFLAGS'].find(EnvironmentWrapper.DEBUG_CFLAGS) >= 0
  assert os.environ['CXXFLAGS'].find(EnvironmentWrapper.RELEASE_CFLAGS) < 0

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  e.unset()
  for key in before:
    assert key in os.environ, "key `%s' from before is not on os.environ" % (key,)
  for key in os.environ:
    assert key in before, "key `%s' was not on os.environ before" % (key,)
  nose.tools.eq_(before, os.environ)

@nose.with_setup(cleanup)
def test_set_debug_false():

  # a few checks before we start
  assert 'CFLAGS' not in os.environ
  assert 'CXXFLAGS' not in os.environ

  e = EnvironmentWrapper(logging.getLogger(), debug=False)

  before = dict(os.environ)

  e.set()
André Anjos's avatar
André Anjos committed
85

86
  nose.tools.eq_(len(os.environ) - len(before), 2)
André Anjos's avatar
André Anjos committed
87

88
89
90
  assert 'CFLAGS' in os.environ
  assert 'CXXFLAGS' in os.environ
  nose.tools.eq_(os.environ['CFLAGS'], e.environ['CFLAGS'])
André Anjos's avatar
André Anjos committed
91
92
  assert os.environ['CFLAGS'].find(EnvironmentWrapper.DEBUG_CFLAGS) < 0
  assert os.environ['CFLAGS'].find(EnvironmentWrapper.RELEASE_CFLAGS) >= 0
93
  nose.tools.eq_(os.environ['CXXFLAGS'], e.environ['CXXFLAGS'])
André Anjos's avatar
André Anjos committed
94
95
96
  assert os.environ['CXXFLAGS'].find(EnvironmentWrapper.DEBUG_CFLAGS) < 0
  assert os.environ['CXXFLAGS'].find(EnvironmentWrapper.RELEASE_CFLAGS) >= 0

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  e.unset()
  for key in before:
    assert key in os.environ, "key `%s' from before is not on os.environ" % (key,)
  for key in os.environ:
    assert key in before, "key `%s' was not on os.environ before" % (key,)
  nose.tools.eq_(before, os.environ)

@nose.with_setup(cleanup)
def test_set_prefixes():

  # a few checks before we start
  assert 'PKG_CONFIG_PATH' not in os.environ

  prefixes = ['/a/b', '/c/d']
  e = EnvironmentWrapper(logging.getLogger(), prefixes=prefixes)

  before = dict(os.environ)

  e.set()
  #nose.tools.eq_(len(os.environ) - len(before), 2)
  assert 'PKG_CONFIG_PATH' in os.environ
  nose.tools.eq_(os.environ['PKG_CONFIG_PATH'], e.environ['PKG_CONFIG_PATH'])
119
120
  assert 'BOB_PREFIX_PATH' in os.environ
  nose.tools.eq_(os.environ['BOB_PREFIX_PATH'], os.pathsep.join(prefixes))
André Anjos's avatar
André Anjos committed
121
122
  assert 'CMAKE_PREFIX_PATH' in os.environ
  nose.tools.eq_(os.environ['CMAKE_PREFIX_PATH'], os.pathsep.join(prefixes))
123
124
125
126
127
128
129
130
131
132
133
134

  e.unset()
  for key in before:
    assert key in os.environ, "key `%s' from before is not on os.environ" % (key,)
  for key in os.environ:
    assert key in before, "key `%s' was not on os.environ before" % (key,)
  nose.tools.eq_(before, os.environ)

@nose.with_setup(cleanup)
def test_set_environment():

  # a few checks before we start
135
  varname = 'BOB_FOO'
136
137
138
139
140
141
142
143
  varvalue = 'abc'
  assert varname not in os.environ

  e = EnvironmentWrapper(logging.getLogger(), environ={varname: varvalue})

  before = dict(os.environ)

  e.set()
André Anjos's avatar
André Anjos committed
144

145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  nose.tools.eq_(len(os.environ) - len(before), 1)
  assert varname in os.environ
  nose.tools.eq_(os.environ[varname], varvalue)

  e.unset()
  for key in before:
    assert key in os.environ, "key `%s' from before is not on os.environ" % (key,)
  for key in os.environ:
    assert key in before, "key `%s' was not on os.environ before" % (key,)
  nose.tools.eq_(before, os.environ)

@nose.with_setup(cleanup)
def test_environ_substitutions():

  # defines the environment with all legal substitutions
  environ = dict(
161
162
163
      BOB_FOO = 'foo',
      BOB_T1 = '${BOB_FOO}:bar',
      BOB_T2 = 'bar$BOB_FOO',
164
165
166
167
168
169
170
171
172
      )

  e = EnvironmentWrapper(logging.getLogger(), environ=environ)

  before = dict(os.environ)

  e.set()
  nose.tools.eq_(len(os.environ) - len(before), 3)
  for key in environ: assert key in os.environ
173
174
175
  nose.tools.eq_(os.environ['BOB_FOO'], environ['BOB_FOO'])
  nose.tools.eq_(os.environ['BOB_T1'], environ['BOB_FOO'] + ':bar')
  nose.tools.eq_(os.environ['BOB_T2'], 'bar' + environ['BOB_FOO'])
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191

  e.unset()
  for key in before:
    assert key in os.environ, "key `%s' from before is not on os.environ" % (key,)
  for key in os.environ:
    assert key in before, "key `%s' was not on os.environ before" % (key,)
  nose.tools.eq_(before, os.environ)

@nose.with_setup(cleanup)
def test_set_multiple():

  # a few checks before we start
  environ = dict(
      CFLAGS='-DNDEBUG',
      CXXFLAGS='${CFLAGS}',
      PKG_CONFIG_PATH='/a/b/lib/pkgconfig',
André Anjos's avatar
André Anjos committed
192
193
      BOB_PREFIX_PATH='/c/d',
      CMAKE_PREFIX_PATH='/d/f',
194
195
196
197
198
199
200
      )

  e = EnvironmentWrapper(logging.getLogger(), debug=True, environ=environ)

  before = dict(os.environ)

  e.set()
André Anjos's avatar
André Anjos committed
201

202
  nose.tools.eq_(len(os.environ) - len(before), 5)
André Anjos's avatar
André Anjos committed
203
204

  nose.tools.eq_(os.environ['CFLAGS'], EnvironmentWrapper.DEBUG_CFLAGS + ' ' + environ['CFLAGS'])
205
  nose.tools.eq_(os.environ['CXXFLAGS'], os.environ['CFLAGS'])
André Anjos's avatar
André Anjos committed
206
207
208
  assert os.environ['BOB_PREFIX_PATH'].startswith(environ['BOB_PREFIX_PATH'])
  assert os.environ['BOB_PREFIX_PATH'].endswith(environ['CMAKE_PREFIX_PATH'])
  assert os.environ['CMAKE_PREFIX_PATH'] == os.environ['BOB_PREFIX_PATH']
209
  assert os.environ['PKG_CONFIG_PATH'].startswith(environ['PKG_CONFIG_PATH'])
210
  assert os.environ['PKG_CONFIG_PATH'].find(environ['BOB_PREFIX_PATH']) >= 0
211
212
213
214
215
216
217

  e.unset()
  for key in before:
    assert key in os.environ, "key `%s' from before is not on os.environ" % (key,)
  for key in os.environ:
    assert key in before, "key `%s' was not on os.environ before" % (key,)
  nose.tools.eq_(before, os.environ)
André Anjos's avatar
André Anjos committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234

@nose.with_setup(cleanup)
def test_preserve_user():

  # a few checks before we start
  environ = dict(
      CFLAGS='-DNDEBUG',
      CXXFLAGS='${CFLAGS}',
      )

  os.environ['CFLAGS'] = '-BUILDOUT-TEST-STRING'

  e = EnvironmentWrapper(logging.getLogger(), debug=True, environ=environ)

  before = dict(os.environ)

  e.set()
André Anjos's avatar
André Anjos committed
235

236
  nose.tools.eq_(len(os.environ) - len(before), 1)
André Anjos's avatar
André Anjos committed
237

André Anjos's avatar
André Anjos committed
238
239
240
241
242
243
244
245
  assert os.environ['CFLAGS'].endswith('-BUILDOUT-TEST-STRING')

  e.unset()
  for key in before:
    assert key in os.environ, "key `%s' from before is not on os.environ" % (key,)
  for key in os.environ:
    assert key in before, "key `%s' was not on os.environ before" % (key,)
  nose.tools.eq_(before, os.environ)