add tests

This commit is contained in:
Petr Korolev 2015-05-25 13:34:37 +03:00
parent c3b9455dfd
commit 757f6d40b4
2 changed files with 40 additions and 5 deletions

View File

@ -139,7 +139,8 @@ module GitHubChangelogGenerator
options[:user] = "skywinder" options[:user] = "skywinder"
options[:project] = "changelog_test" options[:project] = "changelog_test"
else else
options[:user], options[:project] = user_project_from_remote(options[:git_remote]) remote = `git config --get remote.#{options[:git_remote]}.url`
options[:user], options[:project] = user_project_from_remote(remote)
end end
end end
end end
@ -165,10 +166,11 @@ module GitHubChangelogGenerator
end end
end end
# Try to find user and project name from git remote # Try to find user and project name from git remote output
# #
# @return [Tuple] user and project # @param [String] output of git remote command
def self.user_project_from_remote(git_remote) # @return [Arrajy] user and project
def self.user_project_from_remote(remote)
# try to find repo in format: # try to find repo in format:
# origin git@github.com:skywinder/Github-Changelog-Generator.git (fetch) # origin git@github.com:skywinder/Github-Changelog-Generator.git (fetch)
# git@github.com:skywinder/Github-Changelog-Generator.git # git@github.com:skywinder/Github-Changelog-Generator.git
@ -181,7 +183,6 @@ module GitHubChangelogGenerator
remote_structures = [regex1, regex2] remote_structures = [regex1, regex2]
remote = `git config --get remote.#{git_remote}.url`
user = nil user = nil
project = nil project = nil
remote_structures.each do |regex| remote_structures.each do |regex|
@ -199,4 +200,9 @@ module GitHubChangelogGenerator
[user, project] [user, project]
end end
end end
if __FILE__ == $PROGRAM_NAME
remote = "origin https://github.com/skywinder/Changelog.Merger (fetch)"
p GitHubChangelogGenerator::Parser.user_project_from_remote(remote)[0]
end
end end

29
spec/unit/parser_spec.rb Normal file
View File

@ -0,0 +1,29 @@
describe GitHubChangelogGenerator::Parser do
describe "#self.user_project_from_remote" do
context "when remote is 1" do
subject { GitHubChangelogGenerator::Parser.user_project_from_remote("origin https://github.com/skywinder/ActionSheetPicker-3.0 (fetch)") }
it { is_expected.to be_a(Array) }
it { is_expected.to match_array(["skywinder", "ActionSheetPicker-3.0"]) }
end
context "when remote is 2" do
subject { GitHubChangelogGenerator::Parser.user_project_from_remote("https://github.com/skywinder/ActionSheetPicker-3.0") }
it { is_expected.to be_a(Array) }
it { is_expected.to match_array(["skywinder", "ActionSheetPicker-3.0"]) }
end
context "when remote is 3" do
subject { GitHubChangelogGenerator::Parser.user_project_from_remote("https://github.com/skywinder/ActionSheetPicker-3.0") }
it { is_expected.to be_a(Array) }
it { is_expected.to match_array(["skywinder", "ActionSheetPicker-3.0"]) }
end
context "when remote is 4" do
subject { GitHubChangelogGenerator::Parser.user_project_from_remote("origin git@github.com:skywinder/ActionSheetPicker-3.0.git (fetch)") }
it { is_expected.to be_a(Array) }
it { is_expected.to match_array(["skywinder", "ActionSheetPicker-3.0"]) }
end
context "when remote is invalid" do
subject { GitHubChangelogGenerator::Parser.user_project_from_remote("some invalid text") }
it { is_expected.to be_a(Array) }
it { is_expected.to match_array([nil, nil]) }
end
end
end