Skip to content

Commit

Permalink
Merge pull request #207 from unhappychoice/spec/addSpecs
Browse files Browse the repository at this point in the history
Add specs
  • Loading branch information
unhappychoice committed Jul 25, 2020
2 parents bb4a586 + e07ae38 commit 3380ea2
Show file tree
Hide file tree
Showing 17 changed files with 611 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby: ['2.6.x', '2.5.x']
ruby: ['2.7.x', '2.6.x', '2.5.x']
steps:
- uses: actions/checkout@master
- name: Set up Ruby
Expand Down
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ Style/Documentation:

Layout/LineLength:
Max: 120

Metrics/BlockLength:
Exclude:
- 'spec/**/*'
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.6.3
3 changes: 1 addition & 2 deletions lib/circleci/cli/command/watch_command/build_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ def initialize(username, reponame, branch: nil, user: nil)
end

def update
@builds = (Response::Build.all(@username, @reponame) + @builds)
.uniq(&:build_number)
@builds = (Response::Build.all(@username, @reponame) + @builds).uniq(&:build_number)
end

def mark_as_shown(build_number)
Expand Down
19 changes: 12 additions & 7 deletions lib/circleci/cli/command/watch_command/build_watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class BuildWatcher
attr_reader :build

def initialize(build, verbose: false)
@client = Networking::CircleCIPusherClient.new.tap(&:connect)
@build = build
@verbose = verbose
@messages = Hash.new { |h, k| h[k] = [] }
Expand All @@ -19,30 +18,30 @@ def start
end

def stop(status)
@client.unsubscribe(@build.channel_name + '@0')
client.unsubscribe(@build.channel_name + '@0')
notify_stopped(status)
end

private

def bind_event_handling(channel) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
@client.bind_event_json(channel, 'newAction') do |json|
client.bind_event_json(channel, 'newAction') do |json|
if @verbose
print_bordered json['log']['name']
else
print json['log']['name']
end
end

@client.bind_event_json(channel, 'appendAction') do |json|
client.bind_event_json(channel, 'appendAction') do |json|
if @verbose
Thor::Shell::Basic.new.say(json['out']['message'], nil, false)
else
@messages[json['step']] << json['out']['message']
end
end

@client.bind_event_json(channel, 'updateAction') do |json|
client.bind_event_json(channel, 'updateAction') do |json|
next if @verbose

case json['log']['status']
Expand All @@ -56,8 +55,10 @@ def bind_event_handling(channel) # rubocop:disable Metrics/AbcSize, Metrics/Meth
end

def notify_started
title = "👀 Start watching #{@build.project_name} ##{@build.build_number}"
say Printer::BuildPrinter.header_for(@build, title)
say Printer::BuildPrinter.header_for(
@build,
"👀 Start watching #{@build.project_name} ##{@build.build_number}"
)
end

def notify_stopped(status)
Expand All @@ -72,6 +73,10 @@ def notify_stopped(status)
def print_bordered(text)
say Terminal::Table.new(rows: [[text]], style: { width: 120 }).to_s
end

def client
@client ||= Networking::CircleCIPusherClient.new.tap(&:connect)
end
end
end
end
Expand Down
32 changes: 13 additions & 19 deletions lib/circleci/cli/networking/pusher_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,39 @@ module Networking
class CircleCIPusherClient
def connect
PusherClient.logger.level = Logger::ERROR
@socket = PusherClient::Socket.new(app_key, pusher_options)
@socket.connect(true)
socket.connect(true)
end

def bind(channel, event)
@socket.subscribe(channel)
@socket[channel].bind(event) do |data|
yield data
end
socket.subscribe(channel)
socket[channel].bind(event) { |data| yield data }
end

def bind_event_json(channel, event)
bind(channel, event) do |data|
JSON.parse(data).each { |json| yield(json) }
end
bind(channel, event) { |data| JSON.parse(data).each { |json| yield(json) } }
end

def unsubscribe(channel)
@socket.unsubscribe(channel)
socket.unsubscribe(channel)
end

private

def app_key
'1cf6e0e755e419d2ac9a'
end

