Skip to content
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

Merged
merged 6 commits into from Feb 8, 2019
Merged

Conversation

schubi2
Copy link
Member

@schubi2 schubi2 commented Feb 8, 2019

No description provided.

@schubi2 schubi2 changed the title report error for none existing targets WIP report error for none existing targets Feb 8, 2019
@coveralls
Copy link

coveralls commented Feb 8, 2019

Coverage Status

Coverage increased (+0.1%) to 63.084% when pulling 43152a1 on master-bsc-1046083 into 1b9392b on master.

@schubi2 schubi2 changed the title WIP report error for none existing targets report warning for none existing targets Feb 8, 2019
@schubi2 schubi2 mentioned this pull request Feb 8, 2019
Copy link
Member

@dgdavid dgdavid left a 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)
Copy link
Member

@dgdavid dgdavid Feb 8, 2019

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

Copy link
Contributor

@joseivanlopez joseivanlopez left a 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?

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
Copy link
Contributor

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"

@@ -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
Copy link
Contributor

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)
Copy link
Contributor

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," \
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

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:

Suggested change
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

Copy link
Member Author

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 :-)

Copy link
Contributor

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.

@schubi2
Copy link
Member Author

schubi2 commented Feb 8, 2019

Some comments and a general question. Are we allowing to select targets that are not available in the system?
Yes, in AutoYaST

# 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
Copy link
Member Author

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 :-)

Copy link
Contributor

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).

Copy link
Contributor

@joseivanlopez joseivanlopez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@schubi2 schubi2 merged commit 967720e into master Feb 8, 2019
@schubi2 schubi2 deleted the master-bsc-1046083 branch February 8, 2019 14:05
@yast-bot
Copy link

yast-bot commented Feb 8, 2019

❌ Public Jenkins job #23 failed

@yast-bot
Copy link

yast-bot commented Feb 8, 2019

✔️ Internal Jenkins job #22 successfully finished
✔️ Created IBS submit request #183977

@yast-bot
Copy link

✔️ Public Jenkins job #24 successfully finished
✔️ Created OBS submit request #673922

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants