|
| 1 | +require "unindent" |
| 2 | + |
1 | 3 | describe "CLI" do
|
2 | 4 | before :all do
|
3 | 5 | # use local scripts
|
4 | 6 | ENV["PATH"] = "#{File.join(File.dirname(__FILE__),"..","bin")}:#{ENV["PATH"]}"
|
5 | 7 | end
|
6 | 8 |
|
7 |
| - def run(command) |
| 9 | + def run(command, options={}) |
8 | 10 | result = `#{command}`
|
9 |
| - raise "FAILED #{command} : #{result}" unless $?.success? |
| 11 | + raise "FAILED #{command} : #{result}" if $?.success? == !!options[:fail] |
10 | 12 | result
|
11 | 13 | end
|
12 | 14 |
|
| 15 | + def write(file, content) |
| 16 | + File.open(file, 'w'){|f| f.write content } |
| 17 | + end |
| 18 | + |
13 | 19 | around do |example|
|
14 | 20 | dir = "spec/tmp"
|
15 | 21 | run "rm -rf #{dir}"
|
@@ -62,4 +68,135 @@ def run(command)
|
62 | 68 | end
|
63 | 69 | end
|
64 | 70 | end
|
| 71 | + |
| 72 | + describe "pair" do |
| 73 | + def expect_config(result, name, initials, email, options={}) |
| 74 | + global = "cd /tmp && " if options[:global] |
| 75 | + run("#{global}git config user.name").should == "#{name}\n" |
| 76 | + run("#{global}git config user.initials").should == "#{initials}\n" |
| 77 | + run("#{global}git config user.email").should == "#{email}\n" |
| 78 | + |
| 79 | + prefix = (options[:global] ? "global: " : "local: ") |
| 80 | + result.should include "#{prefix}user.name #{name}" |
| 81 | + #result.should include "#{prefix}user.initials #{initials}" # TODO |
| 82 | + result.should include "#{prefix}user.email #{email}" |
| 83 | + end |
| 84 | + |
| 85 | + context "with .pairs file" do |
| 86 | + before do |
| 87 | + write ".pairs", <<-YAML.unindent |
| 88 | + pairs: |
| 89 | + ab: Aa Bb |
| 90 | + bc: Bb Cc |
| 91 | + cd: Cc Dd |
| 92 | +
|
| 93 | + email: |
| 94 | + prefix: the-pair |
| 95 | + domain: the-host.com |
| 96 | + YAML |
| 97 | + end |
| 98 | + |
| 99 | + describe "global" do |
| 100 | + it "sets pairs globally when global: true is set" do |
| 101 | + write ".pairs", File.read(".pairs") + "\nglobal: true" |
| 102 | + result = run "git pair ab" |
| 103 | + expect_config result, "Aa Bb", "ab", "[email protected]", :global => true |
| 104 | + end |
| 105 | + |
| 106 | + it "sets pairs globally when --global is given" do |
| 107 | + result = run "git pair ab --global" |
| 108 | + result.should include "global: user.name Aa Bb" |
| 109 | + expect_config result, "Aa Bb", "ab", "[email protected]", :global => true |
| 110 | + end |
| 111 | + |
| 112 | + it "unsets global config when no argument is passed" do |
| 113 | + run "git pair ab --global" |
| 114 | + run "git pair ab" |
| 115 | + result = run "git pair --global" |
| 116 | + #result.should include "Unset --global user.name, user.email and user.initials" |
| 117 | + expect_config result, "Aa Bb", "ab", "[email protected]" |
| 118 | + result.should_not include("global:") |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + it "can set a single user as pair" do |
| 123 | + result = run "git pair ab" |
| 124 | + expect_config result, "Aa Bb", "ab", "[email protected]" |
| 125 | + end |
| 126 | + |
| 127 | + it "can set a 2 users as pair" do |
| 128 | + result = run "git pair ab bc" |
| 129 | + expect_config result, "Aa Bb & Bb Cc", "ab bc", "[email protected]" |
| 130 | + end |
| 131 | + |
| 132 | + it "can set a n users as pair" do |
| 133 | + result = run "git pair ab bc cd" |
| 134 | + expect_config result, "Aa Bb, Bb Cc & Cc Dd", "ab bc cd", "[email protected]" |
| 135 | + end |
| 136 | + |
| 137 | + it "fails when there is no .git" do |
| 138 | + run "rm -rf .git" |
| 139 | + run "git pair ab", :fail => true |
| 140 | + end |
| 141 | + |
| 142 | + it "finds .pairs file in lower parent folder" do |
| 143 | + run "mkdir foo" |
| 144 | + Dir.chdir "foo" do |
| 145 | + run "git init" |
| 146 | + result = run "git pair ab" |
| 147 | + expect_config result, "Aa Bb", "ab", "[email protected]" |
| 148 | + end |
| 149 | + end |
| 150 | + |
| 151 | + it "unsets local config when no argument is passed" do |
| 152 | + run "git pair ab --global" |
| 153 | + run "git pair bc" |
| 154 | + result = run "git pair" |
| 155 | + #result.should include "Unset user.name, user.email and user.initials" #TODO |
| 156 | + expect_config result, "Aa Bb", "ab", "[email protected]", :global => true |
| 157 | + result.should_not include("local:") |
| 158 | + end |
| 159 | + |
| 160 | + it "uses hard email when given" do |
| 161 | + write ".pairs", File.read(".pairs").sub(/email:.*/m, "email: [email protected]") |
| 162 | + result = run "git pair ab" |
| 163 | + expect_config result, "Aa Bb", "ab", "[email protected]" |
| 164 | + end |
| 165 | + |
| 166 | + it "fails with unknown initials" do |
| 167 | + result = run "git pair xx", :fail => true |
| 168 | + result.should include("Couldn't find author name for initials: xx") |
| 169 | + end |
| 170 | + |
| 171 | + it "uses alternate email prefix" do |
| 172 | + write ".pairs", File.read(".pairs").sub(/ab:.*/, "ab: Aa Bb; blob") |
| 173 | + result = run "git pair ab" |
| 174 | + expect_config result, "Aa Bb", "ab", "[email protected]" |
| 175 | + end |
| 176 | + end |
| 177 | + |
| 178 | + context "without a .pairs file in the tree" do |
| 179 | + around do |example| |
| 180 | + Dir.chdir "/tmp" do |
| 181 | + dir = "git_stats_test" |
| 182 | + run "rm -rf #{dir}" |
| 183 | + run "mkdir #{dir}" |
| 184 | + Dir.chdir dir do |
| 185 | + run "git init" |
| 186 | + example.run |
| 187 | + end |
| 188 | + run "rm -rf #{dir}" |
| 189 | + end |
| 190 | + end |
| 191 | + |
| 192 | + it "fails if it cannot find a pairs file" do |
| 193 | + run "git pair ab", :fail => true |
| 194 | + end |
| 195 | + |
| 196 | + it "prints instructions" do |
| 197 | + result = run "git pair ab", :fail => true |
| 198 | + result.should include("Could not find a .pairs file. Create a YAML file in your project or home directory.") |
| 199 | + end |
| 200 | + end |
| 201 | + end |
65 | 202 | end
|
0 commit comments