Sha256: 19b1f68f4855528053a2da08dfaa534d9e080047d5140c3cf4b9fedc12be46f2

Contents?: true

Size: 1.63 KB

Versions: 145

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

require 'eac_git/local'
require 'eac_ruby_utils/envs'
require 'fileutils'
require 'securerandom'
require 'tmpdir'

module EacGit
  module Rspec
    module StubbedGitLocalRepo
      def stubbed_git_local_repo(bare = false)
        path = ::Dir.mktmpdir
        ::EacRubyUtils::Envs.local.command(stubbed_git_local_repo_args(path, bare)).execute!
        repo = StubbedGitRepository.new(path)
        repo.command('config', 'user.email', 'theuser@example.net').execute!
        repo.command('config', 'user.name', 'The User').execute!
        repo
      end

      private

      def stubbed_git_local_repo_args(path, bare)
        r = %w[git init]
        r << '--bare' if bare
        r + [path]
      end

      class StubbedGitRepository < ::EacGit::Local
        def file(*subpath)
          StubbedGitRepositoryFile.new(self, subpath)
        end

        # @return [EacGit::Local::Commit
        def random_commit
          content = ::SecureRandom.hex
          file = "#{content}.txt"
          file(file).write(content)
          command('add', file).execute!
          command('commit', '-m', "Random commit: #{file}.").execute!
          head
        end
      end

      class StubbedGitRepositoryFile
        attr_reader :git, :subpath

        def initialize(git, subpath)
          @git = git
          @subpath = subpath
        end

        def path
          git.root_path.join(*subpath)
        end

        def touch
          ::FileUtils.touch(path.to_path)
        end

        def delete
          path.unlink
        end

        def write(content)
          path.write(content)
        end
      end
    end
  end
end

Version data entries

145 entries across 145 versions & 3 rubygems

Version Path
eac_tools-0.84.1 sub/eac_git/lib/eac_git/rspec/stubbed_git_local_repo.rb
eac_tools-0.84.0 sub/eac_git/lib/eac_git/rspec/stubbed_git_local_repo.rb
eac_tools-0.83.0 sub/eac_git/lib/eac_git/rspec/stubbed_git_local_repo.rb
eac_tools-0.82.0 sub/eac_git/lib/eac_git/rspec/stubbed_git_local_repo.rb
eac_tools-0.81.0 sub/eac_git/lib/eac_git/rspec/stubbed_git_local_repo.rb
eac_tools-0.80.0 sub/eac_git/lib/eac_git/rspec/stubbed_git_local_repo.rb
eac_tools-0.79.0 sub/eac_git/lib/eac_git/rspec/stubbed_git_local_repo.rb
eac_tools-0.78.0 sub/eac_git/lib/eac_git/rspec/stubbed_git_local_repo.rb
eac_tools-0.77.1 sub/eac_git/lib/eac_git/rspec/stubbed_git_local_repo.rb
eac_tools-0.77.0 sub/eac_git/lib/eac_git/rspec/stubbed_git_local_repo.rb
eac_tools-0.76.1 sub/eac_git/lib/eac_git/rspec/stubbed_git_local_repo.rb
eac_tools-0.76.0 sub/eac_git/lib/eac_git/rspec/stubbed_git_local_repo.rb
eac_tools-0.75.2 sub/eac_git/lib/eac_git/rspec/stubbed_git_local_repo.rb
eac_tools-0.75.1 sub/eac_git/lib/eac_git/rspec/stubbed_git_local_repo.rb
eac_tools-0.75.0 sub/eac_git/lib/eac_git/rspec/stubbed_git_local_repo.rb
eac_tools-0.74.1 sub/eac_git/lib/eac_git/rspec/stubbed_git_local_repo.rb
eac_tools-0.74.0 sub/eac_git/lib/eac_git/rspec/stubbed_git_local_repo.rb
eac_tools-0.73.0 sub/eac_git/lib/eac_git/rspec/stubbed_git_local_repo.rb
eac_tools-0.72.0 sub/eac_git/lib/eac_git/rspec/stubbed_git_local_repo.rb
eac_tools-0.70.1 sub/eac_git/lib/eac_git/rspec/stubbed_git_local_repo.rb