Skip to content

[Feature Idea] Sandbox Mode #46

Description

@albertalef

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.sandbox do
  sh do
    mkdir("-p", "config")
    touch("config/settings.yml")
    # Files exist only in sandbox
  end
end
# Automatically cleaned up

RSpec.describe "File operations" do
  around do |example|
    RubyShell.sandbox { example.run }
  end

  def subject_method
    sh do
      mkdir("-p", "data/cache")
      rm("-rf", "data")
    end
  end

  it "safely tests rm -rf" do
    subject_method

    # Real filesystem untouched!
    expect(Dir.exist?("data")).to be false
  end
end

# Keep sandbox for debugging
RubyShell.sandbox(keep: true) do |path|
  puts "Sandbox at: #{path}"
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions