Skip to content

Commit b00aafd

Browse files
committed
fix(testing): OS Agnostic directory handling
Use the stdlib to generate temp directories and build filenames rather than hardcoded strings with forward-slash separators. - Should allow the tests to run on non-unixy operating systems - Should remove the possibility of conflicts with existing files - Might allow us to parallelize tests? May look into it later. I don't have a windows machine to test this on so I can't guarantee it will work.
1 parent 6faf79d commit b00aafd

7 files changed

Lines changed: 17 additions & 19 deletions

File tree

docs/devs/contributing/testing.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,3 @@ The primary way to run these checks is `rake`:
1919
* Depending on your environment, you may need to prefix all rake commands with `bundle exec`
2020
* Simplecov is set up -- you'll get a measurement of coverage in the test output and a nice report
2121
in the `coverage` directory.
22-
* The tests use the `/tmp/` directory directly, which I'm pretty sure means it won't work on
23-
Windows. This is fixable, so if that gets in your way just ask.

test/integration/integration_test_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def tested_base(params = 'rms.jpg')
2828
end
2929

3030
def rms_filename(width: 100, format: 'jpg')
31-
File.join temp_dir, rms_url(width: width, format: format)
31+
temp_dir(rms_url(width: width, format: format))
3232
end
3333

3434
def rms_url(width: 100, format: 'jpg')
@@ -60,7 +60,7 @@ def spx_url(width: 100, format: 'jpg')
6060
end
6161

6262
def spx_filename(width: 100, format: 'jpg', crop: '')
63-
File.join temp_dir, spx_url(width: width, format: format, crop: crop)
63+
temp_dir(spx_url(width: width, format: format, crop: crop))
6464
end
6565

6666
def std_spx_ss

test/integration/test_params.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test_crop
6363
assert_equal correct, output.at_css('img')['srcset']
6464

6565
generated_dimensions =
66-
Vips::Image.new_from_file(temp_dir('generated/rms-100-7ce20d78f.jpg'))
66+
Vips::Image.new_from_file(temp_dir('generated', 'rms-100-7ce20d78f.jpg'))
6767
.size
6868

6969
assert_in_delta aspect_float(10, 1),
@@ -117,7 +117,7 @@ def test_escaped_whitespace
117117

118118
assert_equal src, img['src']
119119
assert_equal ss, img['srcset']
120-
assert_path_exists(temp_dir('generated/rms with space-100-9f9ef26e5.jpg'))
120+
assert_path_exists(temp_dir('generated', 'rms with space-100-9f9ef26e5.jpg'))
121121
end
122122

123123
def test_quoted_whitespace
@@ -132,7 +132,7 @@ def test_quoted_whitespace
132132

133133
assert_equal src, img['src']
134134
assert_equal ss, img['srcset']
135-
assert_path_exists(temp_dir('generated/rms with space-100-9f9ef26e5.jpg'))
135+
assert_path_exists(temp_dir('generated', 'rms with space-100-9f9ef26e5.jpg'))
136136
end
137137

138138
def test_empty_params

test/integration/test_presets.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def teardown
1515
# formats webp, original
1616
def test_picture_files
1717
tested('auto rms.jpg')
18-
files = Dir.glob(temp_dir('generated' + '/*'))
18+
files = Dir.glob(temp_dir('generated', '*'))
1919

2020
assert_equal(6, files.length)
2121
assert_includes stdout, 'Generating'
@@ -218,7 +218,7 @@ def test_fallback_exists
218218
File.unstub :exist?
219219
tested 'fallback rms.jpg'
220220

221-
files = Dir.glob(temp_dir('generated') + '/rms-35-?????????.webp')
221+
files = Dir.glob(temp_dir('generated', 'rms-35-?????????.webp'))
222222

223223
assert_equal 1, files.length
224224
end
@@ -271,10 +271,10 @@ def test_crop
271271
tested 'crop rms.jpg mobile: spx.jpg'
272272

273273
rms_dimensions =
274-
Image.new_from_file(temp_dir('generated/rms-100-ced21b33d.jpg')).size
274+
Image.new_from_file(temp_dir('generated', 'rms-100-ced21b33d.jpg')).size
275275

276276
spx_dimensions =
277-
Image.new_from_file(temp_dir('generated/spx-100-63d4bc0d5.jpg')).size
277+
Image.new_from_file(temp_dir('generated', 'spx-100-63d4bc0d5.jpg')).size
278278

279279
assert_in_delta aspect_float(3, 2), aspect_float(*rms_dimensions), 0.03
280280
assert_in_delta aspect_float(16, 9), aspect_float(*spx_dimensions), 0.03

test/test_helper.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ def aspect_float(width, height)
4040
end
4141

4242
def temp_dir(*descendents)
43-
File.join '/tmp/jpt', *descendents
43+
@temp_dir ||= Dir.mktmpdir
44+
45+
File.join(@temp_dir, *descendents)
4446
end
4547

4648
# We're having trouble with tests failing because whatever system is running

test/unit/images/test_image_file.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def format
3838
end
3939

4040
def filename
41-
File.join(temp_dir, dest_name)
41+
temp_dir(dest_name)
4242
end
4343

4444
def gen_config
@@ -186,8 +186,6 @@ def test_no_strip_metadata
186186
end
187187

188188
def test_dest_dir
189-
refute_path_exists temp_dir
190-
191189
tested
192190

193191
assert_path_exists temp_dir

test/unit/test_cache.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class TestCache < Minitest::Test
66

77
def setup
88
PictureTag.stubs(site: Object.new)
9-
PictureTag.site.stubs(cache_dir: '/tmp/', config: {})
9+
PictureTag.site.stubs(cache_dir: temp_dir, config: {})
1010
end
1111

1212
def tested(name = 'img.jpg')
@@ -37,7 +37,7 @@ def test_write_data
3737
tested[:digest] = 'scruffer pupper'
3838
tested.write
3939

40-
assert_path_exists temp_dir('img.jpg.json')
40+
assert_path_exists temp_dir('jpt', 'img.jpg.json')
4141
end
4242

4343
def test_retrieve_data
@@ -49,11 +49,11 @@ def test_retrieve_data
4949

5050
# Handles filenames with directories in them
5151
def test_subdirectory_name
52-
tested('somedir/img.jpg')
52+
tested(File.join('somedir', 'img.jpg'))
5353
tested[:digest] = 'abc123'
5454
tested.write
5555

56-
assert_path_exists temp_dir('somedir/img.jpg.json')
56+
assert_path_exists temp_dir('jpt', 'somedir', 'img.jpg.json')
5757
end
5858

5959
# Jekyll has a flag to disable caching; we must respect it.

0 commit comments

Comments
 (0)