# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2025-2026, by Samuel Williams.

require "tmpdir"
require "fileutils"

module Sus
	module Fixtures
		# Provides a temporary directory context for tests that need isolated file system access.
		module TemporaryDirectoryContext
			# Set up a temporary directory before the test and clean it up after.
			# @yields {|&block| ...} The test block to execute.
			def around(&block)
				root = Dir.mktmpdir
				
				begin
					@root = root
					
					super(&block)
				ensure
					@root = nil
					
					# Use forced removal so cleanup tolerates paths which were already removed by the test or an external process.
					FileUtils.remove_entry(root, true)
				end
			end
			
			# @attribute [String] The path to the temporary directory root.
			attr :root
		end
	end
end
