Skip to content

Commit

Permalink
write properly default including its submenu (bnc#986005)
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Jul 12, 2016
1 parent 5536a24 commit c9e5381
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package/yast2-bootloader.spec
Expand Up @@ -28,7 +28,7 @@ BuildRequires: yast2 >= 3.1.176
BuildRequires: yast2-devtools >= 3.1.10
BuildRequires: yast2-ruby-bindings >= 1.0.0
BuildRequires: yast2-storage
BuildRequires: rubygem(%rb_default_ruby_abi:cfa_grub2) >= 0.4.1
BuildRequires: rubygem(%rb_default_ruby_abi:cfa_grub2) >= 0.5.0
BuildRequires: rubygem(%rb_default_ruby_abi:rspec)
BuildRequires: rubygem(%rb_default_ruby_abi:yast-rake)
PreReq: /bin/sed %fillup_prereq
Expand All @@ -41,7 +41,7 @@ Requires: yast2-packager >= 2.17.24
Requires: yast2-pkg-bindings >= 2.17.25
Requires: yast2-storage >= 2.18.18
# GRUB_DEFAULT attribute
Requires: rubygem(%rb_default_ruby_abi:cfa_grub2) >= 0.4.1
Requires: rubygem(%rb_default_ruby_abi:cfa_grub2) >= 0.5.0
# lenses are needed as cfa_grub2 depends only on augeas bindings, but also
# lenses are needed here
Requires: augeas-lenses
Expand Down
36 changes: 29 additions & 7 deletions src/lib/bootloader/sections.rb
Expand Up @@ -13,7 +13,8 @@ class Sections
# @param [CFA::Grub2::GrubCfg, nil] grub_cfg - loaded parsed grub cfg tree
# or nil if not available yet
def initialize(grub_cfg = nil)
@all = grub_cfg ? grub_cfg.sections : []
@data = grub_cfg ? grub_cfg.boot_entries : []
@all = @data.map { |e| e[:title] }
end

# @return [String] name of default section
Expand All @@ -22,12 +23,9 @@ def default

return @default = "" if Yast::Stage.initial

# Execute.on_target can return nil if call failed. It shows users error popup, but bootloader
# can continue with empty default section
saved = Yast::Execute.on_target("/usr/bin/grub2-editenv", "list", stdout: :capture) || ""
saved_line = saved.lines.grep(/saved_entry=/).first
default_path = read_default

@default = saved_line ? saved_line[/saved_entry=(.*)$/, 1] : all.first
@default = default_path ? path_to_title(default_path) : all.first
end

# Sets default section internally
Expand All @@ -44,7 +42,31 @@ def default=(value)
# writes default to system making it persistent
def write
return if default.empty?
Yast::Execute.on_target("/usr/sbin/grub2-set-default", default)

Yast::Execute.on_target("/usr/sbin/grub2-set-default", title_to_path(default))
end

private

def read_default
# Execute.on_target can return nil if call failed. It shows users error popup, but bootloader
# can continue with empty default section
saved = Yast::Execute.on_target("/usr/bin/grub2-editenv", "list", stdout: :capture) || ""
saved_line = saved.lines.grep(/saved_entry=/).first

saved_line ? saved_line[/saved_entry=(.*)$/, 1] : nil
end

def path_to_title(path)
entry = @data.find { |e| e[:path] == path }

entry ? entry[:title] : path
end

def title_to_path(title)
entry = @data.find { |e| e[:title] == title }

entry ? entry[:path] : title
end
end
end
6 changes: 5 additions & 1 deletion test/grub2_widgets_test.rb
Expand Up @@ -594,7 +594,11 @@ def stub_widget_value(id, value)

describe Bootloader::DefaultSectionWidget do
before do
grub_cfg = double(sections: ["openSUSE", "windows"])
sections = [
{ title: "openSUSE", path: "openSUSE" },
{ title: "windows", path: "windows" },
]
grub_cfg = double(boot_entries: sections)
assign_bootloader
sections = Bootloader::Sections.new(grub_cfg)
# fake section list
Expand Down
17 changes: 15 additions & 2 deletions test/sections_test.rb
Expand Up @@ -6,7 +6,11 @@

describe Bootloader::Sections do
subject do
grub_cfg = double("CFA::Grub2::GrubCfg", sections: ["linux", "windows"])
sections = [
{ title: "linux", path: "linux" },
{ title: "windows", path: "alien>windows" },
]
grub_cfg = double("CFA::Grub2::GrubCfg", boot_entries: sections)
Bootloader::Sections.new(grub_cfg)
end

Expand All @@ -20,7 +24,7 @@
it "gets name of default section stored in grub2" do
expect(Yast::Execute).to receive(:on_target)
.with("/usr/bin/grub2-editenv", "list", stdout: :capture)
.and_return("saved_entry=windows\nbla_bla=no\n")
.and_return("saved_entry=alien>windows\nbla_bla=no\n")

expect(subject.default).to eq "windows"
end
Expand Down Expand Up @@ -57,5 +61,14 @@

subject.write
end

it "converts default value to its path" do
subject.default = "windows"

expect(Yast::Execute).to receive(:on_target)
.with("/usr/sbin/grub2-set-default", "alien>windows")

subject.write
end
end
end

0 comments on commit c9e5381

Please sign in to comment.