test_envwrapper.py 6.87 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
  assert os.environ['CFLAGS'].find(EnvironmentWrapper.DEBUG_CFLAGS) >= 0
61
  assert 'CXXFLAGS' in os.environ
André Anjos's avatar
André Anjos committed
62
63
  assert os.environ['CXXFLAGS'].find(EnvironmentWrapper.DEBUG_CFLAGS) >= 0

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  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
83

André Anjos's avatar
André Anjos committed
84
  nose.tools.eq_(len(os.environ) - len(before), 0)
André Anjos's avatar
André Anjos committed
85

André Anjos's avatar
André Anjos committed
86
87
  assert 'CFLAGS' not in os.environ
  assert 'CXXFLAGS' not in os.environ
André Anjos's avatar
André Anjos committed
88

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  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'])
111
112
  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
113
114
  assert 'CMAKE_PREFIX_PATH' in os.environ
  nose.tools.eq_(os.environ['CMAKE_PREFIX_PATH'], os.pathsep.join(prefixes))
115
116
117
118
119
120
121
122
123
124
125
126

  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
127
  varname = 'BOB_FOO'
128
129
130
131
132
133
134
135
  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
136

137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  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(
153
154
155
      BOB_FOO = 'foo',
      BOB_T1 = '${BOB_FOO}:bar',
      BOB_T2 = 'bar$BOB_FOO',
156
157
158
159
160
161
162
163
164
      )

  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
165
166
167
  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'])
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

  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
184
185
      BOB_PREFIX_PATH='/c/d',
      CMAKE_PREFIX_PATH='/d/f',
186
187
188
189
190
191
192
      )

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

  before = dict(os.environ)

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

194
  nose.tools.eq_(len(os.environ) - len(before), 5)
André Anjos's avatar
André Anjos committed
195
196

  nose.tools.eq_(os.environ['CFLAGS'], EnvironmentWrapper.DEBUG_CFLAGS + ' ' + environ['CFLAGS'])
197
  nose.tools.eq_(os.environ['CXXFLAGS'], os.environ['CFLAGS'])
André Anjos's avatar
André Anjos committed
198
199
200
  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']
201
  assert os.environ['PKG_CONFIG_PATH'].startswith(environ['PKG_CONFIG_PATH'])
202
  assert os.environ['PKG_CONFIG_PATH'].find(environ['BOB_PREFIX_PATH']) >= 0
203
204
205
206
207
208
209

  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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

@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
227

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

André Anjos's avatar
André Anjos committed
230
231
232
233
234
235
236
237
  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)