Skip to content

Commit

Permalink
add adding integration specs and some crappy helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
cristibalan committed Aug 19, 2008
1 parent 21b6608 commit 0d77e1e
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 66 deletions.
118 changes: 52 additions & 66 deletions lib/braid/operations.rb
@@ -1,47 +1,19 @@
require 'singleton'
require 'rubygems'
require 'open4'

module Braid
module Operations
class ShellExecutionError < BraidError
def initialize(err = nil)
@err = err
end

def message
@err.to_s.split("\n").first
end
end
class VersionTooLow < BraidError
def initialize(command, version)
@command = command
@version = version.to_s.split("\n").first
end

def message
"#{@command} version too low: #{@version}"
end
end
class UnknownRevision < BraidError
def message
"unknown revision: #{super}"
end
class VersionError < BraidError
end
class LocalChangesPresent < BraidError
def message
"local changes are present"
end
end

# The command proxy is meant to encapsulate commands such as git, git-svn and svn, that work with subcommands.
class Proxy
include Singleton

def self.command; name.split('::').last.downcase; end # hax!

# The command proxy is meant to encapsulate commands such as git, git svn and svn, that work with subcommands.
#
# It is expected that subclasses override the command method, define a COMMAND constant and have a VersionTooLow exception.
class CommandProxy
def version
status, out, err = exec!("#{self.class.command} --version")
status, out, err = exec!("#{self.class::COMMAND} --version")
out.sub(/^.* version/, "").strip
end

Expand All @@ -68,7 +40,7 @@ def require_version(required)
end

def require_version!(required)
require_version(required) || raise(VersionTooLow.new(self.class.command, version))
require_version(required) || raise(self.class::VersionTooLow, version)
end

private
Expand All @@ -78,7 +50,7 @@ def command(name)
end

def invoke(arg, *args)
exec!("#{command(arg)} #{args.join(' ')}".strip)[1].strip # return stdout
exec!("#{command(arg)} #{args.join(' ')}".strip)[1] # return stdout
end

def method_missing(name, *args)
Expand All @@ -93,8 +65,8 @@ def exec(cmd)

out, err = nil
status = Open4.popen4(cmd) do |pid, stdin, stdout, stderr|
out = stdout.read
err = stderr.read
out = stdout.read.strip
err = stderr.read.strip
end.exitstatus
[status, out, err]

Expand All @@ -109,9 +81,18 @@ def exec!(cmd)
end
end

class Git < Proxy
def commit(message, *args)
status, out, err = exec("git commit -m #{message.inspect} --no-verify #{args.join(' ')}")
class Git < CommandProxy
COMMAND = "git"

class UnknownRevision < BraidError
end
class LocalChangesPresent < BraidError
end
class VersionTooLow < VersionError
end

def commit(message)
status, out, err = exec("git commit -m #{message.inspect} --no-verify")

if status == 0
true
Expand All @@ -125,7 +106,7 @@ def commit(message, *args)
def fetch(remote)
# open4 messes with the pipes of index-pack
system("git fetch -n #{remote} &> /dev/null")
raise ShellExecutionError, "could not fetch" unless $? == 0
raise ShellExecutionError unless $? == 0
true
end

Expand Down Expand Up @@ -219,38 +200,40 @@ def branch
out[2..-1]
end

def apply(diff, *args)
err = nil
status = Open4.popen4("git apply --index --whitespace=nowarn #{args.join(' ')} -") do |pid, stdin, stdout, stderr|
def apply(diff)
# always uses index
status = Open4.popen4("git apply --index -") do |pid, stdin, stdout, stderr|
stdin.puts(diff)
stdin.close

err = stderr.read
end.exitstatus
raise ShellExecutionError, err unless status == 0
raise ShellExecutionError unless status == 0
true
end

private
def command(name)
"#{self.class.command} #{name.to_s.gsub('_', '-')}"
"#{COMMAND} #{name.to_s.gsub('_', '-')}"
end
end

class GitSvn < Proxy
def self.command; "git svn"; end
class GitSvn < CommandProxy
COMMAND = "git svn"

class UnknownRevision < BraidError
end
class VersionTooLow < VersionError
end

def commit_hash(remote, revision)
out = invoke(:log, "--show-commit --oneline", "-r #{revision}", remote)
part = out.to_s.split(" | ")[1]
raise UnknownRevision, "r#{revision}" unless part
Git.instance.rev_parse(part) # FIXME ugly ugly ugly
part = out.split(" | ")[1]
raise UnknownRevision, "unknown revision: #{revision}" unless part
Git.new.rev_parse(part) # FIXME ugly ugly ugly
end

