Skip to content

Commit fbd5bc1

Browse files
Tolerate temporary directory cleanup failures
1 parent 9540e78 commit fbd5bc1

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

lib/sus/fixtures/temporary_directory_context.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Copyright, 2025-2026, by Samuel Williams.
55

66
require "tmpdir"
7+
require "fileutils"
78

89
module Sus
910
module Fixtures
@@ -12,10 +13,17 @@ module TemporaryDirectoryContext
1213
# Set up a temporary directory before the test and clean it up after.
1314
# @yields {|&block| ...} The test block to execute.
1415
def around(&block)
15-
Dir.mktmpdir do |root|
16+
root = Dir.mktmpdir
17+
18+
begin
1619
@root = root
20+
1721
super(&block)
22+
ensure
1823
@root = nil
24+
25+
# Use forced removal so cleanup tolerates paths which were already removed by the test or an external process.
26+
FileUtils.remove_entry(root, true)
1927
end
2028
end
2129

@@ -24,4 +32,3 @@ def around(&block)
2432
end
2533
end
2634
end
27-

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Make `Sus::Fixtures::TemporaryDirectoryContext` ignore temporary directory cleanup failures.
6+
37
## v0.37.1
48

59
- Fixed `Sus::Mock#wrap` to forward blocks to the original method, and fixed `receive(...).with_block(...)` to use the supplied predicate.

test/sus/fixtures/temporary_directory_context.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
describe Sus::Fixtures::TemporaryDirectoryContext do
99
include Sus::Fixtures::TemporaryDirectoryContext
1010

11+
let(:assertions) {Sus::Assertions.new(output: Sus::Output.buffered)}
12+
1113
it "creates a temporary directory" do
1214
expect(root).not.to be(:nil?)
1315
expect(root).to be_a(String)
@@ -26,4 +28,37 @@
2628
expect(File.exist?(test_file)).to be == true
2729
expect(File.read(test_file)).to be == "test content"
2830
end
31+
32+
it "removes the temporary directory after use" do
33+
context = Sus.base("temporary directory")
34+
context.include(Sus::Fixtures::TemporaryDirectoryContext)
35+
instance = context.new(assertions)
36+
37+
root = nil
38+
39+
instance.around do
40+
root = instance.root
41+
42+
expect(File.directory?(root)).to be == true
43+
end
44+
45+
expect(File.exist?(root)).to be == false
46+
end
47+
48+
it "tolerates already removed temporary directories during cleanup" do
49+
context = Sus.base("temporary directory")
50+
context.include(Sus::Fixtures::TemporaryDirectoryContext)
51+
instance = context.new(assertions)
52+
53+
root = nil
54+
55+
expect do
56+
instance.around do
57+
root = instance.root
58+
FileUtils.remove_entry(root)
59+
end
60+
end.not.to raise_exception
61+
62+
expect(File.exist?(root)).to be == false
63+
end
2964
end

0 commit comments

Comments
 (0)