Skip to content

Commit de88fe5

Browse files
committed
avoiding unicode errors when using exec
1 parent a36e46d commit de88fe5

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

test/test_examples.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# coding=utf-8
22

33
import os
4-
4+
import re
55
import pytest
66
import json as json_import
77

@@ -28,9 +28,10 @@
2828
print('warning: no .env file loaded')
2929

3030

31-
@pytest.mark.skipif(os.getenv('VCAP_SERVICES') is None, reason='requires VCAP_SERVICES')
31+
@pytest.mark.skipif(os.getenv('VCAP_SERVICES') is None,
32+
reason='requires VCAP_SERVICES')
3233
def test_examples():
33-
vcapServices = json_import.loads(os.getenv('VCAP_SERVICES'))
34+
vcap_services = json_import.loads(os.getenv('VCAP_SERVICES'))
3435
examples = glob(examples_path)
3536
for example in examples:
3637
name = example.split('/')[-1]
@@ -40,13 +41,17 @@ def test_examples():
4041
continue
4142

4243
# exclude tests if there are no credentials for that service
43-
serviceName = name[:-6] if not name.startswith('visual_recognition') else 'watson_vision_combined'
44+
service_name = name[:-6] if not name.startswith('visual_recognition')\
45+
else 'watson_vision_combined'
4446

45-
if serviceName not in vcapServices:
46-
print('%s does not have credentials in VCAP_SERVICES', serviceName)
47+
if service_name not in vcap_services:
48+
print('%s does not have credentials in VCAP_SERVICES',
49+
service_name)
4750
continue
4851

4952
try:
50-
exec(open(example).read(), globals())
53+
service_file = open(example).read()
54+
exec(re.sub('# coding=utf-8', '', service_file), globals())
5155
except Exception as e:
52-
assert False, 'example in file ' + name + ' failed with error: ' + str(e)
56+
assert False, 'example in file ' + name + ' failed with error: '\
57+
+ str(e)

0 commit comments

Comments
 (0)