def fetch(remote)
# open4 messes with the pipes of index-pack
system("git svn fetch #{remote} &> /dev/null")
raise ShellExecutionError, "could not fetch" unless $? == 0
true
end

Expand All @@ -261,11 +244,13 @@ def init(remote, path)

private
def command(name)
"#{self.class.command} #{name}"
"#{COMMAND} #{name}"
end
end

class Svn < Proxy
class Svn < CommandProxy
COMMAND = "svn"

def clean_revision(revision)
revision.to_i if revision
end
Expand All @@ -279,17 +264,18 @@ def head_revision(path)
end

module VersionControl
def git
Git.instance
end
private
def git
Git.new
end

def git_svn
GitSvn.instance
end
def git_svn
GitSvn.new
end

def svn
Svn.instance
end
def svn
Svn.new
end
end
end
end
56 changes: 56 additions & 0 deletions test/integration/adding_spec.rb
@@ -0,0 +1,56 @@
require File.dirname(__FILE__) + '/../integration_helper'

describe "Adding a mirror in a clean repository" do

before do
FileUtils.rm_rf(TMP_PATH)
FileUtils.mkdir_p(TMP_PATH)
end


describe "from a git repository" do
before do
@shiny = create_git_repo_from_fixture("shiny")
@skit1 = create_git_repo_from_fixture("skit1")
end

it "should add the files and commit" do
in_dir(@shiny) do
`braid add --type git #{@skit1}`
end

file_name = "skit1/layouts/layout.liquid"
output = `diff -U 3 #{File.join(FIXTURE_PATH, file_name)} #{File.join(TMP_PATH, "shiny", file_name)}`
$?.should.be.success

output = `git log --pretty=oneline`.split("\n")
output.length.should == 2
output[0].should =~ "Add mirror 'skit1/'"
end
end

describe "from an svn repository" do
before do
@shiny = create_git_repo_from_fixture("shiny")
@skit1 = create_svn_repo_from_fixture("skit1")
end

it "should add the files and commit" do
in_dir(@shiny) do
`braid add --type svn #{@skit1}`
end

file_name = "skit1/layouts/layout.liquid"
output = `diff -U 3 #{File.join(FIXTURE_PATH, file_name)} #{File.join(TMP_PATH, "shiny", file_name)}`
$?.should.be.success

output = `git log --pretty=oneline`.split("\n")
output.length.should == 2
output[0].should =~ "Add mirror 'skit1/'"
end
end



end

67 changes: 67 additions & 0 deletions test/integration_helper.rb
@@ -0,0 +1,67 @@
require 'rubygems'
require 'test/spec'
require 'mocha'

#require File.dirname(__FILE__) + '/../lib/braid'

require 'tempfile'
require 'fileutils'
require 'pathname'

#tmp_file = Tempfile.new("braid")
#tmp_file_path = tmp_file.path
#tmp_file.unlink
#TMP = File.basename(tmp_file_path)

TMP_PATH = File.join(Dir.tmpdir, "braid_integration")
BRAID_PATH = Pathname.new(File.dirname(__FILE__)).parent.realpath
FIXTURE_PATH = File.join(BRAID_PATH, "test", "fixtures")
FileUtils.mkdir_p(TMP_PATH)

#def exec(cmd)
# `cd #{TMP} && #{cmd}`
#end

def in_dir(dir = TMP_PATH)
Dir.chdir(dir)
yield
end

def run_cmds(ary)
ary.each do |cmd|
cmd = cmd.strip!
out = `#{cmd}`
end
end

def create_git_repo_from_fixture(fixture_name)
git_repo = File.join(TMP_PATH, fixture_name)
FileUtils.cp_r(File.join(FIXTURE_PATH, fixture_name), TMP_PATH)
in_dir(git_repo) do
run_cmds(<<-EOD)
git init
git add *
git commit -m "initial commit of #{fixture_name}"
EOD
end
git_repo
end

def create_svn_repo_from_fixture(fixture_name)
svn_wc = File.join(TMP_PATH, fixture_name + "_repo")
svn_repo = File.join(TMP_PATH, fixture_name)
run_cmds(<<-EOD)
svnadmin create #{svn_repo}
svn co file://#{svn_repo} #{svn_wc}
EOD
FileUtils.cp_r("#{FIXTURE_PATH}/#{fixture_name}/.", svn_wc)
in_dir(svn_wc) do
run_cmds(<<-EOD)
svn add *
svn commit -m "initial commit of #{fixture_name}"
EOD
end
"file://#{svn_repo}"
end


0 comments on commit 0d77e1e

Please sign in to comment.