From 19711125b25e8865ff3472fddb7a9cb9c2c3a5e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imobach=20Gonz=C3=A1lez=20Sosa?= Date: Mon, 27 Jan 2025 17:28:48 +0000 Subject: [PATCH 1/2] fix(service): fix handling of scripts filenames on import --- service/lib/agama/autoyast/scripts_reader.rb | 28 +++++++++- .../agama/autoyast/scripts_reader_test.rb | 53 +++++++++++++++---- 2 files changed, 69 insertions(+), 12 deletions(-) diff --git a/service/lib/agama/autoyast/scripts_reader.rb b/service/lib/agama/autoyast/scripts_reader.rb index 78ddb8b55a..099a058517 100755 --- a/service/lib/agama/autoyast/scripts_reader.rb +++ b/service/lib/agama/autoyast/scripts_reader.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -# Copyright (c) [2024] SUSE LLC +# Copyright (c) [2024-2025] SUSE LLC # # All Rights Reserved. # @@ -20,6 +20,7 @@ # find current contact information at www.suse.com. require "yast" +Yast.import "URL" # :nodoc: module Agama @@ -50,6 +51,7 @@ def initialize(profile) # # @return [Hash] Agama "scripts" section def read + @index = 0 scripts = {} .merge(read_post_scripts) .merge(read_init_scripts) @@ -100,7 +102,7 @@ def read_init_scripts # @param section [Hash] AutoYaST script section def read_script(section) script = { - "name" => section["file_name"] + "name" => filename_for(section) } if section["location"] @@ -121,6 +123,28 @@ def read_post_script(section) read_script(section) .merge("chroot" => section.fetch("chrooted", false)) end + + # Extracts the name of the script + # + # If the "filename" attribute is defined, it is used. Otherwise, it tries + # to infer the name from the "location" attribute. If the "location" is + # not defined, it uses a generic name plus the index of the script. + # + # @param section [Hash] AutoYaST script definition + # @return [String] + def filename_for(section) + return section["filename"] if section["filename"] + + location = section["location"].to_s + if !location.empty? + url = Yast::URL.Parse(location) + path = File.basename(url["path"].to_s) + return path unless path.empty? || path == "/" + end + + @index += 1 + "script-#{@index}" + end end end end diff --git a/service/test/agama/autoyast/scripts_reader_test.rb b/service/test/agama/autoyast/scripts_reader_test.rb index e1c7497834..f23a1e89b1 100644 --- a/service/test/agama/autoyast/scripts_reader_test.rb +++ b/service/test/agama/autoyast/scripts_reader_test.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -# Copyright (c) [2024] SUSE LLC +# Copyright (c) [2024-2025] SUSE LLC # # All Rights Reserved. # @@ -32,21 +32,43 @@ context "when the script definition includes the sources" do let(:script) do - { "file_name" => "script.sh", - "location" => "https://example.com/script.sh" } + { "filename" => "script.sh", + "location" => "https://example.com/script.sh" } end it "sets the \"url\" to the \"location\"" do scripts = subject.read["scripts"][section] expect(scripts.first).to include("url" => "https://example.com/script.sh") end + + context "and the script filename is not specified" do + let(:script) do + { "location" => "https://example.com/script.sh" } + end + + it "uses the path as the filename" do + scripts = subject.read["scripts"][section] + expect(scripts.first).to include("name" => "script.sh") + end + + context "but there is no path in the URL" do + let(:script) do + { "location" => "https://example.com/" } + end + + it "uses the script type as the filename" do + scripts = subject.read["scripts"][section] + expect(scripts.first).to include("name" => "script-1") + end + end + end end context "when the script definition specifies a location" do let(:script) do { - "file_name" => "script.sh", - "source" => "#!/bin/bash\necho 'Hello World!'" + "filename" => "script.sh", + "source" => "#!/bin/bash\necho 'Hello World!'" } end @@ -54,6 +76,17 @@ scripts = subject.read["scripts"][section] expect(scripts.first).to include("body" => "#!/bin/bash\necho 'Hello World!'") end + + context "and the script filename is not specified" do + let(:script) do + { "source" => "#!/bin/bash\necho 'Hello World!'" } + end + + it "uses the script type as the filename" do + scripts = subject.read["scripts"][section] + expect(scripts.first).to include("name" => "script-1") + end + end end end @@ -77,9 +110,9 @@ it_behaves_like "a script reader", "chroot-scripts", "post" let(:chroot_script) do - { "file_name" => "test.sh", - "chrooted" => true, - "source" => "#!/bin/bash\necho 'Hello World!'" } + { "filename" => "test.sh", + "chrooted" => true, + "source" => "#!/bin/bash\necho 'Hello World!'" } end let(:profile) do @@ -92,8 +125,8 @@ context "when the \"chrooted\" option is not set" do let(:chroot_script) do - { "file_name" => "test.sh", - "source" => "#!/bin/bash\necho 'Hello World!'" } + { "filename" => "test.sh", + "source" => "#!/bin/bash\necho 'Hello World!'" } end it "sets the \"chroot\" option to false" do From 2d86bb9e01af78221bc3e83cdeceba91388a3961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imobach=20Gonz=C3=A1lez=20Sosa?= Date: Mon, 27 Jan 2025 17:42:01 +0000 Subject: [PATCH 2/2] docs(service): update the changes file --- service/package/rubygem-agama-yast.changes | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/service/package/rubygem-agama-yast.changes b/service/package/rubygem-agama-yast.changes index 20c4d68ee7..cab68f4a2a 100644 --- a/service/package/rubygem-agama-yast.changes +++ b/service/package/rubygem-agama-yast.changes @@ -1,3 +1,9 @@ +------------------------------------------------------------------- +Mon Jan 27 17:38:53 UTC 2025 - Imobach Gonzalez Sosa + +- Fix handling of script names from AutoYaST profiles + (gh#agama-project/agama#1903). + ------------------------------------------------------------------- Fri Jan 24 09:33:27 UTC 2025 - Imobach Gonzalez Sosa