You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Problem: Testing file operations risks affecting the real filesystem, tests can pollute each other's state, and testing destructive commands like rm -rf is dangerous.
Solution: Run commands in an isolated temporary directory that is automatically cleaned up, ensuring complete filesystem isolation.
require"rubyshell/testing"RubyShell.sandboxdoshdomkdir("-p","config")touch("config/settings.yml")# Files exist only in sandboxendend# Automatically cleaned upRSpec.describe"File operations"doarounddo |example|
RubyShell.sandbox{example.run}enddefsubject_methodshdomkdir("-p","data/cache")rm("-rf","data")endendit"safely tests rm -rf"dosubject_method# Real filesystem untouched!expect(Dir.exist?("data")).tobefalseendend# Keep sandbox for debuggingRubyShell.sandbox(keep: true)do |path|
puts"Sandbox at: #{path}"end
Activity
[-]Feature: Sandbox Mode[/-][+][Feature Idea] Sandbox Mode[/+]