From 0c2678db956b981c3552eaedbcd16d968f38f043 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imobach=20Gonz=C3=A1lez=20Sosa?= Date: Wed, 22 Jan 2025 11:32:52 +0000 Subject: [PATCH] feat(ruby): implement the ScriptProblem callback --- service/lib/agama/software/callbacks.rb | 1 + .../lib/agama/software/callbacks/script.rb | 81 +++++++++++++++++++ .../agama/software/callbacks/script_test.rb | 74 +++++++++++++++++ 3 files changed, 156 insertions(+) create mode 100644 service/lib/agama/software/callbacks/script.rb create mode 100644 service/test/agama/software/callbacks/script_test.rb diff --git a/service/lib/agama/software/callbacks.rb b/service/lib/agama/software/callbacks.rb index 7e43a75be..55c86112d 100644 --- a/service/lib/agama/software/callbacks.rb +++ b/service/lib/agama/software/callbacks.rb @@ -30,5 +30,6 @@ module Callbacks require "agama/software/callbacks/media" require "agama/software/callbacks/progress" require "agama/software/callbacks/provide" +require "agama/software/callbacks/script" require "agama/software/callbacks/signature" require "agama/software/callbacks/source" diff --git a/service/lib/agama/software/callbacks/script.rb b/service/lib/agama/software/callbacks/script.rb new file mode 100644 index 000000000..442c1ab69 --- /dev/null +++ b/service/lib/agama/software/callbacks/script.rb @@ -0,0 +1,81 @@ +# frozen_string_literal: true + +# Copyright (c) [2025] SUSE LLC +# +# All Rights Reserved. +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of version 2 of the GNU General Public License as published +# by the Free Software Foundation. +# +# This program 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 General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, contact SUSE LLC. +# +# To contact SUSE LLC about this file by physical or electronic mail, you may +# find current contact information at www.suse.com. + +require "yast" +require "agama/question" + +Yast.import "Pkg" + +module Agama + module Software + module Callbacks + # Script callbacks + class Script + include Yast::I18n + + # Constructor + # + # @param questions_client [Agama::DBus::Clients::Questions] + # @param logger [Logger] + def initialize(questions_client, logger) + @questions_client = questions_client + @logger = logger + end + + # Register the callbacks + def setup + Pkg.CallbackScriptProblem( + fun_ref(method(:ScriptProblem), "string (string)") + ) + end + + # DoneProvide callback + # + # @param description [String] Problem description + # @return [String] "I" for ignore, "R" for retry and "C" for abort (not implemented) + # @see https://github.com/yast/yast-yast2/blob/19180445ab935a25edd4ae0243aa7a3bcd09c9de/library/packages/src/modules/PackageCallbacks.rb#L620 + def script_problem(description) + logger.debug "ScriptProblem callback: description: #{description}" + + message = _("There was a problem running a package script.") + question = Agama::Question.new( + qclass: "software.script_problem", + text: message, + options: [:Retry, :Ignore], + default_option: :Retry, + data: { "details" => description } + ) + questions_client.ask(question) do |question_client| + (question_client.answer == :Retry) ? "R" : "I" + end + end + + private + + # @return [Agama::DBus::Clients::Questions] + attr_reader :questions_client + + # @return [Logger] + attr_reader :logger + end + end + end +end diff --git a/service/test/agama/software/callbacks/script_test.rb b/service/test/agama/software/callbacks/script_test.rb new file mode 100644 index 000000000..95a596820 --- /dev/null +++ b/service/test/agama/software/callbacks/script_test.rb @@ -0,0 +1,74 @@ +# frozen_string_literal: true + +# Copyright (c) [2022-2023] SUSE LLC +# +# All Rights Reserved. +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of version 2 of the GNU General Public License as published +# by the Free Software Foundation. +# +# This program 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 General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, contact SUSE LLC. +# +# To contact SUSE LLC about this file by physical or electronic mail, you may +# find current contact information at www.suse.com. + +require_relative "../../../test_helper" +require "agama/software/callbacks/script" +require "agama/dbus/clients/questions" +require "agama/dbus/clients/question" + +describe Agama::Software::Callbacks::Script do + subject { described_class.new(questions_client, logger) } + + let(:questions_client) { instance_double(Agama::DBus::Clients::Questions) } + + let(:logger) { Logger.new($stdout, level: :warn) } + + let(:answer) { :Retry } + + let(:description) { "Some description" } + + describe "#script_problem" do + before do + allow(questions_client).to receive(:ask).and_yield(question_client) + allow(question_client).to receive(:answer).and_return(answer) + end + + let(:question_client) { instance_double(Agama::DBus::Clients::Question) } + + it "registers a question with the details" do + expect(questions_client).to receive(:ask) do |q| + expect(q.text).to include("running a package script") + expect(q.data).to include( + "details" => description + ) + end + subject.script_problem(description) + end + + context "when the user answers :Retry" do + let(:answer) { :Retry } + + it "returns 'R'" do + ret = subject.script_problem(description) + expect(ret).to eq("R") + end + end + + context "when the user answers :Ignore" do + let(:answer) { :Ignore } + + it "returns 'I'" do + ret = subject.script_problem(description) + expect(ret).to eq("I") + end + end + end +end