Class: Ace::TestSupport::Fixtures::GitMocks::MockGitRepo
- Inherits:
-
Object
- Object
- Ace::TestSupport::Fixtures::GitMocks::MockGitRepo
- Defined in:
- lib/ace/test_support/fixtures/git_mocks.rb
Overview
Mock git repository for fast unit tests without subprocess calls Provides a temp directory with files but no actual git init Use this for testing code that reads files but doesn’t need real git commands
Instance Attribute Summary collapse
-
#commits ⇒ Object
readonly
Returns the value of attribute commits.
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#add_commit(hash, message: "Test commit", files: nil) ⇒ Object
Record a mock commit (no subprocess).
-
#add_file(filename, content) ⇒ Object
Add a file to the mock repo (instant, no git add).
-
#cleanup ⇒ Object
Clean up the temp directory.
-
#head ⇒ String?
Get the latest commit hash (or nil if no commits).
-
#initialize {|self| ... } ⇒ MockGitRepo
constructor
Create a mock git repository.
-
#reset! ⇒ Object
Reset the mock repo state without destroying the temp directory Useful for reusing the same mock repo across multiple test assertions.
-
#status_clean? ⇒ Boolean
Simulate git status output (mock repos are always “clean”).
Constructor Details
#initialize {|self| ... } ⇒ MockGitRepo
Create a mock git repository
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ace/test_support/fixtures/git_mocks.rb', line 32 def initialize @path = Dir.mktmpdir("ace-mock-git-repo") @commits = [] @files = {} # Create a fake .git directory to pass validation checks # This is much faster than running `git init` (~150ms savings) FileUtils.mkdir_p(File.join(@path, ".git")) yield self if block_given? end |
Instance Attribute Details
#commits ⇒ Object (readonly)
Returns the value of attribute commits.
23 24 25 |
# File 'lib/ace/test_support/fixtures/git_mocks.rb', line 23 def commits @commits end |
#files ⇒ Object (readonly)
Returns the value of attribute files.
23 24 25 |
# File 'lib/ace/test_support/fixtures/git_mocks.rb', line 23 def files @files end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
23 24 25 |
# File 'lib/ace/test_support/fixtures/git_mocks.rb', line 23 def path @path end |
Instance Method Details
#add_commit(hash, message: "Test commit", files: nil) ⇒ Object
Record a mock commit (no subprocess)
57 58 59 60 61 62 63 |
# File 'lib/ace/test_support/fixtures/git_mocks.rb', line 57 def add_commit(hash, message: "Test commit", files: nil) @commits << { hash: hash, message: , files: files || @files.keys.dup } end |
#add_file(filename, content) ⇒ Object
Add a file to the mock repo (instant, no git add)
46 47 48 49 50 51 |
# File 'lib/ace/test_support/fixtures/git_mocks.rb', line 46 def add_file(filename, content) full_path = File.join(@path, filename) FileUtils.mkdir_p(File.dirname(full_path)) File.write(full_path, content) @files[filename] = content end |
#cleanup ⇒ Object
Clean up the temp directory
78 79 80 |
# File 'lib/ace/test_support/fixtures/git_mocks.rb', line 78 def cleanup FileUtils.rm_rf(@path) if @path && Dir.exist?(@path) end |
#head ⇒ String?
Get the latest commit hash (or nil if no commits)
73 74 75 |
# File 'lib/ace/test_support/fixtures/git_mocks.rb', line 73 def head @commits.last&.dig(:hash) end |
#reset! ⇒ Object
Reset the mock repo state without destroying the temp directory Useful for reusing the same mock repo across multiple test assertions
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ace/test_support/fixtures/git_mocks.rb', line 84 def reset! @commits = [] @files = {} # Clear files but keep .git directory Dir.glob(File.join(@path, "*")).each do |f| next if File.basename(f) == ".git" FileUtils.rm_rf(f) end end |
#status_clean? ⇒ Boolean
Simulate git status output (mock repos are always “clean”)
67 68 69 |
# File 'lib/ace/test_support/fixtures/git_mocks.rb', line 67 def status_clean? true end |