gitsu 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,177 @@
1
+ require 'spec_helper'
2
+
3
+ module GitSu
4
+ describe Gitsu do
5
+ let(:output) { double('output') }
6
+ let(:switcher) { double('switcher') }
7
+ let(:gitsu) { GitSu::Gitsu.new(switcher, output) }
8
+
9
+ describe '#go' do
10
+ context 'when query string provided' do
11
+ context 'when no scope specified' do
12
+ it 'switches to the specified user in the default scope' do
13
+ switcher.should_receive(:request).with('Joe Bloggs', :default)
14
+ gitsu.go ['Joe', 'Bloggs']
15
+ end
16
+ end
17
+
18
+ context 'when local scope specified' do
19
+ context 'as short option (-l)' do
20
+ it 'switches to the specified user in the default scope' do
21
+ switcher.should_receive(:request).with('Joe Bloggs', :local)
22
+ gitsu.go ['Joe', 'Bloggs', "-l"]
23
+ end
24
+ end
25
+ context 'as long option (--local)' do
26
+ it 'switches to the specified user in the default scope' do
27
+ switcher.should_receive(:request).with('Joe Bloggs', :local)
28
+ gitsu.go ['Joe', 'Bloggs', "--local"]
29
+ end
30
+ end
31
+ end
32
+
33
+ context 'when global scope specified' do
34
+ context 'as short option (-g)' do
35
+ it 'switches to the specified user in the global scope' do
36
+ switcher.should_receive(:request).with('Joe Bloggs', :global)
37
+ gitsu.go ['Joe', 'Bloggs', "-g"]
38
+ end
39
+ end
40
+ context 'as long option (--global)' do
41
+ it 'switches to the specified user in the global scope' do
42
+ switcher.should_receive(:request).with('Joe Bloggs', :global)
43
+ gitsu.go ['Joe', 'Bloggs', "--global"]
44
+ end
45
+ end
46
+ end
47
+
48
+ context 'when system scope specified' do
49
+ context 'as short option (-s)' do
50
+ it 'switches to the specified user in the system scope' do
51
+ switcher.should_receive(:request).with('Joe Bloggs', :system)
52
+ gitsu.go ['Joe', 'Bloggs', "-s"]
53
+ end
54
+ end
55
+ context 'as long option (--system)' do
56
+ it 'switches to the specified user in the system scope' do
57
+ switcher.should_receive(:request).with('Joe Bloggs', :system)
58
+ gitsu.go ['Joe', 'Bloggs', "--system"]
59
+ end
60
+ end
61
+ end
62
+
63
+ context 'when multiple scopes specified' do
64
+ it 'switches to the specified user in the specified scopes' do
65
+ switcher.should_receive(:request).with('Joe Bloggs', :local)
66
+ switcher.should_receive(:request).with('Joe Bloggs', :global)
67
+ switcher.should_receive(:request).with('Joe Bloggs', :system)
68
+ gitsu.go ['Joe', 'Bloggs', "--local", "--global", "--system"]
69
+ end
70
+ end
71
+ end
72
+
73
+ context 'when no query provided' do
74
+ context 'when no scope specified' do
75
+ it 'prints all users' do
76
+ switcher.should_receive(:print_current).with(:all)
77
+ gitsu.go []
78
+ end
79
+ end
80
+
81
+ context 'when scope specified' do
82
+ it 'prints the current user in the specified scope' do
83
+ switcher.should_receive(:print_current).with(:system)
84
+ gitsu.go ["--system"]
85
+ end
86
+ end
87
+
88
+ context 'when multiple scopes specified' do
89
+ it 'prints the current user in the specified scopes' do
90
+ switcher.should_receive(:print_current).with(:local, :system)
91
+ gitsu.go ["--local", "--system"]
92
+ end
93
+ end
94
+ end
95
+
96
+ context 'when "clear" option passed' do
97
+ context "no scope is specified" do
98
+ context 'short option (-c)' do
99
+ it 'clears all Git users' do
100
+ switcher.should_receive(:clear).with(:all)
101
+ gitsu.go ["-c"]
102
+ end
103
+ end
104
+
105
+ context 'long option (--clear)' do
106
+ it 'clears all Git users' do
107
+ switcher.should_receive(:clear).with(:all)
108
+ gitsu.go ["--clear"]
109
+ end
110
+ end
111
+ end
112
+
113
+ context 'scope is specified' do
114
+ it 'clears the user in that scope' do
115
+ switcher.should_receive(:clear).with(:local)
116
+ gitsu.go ["--clear", "--local"]
117
+ end
118
+ end
119
+
120
+ context 'mulitple scopes are specified' do
121
+ it 'clears the user in those scopes' do
122
+ switcher.should_receive(:clear).with(:local, :system)
123
+ gitsu.go ["--clear", "--local", "--system"]
124
+ end
125
+ end
126
+ end
127
+
128
+ context 'when "add" option passed' do
129
+ context 'short option (-a)' do
130
+ it 'adds the specified user' do
131
+ switcher.should_receive(:add).with("John Galt <jgalt@example.com>")
132
+ gitsu.go ["-a", "John Galt <jgalt@example.com>"]
133
+ end
134
+ end
135
+
136
+ context 'long option (--add)' do
137
+ it 'adds the specified user' do
138
+ switcher.should_receive(:add).with("John Galt <jgalt@example.com>")
139
+ gitsu.go ["--add", "John Galt <jgalt@example.com>"]
140
+ end
141
+ end
142
+ end
143
+
144
+ context 'when "list" option passed' do
145
+ context 'short option (-t)' do
146
+ it 'lists the users' do
147
+ switcher.should_receive(:list)
148
+ gitsu.go ["-t"]
149
+ end
150
+ end
151
+
152
+ context 'long option (--list)' do
153
+ it 'lists the users' do
154
+ switcher.should_receive(:list)
155
+ gitsu.go ["--list"]
156
+ end
157
+ end
158
+ end
159
+
160
+ context 'when "edit" option passed' do
161
+ context 'short option (-e)' do
162
+ it 'edits the Gitsu config file' do
163
+ switcher.should_receive(:edit_config)
164
+ gitsu.go ["-e"]
165
+ end
166
+ end
167
+
168
+ context 'long option (--edit)' do
169
+ it 'edits the Gitsu config file' do
170
+ switcher.should_receive(:edit_config)
171
+ gitsu.go ["--edit"]
172
+ end
173
+ end
174
+ end
175
+ end
176
+ end
177
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ module GitSu
4
+ describe Runner do
5
+ let(:output) { double('output') }
6
+ let(:runner) { Runner.new(output) }
7
+
8
+ describe "#run" do
9
+ it "runs the block it's passed" do
10
+ ran = false
11
+ runner.run { ran = true }
12
+ ran.should be true
13
+ end
14
+
15
+ it "catches interruptions" do
16
+ output.should_receive(:puts).with "Interrupted"
17
+ runner.run { raise Interrupt }
18
+ end
19
+
20
+ it "prints errors" do
21
+ output.should_receive(:puts).with "Bad stuff happened"
22
+ runner.run { raise "Bad stuff happened" }
23
+ end
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,196 @@
1
+ require 'spec_helper'
2
+
3
+ module GitSu
4
+
5
+ describe Switcher do
6
+ let(:git) { double('git') }
7
+ let(:user_list) { double('user_list') }
8
+ let(:output) { double('output').as_null_object }
9
+ let(:switcher) { Switcher.new(git, user_list, output) }
10
+ let(:a_user) { User.new("Johnny Bravo", "jbravo@example.com") }
11
+
12
+ describe '#request' do
13
+ context "when request is a fully-qualified user string (e.g. 'John Galt <jgalt@example.com>'" do
14
+ it "switches to user" do
15
+ user = User.new('John Galt', 'jgalt@example.com')
16
+ git.should_receive(:select_user).with(user, :global)
17
+ git.should_receive(:render).with(user).and_return(user.to_s)
18
+ output.should_receive(:puts).with("Switched global user to John Galt <jgalt@example.com>")
19
+ switcher.request('John Galt <jgalt@example.com>', :global)
20
+ end
21
+ end
22
+
23
+ context "when no matching user found" do
24
+ it "does not switch user" do
25
+ user_list.should_receive(:find).with('asdfasdf').and_return User::NONE
26
+ output.should_receive(:puts).with("No user found matching 'asdfasdf'")
27
+ switcher.request('asdfasdf', :global)
28
+ end
29
+ end
30
+
31
+ context "when user is in user list" do
32
+ it "switches to requested user" do
33
+ user = User.new('John Galt', 'jgalt@example.com')
34
+
35
+ user_list.should_receive(:find).with("john").and_return user
36
+ git.should_receive(:select_user).with user, :global
37
+ git.should_receive(:render).with(user).and_return user.to_s
38
+ output.should_receive(:puts).with("Switched global user to John Galt <jgalt@example.com>")
39
+ switcher.request "john", :global
40
+ end
41
+ end
42
+
43
+ context "when 'default' scope is specified" do
44
+ it "switches user in configured default scope" do
45
+ user = User.new('John Galt', 'jgalt@example.com')
46
+
47
+ user_list.should_receive(:find).with("john").and_return user
48
+ git.should_receive(:default_select_scope).and_return(:global)
49
+ git.should_receive(:select_user).with user, :global
50
+ git.should_receive(:render).with(user).and_return user.to_s
51
+ output.should_receive(:puts).with("Switched global user to John Galt <jgalt@example.com>")
52
+ switcher.request "john", :default
53
+ end
54
+ end
55
+ end
56
+
57
+ describe '#print_current' do
58
+ context "when 'all' scope is specified" do
59
+ it "prints all users" do
60
+ local_user = User.new('Johnny Local', 'jlocal@example.com')
61
+ global_user = User.new('Johnny Global', 'jglobal@example.com')
62
+ system_user = User.new('Johnny System', 'jsystem@example.com')
63
+ derived_user = local_user
64
+
65
+ git.should_receive(:render_user).with(:derived).and_return derived_user.to_s
66
+ git.should_receive(:render_user).with(:local).and_return local_user.to_s
67
+ git.should_receive(:render_user).with(:global).and_return global_user.to_s
68
+ git.should_receive(:render_user).with(:system).and_return system_user.to_s
69
+ output.should_receive(:puts).with("Current user: Johnny Local <jlocal@example.com>")
70
+ output.should_receive(:puts).with("Local: Johnny Local <jlocal@example.com>")
71
+ output.should_receive(:puts).with("Global: Johnny Global <jglobal@example.com>")
72
+ output.should_receive(:puts).with("System: Johnny System <jsystem@example.com>")
73
+ switcher.print_current(:all)
74
+ end
75
+ end
76
+
77
+ context "when a scope is specified" do
78
+ context "when there is a user selected" do
79
+ it "prints the current user" do
80
+ current_user = User.new('John Galt', 'jgalt@example.com')
81
+ git.should_receive(:selected_user).with(:global).and_return current_user
82
+ git.should_receive(:render_user).with(:global).and_return current_user.to_s
83
+ output.should_receive(:puts).with("John Galt <jgalt@example.com>")
84
+ switcher.print_current(:global)
85
+ end
86
+ end
87
+
88
+ context "when there is no user selected" do
89
+ it 'does not print anything' do
90
+ git.should_receive(:selected_user).with(:local).and_return User::NONE
91
+ switcher.print_current(:local)
92
+ end
93
+ end
94
+ end
95
+
96
+ context "when multiple scopes are specified" do
97
+ context "when there is a user selected in all scopes" do
98
+ it "prints the current user in those scopes" do
99
+ local_user = User.new('John Local', 'jl@example.com')
100
+ global_user = User.new('John Global', 'jg@example.com')
101
+ git.should_receive(:selected_user).with(:local).and_return local_user
102
+ git.should_receive(:render_user).with(:local).and_return local_user.to_s
103
+ git.should_receive(:selected_user).with(:global).and_return global_user
104
+ git.should_receive(:render_user).with(:global).and_return global_user.to_s
105
+ output.should_receive(:puts).with("John Local <jl@example.com>")
106
+ output.should_receive(:puts).with("John Global <jg@example.com>")
107
+ switcher.print_current(:local, :global)
108
+ end
109
+ end
110
+
111
+ context "when there is no user selected in one scope" do
112
+ it 'prints only users for scopes that have users' do
113
+ global_user = User.new('John Global', 'jg@example.com')
114
+ git.should_receive(:selected_user).with(:local).and_return User::NONE
115
+ git.should_receive(:selected_user).with(:global).and_return global_user
116
+ git.should_receive(:render_user).with(:global).and_return global_user.to_s
117
+ output.should_receive(:puts).with("John Global <jg@example.com>")
118
+ switcher.print_current(:local, :global)
119
+ end
120
+ end
121
+ end
122
+ end
123
+
124
+ describe '#add' do
125
+ context "when the user doesn't already exist in the user list" do
126
+ it "adds the specified user to the user list" do
127
+ user_list.should_receive(:list).and_return []
128
+ user_list.should_receive(:add).with User.new('John Galt', 'jgalt@example.com')
129
+ output.should_receive(:puts).with("User 'John Galt <jgalt@example.com>' added to users")
130
+ switcher.add("John Galt <jgalt@example.com>")
131
+ end
132
+ end
133
+ context "when the user already exists in the user list" do
134
+ it "doesn't add the user to the user list" do
135
+ user_list.should_receive(:list).and_return [User.new('John Galt', 'jgalt@example.com')]
136
+ output.should_receive(:puts).with("User 'John Galt <jgalt@example.com>' already in user list")
137
+ switcher.add("John Galt <jgalt@example.com>")
138
+ end
139
+ end
140
+ context "when the input is not in expected format" do
141
+ it "prints an error" do
142
+ output.should_receive(:puts).with("Couldn't parse 'xxx' as user (expected user in format: 'John Smith <jsmith@example.com>')")
143
+ switcher.add("xxx")
144
+ end
145
+ end
146
+ end
147
+
148
+ describe '#list' do
149
+ it "lists the configured users" do
150
+ users = []
151
+ users << User.new("User One", "1@example.com")
152
+ users << User.new("User Two", "2@example.com")
153
+
154
+ user_list.should_receive(:list).and_return(users)
155
+ git.should_receive(:render).with(users.first).and_return(users.first.to_s)
156
+ output.should_receive(:puts).with("User One <1@example.com>")
157
+ git.should_receive(:render).with(users.last).and_return(users.last.to_s)
158
+ output.should_receive(:puts).with("User Two <2@example.com>")
159
+ switcher.list
160
+ end
161
+ end
162
+
163
+ describe '#clear' do
164
+ context "when a scope is specified" do
165
+ it "clears the current user in the specified scope" do
166
+ git.should_receive(:clear_user).with(:local)
167
+ switcher.clear :local
168
+ end
169
+ end
170
+ context "when multiple scopes are specified" do
171
+ it "clears Git users in the specified scopes" do
172
+ output.should_receive(:puts).with("Clearing Git users in local and global scopes")
173
+ git.should_receive(:clear_user).with(:local)
174
+ git.should_receive(:clear_user).with(:global)
175
+ switcher.clear :local,:global
176
+ end
177
+ end
178
+ context "when 'all' scope is specified" do
179
+ it "clears all Git users" do
180
+ output.should_receive(:puts).with("Clearing Git users in all scopes")
181
+ git.should_receive(:clear_user).with(:local)
182
+ git.should_receive(:clear_user).with(:global)
183
+ git.should_receive(:clear_user).with(:system)
184
+ switcher.clear :all
185
+ end
186
+ end
187
+ end
188
+
189
+ describe '#edit_config' do
190
+ it "tells Git to open the Gitsu config file in an editor" do
191
+ git.should_receive(:edit_gitsu_config)
192
+ switcher.edit_config
193
+ end
194
+ end
195
+ end
196
+ end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+ require 'yaml'
3
+
4
+ module GitSu
5
+ describe UserList do
6
+
7
+ let(:user_file) { "/tmp/#{rand}" }
8
+ let(:user_list) { UserList.new(user_file) }
9
+ after { File.delete user_file }
10
+
11
+ describe "#add" do
12
+ it "adds a user to the list" do
13
+ user_list.add User.new("John Galt", "jgalt@example.com")
14
+
15
+ user_map = YAML.parse_file(user_file).transform
16
+ user_map.keys.should include("jgalt@example.com")
17
+ user_map["jgalt@example.com"].should == "John Galt"
18
+ end
19
+ end
20
+
21
+ describe "#list" do
22
+ context "when there are no users configured" do
23
+ it "returns an empty array" do
24
+ user_list.list.should be_empty
25
+ end
26
+ end
27
+
28
+ context "when there are users configured" do
29
+ it "returns an array of all the users" do
30
+ user_list.add User.new("John Galt", "jgalt@example.com")
31
+
32
+ list = user_list.list
33
+ list.size.should be 1
34
+ list.should include User.new("John Galt", "jgalt@example.com")
35
+ end
36
+ end
37
+ end
38
+
39
+ describe "#find" do
40
+ context "when a matching user exists" do
41
+ it "returns the matching user by first name" do
42
+ user_list.add User.new("John Galt", "jgalt@example.com")
43
+
44
+ user_list.find("john").should == User.new("John Galt", "jgalt@example.com")
45
+ user_list.find("John").should == User.new("John Galt", "jgalt@example.com")
46
+ end
47
+ it "returns the matching user by email snippet" do
48
+ user_list.add User.new("John Galt", "jgalt@example.com")
49
+
50
+ user_list.find("example").should == User.new("John Galt", "jgalt@example.com")
51
+ end
52
+ it "returns the matching user by initials" do
53
+ user_list.add User.new("John Galt", "john.galt@example.com")
54
+
55
+ user_list.find("jg").should == User.new("John Galt", "john.galt@example.com")
56
+ user_list.find("JG").should == User.new("John Galt", "john.galt@example.com")
57
+ end
58
+ it "favours prefix over innards" do
59
+ user_list.add User.new("Matthew Jackson", "mj@example.com")
60
+ user_list.add User.new("Thomas Hickleton", "tom@example.com")
61
+ user_list.add User.new("John Smith", "js@example.com")
62
+
63
+ user_list.find("th").should eq User.new("Thomas Hickleton", "tom@example.com")
64
+ user_list.find("s").should eq User.new("John Smith", "js@example.com")
65
+ end
66
+ it "favours full firstname match over partial one" do
67
+ user_list.add User.new("Matthew Jackson", "mj@example.com")
68
+ user_list.add User.new("Mat Jackson", "zz@example.com")
69
+
70
+ user_list.find("mat").should eq User.new("Mat Jackson", "zz@example.com")
71
+ end
72
+ end
73
+
74
+ context "when no matching user exists" do
75
+ it "returns no user" do
76
+ user_list.find("john").should be User::NONE
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+