From d52e6ea10e1239cfe3f9e568f2a75f07154cab52 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sun, 29 May 2022 12:17:28 +0200 Subject: [PATCH] Prepare for testing --- Gemfile.lock | 3 ++ format-staged.gemspec | 3 +- spec/git.rb | 72 +++++++++++++++++++++++++++++++++++++++++++ spec/test_spec.rb | 22 +++++++++++++ 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 spec/git.rb create mode 100644 spec/test_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index 1576cac..f7fd5a9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -45,6 +45,8 @@ GEM parser (>= 3.1.1.0) rubocop-rake (0.6.0) rubocop (~> 1.0) + rubocop-rspec (2.11.1) + rubocop (~> 1.19) ruby-progressbar (1.11.0) unicode-display_width (2.1.0) @@ -58,6 +60,7 @@ DEPENDENCIES rspec rubocop (~> 1.29) rubocop-rake (~> 0.6) + rubocop-rspec BUNDLED WITH 2.1.4 diff --git a/format-staged.gemspec b/format-staged.gemspec index e48cb9e..8640d53 100644 --- a/format-staged.gemspec +++ b/format-staged.gemspec @@ -20,9 +20,10 @@ Gem::Specification.new do |s| s.add_dependency 'colorize' s.add_development_dependency 'rake', '~> 13.0' + s.add_development_dependency 'rspec' s.add_development_dependency 'rubocop', '~> 1.29' s.add_development_dependency 'rubocop-rake', '~> 0.6' - s.add_development_dependency 'rspec' + s.add_development_dependency 'rubocop-rspec' s.metadata = { 'rubygems_mfa_required' => 'true' diff --git a/spec/git.rb b/spec/git.rb new file mode 100644 index 0000000..6d5ec56 --- /dev/null +++ b/spec/git.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +require 'tmpdir' +require 'fileutils' +require 'English' + +## +# Test helpers for managing a git repository +module Git + ## + # A git repository + class Repo + attr_reader :path + + def initialize + @path = Dir.mktmpdir + + git 'init' + end + + def file_in_tree(name, content) + set_content name, content + stage name + commit "Add #{name}" + end + + def set_content(name, content) + absolute = Pathname.new(path) + name + + FileUtils.mkdir_p absolute.dirname + File.write absolute, content.end_with?("\n") ? content : "#{content}\n" + end + + def stage(file) + git 'add', file + end + + def commit(message) + git 'commit', '-m', message + end + + def content(file) + git 'show', ":#{file}" + end + + def cleanup + FileUtils.remove_entry path + end + + def git(*cmd) + in_repo do + output = IO.popen(['git'] + cmd) do |io| + io.read.chomp + end + + raise 'Failed to run git' unless $CHILD_STATUS.success? + + output + end + end + + def in_repo(&block) + Dir.chdir path, &block + end + end + + def self.new_repo + repo = Repo.new + yield repo if block_given? + repo + end +end diff --git a/spec/test_spec.rb b/spec/test_spec.rb new file mode 100644 index 0000000..778f761 --- /dev/null +++ b/spec/test_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require_relative 'git' + +require 'format_staged' + +describe FormatStaged do + def repo + @repo ||= Git.new_repo do |r| + r.file_in_tree 'index.js', <<~CONTENT + function foo () { + return 'foo' + } + CONTENT + end + end + + after :each do + @repo&.cleanup + @repo = nil + end +end