Skip to content

Commit 930b932

Browse files
Add Sus::Fixtures::TemporaryDirectoryContext.
1 parent af33edc commit 930b932

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

lib/sus/fixtures.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Copyright, 2022, by Samuel Williams.
55

66
module Sus
7+
# @namespace
78
module Fixtures
89
end
910
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2024, by Samuel Williams.
5+
6+
require "tmpdir"
7+
8+
module Sus
9+
module Fixtures
10+
module TemporaryDirectoryContext
11+
def around(&block)
12+
Dir.mktmpdir do |root|
13+
@root = root
14+
super(&block)
15+
@root = nil
16+
end
17+
end
18+
19+
attr :root
20+
end
21+
end
22+
end
23+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2025, by Samuel Williams.
5+
6+
describe Sus::Fixtures::TemporaryDirectoryContext do
7+
include Sus::Fixtures::TemporaryDirectoryContext
8+
9+
it "creates a temporary directory" do
10+
expect(root).not.to be(:nil?)
11+
expect(root).to be_a(String)
12+
expect(File.directory?(root)).to be == true
13+
end
14+
15+
it "provides access to the root path" do
16+
expect(root).to be_a(String)
17+
expect(root).not.to be(:empty?)
18+
end
19+
20+
it "allows creating files in the temporary directory" do
21+
test_file = File.join(root, "test.txt")
22+
File.write(test_file, "test content")
23+
24+
expect(File.exist?(test_file)).to be == true
25+
expect(File.read(test_file)).to be == "test content"
26+
end
27+
end

0 commit comments

Comments
 (0)