From f4b99c668a3c169d91c8a5c2974085aeff18731d Mon Sep 17 00:00:00 2001 From: James Couball Date: Fri, 16 Oct 2020 10:27:27 -0700 Subject: [PATCH] Add logging to the RubyGit gem --- lib/ruby_git.rb | 52 ++++++++++++++----- lib/ruby_git/worktree.rb | 1 + ruby_git.gemspec | 2 + spec/lib/ruby_git/worktree_initialize_spec.rb | 28 ++++++++++ spec/lib/ruby_git_spec.rb | 15 ++++++ 5 files changed, 86 insertions(+), 12 deletions(-) create mode 100644 spec/lib/ruby_git/worktree_initialize_spec.rb diff --git a/lib/ruby_git.rb b/lib/ruby_git.rb index cb57732..80db117 100644 --- a/lib/ruby_git.rb +++ b/lib/ruby_git.rb @@ -6,6 +6,8 @@ require 'ruby_git/version' require 'ruby_git/worktree' +require 'null_logger' + # RubyGit is an object-oriented wrapper for the `git` command line tool for # working with Worktrees and Repositories. It tries to make more sense out # of the Git command line. @@ -13,18 +15,44 @@ # @api public # module RubyGit - # Return information about the git binary used by this library - # - # Use this object to set the path to the git binary to use or to see the - # path being used. - # - # @example Setting the git binary path - # RubyGit.git.path = '/usr/local/bin/git' - # - # @return [RubyGit::GitBinary] - # - def self.git - (@git ||= RubyGit::GitBinary.new) + @git = RubyGit::GitBinary.new + + class << self + # Information about the git binary used by the RubyGit gem + # + # Use this object to set the path to the git binary to use or to see the + # path being used. + # + # @example Setting the git binary path + # RubyGit.git.path = '/usr/local/bin/git' + # + # @return [RubyGit::GitBinary] the git binary object + # + attr_reader :git + end + + @logger = NullLogger.new + + class << self + # The logger used by the RubyGit gem + # + # The default value is a NullLogger + # + # @example Using the logger + # RubyGit.logger.debug('Debug message') + # + # @example Setting the logger + # require 'logger' + # require 'stringio' + # log_device = StringIO.new + # RubyGit.logger = Logger.new(log_device, level: Logger::DEBUG) + # RubyGit.logger.debug('Debug message') + # log_device.string.include?('Debug message') + # => true + # + # @return [Logger] the logger used by the RubyGit gem + # + attr_accessor :logger end # Create an empty Git repository under the root worktree `path` diff --git a/lib/ruby_git/worktree.rb b/lib/ruby_git/worktree.rb index 2205dd8..6aaa0e9 100644 --- a/lib/ruby_git/worktree.rb +++ b/lib/ruby_git/worktree.rb @@ -115,6 +115,7 @@ def initialize(worktree_path) raise RubyGit::Error, "Path '#{worktree_path}' not valid." unless File.directory?(worktree_path) @path = root_path(worktree_path) + RubyGit.logger.debug("Created #{inspect}") end # Find the root path of a worktree containing `path` diff --git a/ruby_git.gemspec b/ruby_git.gemspec index b7d404c..f58a7d6 100644 --- a/ruby_git.gemspec +++ b/ruby_git.gemspec @@ -40,6 +40,8 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ['lib'] + spec.add_runtime_dependency 'null-logger', '~> 0.1' + spec.add_development_dependency 'bump', '~> 0.9' spec.add_development_dependency 'bundler-audit', '~> 0.7' spec.add_development_dependency 'github_changelog_generator', '~> 1.15' diff --git a/spec/lib/ruby_git/worktree_initialize_spec.rb b/spec/lib/ruby_git/worktree_initialize_spec.rb new file mode 100644 index 0000000..20f33ad --- /dev/null +++ b/spec/lib/ruby_git/worktree_initialize_spec.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require 'logger' +require 'stringio' +require 'tmpdir' + +RSpec.describe RubyGit::Worktree do + describe '.initialize' do + subject { described_class.open(worktree_path) } + let(:tmpdir) { Dir.mktmpdir } + after { FileUtils.rm_rf(tmpdir) if File.exist?(tmpdir) } + + context 'with a valid worktree path' do + let(:worktree_path) { tmpdir } + before do + raise RuntimeError unless system('git init', chdir: worktree_path, %i[out err] => IO::NULL) + end + it 'should log that a Worktree object was created at debug level' do + log_device = StringIO.new + saved_logger = RubyGit.logger + RubyGit.logger = Logger.new(log_device, level: Logger::DEBUG) + RubyGit::Worktree.new(worktree_path) + RubyGit.logger = saved_logger + expect(log_device.string).to include(' : Created #