def pusher_options
{
def socket
@socket ||= PusherClient::Socket.new(
'1cf6e0e755e419d2ac9a',
secure: true,
auth_method: proc { |a, b| auth(a, b) },
logger: Logger.new('/dev/null')
}
)
end

def auth(socket_id, channel)
data = { socket_id: socket_id, channel_name: channel.name }
token = ENV['CIRCLE_CI_TOKEN'] || ask('Circle CI token ? :')
res = connection.post("/auth/pusher?circle-token=#{token}", data)
res = connection.post(
"/auth/pusher?circle-token=#{token}",
{ socket_id: socket_id, channel_name: channel.name }
)
JSON.parse(res.body)['auth']
end

Expand Down
2 changes: 1 addition & 1 deletion spec/circleci/cli/command/base_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'spec_helper'

describe CircleCI::CLI::Command::BaseCommand do # rubocop:disable Metrics/BlockLength
describe CircleCI::CLI::Command::BaseCommand do
describe '.reponame' do
subject { CircleCI::CLI::Command::BaseCommand.reponame }

Expand Down
2 changes: 1 addition & 1 deletion spec/circleci/cli/command/build_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'spec_helper'

describe CircleCI::CLI::Command::BuildCommand, type: :command do # rubocop:disable Metrics/BlockLength
describe CircleCI::CLI::Command::BuildCommand, type: :command do
shared_examples_for 'a command show build information' do
let(:expected_output) do
<<~EXPECTED
Expand Down
2 changes: 1 addition & 1 deletion spec/circleci/cli/command/builds_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'spec_helper'

describe CircleCI::CLI::Command::BuildsCommand, type: :command do # rubocop:disable Metrics/BlockLength
describe CircleCI::CLI::Command::BuildsCommand, type: :command do
shared_examples_for 'a command show builds information' do
let(:expected_output) do
<<~EXPECTED
Expand Down
2 changes: 1 addition & 1 deletion spec/circleci/cli/command/retry_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'spec_helper'

describe CircleCI::CLI::Command::RetryCommand, type: :command do # rubocop:disable Metrics/BlockLength
describe CircleCI::CLI::Command::RetryCommand, type: :command do
shared_examples_for 'a command retries build' do
it 'should retry build' do
allow(CircleCI::CLI::Command::RetryCommand).to receive(:say) {}
Expand Down
66 changes: 66 additions & 0 deletions spec/circleci/cli/command/watch_command/build_repository_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

require 'spec_helper'

describe CircleCI::CLI::Command::BuildRepository do
let(:build) do
CircleCI::CLI::Response::Build.new(
{
'username' => 'unhappychoice',
'reponame' => 'default_reponame_from_api',
'status' => 'running',
'build_num' => 1234,
'branch' => 'master',
'author_name' => 'unhappychoice',
'build_time_millis' => 23_900,
'start_time' => 100_000,
'steps' => [
{
'status' => 'success',
'actions' => [
{ 'type' => 'build', 'name' => 'Build', 'status' => 'success', 'run_time_millis' => 300 }
]
},
{
'status' => 'success',
'actions' => [
{ 'type' => 'test', 'name' => 'Test', 'status' => 'success', 'run_time_millis' => 22_200 }
]
}
]
}
)
end

before do
allow(CircleCI::CLI::Response::Build).to receive(:all) { [build] }
end

describe '#update' do
subject { CircleCI::CLI::Command::BuildRepository.new(build.username, build.reponame).update }

it { is_expected.to match_array [build] }
end

describe '#mark_as_shown' do
subject do
CircleCI::CLI::Command::BuildRepository.new(build.username, build.reponame).mark_as_shown(build.build_number)
end

it { is_expected.to match_array [build.build_number] }
end

describe '#builds_to_show' do
subject { CircleCI::CLI::Command::BuildRepository.new(build.username, build.reponame).builds_to_show }

it { is_expected.to match_array [build] }
end

describe '#build_for' do
subject do
CircleCI::CLI::Command::BuildRepository.new(build.username, build.reponame).build_for(build.build_number)
end

it { is_expected.to eq build }
end
end
Loading

0 comments on commit 3380ea2

Please sign in to comment.