toolshed 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +14 -0
- data/Rakefile +11 -0
- data/bin/toolshed.rb +95 -16
- data/lib/toolshed/base.rb +7 -0
- data/lib/toolshed/cli.rb +13 -6
- data/lib/toolshed/client.rb +105 -83
- data/lib/toolshed/commands/checkout_branch.rb +26 -0
- data/lib/toolshed/commands/create_branch.rb +60 -0
- data/lib/toolshed/commands/create_pivotal_tracker_note.rb +9 -4
- data/lib/toolshed/commands/create_pull_request.rb +137 -0
- data/lib/toolshed/commands/delete_branch.rb +27 -0
- data/lib/toolshed/commands/get_daily_time_update.rb +21 -0
- data/lib/toolshed/commands/get_pivotal_tracker_story_information.rb +6 -3
- data/lib/toolshed/commands/list_branches.rb +13 -0
- data/lib/toolshed/commands/push_branch.rb +11 -0
- data/lib/toolshed/commands/update_pivotal_tracker_story_status.rb +8 -5
- data/lib/toolshed/git/git.rb +134 -0
- data/lib/toolshed/git/github.rb +88 -0
- data/lib/toolshed/ticket_tracking/pivotal_tracker.rb +111 -0
- data/lib/toolshed/ticket_tracking/ticket_tracking.rb +6 -0
- data/lib/toolshed/time_tracking/harvest.rb +68 -0
- data/lib/toolshed/time_tracking/time_tracking.rb +6 -0
- data/lib/toolshed/version.rb +1 -1
- data/lib/toolshed.rb +8 -2
- data/test/commands/checkout_branch_test.rb +28 -0
- data/test/commands/commands_helper.rb +2 -0
- data/test/commands/create_branch_test.rb +44 -0
- data/test/commands/create_pull_request_test.rb +125 -0
- data/test/commands/delete_branch_test.rb +75 -0
- data/test/commands/push_branch_test.rb +49 -0
- data/test/config.rb +1 -0
- data/test/git/git_helper.rb +31 -0
- data/test/git/git_test.rb +115 -0
- data/test/git/github_test.rb +91 -0
- data/test/helper.rb +98 -0
- data/toolshed.gemspec +8 -0
- metadata +151 -9
- data/lib/toolshed/commands/checkout_git_branch.rb +0 -23
- data/lib/toolshed/commands/create_git_branch.rb +0 -28
- data/lib/toolshed/commands/create_github_pull_request.rb +0 -77
- data/lib/toolshed/commands/push_git_branch.rb +0 -10
- data/lib/toolshed/github.rb +0 -89
- data/lib/toolshed/pivotal_tracker.rb +0 -87
data/lib/toolshed.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require "toolshed/version"
|
2
2
|
require 'httparty'
|
3
3
|
require 'pivotal-tracker'
|
4
|
+
require 'harvested'
|
5
|
+
require 'veto'
|
4
6
|
|
5
7
|
module Toolshed
|
6
8
|
BLANK_REGEX = /\S+/
|
@@ -20,6 +22,10 @@ end
|
|
20
22
|
|
21
23
|
require 'toolshed/base'
|
22
24
|
require 'toolshed/client'
|
23
|
-
require 'toolshed/
|
24
|
-
require 'toolshed/
|
25
|
+
require 'toolshed/git/git'
|
26
|
+
require 'toolshed/git/github'
|
27
|
+
require 'toolshed/ticket_tracking/ticket_tracking'
|
28
|
+
require 'toolshed/ticket_tracking/pivotal_tracker'
|
29
|
+
require 'toolshed/time_tracking/time_tracking'
|
30
|
+
require 'toolshed/time_tracking/harvest'
|
25
31
|
require 'toolshed/error'
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'commands/commands_helper'
|
2
|
+
require 'toolshed/commands/checkout_branch'
|
3
|
+
|
4
|
+
class CheckoutBranchTest < Test::Unit::TestCase
|
5
|
+
def test_checkout_branch
|
6
|
+
current_branch = Toolshed::Git::Base.branch_name
|
7
|
+
|
8
|
+
new_branch_name = random_branch_name
|
9
|
+
create_and_checkout_branch(new_branch_name)
|
10
|
+
|
11
|
+
output = capture_stdout { Toolshed::Commands::CheckoutBranch.new.execute({}, { branch_name: current_branch }) }
|
12
|
+
|
13
|
+
assert_match /Switched to 'master'/, output
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_checkout_branch_prompt
|
17
|
+
current_branch = Toolshed::Git::Base.branch_name
|
18
|
+
|
19
|
+
new_branch_name = random_branch_name
|
20
|
+
create_and_checkout_branch(new_branch_name)
|
21
|
+
|
22
|
+
# stub the possible input
|
23
|
+
Toolshed::Commands::CheckoutBranch.any_instance.stubs(:read_user_input).returns(current_branch)
|
24
|
+
|
25
|
+
output = capture_stdout { Toolshed::Commands::CheckoutBranch.new.execute({}) }
|
26
|
+
assert_match /Switched to 'master'/, output
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'commands/commands_helper'
|
2
|
+
require 'toolshed/commands/create_branch'
|
3
|
+
|
4
|
+
class CreateBranchTest < Test::Unit::TestCase
|
5
|
+
def test_create_new_branch_passing_in_branch_name_branch_from
|
6
|
+
Toolshed::Client.pull_from_remote_name = 'origin'
|
7
|
+
Toolshed::Client.push_to_remote_name = 'origin'
|
8
|
+
|
9
|
+
current_branch = Toolshed::Git::Base.branch_name
|
10
|
+
new_branch_name = ::Faker::Lorem.word.downcase
|
11
|
+
|
12
|
+
output = capture_stdout { Toolshed::Commands::CreateBranch.new.execute({}, { branch_name: new_branch_name, branch_from: 'development' }) }
|
13
|
+
|
14
|
+
assert_match /Branch #{new_branch_name} has been created/, output
|
15
|
+
|
16
|
+
assert_equal new_branch_name, Toolshed::Git::Base.branch_name
|
17
|
+
assert_equal 'development', Toolshed::Git::Base.branched_from
|
18
|
+
|
19
|
+
Toolshed::Git::Base.checkout(current_branch)
|
20
|
+
delete_branch(new_branch_name)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_create_new_branch_not_passing_in_branch_name_or_branch_from
|
24
|
+
Toolshed::Client.pull_from_remote_name = 'origin'
|
25
|
+
Toolshed::Client.push_to_remote_name = 'origin'
|
26
|
+
|
27
|
+
current_branch = Toolshed::Git::Base.branch_name
|
28
|
+
new_branch_name = ::Faker::Lorem.word.downcase
|
29
|
+
|
30
|
+
# stub the possible input
|
31
|
+
Toolshed::Commands::CreateBranch.any_instance.stubs(:read_user_input_branch_name).returns(new_branch_name)
|
32
|
+
Toolshed::Commands::CreateBranch.any_instance.stubs(:read_user_input_branch_from).returns('development')
|
33
|
+
|
34
|
+
output = capture_stdout { Toolshed::Commands::CreateBranch.new.execute({}) }
|
35
|
+
|
36
|
+
assert_match /Branch #{new_branch_name} has been created/, output
|
37
|
+
|
38
|
+
assert_equal new_branch_name, Toolshed::Git::Base.branch_name
|
39
|
+
assert_equal 'development', Toolshed::Git::Base.branched_from
|
40
|
+
|
41
|
+
Toolshed::Git::Base.checkout(current_branch)
|
42
|
+
delete_branch(new_branch_name)
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
require 'commands/commands_helper'
|
2
|
+
require 'toolshed/commands/create_pull_request'
|
3
|
+
|
4
|
+
class CreatePullRequestTest < Test::Unit::TestCase
|
5
|
+
def test_create_github_pull_request_no_ticket_tracking
|
6
|
+
Toolshed::Client.ticket_tracking_tool = ''
|
7
|
+
Toolshed::Client.git_tool = 'github'
|
8
|
+
Toolshed::Client.github_username = 'sample'
|
9
|
+
Toolshed::Client.github_password = 'sample'
|
10
|
+
|
11
|
+
Toolshed::Client.pull_from_repository_user = 'sample'
|
12
|
+
Toolshed::Client.pull_from_repository_name = 'sample_repo'
|
13
|
+
|
14
|
+
# mock up the pull request
|
15
|
+
expected_result = {
|
16
|
+
"html_url" => "github.com/pulls/1",
|
17
|
+
}.to_json
|
18
|
+
http_mock = mock('Net::HTTPResponse')
|
19
|
+
http_mock.stubs(:code => '200', :message => "OK", :content_type => "text/html", :body => expected_result)
|
20
|
+
|
21
|
+
http_party_mock = mock('HTTParty')
|
22
|
+
http_party_mock.stubs(:response => http_mock)
|
23
|
+
|
24
|
+
# create new github object
|
25
|
+
github = Toolshed::Git::Github.new
|
26
|
+
|
27
|
+
github_default_options = github.default_options
|
28
|
+
github_default_options.merge!({
|
29
|
+
body: {
|
30
|
+
title: 'Sample',
|
31
|
+
body: 'Sample Body',
|
32
|
+
head: "#{Toolshed::Client.github_username}:#{Toolshed::Git::Base.branch_name}",
|
33
|
+
base: Toolshed::Git::Base.branched_from
|
34
|
+
}.to_json
|
35
|
+
})
|
36
|
+
|
37
|
+
HTTParty.
|
38
|
+
expects(:post).
|
39
|
+
with("#{Toolshed::Client::GITHUB_BASE_API_URL}repos/#{Toolshed::Client.pull_from_repository_user}/#{Toolshed::Client.pull_from_repository_name}/pulls", github_default_options).
|
40
|
+
returns(http_party_mock)
|
41
|
+
|
42
|
+
# stub the possible input
|
43
|
+
Toolshed::Commands::CreatePullRequest.any_instance.stubs(:read_user_input_add_note_to_ticket).returns(false)
|
44
|
+
|
45
|
+
output = capture_stdout { Toolshed::Commands::CreatePullRequest.new.execute({}, { title: 'Sample', body: 'Sample Body' }) }
|
46
|
+
assert_match /Created Pull Request: github.com\/pulls\/1/, output
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_create_github_pull_request_with_pivotal_tracker
|
50
|
+
Toolshed::Client.ticket_tracking_tool = 'pivotal_tracker'
|
51
|
+
Toolshed::Client.git_tool = 'github'
|
52
|
+
Toolshed::Client.github_username = 'sample'
|
53
|
+
Toolshed::Client.github_password = 'sample'
|
54
|
+
Toolshed::Client.pivotal_tracker_username = 'ptusername'
|
55
|
+
Toolshed::Client.pivotal_tracker_password = 'ptpassword'
|
56
|
+
Toolshed::Client.default_pivotal_tracker_project_id = '1234'
|
57
|
+
Toolshed::Client.pull_from_repository_user = 'sample'
|
58
|
+
Toolshed::Client.pull_from_repository_name = 'sample_repo'
|
59
|
+
Toolshed::Client.use_defaults = true
|
60
|
+
|
61
|
+
# mock up the pull request
|
62
|
+
expected_result = {
|
63
|
+
"html_url" => "github.com/pulls/1",
|
64
|
+
}.to_json
|
65
|
+
http_mock = mock('Net::HTTPResponse')
|
66
|
+
http_mock.stubs(:code => '200', :message => "OK", :content_type => "text/html", :body => expected_result)
|
67
|
+
|
68
|
+
http_party_mock = mock('HTTParty')
|
69
|
+
http_party_mock.stubs(:response => http_mock)
|
70
|
+
|
71
|
+
# create new github object
|
72
|
+
github = Toolshed::Git::Github.new
|
73
|
+
|
74
|
+
github_default_options = github.default_options
|
75
|
+
github_default_options.merge!({
|
76
|
+
body: {
|
77
|
+
title: 'Sample',
|
78
|
+
body: 'Sample Body',
|
79
|
+
head: "#{Toolshed::Client.github_username}:#{Toolshed::Git::Base.branch_name}",
|
80
|
+
base: Toolshed::Git::Base.branched_from
|
81
|
+
}.to_json
|
82
|
+
})
|
83
|
+
|
84
|
+
HTTParty.
|
85
|
+
expects(:post).
|
86
|
+
with("#{Toolshed::Client::GITHUB_BASE_API_URL}repos/#{Toolshed::Client.pull_from_repository_user}/#{Toolshed::Client.pull_from_repository_name}/pulls", github_default_options).
|
87
|
+
returns(http_party_mock)
|
88
|
+
|
89
|
+
|
90
|
+
# mock up the pivotal_tracker stuff
|
91
|
+
PivotalTracker::Client.expects(:token).
|
92
|
+
with(Toolshed::TicketTracking::PivotalTracker.username, Toolshed::TicketTracking::PivotalTracker.password).
|
93
|
+
returns('3454354token')
|
94
|
+
|
95
|
+
# mock up the project response
|
96
|
+
pivotal_tracker_mock = mock('PivotalTracker::Client')
|
97
|
+
pivotal_tracker_mock.stubs(:id => '1')
|
98
|
+
|
99
|
+
PivotalTracker::Project.expects(:find).
|
100
|
+
with(Toolshed::Client.default_pivotal_tracker_project_id).
|
101
|
+
returns(pivotal_tracker_mock)
|
102
|
+
|
103
|
+
# mock up the story information
|
104
|
+
pivotal_tracker_story_mock = mock('PivotalTracker::Story')
|
105
|
+
pivotal_tracker_story_mock.stubs(:url => 'http://www.example.com', :id => '1', :name => "Test Title")
|
106
|
+
|
107
|
+
Toolshed::TicketTracking::PivotalTracker.any_instance.expects(:story_information).
|
108
|
+
with('1').
|
109
|
+
returns(pivotal_tracker_story_mock)
|
110
|
+
|
111
|
+
# stub the possible input
|
112
|
+
Toolshed::Commands::CreatePullRequest.any_instance.stubs(:read_user_input_ticket_tracker_ticket_id).returns('1')
|
113
|
+
Toolshed::Commands::CreatePullRequest.any_instance.stubs(:read_user_input_add_note_to_ticket).returns(false)
|
114
|
+
|
115
|
+
output = capture_stdout { Toolshed::Commands::CreatePullRequest.new.execute({}, { title: 'Sample', body: 'Sample Body' }) }
|
116
|
+
assert_match /Created Pull Request: github.com\/pulls\/1/, output
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_create_github_pull_request_with_invalid_ticket_tracker
|
120
|
+
Toolshed::Client.ticket_tracking_tool = 'unfuddle'
|
121
|
+
Toolshed::Client.git_tool = 'github'
|
122
|
+
output = capture_stdout { Toolshed::Commands::CreatePullRequest.new.execute({}, { title: 'Sample', body: 'Sample Body' }) }
|
123
|
+
assert_match /Ticket tracking tool is not supported at this time/, output
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'commands/commands_helper'
|
2
|
+
require 'toolshed/commands/delete_branch'
|
3
|
+
|
4
|
+
class DeleteBranchTest < Test::Unit::TestCase
|
5
|
+
def test_delete_branch_with_branch_name_passed
|
6
|
+
current_branch = Toolshed::Git::Base.branch_name
|
7
|
+
|
8
|
+
new_branch_name = random_branch_name
|
9
|
+
create_and_checkout_branch(new_branch_name, 'master')
|
10
|
+
|
11
|
+
# go to the remote repo and verify it exists
|
12
|
+
Dir.chdir(File.join(TEST_ROOT, "remote"))
|
13
|
+
remote_current_branch = Toolshed::Git::Base.branch_name
|
14
|
+
Toolshed::Git::Base.checkout(new_branch_name)
|
15
|
+
assert_equal new_branch_name, Toolshed::Git::Base.branch_name
|
16
|
+
Toolshed::Git::Base.checkout(remote_current_branch)
|
17
|
+
|
18
|
+
Dir.chdir(File.join(TEST_ROOT, "local"))
|
19
|
+
Toolshed::Git::Base.checkout(current_branch)
|
20
|
+
|
21
|
+
output = capture_stdout { Toolshed::Commands::DeleteBranch.new.execute({}, { branch_name: new_branch_name }) }
|
22
|
+
assert_match /#{new_branch_name} has been deleted/, output
|
23
|
+
|
24
|
+
branch_found = `git branch | grep #{new_branch_name}`
|
25
|
+
assert_equal '', branch_found
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_delete_branch_with_ticket_id_only_passed
|
29
|
+
current_branch = Toolshed::Git::Base.branch_name
|
30
|
+
|
31
|
+
new_branch_name = "1234333_#{random_branch_name}"
|
32
|
+
create_and_checkout_branch(new_branch_name, 'master')
|
33
|
+
|
34
|
+
# go to the remote repo and verify it exists
|
35
|
+
Dir.chdir(File.join(TEST_ROOT, "remote"))
|
36
|
+
remote_current_branch = Toolshed::Git::Base.branch_name
|
37
|
+
Toolshed::Git::Base.checkout(new_branch_name)
|
38
|
+
assert_equal new_branch_name, Toolshed::Git::Base.branch_name
|
39
|
+
Toolshed::Git::Base.checkout(remote_current_branch)
|
40
|
+
|
41
|
+
Dir.chdir(File.join(TEST_ROOT, "local"))
|
42
|
+
Toolshed::Git::Base.checkout(current_branch)
|
43
|
+
|
44
|
+
output = capture_stdout { Toolshed::Commands::DeleteBranch.new.execute({}, { branch_name: '1234333' }) }
|
45
|
+
assert_match /#{new_branch_name} has been deleted/, output
|
46
|
+
|
47
|
+
branch_found = `git branch | grep #{new_branch_name}`
|
48
|
+
assert_equal '', branch_found
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_delete_branch_without_branch_name_passed
|
52
|
+
current_branch = Toolshed::Git::Base.branch_name
|
53
|
+
|
54
|
+
new_branch_name = random_branch_name
|
55
|
+
create_and_checkout_branch(new_branch_name, 'master')
|
56
|
+
|
57
|
+
# go to the remote repo and verify it exists
|
58
|
+
Dir.chdir(File.join(TEST_ROOT, "remote"))
|
59
|
+
remote_current_branch = Toolshed::Git::Base.branch_name
|
60
|
+
Toolshed::Git::Base.checkout(new_branch_name)
|
61
|
+
assert_equal new_branch_name, Toolshed::Git::Base.branch_name
|
62
|
+
Toolshed::Git::Base.checkout(remote_current_branch)
|
63
|
+
|
64
|
+
Dir.chdir(File.join(TEST_ROOT, "local"))
|
65
|
+
Toolshed::Git::Base.checkout(current_branch)
|
66
|
+
|
67
|
+
Toolshed::Commands::DeleteBranch.any_instance.stubs(:read_user_input).returns(new_branch_name)
|
68
|
+
|
69
|
+
output = capture_stdout { Toolshed::Commands::DeleteBranch.new.execute({}) }
|
70
|
+
assert_match /#{new_branch_name} has been deleted/, output
|
71
|
+
|
72
|
+
branch_found = `git branch | grep #{new_branch_name}`
|
73
|
+
assert_equal '', branch_found
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'commands/commands_helper'
|
2
|
+
require 'toolshed/commands/push_branch'
|
3
|
+
|
4
|
+
class PushBranchTest < Test::Unit::TestCase
|
5
|
+
def test_push_branch_current_working_branch
|
6
|
+
Toolshed::Client.push_to_remote_name = 'origin'
|
7
|
+
|
8
|
+
current_branch = Toolshed::Git::Base.branch_name
|
9
|
+
|
10
|
+
new_branch_name = random_branch_name
|
11
|
+
create_and_checkout_branch(new_branch_name, 'master')
|
12
|
+
|
13
|
+
output = capture_stdout { Toolshed::Commands::PushBranch.new.execute({}) }
|
14
|
+
assert_match /#{new_branch_name} has been pushed/, output
|
15
|
+
|
16
|
+
Toolshed::Git::Base.checkout(current_branch)
|
17
|
+
delete_branch(new_branch_name)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_push_branch_by_ticket_id
|
21
|
+
Toolshed::Client.push_to_remote_name = 'origin'
|
22
|
+
|
23
|
+
current_branch = Toolshed::Git::Base.branch_name
|
24
|
+
|
25
|
+
new_branch_name = "555558_#{random_branch_name}"
|
26
|
+
create_and_checkout_branch(new_branch_name, 'master')
|
27
|
+
|
28
|
+
output = capture_stdout { Toolshed::Commands::PushBranch.new.execute({}, { branch_name: '555558' }) }
|
29
|
+
assert_match /#{new_branch_name} has been pushed/, output
|
30
|
+
|
31
|
+
Toolshed::Git::Base.checkout(current_branch)
|
32
|
+
delete_branch(new_branch_name)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_push_branch_current_working_branch_with_force
|
36
|
+
Toolshed::Client.push_to_remote_name = 'origin'
|
37
|
+
|
38
|
+
current_branch = Toolshed::Git::Base.branch_name
|
39
|
+
|
40
|
+
new_branch_name = random_branch_name
|
41
|
+
create_and_checkout_branch(new_branch_name, 'master')
|
42
|
+
|
43
|
+
output = capture_stdout { Toolshed::Commands::PushBranch.new.execute({}, { force: true }) }
|
44
|
+
assert_match /#{new_branch_name} has been pushed/, output
|
45
|
+
|
46
|
+
Toolshed::Git::Base.checkout(current_branch)
|
47
|
+
delete_branch(new_branch_name)
|
48
|
+
end
|
49
|
+
end
|
data/test/config.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
TEST_ROOT = File.expand_path(File.dirname(__FILE__))
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
def create_and_checkout_branch(name, branch_from='master')
|
4
|
+
save_stash
|
5
|
+
|
6
|
+
until system("git checkout -b #{name} origin/#{branch_from} #{Toolshed::Client.git_quiet}")
|
7
|
+
sleep 1
|
8
|
+
end
|
9
|
+
|
10
|
+
until system("git push origin #{name} #{Toolshed::Client.git_quiet}")
|
11
|
+
sleep 1
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def pop_stash
|
16
|
+
system("git stash pop #{Toolshed::Client.git_quiet}")
|
17
|
+
end
|
18
|
+
|
19
|
+
def save_stash
|
20
|
+
system("git stash save #{Toolshed::Client.git_quiet}")
|
21
|
+
end
|
22
|
+
|
23
|
+
def delete_branch(branch_name)
|
24
|
+
until system("git branch -D #{branch_name} --quiet")
|
25
|
+
sleep 1
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def random_branch_name
|
30
|
+
"#{::Faker::Lorem.word.downcase}_#{Time.now.to_i}"
|
31
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require 'git/git_helper'
|
2
|
+
|
3
|
+
class GitTest < Test::Unit::TestCase
|
4
|
+
def test_get_branch_name
|
5
|
+
current_branch = Toolshed::Git::Base.branch_name
|
6
|
+
|
7
|
+
new_branch_name = random_branch_name
|
8
|
+
create_and_checkout_branch(new_branch_name)
|
9
|
+
|
10
|
+
assert_equal new_branch_name, Toolshed::Git::Base.branch_name
|
11
|
+
|
12
|
+
Toolshed::Git::Base.checkout(current_branch)
|
13
|
+
pop_stash
|
14
|
+
delete_branch(new_branch_name)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_checkout_branch
|
18
|
+
current_branch = Toolshed::Git::Base.branch_name
|
19
|
+
|
20
|
+
new_branch_name = random_branch_name
|
21
|
+
create_and_checkout_branch(new_branch_name)
|
22
|
+
|
23
|
+
Toolshed::Git::Base.checkout(current_branch)
|
24
|
+
|
25
|
+
assert_equal current_branch, Toolshed::Git::Base.branch_name
|
26
|
+
|
27
|
+
delete_branch(new_branch_name)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_checkout_branch_by_id
|
31
|
+
current_branch = Toolshed::Git::Base.branch_name
|
32
|
+
|
33
|
+
branch_id = "124233"
|
34
|
+
|
35
|
+
new_branch_name = random_branch_name
|
36
|
+
new_branch_name = "#{branch_id}_#{new_branch_name}"
|
37
|
+
create_and_checkout_branch(new_branch_name)
|
38
|
+
|
39
|
+
Toolshed::Git::Base.checkout(current_branch)
|
40
|
+
assert_equal current_branch, Toolshed::Git::Base.branch_name
|
41
|
+
|
42
|
+
Toolshed::Git::Base.checkout(branch_id)
|
43
|
+
assert_equal new_branch_name, Toolshed::Git::Base.branch_name
|
44
|
+
|
45
|
+
Toolshed::Git::Base.checkout(current_branch)
|
46
|
+
delete_branch(new_branch_name)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_branched_from
|
50
|
+
current_branch = Toolshed::Git::Base.branch_name
|
51
|
+
current_branch_branched_from = Toolshed::Git::Base.branched_from
|
52
|
+
|
53
|
+
new_branch_name = random_branch_name
|
54
|
+
create_and_checkout_branch(new_branch_name, 'development')
|
55
|
+
|
56
|
+
assert_equal 'development', Toolshed::Git::Base.branched_from
|
57
|
+
|
58
|
+
Toolshed::Git::Base.checkout(current_branch)
|
59
|
+
delete_branch(new_branch_name)
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_create_new_branch
|
63
|
+
current_branch = Toolshed::Git::Base.branch_name
|
64
|
+
|
65
|
+
new_branch_name = random_branch_name
|
66
|
+
git = Toolshed::Git::Base.new({
|
67
|
+
from_remote_branch_name: 'development',
|
68
|
+
to_remote_branch_name: new_branch_name,
|
69
|
+
from_remote_name: 'origin',
|
70
|
+
to_remote_name: 'origin',
|
71
|
+
})
|
72
|
+
git.create_branch
|
73
|
+
|
74
|
+
assert_equal new_branch_name, Toolshed::Git::Base.branch_name
|
75
|
+
assert_equal 'development', Toolshed::Git::Base.branched_from
|
76
|
+
|
77
|
+
Toolshed::Git::Base.checkout(current_branch)
|
78
|
+
delete_branch(new_branch_name)
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_create_branch_from_remote_name_empty
|
82
|
+
new_branch_name = random_branch_name
|
83
|
+
git = Toolshed::Git::Base.new({
|
84
|
+
from_remote_branch_name: '',
|
85
|
+
to_remote_branch_name: new_branch_name,
|
86
|
+
from_remote_name: 'origin',
|
87
|
+
to_remote_name: 'origin',
|
88
|
+
})
|
89
|
+
|
90
|
+
exception = assert_raise(Veto::InvalidEntity) { git.create_branch }
|
91
|
+
assert_equal 'from_remote_branch_name is not present', exception.message.first
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_delete_branch
|
95
|
+
current_branch = Toolshed::Git::Base.branch_name
|
96
|
+
|
97
|
+
new_branch_name = random_branch_name
|
98
|
+
create_and_checkout_branch(new_branch_name, 'master')
|
99
|
+
|
100
|
+
# go to the remote repo and verify it exists
|
101
|
+
Dir.chdir(File.join(TEST_ROOT, "remote"))
|
102
|
+
remote_current_branch = Toolshed::Git::Base.branch_name
|
103
|
+
Toolshed::Git::Base.checkout(new_branch_name)
|
104
|
+
assert_equal new_branch_name, Toolshed::Git::Base.branch_name
|
105
|
+
Toolshed::Git::Base.checkout(remote_current_branch)
|
106
|
+
|
107
|
+
Dir.chdir(File.join(TEST_ROOT, "local"))
|
108
|
+
Toolshed::Git::Base.checkout(current_branch)
|
109
|
+
|
110
|
+
Toolshed::Git::Base.delete(new_branch_name)
|
111
|
+
|
112
|
+
branch_found = `git branch | grep #{new_branch_name}`
|
113
|
+
assert_equal '', branch_found
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'git/git_helper'
|
2
|
+
|
3
|
+
class GitHubTest < Test::Unit::TestCase
|
4
|
+
def test_list_branches
|
5
|
+
Toolshed::Client.github_username = 'sample'
|
6
|
+
Toolshed::Client.pull_from_repository_name = 'sample'
|
7
|
+
|
8
|
+
expected_result = [
|
9
|
+
{
|
10
|
+
"name" => "master",
|
11
|
+
"commit" => {
|
12
|
+
"sha" => "6dcb09b5b57875f334f61aebed695e2e4193db5e",
|
13
|
+
"url" => "https://api.github.com/repos/octocat/Hello-World/commits/c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc"
|
14
|
+
}
|
15
|
+
},
|
16
|
+
{
|
17
|
+
"name" => "develop",
|
18
|
+
"commit" => {
|
19
|
+
"sha" => "6dcb09b5b57875f334f61aebed695e2e4193db5e",
|
20
|
+
"url" => "https://api.github.com/repos/octocat/Hello-World/commits/345lj5ae6c19d5c5df71a34c7fbeeda2479ccbc"
|
21
|
+
}
|
22
|
+
}
|
23
|
+
].to_json
|
24
|
+
http_mock = mock('Net::HTTPResponse')
|
25
|
+
http_mock.stubs(:code => '200', :message => "OK", :content_type => "text/html", :body => expected_result)
|
26
|
+
|
27
|
+
http_party_mock = mock('HTTParty')
|
28
|
+
http_party_mock.stubs(:response => http_mock)
|
29
|
+
|
30
|
+
# create new github object
|
31
|
+
github = Toolshed::Git::Github.new
|
32
|
+
|
33
|
+
HTTParty.
|
34
|
+
expects(:get).
|
35
|
+
with("#{Toolshed::Client::GITHUB_BASE_API_URL}repos/#{Toolshed::Client.github_username}/#{Toolshed::Client.pull_from_repository_name}/branches", github.default_options).
|
36
|
+
returns(http_party_mock)
|
37
|
+
|
38
|
+
assert_equal JSON.parse(expected_result), github.list_branches
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_create_pull_request
|
42
|
+
current_branch = Toolshed::Git::Base.branch_name
|
43
|
+
new_branch_name = '1234_testing'
|
44
|
+
|
45
|
+
Toolshed::Client.pull_from_repository_user = 'sample'
|
46
|
+
Toolshed::Client.pull_from_repository_name = 'sample_repo'
|
47
|
+
create_and_checkout_branch(new_branch_name)
|
48
|
+
|
49
|
+
expected_result = {
|
50
|
+
"html_url" => "github.com/pulls/1",
|
51
|
+
}.to_json
|
52
|
+
http_mock = mock('Net::HTTPResponse')
|
53
|
+
http_mock.stubs(:code => '200', :message => "OK", :content_type => "text/html", :body => expected_result)
|
54
|
+
|
55
|
+
http_party_mock = mock('HTTParty')
|
56
|
+
http_party_mock.stubs(:response => http_mock)
|
57
|
+
|
58
|
+
# create new github object
|
59
|
+
github = Toolshed::Git::Github.new
|
60
|
+
|
61
|
+
github_default_options = github.default_options
|
62
|
+
github_default_options.merge!({
|
63
|
+
body: {
|
64
|
+
title: 'Sample',
|
65
|
+
body: 'Sample Body',
|
66
|
+
head: "#{Toolshed::Client.github_username}:#{Toolshed::Git::Base.branch_name}",
|
67
|
+
base: Toolshed::Git::Base.branched_from
|
68
|
+
}.to_json
|
69
|
+
})
|
70
|
+
|
71
|
+
HTTParty.
|
72
|
+
expects(:post).
|
73
|
+
with("#{Toolshed::Client::GITHUB_BASE_API_URL}repos/#{Toolshed::Client.pull_from_repository_user}/#{Toolshed::Client.pull_from_repository_name}/pulls", github_default_options).
|
74
|
+
returns(http_party_mock)
|
75
|
+
|
76
|
+
assert_equal JSON.parse(expected_result), github.create_pull_request('Sample', 'Sample Body')
|
77
|
+
|
78
|
+
Toolshed::Git::Base.checkout(current_branch)
|
79
|
+
delete_branch(new_branch_name)
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_get_username
|
83
|
+
Toolshed::Client.github_username = 'tester'
|
84
|
+
assert_equal 'tester', Toolshed::Git::Github.username
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_get_password
|
88
|
+
Toolshed::Client.github_password = 'tester1234'
|
89
|
+
assert_equal 'tester1234', Toolshed::Git::Github.password
|
90
|
+
end
|
91
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'config'
|
2
|
+
require 'simplecov'
|
3
|
+
|
4
|
+
if (ENV["COVERAGE"])
|
5
|
+
SimpleCov.start do
|
6
|
+
add_filter 'test/'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'test/unit'
|
11
|
+
require 'mocha/test_unit'
|
12
|
+
require 'faker'
|
13
|
+
require 'veto'
|
14
|
+
require 'fileutils'
|
15
|
+
require 'json'
|
16
|
+
|
17
|
+
require 'toolshed'
|
18
|
+
|
19
|
+
Test::Unit.at_start do
|
20
|
+
Toolshed::Client.use_git_submodules = false
|
21
|
+
#Toolshed::Client.git_quiet = '&> /dev/null' unless ENV['RUNNING_ON_CI']
|
22
|
+
|
23
|
+
I18n.config.enforce_available_locales = true
|
24
|
+
|
25
|
+
FileUtils.rm_rf(File.join(TEST_ROOT, "remote"))
|
26
|
+
FileUtils.rm_rf(File.join(TEST_ROOT, "local"))
|
27
|
+
|
28
|
+
# setup a fake remote directory so we can reference everything locally
|
29
|
+
if (Dir.exists? (File.join(TEST_ROOT, "remote")))
|
30
|
+
Dir.rmdir(File.join(TEST_ROOT, "remote"))
|
31
|
+
end
|
32
|
+
|
33
|
+
Dir.mkdir(File.join(TEST_ROOT, "remote"), 0777)
|
34
|
+
Dir.chdir(File.join(TEST_ROOT, "remote"))
|
35
|
+
|
36
|
+
# setup a couple of branches acting as the remote repository
|
37
|
+
until system("git init #{Toolshed::Client.git_quiet}")
|
38
|
+
sleep 1
|
39
|
+
end
|
40
|
+
|
41
|
+
FileUtils.touch('file.txt')
|
42
|
+
|
43
|
+
until system("git add file.txt #{Toolshed::Client.git_quiet}")
|
44
|
+
sleep 1
|
45
|
+
end
|
46
|
+
|
47
|
+
until system("git commit -m 'Add empty file as commit' #{Toolshed::Client.git_quiet}")
|
48
|
+
sleep 1
|
49
|
+
end
|
50
|
+
|
51
|
+
until system("git checkout -b development master #{Toolshed::Client.git_quiet}")
|
52
|
+
sleep 1
|
53
|
+
end
|
54
|
+
|
55
|
+
until system("git checkout master #{Toolshed::Client.git_quiet}")
|
56
|
+
sleep 1
|
57
|
+
end
|
58
|
+
|
59
|
+
if (Dir.exists? (File.join(TEST_ROOT, "local")))
|
60
|
+
Dir.rmdir(File.join(TEST_ROOT, "local"))
|
61
|
+
end
|
62
|
+
|
63
|
+
Dir.mkdir(File.join(TEST_ROOT, "local"), 0777)
|
64
|
+
Dir.chdir(File.join(TEST_ROOT, "local"))
|
65
|
+
|
66
|
+
# setup the new repository with an empty set this is configured in the config.rb file
|
67
|
+
until system("git init #{Toolshed::Client.git_quiet}")
|
68
|
+
sleep 1
|
69
|
+
end
|
70
|
+
|
71
|
+
until system("git remote add origin #{File.join(TEST_ROOT, "remote")} #{Toolshed::Client.git_quiet}")
|
72
|
+
sleep 1
|
73
|
+
end
|
74
|
+
|
75
|
+
until system("git remote update #{Toolshed::Client.git_quiet}")
|
76
|
+
sleep 1
|
77
|
+
end
|
78
|
+
|
79
|
+
until system("git checkout -b master origin/master #{Toolshed::Client.git_quiet}")
|
80
|
+
sleep 1
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
Test::Unit.at_exit do
|
85
|
+
FileUtils.rm_rf(File.join(TEST_ROOT, "remote"))
|
86
|
+
FileUtils.rm_rf(File.join(TEST_ROOT, "local"))
|
87
|
+
end
|
88
|
+
|
89
|
+
def capture_stdout(&block)
|
90
|
+
original_stdout = $stdout
|
91
|
+
$stdout = fake = StringIO.new
|
92
|
+
begin
|
93
|
+
yield
|
94
|
+
ensure
|
95
|
+
$stdout = original_stdout
|
96
|
+
end
|
97
|
+
fake.string
|
98
|
+
end
|