diff --git a/lib/differential.rb b/lib/differential.rb index a0a4993..e794b6c 100644 --- a/lib/differential.rb +++ b/lib/differential.rb @@ -1,33 +1,35 @@ # frozen_string_literal: true # # Copyright (C) 2016 Harald Sitter # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) version 3, or any # later version accepted by the membership of KDE e.V. (or its # successor approved by the membership of KDE e.V.), which shall # act as a proxy defined in Section 6 of version 3 of the license. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library. If not, see . require(File.expand_path('connection', File.dirname(__FILE__))) require(File.expand_path('representation', File.dirname(__FILE__))) module Conduit # A code review class Differential < Representation class << self def get(id, connection = Connection.new) - new(connection, connection.call('differential.query', ids: [id])[0]) + data = connection.call('differential.query', ids: [id]) + raise 'Empty response from Phabricator' if !data || data.empty? + new(connection, data[0]) end end end end diff --git a/test/plugin_test.rb b/test/plugin_test.rb index dab8fe7..e7cde56 100644 --- a/test/plugin_test.rb +++ b/test/plugin_test.rb @@ -1,124 +1,124 @@ # encoding: utf-8 require(File.expand_path('test_helper', File.dirname(__FILE__))) # Dud base class. We mocha this for functionality later. class Plugin class Config class StringValue def initialize(_, _ = {}) end end def self.register(_) end end def map(*args) end def bot end end require 'phabricator' class PluginTest < Test::Unit::TestCase # NB: mocha is stupid with the quotes and can't tell single from double! def setup config = mock('config') # Do not give an api_token as we need the environment to take over. config.stubs(:[]).with('phabricator.api_token').returns(nil) bot = mock('bot') bot.stubs(:config).returns(config) Plugin.any_instance.stubs(:bot).returns(bot) end def message_double channel = mock('message-channel') channel.stubs(:name).returns('#message-double-channel') mock('message').tap { |m| m.stubs(:channel).returns(channel) } end def test_get_task_unreplied message = message_double message.stubs(:message).returns('yolo T123 T456 meow T789') message.stubs(:replied?).returns(false) plugin = PhabricatorPlugin.new plugin.expects(:task).with(message, { :number => '123' }) plugin.expects(:task).with(message, { :number => '456' }) plugin.expects(:task).with(message, { :number => '789' }) plugin.unreplied(message) end def test_get_diff_unreplied message = message_double message.stubs(:message).returns('yolo D123 D456 meow D789') message.stubs(:replied?).returns(false) plugin = PhabricatorPlugin.new plugin.expects(:diff).with(message, { :number => '123' }) plugin.expects(:diff).with(message, { :number => '456' }) plugin.expects(:diff).with(message, { :number => '789' }) plugin.unreplied(message) end def test_task message = message_double message.expects(:reply).with('Task 2970 "aptly sftp publishing to files.kde" [Open,Normal] {Neon} https://phabricator.kde.org/T2970') VCR.use_cassette("#{self.class}/#{__method__}") do plugin = PhabricatorPlugin.new plugin.task(message, :number => 2970) end end def test_task_fail message = message_double message.expects(:notify).with('Task not found ¯\_(ツ)_/¯ ConduitError ERR_BAD_TASK: No such Maniphest task exists.') VCR.use_cassette(__method__) do plugin = PhabricatorPlugin.new plugin.task(message, :number => -1) end end def test_diff message = message_double message.expects(:reply).with('Diff 2300 "always load about-distro in ctor" [Closed] https://phabricator.kde.org/D2300') VCR.use_cassette("#{self.class}/#{__method__}") do plugin = PhabricatorPlugin.new plugin.diff(message, :number => 2300) end end def test_diff_fail message = message_double - message.expects(:notify).with('Diff not found ¯\_(ツ)_/¯ can\'t dup NilClass') + message.expects(:notify).with('Diff not found ¯\_(ツ)_/¯ Empty response from Phabricator') VCR.use_cassette(__method__) do plugin = PhabricatorPlugin.new plugin.diff(message, :number => -1) end end def test_skip message = message_double message.channel.stubs(:name).returns('#kde-bugs-activity') plugin = PhabricatorPlugin.new plugin.unreplied(message) end def test_task_with_no_projects message = message_double message.expects(:reply).with('Task 8149 "Rewritten Dragon player UI in Kirigami (QML)" [Open,Wishlist] {} https://phabricator.kde.org/T8149') VCR.use_cassette("#{self.class}/#{__method__}") do plugin = PhabricatorPlugin.new plugin.task(message, :number => 8149) end end end