From 57348a626811688eb014d97d5aa401a10b3898bd Mon Sep 17 00:00:00 2001 From: Wild Kat Date: Wed, 25 Apr 2018 16:56:20 +0200 Subject: move hook tests into own directory and regen rubocop todo --- .rubocop_todo.yml | 10 +-- spec/githubrepo_spec.rb | 170 ------------------------------------------- spec/hook/githubrepo_spec.rb | 170 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 175 insertions(+), 175 deletions(-) delete mode 100644 spec/githubrepo_spec.rb create mode 100644 spec/hook/githubrepo_spec.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index b4c59f2..db70e00 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2018-04-21 13:24:05 +0200 using RuboCop version 0.55.0. +# on 2018-04-25 15:40:14 +0200 using RuboCop version 0.55.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -354,7 +354,7 @@ Style/EmptyMethod: Exclude: - 'lib/oxidized/hook.rb' -# Offense count: 2 +# Offense count: 1 # Cop supports --auto-correct. Style/ExpandPathArguments: Exclude: @@ -405,7 +405,7 @@ Style/IfInsideElse: Exclude: - 'lib/oxidized/output/file.rb' -# Offense count: 38 +# Offense count: 39 # Cop supports --auto-correct. Style/IfUnlessModifier: Enabled: false @@ -526,7 +526,7 @@ Style/ParenthesesAroundCondition: - 'lib/oxidized/model/powerconnect.rb' - 'lib/oxidized/source/http.rb' -# Offense count: 9 +# Offense count: 6 # Cop supports --auto-correct. # Configuration parameters: PreferredDelimiters. Style/PercentLiteralDelimiters: @@ -733,7 +733,7 @@ Style/ZeroLengthPredicate: - 'lib/oxidized/model/ciscosmb.rb' - 'lib/oxidized/output/git.rb' -# Offense count: 269 +# Offense count: 270 # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # URISchemes: http, https Metrics/LineLength: diff --git a/spec/githubrepo_spec.rb b/spec/githubrepo_spec.rb deleted file mode 100644 index 67c99f0..0000000 --- a/spec/githubrepo_spec.rb +++ /dev/null @@ -1,170 +0,0 @@ -require 'spec_helper' -require 'rugged' -require 'oxidized/hook/githubrepo' - -describe GithubRepo do - let(:credentials) { mock } - let(:remote) { mock } - let(:remotes) { mock } - let(:repo_head) { mock } - let(:repo) { mock } - let(:gr) { GithubRepo.new } - - before do - Oxidized.asetus = Asetus.new - Oxidized.config.log = '/dev/null' - Oxidized.setup_logger - Oxidized.config.output.default = 'git' - end - - describe '#validate_cfg!' do - before do - gr.expects(:respond_to?).with(:validate_cfg!).returns(false) # `cfg=` call - end - - it 'raise a error when `remote_repo` is not configured' do - Oxidized.config.hooks.github_repo_hook = { type: 'githubrepo' } - gr.cfg = Oxidized.config.hooks.github_repo_hook - proc { gr.validate_cfg! }.must_raise(KeyError) - end - end - - describe "#fetch_and_merge_remote" do - before(:each) do - Oxidized.config.hooks.github_repo_hook.remote_repo = 'git@github.com:username/foo.git' - Proc.expects(:new).returns(credentials) - repo_head.expects(:name).returns('refs/heads/master') - gr.cfg = Oxidized.config.hooks.github_repo_hook - end - - it "should not try to merge when there is no update in remote branch" do - repo.expects(:fetch).with('origin', ['refs/heads/master'], credentials: credentials).returns(Hash.new(0)) - repo.expects(:branches).never - repo.expects(:head).returns(repo_head) - gr.fetch_and_merge_remote(repo).must_equal nil - end - describe "when there is update considering conflicts" do - let(:merge_index) { mock } - let(:their_branch) { mock } - - before(:each) do - repo.expects(:fetch).with('origin', ['refs/heads/master'], credentials: credentials).returns(total_deltas: 1) - their_branch.expects(:target_id).returns(1) - repo_head.expects(:target_id).returns(2) - repo.expects(:merge_commits).with(2, 1).returns(merge_index) - repo.expects(:branches).returns("origin/master" => their_branch) - end - - it "should not try merging when there's conflict" do - repo.expects(:head).twice.returns(repo_head) - their_branch.expects(:name).returns("origin/master") - merge_index.expects(:conflicts?).returns(true) - Rugged::Commit.expects(:create).never - gr.fetch_and_merge_remote(repo).must_equal nil - end - - it "should merge when there is no conflict" do - repo.expects(:head).times(3).returns(repo_head) - their_branch.expects(:target).returns("their_target") - their_branch.expects(:name).twice.returns("origin/master") - repo_head.expects(:target).returns("our_target") - merge_index.expects(:write_tree).with(repo).returns("tree") - merge_index.expects(:conflicts?).returns(false) - Rugged::Commit.expects(:create).with(repo, - parents: ["our_target", "their_target"], - tree: "tree", - message: "Merge remote-tracking branch 'origin/master'", - update_ref: "HEAD").returns(1) - gr.fetch_and_merge_remote(repo).must_equal 1 - end - end - end - - describe "#run_hook" do - let(:group) { nil } - let(:ctx) { OpenStruct.new(node: node) } - let(:node) do - Oxidized::Node.new(ip: '127.0.0.1', group: group, model: 'junos', output: 'git') - end - - before do - Proc.expects(:new).returns(credentials) - repo_head.expects(:name).twice.returns('refs/heads/master') - repo.expects(:head).twice.returns(repo_head) - repo.expects(:path).returns('/foo.git') - repo.expects(:fetch).with('origin', ['refs/heads/master'], credentials: credentials).returns(Hash.new(0)) - end - - describe 'when there is only one repository and no groups' do - before do - Oxidized.config.output.git.repo = '/foo.git' - remote.expects(:url).returns('https://github.com/username/foo.git') - remote.expects(:push).with(['refs/heads/master'], credentials: credentials).returns(true) - repo.expects(:remotes).returns('origin' => remote) - Rugged::Repository.expects(:new).with('/foo.git').returns(repo) - end - - it "will push to the remote repository using https" do - Oxidized.config.hooks.github_repo_hook.remote_repo = 'https://github.com/username/foo.git' - Oxidized.config.hooks.github_repo_hook.username = 'username' - Oxidized.config.hooks.github_repo_hook.password = 'password' - Proc.expects(:new).returns(credentials) - gr.cfg = Oxidized.config.hooks.github_repo_hook - gr.run_hook(ctx).must_equal true - end - - it "will push to the remote repository using ssh" do - Oxidized.config.hooks.github_repo_hook.remote_repo = 'git@github.com:username/foo.git' - Proc.expects(:new).returns(credentials) - gr.cfg = Oxidized.config.hooks.github_repo_hook - gr.run_hook(ctx).must_equal true - end - end - - describe "when there are groups" do - let(:group) { 'ggrroouupp' } - - before do - Proc.expects(:new).returns(credentials) - Rugged::Repository.expects(:new).with(repository).returns(repo) - - repo.expects(:remotes).twice.returns(remotes) - remotes.expects(:[]).with('origin').returns(nil) - remotes.expects(:create).with('origin', create_remote).returns(remote) - remote.expects(:url).returns('url') - remote.expects(:push).with(['refs/heads/master'], credentials: credentials).returns(true) - end - - describe 'and there are several repositories' do - let(:create_remote) { 'ggrroouupp#remote_repo' } - let(:repository) { '/ggrroouupp.git' } - - before do - Oxidized.config.output.git.repo.ggrroouupp = repository - Oxidized.config.hooks.github_repo_hook.remote_repo.ggrroouupp = 'ggrroouupp#remote_repo' - end - - it 'will push to the node group repository' do - gr.cfg = Oxidized.config.hooks.github_repo_hook - gr.run_hook(ctx).must_equal true - end - end - - describe 'and has a single repository' do - let(:create_remote) { 'github_repo_hook#remote_repo' } - let(:repository) { '/foo.git' } - - before do - Oxidized.config.output.git.repo = repository - Oxidized.config.hooks.github_repo_hook.remote_repo = 'github_repo_hook#remote_repo' - Oxidized.config.output.git.single_repo = true - end - - it 'will push to the correct repository' do - gr.cfg = Oxidized.config.hooks.github_repo_hook - gr.run_hook(ctx).must_equal true - end - end - end - end -end diff --git a/spec/hook/githubrepo_spec.rb b/spec/hook/githubrepo_spec.rb new file mode 100644 index 0000000..67c99f0 --- /dev/null +++ b/spec/hook/githubrepo_spec.rb @@ -0,0 +1,170 @@ +require 'spec_helper' +require 'rugged' +require 'oxidized/hook/githubrepo' + +describe GithubRepo do + let(:credentials) { mock } + let(:remote) { mock } + let(:remotes) { mock } + let(:repo_head) { mock } + let(:repo) { mock } + let(:gr) { GithubRepo.new } + + before do + Oxidized.asetus = Asetus.new + Oxidized.config.log = '/dev/null' + Oxidized.setup_logger + Oxidized.config.output.default = 'git' + end + + describe '#validate_cfg!' do + before do + gr.expects(:respond_to?).with(:validate_cfg!).returns(false) # `cfg=` call + end + + it 'raise a error when `remote_repo` is not configured' do + Oxidized.config.hooks.github_repo_hook = { type: 'githubrepo' } + gr.cfg = Oxidized.config.hooks.github_repo_hook + proc { gr.validate_cfg! }.must_raise(KeyError) + end + end + + describe "#fetch_and_merge_remote" do + before(:each) do + Oxidized.config.hooks.github_repo_hook.remote_repo = 'git@github.com:username/foo.git' + Proc.expects(:new).returns(credentials) + repo_head.expects(:name).returns('refs/heads/master') + gr.cfg = Oxidized.config.hooks.github_repo_hook + end + + it "should not try to merge when there is no update in remote branch" do + repo.expects(:fetch).with('origin', ['refs/heads/master'], credentials: credentials).returns(Hash.new(0)) + repo.expects(:branches).never + repo.expects(:head).returns(repo_head) + gr.fetch_and_merge_remote(repo).must_equal nil + end + describe "when there is update considering conflicts" do + let(:merge_index) { mock } + let(:their_branch) { mock } + + before(:each) do + repo.expects(:fetch).with('origin', ['refs/heads/master'], credentials: credentials).returns(total_deltas: 1) + their_branch.expects(:target_id).returns(1) + repo_head.expects(:target_id).returns(2) + repo.expects(:merge_commits).with(2, 1).returns(merge_index) + repo.expects(:branches).returns("origin/master" => their_branch) + end + + it "should not try merging when there's conflict" do + repo.expects(:head).twice.returns(repo_head) + their_branch.expects(:name).returns("origin/master") + merge_index.expects(:conflicts?).returns(true) + Rugged::Commit.expects(:create).never + gr.fetch_and_merge_remote(repo).must_equal nil + end + + it "should merge when there is no conflict" do + repo.expects(:head).times(3).returns(repo_head) + their_branch.expects(:target).returns("their_target") + their_branch.expects(:name).twice.returns("origin/master") + repo_head.expects(:target).returns("our_target") + merge_index.expects(:write_tree).with(repo).returns("tree") + merge_index.expects(:conflicts?).returns(false) + Rugged::Commit.expects(:create).with(repo, + parents: ["our_target", "their_target"], + tree: "tree", + message: "Merge remote-tracking branch 'origin/master'", + update_ref: "HEAD").returns(1) + gr.fetch_and_merge_remote(repo).must_equal 1 + end + end + end + + describe "#run_hook" do + let(:group) { nil } + let(:ctx) { OpenStruct.new(node: node) } + let(:node) do + Oxidized::Node.new(ip: '127.0.0.1', group: group, model: 'junos', output: 'git') + end + + before do + Proc.expects(:new).returns(credentials) + repo_head.expects(:name).twice.returns('refs/heads/master') + repo.expects(:head).twice.returns(repo_head) + repo.expects(:path).returns('/foo.git') + repo.expects(:fetch).with('origin', ['refs/heads/master'], credentials: credentials).returns(Hash.new(0)) + end + + describe 'when there is only one repository and no groups' do + before do + Oxidized.config.output.git.repo = '/foo.git' + remote.expects(:url).returns('https://github.com/username/foo.git') + remote.expects(:push).with(['refs/heads/master'], credentials: credentials).returns(true) + repo.expects(:remotes).returns('origin' => remote) + Rugged::Repository.expects(:new).with('/foo.git').returns(repo) + end + + it "will push to the remote repository using https" do + Oxidized.config.hooks.github_repo_hook.remote_repo = 'https://github.com/username/foo.git' + Oxidized.config.hooks.github_repo_hook.username = 'username' + Oxidized.config.hooks.github_repo_hook.password = 'password' + Proc.expects(:new).returns(credentials) + gr.cfg = Oxidized.config.hooks.github_repo_hook + gr.run_hook(ctx).must_equal true + end + + it "will push to the remote repository using ssh" do + Oxidized.config.hooks.github_repo_hook.remote_repo = 'git@github.com:username/foo.git' + Proc.expects(:new).returns(credentials) + gr.cfg = Oxidized.config.hooks.github_repo_hook + gr.run_hook(ctx).must_equal true + end + end + + describe "when there are groups" do + let(:group) { 'ggrroouupp' } + + before do + Proc.expects(:new).returns(credentials) + Rugged::Repository.expects(:new).with(repository).returns(repo) + + repo.expects(:remotes).twice.returns(remotes) + remotes.expects(:[]).with('origin').returns(nil) + remotes.expects(:create).with('origin', create_remote).returns(remote) + remote.expects(:url).returns('url') + remote.expects(:push).with(['refs/heads/master'], credentials: credentials).returns(true) + end + + describe 'and there are several repositories' do + let(:create_remote) { 'ggrroouupp#remote_repo' } + let(:repository) { '/ggrroouupp.git' } + + before do + Oxidized.config.output.git.repo.ggrroouupp = repository + Oxidized.config.hooks.github_repo_hook.remote_repo.ggrroouupp = 'ggrroouupp#remote_repo' + end + + it 'will push to the node group repository' do + gr.cfg = Oxidized.config.hooks.github_repo_hook + gr.run_hook(ctx).must_equal true + end + end + + describe 'and has a single repository' do + let(:create_remote) { 'github_repo_hook#remote_repo' } + let(:repository) { '/foo.git' } + + before do + Oxidized.config.output.git.repo = repository + Oxidized.config.hooks.github_repo_hook.remote_repo = 'github_repo_hook#remote_repo' + Oxidized.config.output.git.single_repo = true + end + + it 'will push to the correct repository' do + gr.cfg = Oxidized.config.hooks.github_repo_hook + gr.run_hook(ctx).must_equal true + end + end + end + end +end -- cgit v1.2.1