Class: Ace::TestSupport::Fixtures::GitMocks::MockGitRepo

Inherits:
Object
  • Object
show all
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

Examples:

repo = MockGitRepo.new
repo.add_file("secret.txt", "TOKEN=ghp_abc123")
repo.add_commit("abc1234", message: "Add secret")
# Test code that examines repo structure
repo.cleanup

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|self| ... } ⇒ MockGitRepo

Create a mock git repository

Examples:

Block initializer for concise setup

repo = MockGitRepo.new do |r|
  r.add_file("secret.txt", "TOKEN=abc123")
  r.add_commit("abc1234", message: "Add secret")
end

Yields:

  • (self)

    Optionally yields self for one-line setup patterns



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

#commitsObject (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

#filesObject (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

#pathObject (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)

Parameters:

  • hash (String)

    Commit hash

  • message (String) (defaults to: "Test commit")

    Commit message

  • files (Array<String>) (defaults to: nil)

    Files in this commit



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: message,
    files: files || @files.keys.dup
  }
end

#add_file(filename, content) ⇒ Object

Add a file to the mock repo (instant, no git add)

Parameters:

  • filename (String)

    Relative path within repo

  • content (String)

    File content



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

#cleanupObject

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

#headString?

Get the latest commit hash (or nil if no commits)

Returns:

  • (String, nil)

    Last commit hash



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”)

Returns:

  • (Boolean)

    Always true for mock repos



67
68
69
# File 'lib/ace/test_support/fixtures/git_mocks.rb', line 67

def status_clean?
  true
end