require 'dply/git' require 'repo' module Dply describe Git do before :all do @work_dir = "tmp/git" @source_repo = "#{Dir.pwd}/tmp/repo.git" FileUtils.rm_rf @work_dir FileUtils.mkdir_p @work_dir end def source_repo @source_repo end def tmp_dir(&block) d = Dir.mktmpdir nil, "#{Dir.pwd}/#{@work_dir}" Dir.chdir(d) { yield d } end describe ".pull" do it "checks out the given branch and pulls any changes to it from upstream" do tmp_dir do |d| upstream = "#{d}/upstream" system! "git clone #{source_repo} upstream" system! "git clone #{upstream} repo" Dir.chdir "upstream" do system! "git checkout -b new_data" system! "echo 'new_data' > new_data" system! "git add new_data; git commit -m 'commit'" end Dir.chdir "repo" do suppress_output { Git.pull "new_data" } expect(`git rev-parse --abbrev-ref HEAD`.chomp).to eq('new_data') expect(File).to exist('new_data') end end end end describe ".checkout" do it "checks out the given branch" do upstream = source_repo tmp_dir do system! "git clone #{upstream} repo", quiet: true Dir.chdir "repo" do # local tracking branch doesn't exist suppress_output { Git.checkout "some_branch" } expect(`git rev-parse --abbrev-ref HEAD`.chomp).to eq("some_branch") #local tracking branch exists suppress_output { Git.checkout "master" } expect(`git rev-parse --abbrev-ref HEAD`.chomp).to eq("master") end end end end describe ".clone" do it "clones the repo from mirror and sets the upstream to original" do mirror = source_repo original = "/some/original/repo.git" tmp_dir do suppress_output { Git.clone original, "repo", mirror: mirror } Dir.chdir "repo" do expect(Git.remote_url).to eq(original) end end end it "clones the repo from original source when mirror is not specified" do original = source_repo tmp_dir do suppress_output { Git.clone original, "repo", mirror: nil } Dir.chdir "repo" do expect(Git.remote_url).to eq(original) end end end end describe ".clean" do it "cleans any changes made to the working tree (resets HEAD state and removes untracked files)" do upstream = source_repo tmp_dir do system! "git clone #{upstream} repo", quiet: true Dir.chdir "repo" do system! "echo asda > my_new_file" #new system! "echo existing > Gemfile" #existing suppress_output { Git.clean } lines = IO.popen(%w(git status --porcelain)) { |f| f.readlines } expect(lines.size).to eq(0) end end end end describe ".tracking_branch" do it "returns the tracking branch for given branch" do upstream = source_repo branch = "some_branch" tmp_dir do system! "git clone #{upstream} repo", quiet: true Dir.chdir "repo" do system! "git checkout -b #{branch} -t origin/#{branch}", quiet: true expect(Git.tracking_branch(branch)).to eq("origin/#{branch}") end end end end describe ".remote_url" do it "returns the url for origin upstream" do upstream = source_repo tmp_dir do |d| system! "git clone #{upstream} repo", quiet: true Dir.chdir("repo") do expect(Git.remote_url).to eq(upstream) end end end end describe ".commit_id" do it "returns the commit id" do Dir.chdir source_repo do commit_id = `git rev-parse HEAD`.chomp expect(Git.commit_id).to eq(commit_id) end end end end end