Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d9d9142

Browse files
committed
fix to_image test paths
1 parent ff4a2b2 commit d9d9142

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

plotly-package/plotly/tests/test_orca/test_to_image.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import plotly
12
import plotly.io as pio
23
import plotly.graph_objs as go
34
import os
@@ -13,20 +14,21 @@
1314

1415
# Constants
1516
# ---------
16-
images_root = 'plotly/tests/test_orca/images/'
17-
17+
project_root = os.path.dirname(os.path.realpath(plotly.__file__))
18+
images_root = os.path.join(project_root, 'tests', 'test_orca', 'images')
19+
print(images_root)
1820
if sys.platform.startswith('linux'):
19-
images_dir = images_root + 'linux/'
21+
images_dir = os.path.join(images_root, 'linux')
2022
elif sys.platform == 'darwin':
21-
images_dir = images_root + 'darwin/'
23+
images_dir = os.path.join(images_root, 'darwin')
2224
else:
2325
raise ValueError(
2426
'No reference images available for platform: {platform}'
2527
.format(platform=sys.platform))
2628

2729

28-
failed_dir = images_dir + 'failed/'
29-
tmp_dir = images_dir + 'tmp/'
30+
failed_dir = os.path.join(images_dir, 'failed/')
31+
tmp_dir = os.path.join(images_dir, 'tmp/')
3032
# These formats are deterministic. PDF and svg don't seem to be
3133
image_formats = ['eps']
3234

@@ -157,7 +159,7 @@ def latexfig():
157159
# Utilities
158160
# ---------
159161
def assert_image_bytes(img_bytes, file_name, _raise=True):
160-
expected_img_path = images_dir + file_name
162+
expected_img_path = os.path.join(images_dir, file_name)
161163

162164
try:
163165
with open(expected_img_path, 'rb') as f:
@@ -166,7 +168,7 @@ def assert_image_bytes(img_bytes, file_name, _raise=True):
166168
assert expected == img_bytes
167169

168170
except (OSError, AssertionError) as e:
169-
failed_path = failed_dir + file_name
171+
failed_path = os.path.join(failed_dir, file_name)
170172
print('Saving failed image to "{failed_path}"'
171173
.format(failed_path=failed_path))
172174

@@ -199,13 +201,13 @@ def test_write_image_string(fig1, format):
199201
file_name = 'fig1.' + format
200202
file_path = tmp_dir + file_name
201203

202-
pio.write_image(fig1, tmp_dir + file_name,
204+
pio.write_image(fig1, os.path.join(tmp_dir, file_name),
203205
format=format, width=700, height=500)
204206

205207
with open(file_path, 'rb') as f:
206208
written_bytes = f.read()
207209

208-
with open(images_dir + file_name, 'rb') as f:
210+
with open(os.path.join(images_dir, file_name), 'rb') as f:
209211
expected_bytes = f.read()
210212

211213
assert written_bytes == expected_bytes
@@ -214,7 +216,7 @@ def test_write_image_string(fig1, format):
214216
def test_write_image_writeable(fig1, format):
215217

216218
file_name = 'fig1.' + format
217-
with open(images_dir + file_name, 'rb') as f:
219+
with open(os.path.join(images_dir, file_name), 'rb') as f:
218220
expected_bytes = f.read()
219221

220222
mock_file = MagicMock()
@@ -227,54 +229,54 @@ def test_write_image_writeable(fig1, format):
227229
def test_write_image_string_format_inference(fig1, format):
228230
# Build file paths
229231
file_name = 'fig1.' + format
230-
file_path = tmp_dir + file_name
232+
file_path = os.path.join(tmp_dir, file_name)
231233

232234
# Use file extension to infer image type.
233-
pio.write_image(fig1, tmp_dir + file_name,
235+
pio.write_image(fig1, os.path.join(tmp_dir, file_name),
234236
width=700, height=500)
235237

236238
with open(file_path, 'rb') as f:
237239
written_bytes = f.read()
238240

239-
with open(images_dir + file_name, 'rb') as f:
241+
with open(os.path.join(images_dir, file_name), 'rb') as f:
240242
expected_bytes = f.read()
241243

242244
assert written_bytes == expected_bytes
243245

244246

245247
def test_write_image_string_no_extension_failure(fig1):
246248
# No extension
247-
file_path = tmp_dir + 'fig1'
249+
file_path = os.path.join(tmp_dir, 'fig1')
248250

249251
# Use file extension to infer image type.
250252
with pytest.raises(ValueError) as err:
251-
pio.write_image(fig1, file_path + 'fig1')
253+
pio.write_image(fig1, os.path.join(file_path, 'fig1'))
252254

253255
assert 'add a file extension or specify the type' in str(err.value)
254256

255257

256258
def test_write_image_string_bad_extension_failure(fig1):
257259
# Bad extension
258-
file_path = tmp_dir + 'fig1.bogus'
260+
file_path = os.path.join(tmp_dir, 'fig1.bogus')
259261

260262
# Use file extension to infer image type.
261263
with pytest.raises(ValueError) as err:
262-
pio.write_image(fig1, file_path + 'fig1')
264+
pio.write_image(fig1, os.path.join(file_path, 'fig1'))
263265

264266
assert 'must be specified as one of the following' in str(err.value)
265267

266268

267269
def test_write_image_string_bad_extension_override(fig1):
268270
# Bad extension
269271
file_name = 'fig1.bogus'
270-
tmp_path = tmp_dir + file_name
272+
tmp_path = os.path.join(tmp_dir, file_name)
271273

272274
pio.write_image(fig1, tmp_path, format='eps', width=700, height=500)
273275

274276
with open(tmp_path, 'rb') as f:
275277
written_bytes = f.read()
276278

277-
with open(images_dir + 'fig1.eps', 'rb') as f:
279+
with open(os.path.join(images_dir, 'fig1.eps'), 'rb') as f:
278280
expected_bytes = f.read()
279281

280282
assert written_bytes == expected_bytes

0 commit comments

Comments
 (0)