From 134745e461229647c8d403fa13909869f53c2eca Mon Sep 17 00:00:00 2001 From: Lukas Ocilka Date: Mon, 1 Jun 2015 15:03:13 +0200 Subject: [PATCH 1/5] Tests for new value_for method --- library/general/test/linuxrc_test.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/library/general/test/linuxrc_test.rb b/library/general/test/linuxrc_test.rb index 1c00f544b..a982e9c21 100755 --- a/library/general/test/linuxrc_test.rb +++ b/library/general/test/linuxrc_test.rb @@ -187,4 +187,30 @@ def load_install_inf(defaults_replacement = {}) expect(subject.keys.sort).to eq(DEFAULT_INSTALL_INF.keys.sort) end end + + describe "#value_for" do + context "when key is defined in install.inf (Linuxrc commandline)" do + it "returns value for given key" do + load_install_inf({ + "test_1" => "123", + "T-E-S-T-2" => "456", + "TeSt3" => "678", + "Cmdline" => "test4=890 test5=10,11,12", + }) + + expect(subject.value_for("test_1")).to eq("123") + expect(subject.value_for("TEsT2")).to eq("456") + expect(subject.value_for("T_e_St_3")).to eq("678") + expect(subject.value_for("T.e.s.t-4")).to eq("890") + expect(subject.value_for("test5")).to eq("10,11,12") + end + end + + context "when key is not defined in install.inf (Linuxrc commandline)" do + it "returns nil" do + load_install_inf + expect(subject.value_for("this-key-is-not-defined")).to eq(nil) + end + end + end end From 5de73543ef912a20ebd047da4c4d77c50731c436 Mon Sep 17 00:00:00 2001 From: Lukas Ocilka Date: Mon, 1 Jun 2015 16:22:24 +0200 Subject: [PATCH 2/5] Implementation of Linuxrc.value_for(string) - Returns value for Linxurc feature/command written on commandline - Matches all keys the same way as Linuxrc all -_. characters are igroned and match is case-insensitive --- library/general/src/modules/Linuxrc.rb | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/library/general/src/modules/Linuxrc.rb b/library/general/src/modules/Linuxrc.rb index 095162ccc..375cef4c6 100644 --- a/library/general/src/modules/Linuxrc.rb +++ b/library/general/src/modules/Linuxrc.rb @@ -211,6 +211,31 @@ def SaveInstallInf(root) true end + # Returns value of a given Linxurc key/feature defined on commandline + # and written into install.inf + # + # @param [String] key + # @return [String] value of a given key + def value_for(feature_key) + ReadInstallInf() + + feature_key = polish(feature_key) + install_inf_features = deep_copy(@install_inf) + + # Some values written on Linuxrc commandline are not know to Linuxrc + # Unless they are also mentioned as `PTOptions`, they will appear as "Cmdline" entry + cmdline_values = install_inf_features.fetch("Cmdline", "").split.map do |cmdline_entry| + key_val = cmdline_entry.split("=") + install_inf_features[key_val[0]] = key_val[1] + end + + matching_keys = install_inf_features.keys.select{|key| polish(key) == feature_key} + return nil if matching_keys.empty? + + # The last key wins as in Linuxrc + matching_keys.map{|key| install_inf_features[key]}.last + end + publish function: :ResetInstallInf, type: "void ()" publish function: :InstallInf, type: "string (string)" publish function: :manual, type: "boolean ()" @@ -224,6 +249,18 @@ def SaveInstallInf(root) publish function: :WriteYaSTInf, type: "void (map )" publish function: :SaveInstallInf, type: "boolean (string)" publish function: :keys, type: "list ()" + publish function: :value_for, type: "string (string)" + + private + + # Removes characters ignored by Linuxrc and turns all to downcase + # + # @param [String] + # @return [String] + def polish(key) + key.downcase.tr("-_\.", "") + end + end Linuxrc = LinuxrcClass.new From 9b2c09f41a0f89679b9806ca995c65d155531058 Mon Sep 17 00:00:00 2001 From: Lukas Ocilka Date: Mon, 1 Jun 2015 16:25:13 +0200 Subject: [PATCH 3/5] Changes and version --- package/yast2.changes | 6 ++++++ package/yast2.spec | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/package/yast2.changes b/package/yast2.changes index 237690fe2..2121f5170 100644 --- a/package/yast2.changes +++ b/package/yast2.changes @@ -1,3 +1,9 @@ +------------------------------------------------------------------- +Mon Jun 1 16:23:37 CEST 2015 - locilka@suse.com + +- Added Linuxrc.value_for (fate#317973) +- 3.1.127 + ------------------------------------------------------------------- Wed May 27 14:36:47 UTC 2015 - jreidinger@suse.com diff --git a/package/yast2.spec b/package/yast2.spec index 5a1cf0d9b..f594e8e36 100644 --- a/package/yast2.spec +++ b/package/yast2.spec @@ -17,7 +17,7 @@ Name: yast2 -Version: 3.1.126 +Version: 3.1.127 Release: 0 URL: https://github.com/yast/yast-yast2 From ff211d56e95f4f3d9a009e02409348dea999d85c Mon Sep 17 00:00:00 2001 From: Lukas Ocilka Date: Mon, 1 Jun 2015 17:01:33 +0200 Subject: [PATCH 4/5] "Perfecting" the code "thanks" to RuboCop --- library/general/src/modules/Linuxrc.rb | 9 ++++----- library/general/test/linuxrc_test.rb | 10 +++++----- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/library/general/src/modules/Linuxrc.rb b/library/general/src/modules/Linuxrc.rb index 375cef4c6..c4713e48a 100644 --- a/library/general/src/modules/Linuxrc.rb +++ b/library/general/src/modules/Linuxrc.rb @@ -222,18 +222,18 @@ def value_for(feature_key) feature_key = polish(feature_key) install_inf_features = deep_copy(@install_inf) - # Some values written on Linuxrc commandline are not know to Linuxrc + # Some values written on Linuxrc commandline are not known to Linuxrc # Unless they are also mentioned as `PTOptions`, they will appear as "Cmdline" entry - cmdline_values = install_inf_features.fetch("Cmdline", "").split.map do |cmdline_entry| + install_inf_features.fetch("Cmdline", "").split.map do |cmdline_entry| key_val = cmdline_entry.split("=") install_inf_features[key_val[0]] = key_val[1] end - matching_keys = install_inf_features.keys.select{|key| polish(key) == feature_key} + matching_keys = install_inf_features.keys.select { |key| polish(key) == feature_key } return nil if matching_keys.empty? # The last key wins as in Linuxrc - matching_keys.map{|key| install_inf_features[key]}.last + matching_keys.map { |key| install_inf_features[key] }.last end publish function: :ResetInstallInf, type: "void ()" @@ -260,7 +260,6 @@ def value_for(feature_key) def polish(key) key.downcase.tr("-_\.", "") end - end Linuxrc = LinuxrcClass.new diff --git a/library/general/test/linuxrc_test.rb b/library/general/test/linuxrc_test.rb index a982e9c21..e334544ca 100755 --- a/library/general/test/linuxrc_test.rb +++ b/library/general/test/linuxrc_test.rb @@ -191,12 +191,12 @@ def load_install_inf(defaults_replacement = {}) describe "#value_for" do context "when key is defined in install.inf (Linuxrc commandline)" do it "returns value for given key" do - load_install_inf({ - "test_1" => "123", + load_install_inf( + "test_1" => "123", "T-E-S-T-2" => "456", - "TeSt3" => "678", - "Cmdline" => "test4=890 test5=10,11,12", - }) + "TeSt3" => "678", + "Cmdline" => "test4=890 test5=10,11,12" + ) expect(subject.value_for("test_1")).to eq("123") expect(subject.value_for("TEsT2")).to eq("456") From 4278f18acf8629e92f4a5b5a9ae67bd9e832c4d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ladislav=20Slez=C3=A1k?= Date: Mon, 1 Jun 2015 20:18:19 +0200 Subject: [PATCH 5/5] improve Linuxrc.value_for added more tests --- library/general/src/modules/Linuxrc.rb | 26 ++++++++++++-------------- library/general/test/linuxrc_test.rb | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/library/general/src/modules/Linuxrc.rb b/library/general/src/modules/Linuxrc.rb index c4713e48a..f4ff17fd2 100644 --- a/library/general/src/modules/Linuxrc.rb +++ b/library/general/src/modules/Linuxrc.rb @@ -215,25 +215,23 @@ def SaveInstallInf(root) # and written into install.inf # # @param [String] key - # @return [String] value of a given key + # @return [String, nil] value of a given key or `nil` if not found def value_for(feature_key) ReadInstallInf() - feature_key = polish(feature_key) - install_inf_features = deep_copy(@install_inf) - # Some values written on Linuxrc commandline are not known to Linuxrc - # Unless they are also mentioned as `PTOptions`, they will appear as "Cmdline" entry - install_inf_features.fetch("Cmdline", "").split.map do |cmdline_entry| - key_val = cmdline_entry.split("=") - install_inf_features[key_val[0]] = key_val[1] - end + # at first check the keys in install.inf + install_inf_key, install_inf_val = @install_inf.find { |k, _v| polish(k) == feature_key } + return install_inf_val if install_inf_key - matching_keys = install_inf_features.keys.select { |key| polish(key) == feature_key } - return nil if matching_keys.empty? + # then check the command line + ret = nil + @install_inf.fetch("Cmdline", "").split.each do |cmdline_entry| + key, val = cmdline_entry.split("=", 2) + ret = val if polish(key) == feature_key + end - # The last key wins as in Linuxrc - matching_keys.map { |key| install_inf_features[key] }.last + ret end publish function: :ResetInstallInf, type: "void ()" @@ -258,7 +256,7 @@ def value_for(feature_key) # @param [String] # @return [String] def polish(key) - key.downcase.tr("-_\.", "") + key.downcase.tr("-_\\.", "") end end diff --git a/library/general/test/linuxrc_test.rb b/library/general/test/linuxrc_test.rb index e334544ca..c1b082ecb 100755 --- a/library/general/test/linuxrc_test.rb +++ b/library/general/test/linuxrc_test.rb @@ -204,6 +204,23 @@ def load_install_inf(defaults_replacement = {}) expect(subject.value_for("T.e.s.t-4")).to eq("890") expect(subject.value_for("test5")).to eq("10,11,12") end + + it "parses commandline with '=' in the value" do + url = "http://example.com?bar=42" + load_install_inf( + "Cmdline" => "test6=#{url}" + ) + + expect(subject.value_for("test_6")).to eq(url) + end + + it "returns the last matching value from command line" do + load_install_inf( + "Cmdline" => "test7=foo test.7=bar test__7=baz" + ) + + expect(subject.value_for("test_7")).to eq("baz") + end end context "when key is not defined in install.inf (Linuxrc commandline)" do