diff --git a/.codeconfig b/.codeconfig new file mode 100644 index 0000000..985d671 --- /dev/null +++ b/.codeconfig @@ -0,0 +1,3 @@ +development_branch = "development" +master_branch = "master" + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..496ee2c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index ec60a07..23a0abe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,3 @@ language: ruby rvm: - - 2.1.1 \ No newline at end of file + - 2.2.2 diff --git a/Gemfile b/Gemfile index a80c363..df3b46e 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ source 'https://rubygems.org' -ruby '2.1.1' +ruby '2.2.2' gemspec +gem "octokit" +gem "parseconfig" diff --git a/Gemfile.lock b/Gemfile.lock index 47ede46..5030bfa 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,39 +8,57 @@ PATH GEM remote: https://rubygems.org/ specs: - aruba (0.5.3) - childprocess (>= 0.3.6) - cucumber (>= 1.1.1) - rspec-expectations (>= 2.7.0) + addressable (2.3.8) + aruba (0.8.1) + childprocess (~> 0.5.6) + contracts (~> 0.9) + cucumber (>= 1.3.19) + rspec-expectations (>= 2.99) builder (3.2.2) - childprocess (0.3.9) + childprocess (0.5.6) ffi (~> 1.0, >= 1.0.11) - cucumber (1.3.8) + contracts (0.10.1) + cucumber (2.0.2) builder (>= 2.1.2) + cucumber-core (~> 1.2.0) diff-lcs (>= 1.1.3) - gherkin (~> 2.12.1) + gherkin (~> 2.12) multi_json (>= 1.7.5, < 2.0) - multi_test (>= 0.0.2) - diff-lcs (1.2.4) - ffi (1.9.0) - gherkin (2.12.1) + multi_test (>= 0.1.2) + cucumber-core (1.2.0) + gherkin (~> 2.12.0) + diff-lcs (1.2.5) + faraday (0.9.1) + multipart-post (>= 1.2, < 3) + ffi (1.9.10) + gherkin (2.12.2) multi_json (~> 1.3) gli (2.8.0) - hub (1.10.6) - json (1.8.0) - multi_json (1.8.0) - multi_test (0.0.2) - rake (10.1.0) - rdoc (4.0.1) - json (~> 1.4) - rspec (2.14.1) - rspec-core (~> 2.14.0) - rspec-expectations (~> 2.14.0) - rspec-mocks (~> 2.14.0) - rspec-core (2.14.5) - rspec-expectations (2.14.2) - diff-lcs (>= 1.1.3, < 2.0) - rspec-mocks (2.14.3) + hub (1.12.4) + multi_json (1.11.2) + multi_test (0.1.2) + multipart-post (2.0.0) + octokit (4.0.1) + sawyer (~> 0.6.0, >= 0.5.3) + parseconfig (1.0.6) + rake (10.4.2) + rdoc (4.2.0) + rspec (3.3.0) + rspec-core (~> 3.3.0) + rspec-expectations (~> 3.3.0) + rspec-mocks (~> 3.3.0) + rspec-core (3.3.2) + rspec-support (~> 3.3.0) + rspec-expectations (3.3.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.3.0) + rspec-mocks (3.3.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.3.0) + rspec-support (3.3.0) + sawyer (0.6.0) + addressable (~> 2.3.5) + faraday (~> 0.8, < 0.10) PLATFORMS ruby @@ -48,6 +66,11 @@ PLATFORMS DEPENDENCIES aruba code! + octokit + parseconfig rake rdoc rspec + +BUNDLED WITH + 1.10.6 diff --git a/bin/code b/bin/code index 0910d54..2f4ca30 100755 --- a/bin/code +++ b/bin/code @@ -13,6 +13,7 @@ rescue LoadError end require 'code/git' +require 'code/github_api' require 'code/branch' require 'code/file_list' require 'code/system' @@ -39,12 +40,20 @@ desc 'Upload your feature branch to GitHub' command :publish, :pr do |c| c.switch [:master], desc: 'Make the pull request based off the master branch' c.action do |global_options, options, args| + + $github_api.ensure_authorized + message = args.join(' ') base_branch = nil base_branch = Code::Branch.master if options[:master] + current_branch = Code::Branch.current - $git.publish(branch: Code::Branch.current, base: base_branch, message: message) + if (current_branch.has_pull_request?) + Code::System.open_in_browser(current_branch.pull_request_url) + else + $git.publish(base: base_branch, message: message) + end end end @@ -55,6 +64,14 @@ command :push, :p do |c| end end +desc "Marks the current branch's pr as awaiting review" +command :ready do |c| + c.action do |global_options, options, args| + $github_api.ensure_authorized + Code::Branch.current.mark_prs_as_ready + end +end + desc "Quickly create a pull request from your changes on development." command :hotfix, :fix do |c| c.action do |global_options, options, args| @@ -84,6 +101,13 @@ command :switch, :s do |c| end end +desc "Opens the repository search interface on github" +command :search do |c| + c.action do |global_options, options, args| + $git.search + end +end + desc 'Deploy to production' command :production do |c| c.action do |global_options, options, args| @@ -119,8 +143,17 @@ command :merged do |c| end end +desc 'Bundles up the current repo' +command :bundle do |c| + c.action do |global_options, options, args| + bundle_path = $git.bundle + Code::System.reveal(bundle_path) + end +end + pre do |global, command, options, args| $git = Code::Git.new + $github_api = Code::GitHubAPI.new true end diff --git a/lib/code/branch.rb b/lib/code/branch.rb index 8d13ab9..418ee1e 100644 --- a/lib/code/branch.rb +++ b/lib/code/branch.rb @@ -1,13 +1,13 @@ +require "code/pull_request" +require "code/config" + module Code class Branch ProtectedBranchError = Class.new(StandardError) PrivateBranchError = Class.new(StandardError) - - MASTER_BRANCH_NAME = 'master' - DEVELOPMENT_BRANCH_NAME = 'development' - PROTECTED_BRANCH_NAMES = %w{master development} + NoPRError = Class.new(StandardError) def self.all_names System.result('git branch').strip.lines.map { |line| line.gsub(/\s|\*/, '') } @@ -17,10 +17,15 @@ def self.all all_names.map { |name| new(name) } end + + # TODO: This should probably be moved to another class or module. + # An identical method is used in match_scorer/base def self.coerce_patterns(*patterns) patterns.flatten.join(' ').split(' ') end + private_class_method :coerce_patterns + def self.matching(*patterns) patterns = coerce_patterns(patterns) all.find { |b| b.matches? *patterns } @@ -44,11 +49,11 @@ def self.current end def self.development - new DEVELOPMENT_BRANCH_NAME + new Config.development_branch_name end def self.master - new MASTER_BRANCH_NAME + new Config.master_branch_name end def self.create(branch_name) @@ -77,15 +82,18 @@ def exists? end def development? - name == DEVELOPMENT_BRANCH_NAME + name == Config.development_branch_name end def master? - name == MASTER_BRANCH_NAME + name == Config.master_branch_name end def protected? - PROTECTED_BRANCH_NAMES.include? name + master_branch_name = Config.master_branch_name + development_branch_name = Config.development_branch_name + protected_branch_names = [ master_branch_name, development_branch_name ] + protected_branch_names.include? name end def feature? @@ -119,10 +127,6 @@ def checkout self end - def authorize_delete! - raise ProtectedBranchError, "The #{name} branch is protected" if protected? - end - def message name.gsub('-', ' ').capitalize end @@ -135,12 +139,51 @@ def hotfix? !! name.match(/^hotfix-/) end - def ensure_public! - raise PrivateBranchError, "#{name} is a private branch" if private? + def has_pull_request? + !pull_request.nil? + end + + def mark_prs_as_ready + label_prs(Config.ready_label) + end + + def mark_prs_as_hotfix + label_prs("hotfix") + end + + def pull_request_url + # This could be a delegate, but it seems easier to read just by + # calling it. + pull_request.url end attr_reader :name alias_method :to_s, :name + private + + def label_prs(label) + raise NoPRError, "There is no PR for the this branch" unless has_pull_request? + + pull_requests.each do |pr| + pr.add_label(label) + end + end + + def authorize_delete! + raise ProtectedBranchError, "The #{name} branch is protected" if protected? + end + + def pull_request + pull_requests.first + end + + def pull_requests + @pull_request ||= PullRequest.for_branch(self) + end + + def ensure_public! + raise PrivateBranchError, "#{name} is a private branch" if private? + end end end diff --git a/lib/code/config.rb b/lib/code/config.rb new file mode 100644 index 0000000..d28aabf --- /dev/null +++ b/lib/code/config.rb @@ -0,0 +1,66 @@ +require "parseconfig" + +module Code + + PROMPT_TEXTS = { + master_branch: "You didn't set the name of the master branch. What is it? (master)", + development_branch: "You didn't set the name of the development branch. What is it? (development)", + ready_label: "You didn't set the label used for marking the PR as ready. What is it? (awaiting review)" + } + + DEFAULTS = { + master_branch: "master", + development_branch: "development", + ready_label: "awaiting review" + } + + module Config + extend self + + def self.get(property) + ensure_file_exists + get_property_value(property) + end + + def self.master_branch_name + self.get(:master_branch) + end + + def self.development_branch_name + self.get(:development_branch) + end + + def self.ready_label + self.get(:ready_label) + end + + private + + def ensure_file_exists + FileUtils.touch ".codeconfig" unless File.exists? ".codeconfig" + end + + def get_property_value(property) + config = ParseConfig.new ".codeconfig" + + if config[property.to_s] + property_value = config[property.to_s] + else + property_value = System.prompt(PROMPT_TEXTS[property] || "Unknown") + property_value = DEFAULTS[property] if (!property_value || property_value.strip.empty?) + + store_property_to_config(property, property_value, config) + end + + property_value + end + + def store_property_to_config(property, property_value, config) + config.add(property, property_value) + + file = File.open ".codeconfig", "w" + config.write file + file.close + end + end +end diff --git a/lib/code/git.rb b/lib/code/git.rb index 7436a3d..3e8825d 100644 --- a/lib/code/git.rb +++ b/lib/code/git.rb @@ -10,9 +10,9 @@ class Git NotOnFeatureBranchError = Class.new(StandardError) UncommittedChangesError = Class.new(StandardError) FeatureExistsError = Class.new(StandardError) + BranchDoesntExistError = Class.new(StandardError) - def path - Dir.pwd + def initialize() end def start(feature) @@ -26,24 +26,6 @@ def start(feature) end end - def stash - System.call('stash') - end - - def unstash - System.call('stash pop') - end - - def with_stash - if uncommitted_changes? - stash - yield - unstash - else - yield - end - end - def hotfix(name) name = "hotfix-#{name}" raise FeatureExistsError, "Branch #{name} already exists" if Branch.exists?(name) @@ -57,7 +39,9 @@ def hotfix(name) end def switch(*patterns) - Branch.matching(*patterns).checkout + branch = Branch.matching(*patterns) + raise BranchDoesntExistError if !branch + branch.checkout end def cancel @@ -70,44 +54,100 @@ def cancel end end + def bundle + System.call "bundle create #{default_bundle_path} --all" + default_bundle_path + end + + # NOT used anywhere at the moment. Did we loose track of a supported command? def commit(message) System.call 'add -A' System.call "commit -m \"#{message}\"" end - def publish(branch:, base: nil, message: '') + def publish(base: nil, message: '') raise NotOnFeatureBranchError, 'Must be on a feature branch to publish your code' unless current_branch.feature? raise UncommittedChangesError, 'You have uncommitted changes' if uncommitted_changes? + push + create_prs_for(base, message) + end - base = infer_base_branch(branch) if base.nil? + def finish + branch = current_branch - branch.push + development_branch.checkout + fetch + development_branch.pull - System.open_in_browser pull_request(branch: branch, base: base, message: message) + branch.delete! end def push current_branch.push end - def pull_request(branch:, base:, message: '') - message = branch.message if message.empty? - command = "hub pull-request -f \"#{message}\" -b #{main_repo}:#{base} -h #{main_repo}:#{branch}" - System.exec(command).strip + def search + System.open_in_browser "https://github.com/#{current_repo_slug}/find/development" end + # NOT used anywhere at the moment. Did we loose track of a supported command? def compare_in_browser(branch) System.exec "hub compare #{branch}" end - def finish - branch = current_branch + def prune_remote_branches + System.call 'remote prune origin' + end - development_branch.checkout - fetch - development_branch.pull + private - branch.delete! + def default_bundle_path + parent_directory = File.expand_path('..', Dir.pwd) + file_name = System.current_directory_name + '.bundle' + File.join(parent_directory, file_name) + end + + def stash + System.call('stash') + end + + def unstash + System.call('stash pop') + end + + def with_stash + if uncommitted_changes? + stash + yield + unstash + else + yield + end + end + + def create_prs_for(base, message) + if current_branch.hotfix? + create_hotfix_prs(message) + else + create_feature_pr(base, message) + end + end + + def pull_request(base:, message: '') + base = development_branch unless base + message = current_branch.message if message.empty? + command = "hub pull-request -f \"#{message}\" -b #{main_repo}:#{base} -h #{main_repo}:#{current_branch}" + System.exec(command).strip + end + + def create_feature_pr(base, message) + System.open_in_browser pull_request(base: base, message: message) + end + + def create_hotfix_prs(message) + System.open_in_browser pull_request(base: master_branch, message: message) + System.open_in_browser pull_request(base: development_branch, message: message) + current_branch.mark_prs_as_hotfix end def uncommitted_changes? @@ -134,18 +174,6 @@ def master_branch Branch.master end - def infer_base_branch(branch) - if branch.hotfix? - master_branch - else - development_branch - end - end - - def prune_remote_branches - System.call 'remote prune origin' - end - def main_repo main_repo_url[/:(\w+)\//,1] end @@ -158,6 +186,10 @@ def repo_url(name) System.result("git ls-remote --get-url #{name}") end + def current_repo_slug + Repository.current.slug + end + end end diff --git a/lib/code/github_api.rb b/lib/code/github_api.rb new file mode 100644 index 0000000..db13fe9 --- /dev/null +++ b/lib/code/github_api.rb @@ -0,0 +1,87 @@ +require "code/repository" +require "code/system" + +require "octokit" +require "securerandom" + +module Code + + class GitHubAPI + + def initialize(repository: Repository.current) + @repository = repository + end + + def ensure_authorized + if not authorized? + username = System.prompt 'GitHub username' + password = System.prompt_hidden 'GitHub password' + + authorize(username: username, password: password) + end + end + + def pull_requests_for_branch(branch) + client = octokit_client_instance_from_token + client.pull_requests(current_repo_slug, head: "#{current_organization}:#{branch.name}") + end + + def label_pr(pull_request, label) + add_label_to_pr(pull_request.number, label) + end + + private + + def octokit_client_instance_from_token + client = Octokit::Client.new access_token: authorization_token + end + + def octokit_client_instance_from_basic_auth(username:, password:) + client = Octokit::Client.new login: username, password: password + end + + def create_token(client) + client.create_authorization(scopes: ["repo"], note: authorization_note)[:token] + end + + def current_organization + @repository.organization + end + + def current_repo_slug + @repository.slug + end + + def current_branch + Branch.current + end + + def add_label_to_pr(number, label) + octokit_client_instance_from_token.add_labels_to_an_issue(current_repo_slug, number, [label]) + end + + def authorize(username:, password:) + client = octokit_client_instance_from_basic_auth(username: username, password: password) + token = create_token(client) + self.authorization_token = token + end + + def authorized? + !authorization_token.empty? + end + + def authorization_token + token = System.result('git config --get oauth.token') + token + end + + def authorization_token=(token) + System.result("git config --global oauth.token #{token}") + end + + def authorization_note + "CoderlyCode-" + SecureRandom.uuid + end + + end +end diff --git a/lib/code/pull_request.rb b/lib/code/pull_request.rb new file mode 100644 index 0000000..1111085 --- /dev/null +++ b/lib/code/pull_request.rb @@ -0,0 +1,50 @@ +require 'code/github_api' + +module Code + class PullRequest + + def initialize(pull_request_info:, github_api: GitHubAPI.new) + @pull_request_info = pull_request_info + @github_api = github_api + end + + def self.for_branch(branch) + fetch_pull_requests_for_branch(branch).map do |pull_request_info| + new(pull_request_info: pull_request_info) + end + end + + def url + pull_request_info[:html_url] + end + + def number + pull_request_info[:number] + end + + def add_label(label) + github_api.label_pr(self, label) + end + + private + + def github_api + @github_api ||= GitHubAPI.new + end + + attr_reader :pull_request_info + + def self.fetch_pull_requests_for_branch(branch) + github_api.pull_requests_for_branch(branch) + end + + # I need a github api instance to create instances + # and also need an api instance once the PullRequest instance is created + # I tried to solve this problem with modules by extending on the two places + # but it seems like overkilling it, I'll duplicate it for now. + def self.github_api + @github_api ||= GitHubAPI.new + end + + end +end \ No newline at end of file diff --git a/lib/code/repository.rb b/lib/code/repository.rb new file mode 100644 index 0000000..fa8ed5d --- /dev/null +++ b/lib/code/repository.rb @@ -0,0 +1,35 @@ +module Code + + class Repository + + def self.current + new(url: System.result("git config --get remote.origin.url")) + end + + def initialize(url:) + @url = url + end + + def organization + organization_name = @url.split("/")[-2] + + possible_prefix = "git@github.com:" + organization_name.sub!(possible_prefix,"") + + organization_name + end + + def name + repo_name_with_extension = @url.split("/").last + repo_name_without_extension = repo_name_with_extension.sub(".git", "") + + repo_name_without_extension + end + + def slug + "#{organization}/#{name}" + end + + end + +end \ No newline at end of file diff --git a/lib/code/system.rb b/lib/code/system.rb index f107e5d..0b286f7 100644 --- a/lib/code/system.rb +++ b/lib/code/system.rb @@ -1,3 +1,7 @@ +require "io/console" + +require 'code/system/os' + module Code module System extend self @@ -6,9 +10,21 @@ module System COLORS = {black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, teal: 36} - def prompt(*args) - print(*args) - gets + def prompt(prompt_text) + print prompt_text + ": " + input = gets + input.strip + end + + def prompt_hidden(prompt_text) + print prompt_text + ": " + input = noecho_gets + puts "" + input.strip + end + + def current_directory_name + File.basename(Dir.getwd) end def error(message) @@ -20,7 +36,13 @@ def open_in_browser(url) end def open(item) - `open #{item}` + command = OS.current.open_command + `#{command} #{item}` + end + + def reveal(item) + command = OS.current.reveal_command + `#{command} #{item}` end def exec(script) @@ -38,6 +60,10 @@ def puts(text) Kernel.puts text end + def noecho_gets + STDIN.noecho(&:gets) + end + def call(params) exec "git #{params}" end diff --git a/lib/code/system/os.rb b/lib/code/system/os.rb new file mode 100644 index 0000000..f9fbcb7 --- /dev/null +++ b/lib/code/system/os.rb @@ -0,0 +1,41 @@ +module Code + module System + class OS + + def self.current + new(platform_string: RUBY_PLATFORM) + end + + def initialize(platform_string:) + @platform_string = platform_string + end + + def windows? + (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ @platform_string) != nil + end + + def unix? + !windows? + end + + def osx? + (/darwin/ =~ @platform_string) != nil + end + + def linux? + unix? and not osx? + end + + def open_command + return "open" if osx? + return "xdg-open" if linux? + end + + def reveal_command + return "open -R" if osx? + return "xdg-open" if linux? + end + + end + end +end diff --git a/spec/code/branch_spec.rb b/spec/code/branch_spec.rb new file mode 100644 index 0000000..a2a8fe2 --- /dev/null +++ b/spec/code/branch_spec.rb @@ -0,0 +1,539 @@ +require "code/branch" +require "code/system" +require "code/pull_request" +require "code/config" + +module Code + describe Branch do + + before(:all) do + Branch.send :include, GitExtras + end + + before do + allow(Config).to receive(:master_branch_name).and_return "master" + allow(Config).to receive(:development_branch_name).and_return "development" + allow(Config).to receive(:ready_label).and_return "awaiting review" + allow(System).to receive(:puts) + Branch.setup_test_repo + end + + describe "self.all_names" do + + before do + Branch.add_branches(["test-branch-1", "test-branch-2"]) + end + + it "returns names of all branches in the repo as strings" do + branch_names = Branch.all_names + + expect(branch_names.length).to eq 3 + expect(branch_names[0]).to eq "master" + expect(branch_names[1]).to eq "test-branch-1" + expect(branch_names[2]).to eq "test-branch-2" + end + end + + describe "self.all" do + + before do + Branch.add_branches(["test-branch-1", "test-branch-2"]) + end + + it "returns Branch instances for all branches in the repo" do + + branches = Branch.all + + expect(branches.length).to eq 3 + expect(branches[0]).to be_a(Branch) + expect(branches[1]).to be_a(Branch) + expect(branches[2]).to be_a(Branch) + expect(branches[0].name).to eq "master" + expect(branches[1].name).to eq "test-branch-1" + expect(branches[2].name).to eq "test-branch-2" + end + end + + describe "self.matching" do + + before do + Branch.add_branches(["test-branch-1", "test-branch-2"]) + end + + it "returns the first branch that matches the patterns" do + expect(Branch.matching('test', 'branch').name).to eq 'test-branch-1' + expect(Branch.matching('test', '2').name).to eq 'test-branch-2' + end + end + + describe "self.exists?" do + + before do + Branch.add_branches(["test-branch-1", "test-branch-2"]) + end + + it "returns true for a localy known branch, false otherwise" do + expect(Branch.exists? "master").to eq true + expect(Branch.exists? "test-branch-1").to eq true + expect(Branch.exists? "test-branch-2").to eq true + expect(Branch.exists? "test-branch-3").to eq false + end + end + + describe "self.current" do + let(:branch) { Branch.current } + + subject { branch } + + it "should currently be on the master branch" do + expect(branch.name).to eq 'master' + end + + context "when creating a new branch" do + + before do + Branch.create "test_branch" + end + + it "should have the right branches" do + expect(Branch.all.count).to eq 2 + end + + it "should have created the branch" do + expect(Branch.new("test_branch")).to exist + end + + context "when deleting a branch" do + before do + Branch.matching("test_branch").delete! + end + + it "should only have one branch left" do + expect(Branch.all.count).to eq 1 + end + + it "should have deleted the branch" do + expect(Branch.new("test_branch")).to_not exist + end + end + + context "when checking out a branch" do + before do + Branch.matching('test_branch').checkout + end + + it "should have changed the branch" do + expect(Branch.current.name).to eq 'test_branch' + end + end + end + + context "when trying to delete a protected branch" do + + before do + Branch.create "development" + end + + it "should now be allowed" do + expect { Branch.matching("development").delete! }.to raise_error Branch::ProtectedBranchError + end + end + end + + describe "self.master" do + it "returns a Branch instance named 'master'" do + master_branch = Branch.master + + expect(master_branch).to be_a Branch + expect(master_branch.name).to eq 'master' + end + end + + describe "self.development" do + it "returns a Branch instance named 'development'" do + development_branch = Branch.development + + expect(development_branch).to be_a Branch + expect(development_branch.name).to eq 'development' + end + end + + describe "self.create" do + it "makes a system call to create a branch and returns a branch instance" do + expect(System).to receive(:call).with("branch test_branch") + + branch = Branch.create "test_branch" + + expect(branch).to be_a Branch + expect(branch.name).to eq "test_branch" + end + end + + describe "self.find" do + it "creates a new Branch instance with specified name, but makes no System.call" do + expect(System).not_to receive(:call).with("branch test_branch") + + branch = Branch.find "test_branch" + + expect(branch).to be_a Branch + expect(branch.name).to eq "test_branch" + end + end + + describe "equality comparer" do + it "compares by branch name" do + branch_a = Branch.new "branch-a" + branch_b = Branch.new "branch-b" + other_branch_a = Branch.new "branch-a" + + expect(branch_a == branch_b).to eq false + expect(branch_a == other_branch_a).to eq true + expect(branch_b == other_branch_a).to eq false + end + end + + describe "#matches?" do + it "returns true if the branch name matches all the patterns" do + branch = Branch.new "test-branch" + + expect(branch.matches? "test").to eq true + expect(branch.matches? "test", "branch").to eq true + expect(branch.matches? "branch").to eq true + expect(branch.matches? "test-branch").to eq true + end + + it "returns false if the branch name matches some or none of the patterns" do + branch = Branch.new "test-branch" + + expect(branch.matches? "test_branch").to eq false + expect(branch.matches? "tst").to eq false + expect(branch.matches? "tst", "test").to eq false + end + end + + describe "#exists" do + + before do + Branch.add_branches(["test-branch-1", "test-branch-2"]) + end + + it "calls System.result and returns the result" do + test_branch_1 = Branch.new "test-branch-1" + + expect(System).to receive(:result).with("git show-ref refs/heads/test-branch-1").and_return("something") + expect(test_branch_1.exists?).to eq true + + + expect(System).to receive(:result).with("git show-ref refs/heads/test-branch-1").and_return("") + expect(test_branch_1.exists?).to eq false + end + + it "returns true if the branch with the provided instance name exists in the git repo" do + test_branch_1 = Branch.new "test-branch-1" + test_branch_2 = Branch.new "test-branch-2" + test_branch_3 = Branch.new "test-branch-3" + + expect(test_branch_1.exists?).to eq true + expect(test_branch_2.exists?).to eq true + expect(test_branch_3.exists?).to eq false + end + end + + describe "#master?" do + it "returns true if branch name is 'master', false otherwise" do + master_branch = Branch.new "master" + expect(master_branch.master?).to eq true + + other_branch = Branch.new "other" + expect(other_branch.master?).to eq false + end + end + + describe "#development?" do + it "returns true if branch name is 'development', false otherwise" do + development_branch = Branch.new "development" + expect(development_branch.development?).to eq true + + other_branch = Branch.new "other" + expect(other_branch.development?).to eq false + end + end + + describe "#protected?" do + it "returns true if branch name is in the protected list, false otherwise" do + development_branch = Branch.new "development" + expect(development_branch.protected?).to eq true + + master_branch = Branch.new "master" + expect(master_branch.protected?).to eq true + + other_branch = Branch.new "other" + expect(other_branch.protected?).to eq false + end + end + + describe "#feature?" do + it "returns the opposite of #protected?" do + random_branch = Branch.new "random" + expect(random_branch).to receive(:protected?).and_return(true) + expect(random_branch.feature?).to eq false + expect(random_branch).to receive(:protected?).and_return(false) + expect(random_branch.feature?).to eq true + end + end + + describe "#delete!" do + it "checks if branch is allowed to be deleted" do + random_branch = Branch.new "random" + + expect(random_branch).to receive(:authorize_delete!).and_return(true) + allow(System).to receive(:call) + + random_branch.delete! + end + + it "makes a system call for 'branch -d branch-name" do + random_branch = Branch.new "random" + allow(random_branch).to receive(:authorize_delete!).and_return(true) + expect(System).to receive(:call).with("branch -d random") + + random_branch.delete! + end + + it "allows to change the deletion flag to -D instead of -d" do + random_branch = Branch.new "random" + allow(random_branch).to receive(:authorize_delete!).and_return(true) + + expect(System).to receive(:call).with("branch -D random") + + random_branch.delete! force: true + + end + + it "allows deletion of a feature branch" do + random_branch = Branch.new "random" + allow(System).to receive(:call) + expect { random_branch.delete! }.not_to raise_error + end + + it "doesn't allow deletion of a protected branch" do + master_branch = Branch.new "master" + expect { master_branch.delete! }.to raise_error Branch::ProtectedBranchError + end + end + + describe "#delete_remote!" do + it "checks if branch is allowed to be deleted" do + random_branch = Branch.new "random" + + expect(random_branch).to receive(:authorize_delete!).and_return(true) + allow(System).to receive(:call) + + random_branch.delete_remote! + end + + it "makes a system call for 'push origin :branch-name" do + random_branch = Branch.new "random" + allow(random_branch).to receive(:authorize_delete!).and_return(true) + expect(System).to receive(:call).with("push origin :random") + + random_branch.delete_remote! + end + + it "allows deletion of a feature branch" do + random_branch = Branch.new "random" + allow(System).to receive(:call) + expect { random_branch.delete_remote! }.not_to raise_error + end + + it "doesn't allow deletion of a protected branch" do + master_branch = Branch.new "master" + expect { master_branch.delete_remote! }.to raise_error Branch::ProtectedBranchError + end + end + + describe "#push" do + it "makes a system call for 'push origin branch-name:branch-name'" do + random_branch = Branch.new "random" + allow(random_branch).to receive(:ensure_public!).and_return(true) + expect(System).to receive(:call).with("push origin random:random") + + random_branch.push + end + + it "allows pushing of a public branch" do + random_branch = Branch.new "random" + allow(System).to receive(:call).with("push origin random:random") + expect { random_branch.push }.not_to raise_error + end + + it "doesn't allow pushing of a private branch" do + master_branch = Branch.new "random-local" + expect { master_branch.push }.to raise_error Branch::PrivateBranchError + end + end + + describe "#pull" do + it "makes a system call for 'pull origin branch-name:branch-name'" do + random_branch = Branch.new "random" + expect(System).to receive(:call).with("pull origin random:random") + random_branch.pull + end + end + + describe "#checkout" do + it "makes a system call for 'checkout branch-name'" do + random_branch = Branch.new "random" + expect(System).to receive(:call).with("checkout random") + random_branch.checkout + end + + it "returns its own instance" do + random_branch = Branch.new "random" + allow(System).to receive(:call) + expect(random_branch.checkout).to eq random_branch + end + end + + describe "#message" do + it "returns the capitalized branch name, where dashes are replaced with spaces" do + random_branch = Branch.new "random" + expect(random_branch.message).to eq "Random" + random_branch = Branch.new "random-branch" + expect(random_branch.message).to eq "Random branch" + end + end + + describe "#private?" do + it "returns true if branch name ends with '-local'" do + random_branch = Branch.new "random" + expect(random_branch.private?).to eq false + random_branch = Branch.new "random-local" + expect(random_branch.private?).to eq true + end + end + + describe "#hotfix?" do + it "returns true if branch name starts with 'hotfix-'" do + random_branch = Branch.new "random" + expect(random_branch.hotfix?).to eq false + random_branch = Branch.new "hotfix-random" + expect(random_branch.hotfix?).to eq true + end + end + + describe "#has_pull_request?" do + it "returns true if the branch has any pull requests, false otherwise" do + random_branch_1 = Branch.new "random_1" + pull_request_1 = PullRequest.new(pull_request_info: { html_url: "example.com" }) + pull_request_2 = PullRequest.new(pull_request_info: { html_url: "example2.com" }) + + expect(PullRequest).to receive(:for_branch).with(random_branch_1).and_return([pull_request_1, pull_request_2]) + expect(random_branch_1.has_pull_request?).to eq true + + random_branch_2 = Branch.new "random_2" + expect(PullRequest).to receive(:for_branch).with(random_branch_2).and_return([]) + expect(random_branch_2.has_pull_request?).to eq false + end + end + + describe "#mark_prs_as_ready" do + it "calls #label_prs with 'awaiting review'" do + random_branch = Branch.new "random" + expect(random_branch).to receive(:label_prs).with("awaiting review") + random_branch.mark_prs_as_ready + end + + it "throws NoPRError if branch has no pull requests" do + random_branch = Branch.new "random" + expect(random_branch).to receive(:has_pull_request?).and_return(false) + expect { random_branch.mark_prs_as_ready }.to raise_error Branch::NoPRError + end + end + + describe "#mark_prs_as_hotfix" do + it "calls #label_prs with 'hotfix'" do + random_branch = Branch.new "random" + expect(random_branch).to receive(:label_prs).with("hotfix") + random_branch.mark_prs_as_hotfix + end + + it "throws NoPRError if branch has no pull requests" do + random_branch = Branch.new "random" + expect(random_branch).to receive(:has_pull_request?).and_return(false) + expect { random_branch.mark_prs_as_hotfix }.to raise_error Branch::NoPRError + end + end + + describe "#pull_request_url" do + it "returns the URL of the first pull request" do + random_branch = Branch.new "random" + pull_request_1 = PullRequest.new(pull_request_info: { html_url: "example.com" }) + pull_request_2 = PullRequest.new(pull_request_info: { html_url: "example2.com" }) + + expect(PullRequest).to receive(:for_branch).and_return([pull_request_1, pull_request_2]) + + expect(random_branch.pull_request_url).to eq "example.com" + end + end + end + + describe "user prompt" do + it "should be made during call to self.development to get the name of the development branch" do + expect(Config).to receive(:development_branch_name) + Branch.development + end + + it "should be made during call to self.development to get the name of the development branch" do + expect(Config).to receive(:master_branch_name) + Branch.master + end + + it "should be made during call to #development? to decide if the current branch is the development branch" do + expect(Config).to receive(:development_branch_name).and_return "test" + branch = Branch.new "test" + expect(branch.development?).to eq true + end + + it "should be made during call to #master? to decide if the current branch is the master branch" do + expect(Config).to receive(:master_branch_name).and_return "test" + branch = Branch.new "test" + expect(branch.master?).to eq true + end + + it "should be made during call to #protected? to decide if the current branch is either master or development" do + expect(Config).to receive(:development_branch_name).twice.and_return "test-dev" + expect(Config).to receive(:master_branch_name).twice.and_return "test-master" + branch = Branch.new "test-dev" + expect(branch.protected?).to eq true + branch = Branch.new "test-master" + expect(branch.protected?).to eq true + end + + it "should be made during call to #feature? to decide if the current branch is either master or development" do + expect(Config).to receive(:development_branch_name).twice.and_return "test-dev" + expect(Config).to receive(:master_branch_name).twice.and_return "test-master" + branch = Branch.new "test-dev" + expect(branch.feature?).to eq false + branch = Branch.new "test-master" + expect(branch.feature?).to eq false + end + + it "should be made during call to #delete! to decide if the current branch is protected" do + expect(Config).to receive(:development_branch_name).and_return "test-dev" + expect(Config).to receive(:master_branch_name).and_return "test-master" + + allow(System).to receive(:call) + branch = Branch.new "test" + branch.delete! + end + + it "should be made during call to #mark_prs_as_ready to determine the label marking the PR as ready" do + expect(Config).to receive(:ready_label).and_return "test-ready" + + branch = Branch.new "test" + allow(branch).to receive(:label_prs) + branch.mark_prs_as_ready + end + end +end diff --git a/spec/code/config_spec.rb b/spec/code/config_spec.rb new file mode 100644 index 0000000..108eb1e --- /dev/null +++ b/spec/code/config_spec.rb @@ -0,0 +1,182 @@ +require "code/config" +require_relative '../support/uses_file_system' + +module Code + describe Config do + include UsesFileSystem + + describe "#get" do + + context "when .codeconfig doesn't exist" do + with_empty_tmp_folder + + it "creates a .codeconfig file" do + expect(File.exists? ".codeconfig").to be false + allow(System).to receive (:prompt) + Config.get "random-property" + expect(File.exists? ".codeconfig").to be true + end + end + + context "with an existing .codeconfig file" do + + context "when config property doesn't exist in the file" do + with_file_and_content(".codeconfig", "") + + it "should prompt the user for the property value" do + expect(System).to receive(:prompt).with("Unknown") + Config.get "property" + end + + it "should return the value the user has provided" do + allow(System).to receive(:prompt).with("Unknown").and_return("test-value") + expect(Config.get "property").to eq ("test-value") + end + + it "should store the provided value into the file" do + allow(System).to receive(:prompt).with("Unknown").and_return("test-value") + Config.get "property" + file_content = File.read ".codeconfig" + expect(file_content.strip).to eq "property = \"test-value\"" + end + end + + context "when config property does exist" do + with_file_and_content(".codeconfig", "property = value\n") + + it "should not prompt the user for the property value" do + expect(System).not_to receive(:prompt) + Config.get "property" + end + + it "should return the stored property value" do + expect(Config.get "property").to eq "value" + end + end + end + end + + describe "#master_branch_name" do + context "when there is no previously stored property value" do + with_file_and_content(".codeconfig", "") + let(:prompt_text) { "You didn't set the name of the master branch. What is it? (master)" } + + it "asks the user for the property value" do + expect(System).to receive(:prompt).with prompt_text + Config.master_branch_name + end + + it "should return the value the user has provided" do + allow(System).to receive(:prompt).with(prompt_text).and_return("test-value") + + expect(Config.master_branch_name).to eq ("test-value") + end + + it "should store the provided value into the file" do + allow(System).to receive(:prompt).with(prompt_text).and_return("test-value") + Config.master_branch_name + file_content = File.read ".codeconfig" + expect(file_content.strip).to eq "master_branch = \"test-value\"" + end + end + + context "when there is a previously stored property value" do + with_file_and_content(".codeconfig", "master_branch = \"value\"\n") + it "should not prompt the user for the property value" do + expect(System).not_to receive(:prompt) + Config.master_branch_name + end + + it "should return the stored property value" do + expect(Config.master_branch_name).to eq "value" + end + end + end + + describe "#development_branch_name" do + context "when there is no previously stored property value" do + with_file_and_content(".codeconfig", "") + let(:prompt_text) { "You didn't set the name of the development branch. What is it? (development)" } + + it "asks the user for the property value" do + expect(System).to receive(:prompt).with prompt_text + Config.development_branch_name + end + + it "should return the value the user has provided" do + allow(System).to receive(:prompt).with(prompt_text).and_return("test-value") + + expect(Config.development_branch_name).to eq ("test-value") + end + + it "should store the provided value into the file" do + allow(System).to receive(:prompt).with(prompt_text).and_return("test-value") + Config.development_branch_name + file_content = File.read ".codeconfig" + expect(file_content.strip).to eq "development_branch = \"test-value\"" + end + end + + context "when there is a previously stored property value" do + with_file_and_content(".codeconfig", "development_branch = \"value\"\n") + it "should not prompt the user for the property value" do + expect(System).not_to receive(:prompt) + Config.development_branch_name + end + + it "should return the stored property value" do + expect(Config.development_branch_name).to eq "value" + end + end + end + + describe "#ready_label" do + context "when there is no previously stored property value" do + with_file_and_content(".codeconfig", "") + let(:prompt_text) { "You didn't set the label used for marking the PR as ready. What is it? (awaiting review)" } + + it "asks the user for the property value" do + expect(System).to receive(:prompt).with prompt_text + Config.ready_label + end + + it "should return the value the user has provided" do + allow(System).to receive(:prompt).with(prompt_text).and_return("test-value") + + expect(Config.ready_label).to eq ("test-value") + end + + it "should store the provided value into the file" do + allow(System).to receive(:prompt).with(prompt_text).and_return("test-value") + Config.ready_label + file_content = File.read ".codeconfig" + expect(file_content.strip).to eq "ready_label = \"test-value\"" + end + end + + context "when there is a previously stored property value" do + with_file_and_content(".codeconfig", "ready_label = \"value\"\n") + it "should not prompt the user for the property value" do + expect(System).not_to receive(:prompt) + Config.ready_label + end + + it "should return the stored property value" do + expect(Config.ready_label).to eq "value" + end + end + end + + describe "default" do + with_file_and_content(".codeconfig", "") + + it "asks the user for the property value" do + allow(System).to receive(:prompt).and_return("\n") + + expect(Config.master_branch_name).to eq "master" + expect(Config.development_branch_name).to eq "development" + expect(Config.ready_label).to eq "awaiting review" + end + end + end +end diff --git a/spec/code/file_list_spec.rb b/spec/code/file_list_spec.rb new file mode 100644 index 0000000..2809dab --- /dev/null +++ b/spec/code/file_list_spec.rb @@ -0,0 +1,29 @@ +require "code/file_list" +require_relative '../support/uses_file_system' + +module Code + + describe FileList do + include UsesFileSystem + + + describe "#matching" do + with_files("random_file", "non_random_file", "other_random_file", "last/random") + + it "returns a list of files matching the patterns" do + list = FileList.new.matching("random") + expect(list.length).to eq 4 + + list = FileList.new.matching("random", "file") + expect(list.length).to eq 3 + + list = FileList.new.matching("file") + expect(list.length).to eq 3 + + list = FileList.new.matching("non", "random") + expect(list.length).to eq 1 + end + + end + end +end \ No newline at end of file diff --git a/spec/code/git_spec.rb b/spec/code/git_spec.rb index 170fe6c..9d62fed 100644 --- a/spec/code/git_spec.rb +++ b/spec/code/git_spec.rb @@ -1,6 +1,7 @@ require 'code/git' require 'code/branch' require 'code/system' +require "code/config" require_relative '../support/git_extras' @@ -17,72 +18,237 @@ module Code end before do + allow(Config).to receive(:master_branch_name).and_return "master" + allow(Config).to receive(:development_branch_name).and_return "development" + allow(System).to receive(:puts) Git.setup_test_repo end - describe 'current_branch' do - let(:branch) { git.current_branch } + describe "#search" do - subject { branch } + before do + allow(System).to receive(:open_in_browser) + end - it 'should currently be on the master branch' do - expect(branch.name).to eq 'master' + it 'should call System.open_in_browser with the proper url' do + expect(System).to receive(:open_in_browser).with('https://github.com/testuser/codegit/find/development') + git.search end + end - context 'when creating a new branch' do + describe "#start" do + it "should raise an error if branch already exists" do + existing_branch = Branch.create "existing-branch" - before do - Branch.create 'test_branch' - end + expect{ git.start "existing-branch" }.to raise_error Git::FeatureExistsError + end - it 'should have the right branches' do - expect(Branch.all.count).to eq 2 - end + it "should checkout the development branch, pull it, then create and switch to a new branch" do + expect(System).to receive(:call).with("checkout development") + expect(System).to receive(:call).with("pull origin development:development") + expect(System).to receive(:call).with("branch new-feature") + expect(System).to receive(:call).with("checkout new-feature") - it 'should have created the branch' do - expect(Branch.new('test_branch')).to exist - end + git.start "new-feature" + end - context 'when deleting a branch' do - before do - Branch.matching('test_branch').delete! - end + it "should not checkout the development branch if already on it" do + dev_branch = Branch.create "development" + allow(git).to receive(:current_branch).and_return(dev_branch) + expect(System).not_to receive(:call).with("checkout development") - it 'should only have one branch left' do - expect(Branch.all.count).to eq 1 - end + git.start "new-feature" + end + + it "should stash and unstash if there are uncommitted changes" do + allow(git).to receive(:uncommitted_changes?).and_return true - it 'should have deleted the branch' do - expect(Branch.new('test_branch')).to_not exist - end - end + allow(System).to receive(:call).with("checkout development") + allow(System).to receive(:call).with("pull origin development:development") + allow(System).to receive(:call).with("branch new-feature") + allow(System).to receive(:call).with("checkout new-feature") - context 'when checking out a branch' do - before do - Branch.matching('test_branch').checkout - end + expect(System).to receive(:call).with("stash") + expect(System).to receive(:call).with("stash pop") + + git.start "new-feature" + end + end - it 'should have changed the branch' do - expect(Branch.current.name).to eq 'test_branch' - end - end + describe "#hotfix" do + it "should raise an error if branch already exists" do + existing_branch = Branch.create "hotfix-branch" + expect{ git.hotfix "branch" }.to raise_error Git::FeatureExistsError end - context 'when trying to delete a protected branch' do + it "should checkout the master branch, pull it, then create and switch to a new branch" do + some_other_branch = Branch.create "random-branch" - before do - Branch.create 'development' - end + allow(git).to receive(:current_branch).and_return(some_other_branch) - it 'should now be allowed' do - expect { Branch.matching('development').delete! }.to raise_error Branch::ProtectedBranchError - end + expect(System).to receive(:call).with("checkout master") + expect(System).to receive(:call).with("pull origin master:master") + expect(System).to receive(:call).with("branch hotfix-branch") + expect(System).to receive(:call).with("checkout hotfix-branch") + git.hotfix "branch" end + it "should not checkout the master branch if already on it" do + # NOTE: the test git repo is on "master" branch to begin + expect(System).not_to receive(:call).with("checkout master") + + git.hotfix "branch" + end + + it "should stash and unstash if there are uncommitted changes" do + allow(git).to receive(:uncommitted_changes?).and_return true + + allow(System).to receive(:call).with("pull origin master:master") + allow(System).to receive(:call).with("branch hotfix-new-feature") + allow(System).to receive(:call).with("checkout hotfix-new-feature") + + expect(System).to receive(:call).with("stash") + expect(System).to receive(:call).with("stash pop") + + git.hotfix "new-feature" + end end + describe "#switch" do + it "should switch to the first branch (alphabetically) matching the provided pattern" do + Branch.create("some-other-branch") + Branch.create("some-random-branch") + + expect(System).to receive(:call).with("checkout some-other-branch") + git.switch("some") + + expect(System).to receive(:call).with("checkout some-random-branch") + git.switch("some", "random") + + expect(System).to receive(:call).with("checkout some-other-branch") + git.switch("some", "other") + + expect(System).to receive(:call).with("checkout some-other-branch") + git.switch("some", "branch") + end + + it "should raise error if no matching branch was found" do + expect{ git.switch("some") }.to raise_error Git::BranchDoesntExistError + end + end + + describe "#finish" do + it "should switch to development, perform a pull and delete the previous branch" do + branch = Branch.create("test-branch") + development_branch = Branch.create("development") + branch.checkout + + expect(System).to receive(:call).with("checkout development").and_call_original + expect(System).to receive(:call).with("pull origin development:development") + expect(System).to receive(:call).with("branch -d test-branch") + + allow(git).to receive(:fetch) + + git.finish + end + end + + describe "#publish" do + it "should raise an error on a protected branch" do + branch = Branch.create("protected") + allow(branch).to receive(:protected?).and_return(true) + allow(git).to receive(:current_branch).and_return(branch) + allow(git).to receive(:uncommitted_changes?).and_return(false) + + expect{ git.publish }.to raise_error Git::NotOnFeatureBranchError + end + + it "should raise an error if there are uncommited_changes" do + branch = Branch.create("test-branch") + allow(git).to receive(:current_branch).and_return(branch) + allow(git).to receive(:uncommitted_changes?).and_return(true) + + expect{ git.publish }.to raise_error Git::UncommittedChangesError + end + + it "should create a PRs against both development and master for a hotfix branch, then label them" do + branch = Branch.create("hotfix-test") + allow(git).to receive(:current_branch).and_return(branch) + allow(git).to receive(:uncommitted_changes?).and_return(false) + allow(git).to receive(:main_repo).and_return("test_org/test_repo") + allow(git).to receive(:push) + + expect(System).to receive(:exec).with("hub pull-request -f \"Hotfix test\" -b test_org/test_repo:development -h test_org/test_repo:hotfix-test").and_return("something") + expect(System).to receive(:exec).with("hub pull-request -f \"Hotfix test\" -b test_org/test_repo:master -h test_org/test_repo:hotfix-test").and_return("something") + expect(branch).to receive(:mark_prs_as_hotfix) + + git.publish + end + + it "should create a PR against development for any other feature branch" do + branch = Branch.create("test-feature") + allow(git).to receive(:current_branch).and_return(branch) + allow(git).to receive(:uncommitted_changes?).and_return(false) + allow(git).to receive(:main_repo).and_return("test_org/test_repo") + allow(git).to receive(:push) + + allow(System).to receive(:exec).with("git ls-remote --get-url origin").and_call_original + expect(System).to receive(:exec).with("hub pull-request -f \"Test feature\" -b test_org/test_repo:development -h test_org/test_repo:test-feature").and_return("something") + + git.publish + + end + end + + describe "#cancel" do + it "should not do anything if already on development branch" do + dev_branch = Branch.create "development" + + allow(git).to receive(:current_branch).and_return(dev_branch) + + expect(STDOUT).to receive(:puts).with("Nothing to cancel (already on development)") + + git.cancel + end + + it "should switch to development, then delete previous branch, with force flag" do + some_branch = Branch.create "some-branch" + Branch.create "development" + + allow(git).to receive(:current_branch).and_return(some_branch) + + expect(System).to receive(:call).with("checkout development") + expect(some_branch).to receive(:delete!).with(force: true) + + git.cancel + end + end + + describe "#commit" do + it "should add all files to the list and commit with the specified message" do + expect(System).to receive(:call).with("add -A") + expect(System).to receive(:call).with("commit -m \"test message\"") + + git.commit "test message" + end + end + + describe "#push" do + it "should push to origin" do + expect(System).to receive(:call).with("push origin master:master") + git.push + end + end + + describe "#prune_remote_branches" do + it "should call the appropriate git command" do + expect(System).to receive(:call).with("remote prune origin") + + git.prune_remote_branches + end + end end -end \ No newline at end of file +end diff --git a/spec/code/github_api_spec.rb b/spec/code/github_api_spec.rb new file mode 100644 index 0000000..cf08c56 --- /dev/null +++ b/spec/code/github_api_spec.rb @@ -0,0 +1,47 @@ +require 'code/github_api' + +module Code + describe GitHubAPI do + let(:api) { GitHubAPI.new } + + describe "#ensure_authorized" do + + context 'when system is authorized' do + before do + allow(api).to receive(:authorized?).and_return(true) + end + + it 'should not call #authorize' do + expect(api).not_to receive(:authorize) + expect(System).not_to receive(:prompt) + expect(System).not_to receive(:prompt_text) + + api.ensure_authorized + end + end + + context 'when system is not authorized' do + before do + allow(api).to receive(:authorized?).and_return(false) + end + + it 'should call #authorize' do + expect(api).to receive(:authorize) + expect(System).to receive(:prompt).once + expect(System).to receive(:prompt_hidden).once + + api.ensure_authorized + end + end + end + + describe "#authorize" do + it "creates a client instance, uses it to create a token and stores it" do + expect(api).to receive(:octokit_client_instance_from_basic_auth).with(username: "test_user", password: "test_pass").and_return("something") + expect(api).to receive(:create_token).with("something").and_return("token") + expect(System).to receive(:result).with("git config --global oauth.token token") + api.send(:authorize, username: "test_user", password: "test_pass") + end + end + end +end diff --git a/spec/code/pull_request_spec.rb b/spec/code/pull_request_spec.rb new file mode 100644 index 0000000..dac489e --- /dev/null +++ b/spec/code/pull_request_spec.rb @@ -0,0 +1,25 @@ +require "code/pull_request" +require "code/branch" + +module Code + describe PullRequest do + # ToDO self.for_branch uses a GitHubAPI instance, which we can't inject + # Not sure how to test that part + + it "properly maps the url and number properties" do + pr = PullRequest.new(pull_request_info: { html_url: "example.com", number: 1 }) + expect(pr.url).to eq "example.com" + expect(pr.number).to eq 1 + end + + describe "#add_label" do + it "calls GitHubAPI#label_pr" do + api = GitHubAPI.new + pr = PullRequest.new(pull_request_info: { html_url: "example.com", number: 1 }, github_api: api) + + expect(api).to receive(:label_pr).with(pr, "label") + pr.add_label("label") + end + end + end +end diff --git a/spec/code/repository_spec.rb b/spec/code/repository_spec.rb new file mode 100644 index 0000000..7c8a3ca --- /dev/null +++ b/spec/code/repository_spec.rb @@ -0,0 +1,75 @@ +require 'code/repository' +require_relative '../support/git_extras' + +module Code + describe Repository do + + describe "self.current" do + before(:all) do + Repository.send :include, GitExtras + end + + before do + allow(System).to receive(:puts) + Repository.setup_test_repo + end + + it "returns a Repository instance from the URL of the current origin remote" do + current_repo = Repository.current + expect(current_repo.slug).to eq "testuser/codegit" + end + end + + describe "#organization" do + it "works with 'https://' format repository urls" do + repo = Repository.new(url: "https:/github.com/test_org/test_name.git") + expect(repo.organization).to eq "test_org" + end + + it "works with 'git@github.com:' format repository urls" do + repo = Repository.new(url: "git@github.com:test_org/test_name.git") + expect(repo.organization).to eq "test_org" + end + + it "works with file system paths" do + repo = Repository.new(url: "/home/random/path/to/git/test_org/test_name") + expect(repo.organization).to eq "test_org" + end + end + + describe "#name" do + it "works with 'https://' format repository urls" do + repo = Repository.new(url: "https:/github.com/test_org/test_name.git") + expect(repo.name).to eq "test_name" + end + + it "works with 'git@github.com:' format repository urls" do + repo = Repository.new(url: "git@github.com:test_org/test_name.git") + expect(repo.name).to eq "test_name" + end + + it "works with file system paths" do + repo = Repository.new(url: "/home/random/path/to/git/test_org/test_name") + expect(repo.name).to eq "test_name" + end + end + + describe "#slug" do + it "works with 'https://' format repository urls" do + repo = Repository.new(url: "https:/github.com/test_org/test_name.git") + expect(repo.slug).to eq "test_org/test_name" + end + + it "works with 'git@github.com:' format repository urls" do + repo = Repository.new(url: "git@github.com:test_org/test_name.git") + expect(repo.slug).to eq "test_org/test_name" + end + + it "works with file system paths" do + repo = Repository.new(url: "/home/random/path/to/git/test_org/test_name") + expect(repo.slug).to eq "test_org/test_name" + end + end + + end +end \ No newline at end of file diff --git a/spec/code/system/os_spec.rb b/spec/code/system/os_spec.rb new file mode 100644 index 0000000..220e4f5 --- /dev/null +++ b/spec/code/system/os_spec.rb @@ -0,0 +1,51 @@ +require 'code/system/os' + +module Code + module System + describe OS do + it "detects linux" do + platform_string ="x86_64-linux" + os_detector = OS.new(platform_string: platform_string) + + expect(os_detector.linux?).to be true + end + + it "detects windows" do + platform_string="i386-mingw32" + os_detector = OS.new(platform_string: platform_string) + + expect(os_detector.windows?).to be true + end + + it "detects unix" do + platform_string ="x86_64-linux" + os_detector = OS.new(platform_string: platform_string) + + expect(os_detector.unix?).to be true + end + + it "detects osx" do + platform_string = "x86_64-darwin14" + os_detector = OS.new(platform_string: platform_string) + + expect(os_detector.osx?).to be true + end + + describe "#open_command" do + it "returns 'open' for osx" do + platform_string ="x86_64-darwin14" + os_detector = OS.new(platform_string: platform_string) + + expect(os_detector.open_command).to eq "open" + end + + it "returns 'xdg-open' for linux" do + platform_string ="x86_64-linux" + os_detector = OS.new(platform_string: platform_string) + + expect(os_detector.open_command).to eq "xdg-open" + end + end + end + end +end \ No newline at end of file diff --git a/spec/code/system_spec.rb b/spec/code/system_spec.rb new file mode 100644 index 0000000..ff84787 --- /dev/null +++ b/spec/code/system_spec.rb @@ -0,0 +1,95 @@ +require "code/system" + +module Code + module System + describe "#prompt" do + it "should print the prompt, then ask for and return the input" do + expect(System).to receive(:print).with("Test: ").and_return("") + expect(System).to receive(:gets).and_return("test_user") + input = System.prompt "Test" + expect(input).to eq "test_user" + end + + it "should strip special characters from the input" do + allow(System).to receive(:print).and_return("") + allow(System).to receive(:gets).and_return("test\n") + + input = System.prompt "" + expect(input).to eq "test" + end + end + + describe "#prompt_hidden" do + it "should print the prompt, ask for input without echo, print a new line and return the input" do + expect(System).to receive(:print).with("Test: ").and_return("") + expect(System).to receive(:noecho_gets).and_return("test_pass") + expect(System).to receive(:puts).and_return("") + input = System.prompt_hidden "Test" + expect(input).to eq "test_pass" + end + + it "should strip special characters from the input" do + allow(System).to receive(:print).and_return("") + allow(System).to receive(:noecho_gets).and_return("test\n") + + input = System.prompt_hidden "" + expect(input).to eq "test" + end + end + + describe "#open_in_browser" do + it "calls #open for a fully defined URL" do + expect(System).to receive(:open) + + System.open_in_browser("https://www.example.com") + end + + it "doesn't call #open for an incomplete URL" do + expect(System).not_to receive(:open) + + System.open_in_browser("www.example.com") + end + + it "doesn't call #open for an invalid URL" do + expect(System).not_to receive(:open) + + System.open_in_browser("000") + end + end + + describe "#error" do + it "calls abort with a red message" do + expect(System).to receive(:abort) + expect(System).to receive(:red).with("message") + + System.error "message" + end + end + + describe "#exec" do + it "calls #puts with a green message, then executes the command using Kernel backtick" do + expect(System).to receive(:puts) + expect(System).to receive(:green).with("message") + expect(System).to receive(:command_failed?).and_return(false) + expect(System).to receive(:`) + System.exec "message" + end + + it "raises a CommandFailedError if command has failed, with red text" do + allow(System).to receive(:puts) + allow(System).to receive(:green) + expect(System).to receive(:red) + expect(System).to receive(:command_failed?).and_return(true) + expect(System).to receive(:`) + expect { System.exec "message" }.to raise_error System::CommandFailedError + end + end + + describe "#call" do + it "calls #exec with git prepended to the command script" do + expect(System).to receive(:exec).with("git some command") + System.call "some command" + end + end + end +end \ No newline at end of file diff --git a/spec/support/git_extras.rb b/spec/support/git_extras.rb index c83eac5..eeabca1 100644 --- a/spec/support/git_extras.rb +++ b/spec/support/git_extras.rb @@ -4,6 +4,11 @@ module Code module GitExtras module ClassMethods + + def test_repo_origin + "https:/github.com/testuser/codegit" + end + def setup_test_repo repo_path = Dir.mktmpdir('codegit') Dir.chdir repo_path @@ -14,6 +19,17 @@ def setup_test_repo System.exec 'touch README' System.call 'add -A' System.call 'commit -m "initial commit"' + System.call "remote add origin #{test_repo_origin}" + end + + def checkout_branch(name) + System.call "checkout #{name}" + end + + def add_branches(names) + names.each do |name| + System.call "branch #{name}" + end end end diff --git a/spec/support/uses_file_system.rb b/spec/support/uses_file_system.rb new file mode 100644 index 0000000..34e5d9e --- /dev/null +++ b/spec/support/uses_file_system.rb @@ -0,0 +1,69 @@ +module Code + module UsesFileSystem + def self.included(group) + group.extend(self) + end + + def with_files(*files) + before do + @pwd = Dir.pwd + @tmp_dir = File.join(File.dirname(__FILE__), 'tmp') + FileUtils.mkdir_p(@tmp_dir) + Dir.chdir(@tmp_dir) + + files.each do |file| + create_file(file) + end + end + + after do + Dir.chdir(@pwd) + FileUtils.rm_rf(@tmp_dir) + end + end + + def with_empty_tmp_folder + before do + @pwd = Dir.pwd + @tmp_dir = File.join(File.dirname(__FILE__), 'tmp') + FileUtils.mkdir_p(@tmp_dir) + Dir.chdir(@tmp_dir) + end + + after do + Dir.chdir(@pwd) + FileUtils.rm_rf(@tmp_dir) + end + end + + def with_file_and_content(file, content) + before do + @pwd = Dir.pwd + @tmp_dir = File.join(File.dirname(__FILE__), 'tmp') + FileUtils.mkdir_p(@tmp_dir) + Dir.chdir(@tmp_dir) + + create_file(file) + set_file_content(file, content) + end + + after do + Dir.chdir(@pwd) + FileUtils.rm_rf(@tmp_dir) + end + end + + private + + def create_file(file) + base_name = File.basename(file) + dir_name = File.dirname(file) + FileUtils.mkdir_p(dir_name) if dir_name + FileUtils.touch(file) + end + + def set_file_content(file, content) + File.write(file, content) + end + end +end \ No newline at end of file