New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
report warning for none existing targets #187
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codewise looks good to me. Just a np/suggestion :)
log.info('Saving default target...') | ||
Yast2::Systemd::Target.set_default(default_target) | ||
unless Yast2::Systemd::Target.find(self.default_target) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NP: matter of taste, I guess. I'd extract this to its own method, something like
def save
return true unless modified?
ensure_target
log.info("saving...")
Yast2::Systemd::Target.set_default(self.default_target)
end
def ensure_target
return if Yast2::Systemd::Target.find(self.default_target)
log.info("#{self.default_target} not found, using mode fallback instead")
Report.Warning(
_("...")
)
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some comments and a general question. Are we allowing to select targets that are not available in the system?
test/services_manager_target_test.rb
Outdated
end | ||
end | ||
|
||
context "when the default target has been changed" do | ||
context "when the default target has not been changed" do | ||
let(:target) { "multi-user" } | ||
|
||
it "returns true" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, fix text: "returns false"
test/services_manager_target_test.rb
Outdated
@@ -136,19 +154,19 @@ class Template < Struct.new( | |||
subject.default_target = target | |||
end | |||
|
|||
context "when the default target has not been changed" do | |||
context "when the default target has been changed" do | |||
let(:target) { "graphical" } | |||
|
|||
it "returns false" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, fix text: "returns true"
log.info('Saving default target...') | ||
Yast2::Systemd::Target.set_default(default_target) | ||
unless Yast2::Systemd::Target.find(self.default_target) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, don't use unless
for multiliner if
. Otherwise, rubocop complains with "Favor modifier unless usage when having a single-line body". Yes, maybe this package is not using rubocop at all, but even though we should follow the standard.
Yast2::Systemd::Target.set_default(default_target) | ||
unless Yast2::Systemd::Target.find(self.default_target) | ||
# TRANSLATORS: error popup, %s is the default target e.g. graphical | ||
Report.Warning(_("Cannot find default target '%s' which is not available," \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The warning sounds a bit weird to me. Why not something like:
Report.Warning(_("Target '%s' is not available. Using text mode as default target.") % default_target)
Note that self
is not required to get the default_target
value.
"using the text mode fallback.") % self.default_target) | ||
self.default_target = BaseTargets::MULTIUSER | ||
end | ||
Yast2::Systemd::Target.set_default(self.default_target) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, self
is not required:
Yast2::Systemd::Target.set_default(default_target)
|
||
it "saves the changes in the underlying system" do | ||
it "reports a warning and sets to multi-user" do | ||
expect(Yast2::Systemd::Target).to receive(:find).and_return(nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you are using this line to make the target unavailable. If so, I prefer to do it in a before block, that is:
expect(Yast2::Systemd::Target).to receive(:find).and_return(nil) | |
context "when default target is not available" do | |
let(:target) { "graphical" } | |
before do | |
allow(Yast2::Systemd::Target).to receive(:find).with(target).and_return(nil) | |
end | |
it "reports a warning" do | |
expect(Yast::Report).to receive(:Warning) | |
subject.save | |
end | |
it "sets 'multi-user' as default target" do | |
expect(Yast2::Systemd::Target).to receive(:set_default).with("multi-user") | |
subject.save | |
end | |
end | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That does not work because you are calling subject.save twice and after the first call you have already set the default_target to mutli-user. I must admit, my first try has also looked like that :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see. If this happens again in the future, maybe a solution is to reset the default_target inside the before
block.
|
# TRANSLATORS: error popup, %s is the default target e.g. graphical | ||
Report.Warning(_("Target '%s' is not available." \ | ||
" Using text mode as default target.") % default_target) | ||
self.default_target = BaseTargets::MULTIUSER |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The self is needed because ruby is taking default_target as a local variable here. That's why I have set the rest to self.... too in order to be consequent. I have no idea why ruby is so here. Just try it out in the testcase :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As commented in IRC, here it is needed because in the setter is not clear if you want to invoke the method or to define a new local variable (ruby stuff xD).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
❌ Public Jenkins job #23 failed |
✔️ Internal Jenkins job #22 successfully finished |
✔️ Public Jenkins job #24 successfully finished |
No description provided.