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
[WIP] refactoring autoconverted ruby code into ruby code #40
Conversation
Sorry, in the past we have seen that such refactoring often leads to bugs. So it would be good to mention this in package/*.changes. It appears you have user RuboCop for this. Why not commit also the configuration file? |
sure, i used a rubocop configuration used by yast. I will attach it. |
rubocop_yast_style.yml
Outdated
|
||
# do not force %r | ||
Style/RegexpLiteral: | ||
Enabled: false |
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.
thanks for you ruby style guide, but better to reference it. See how other modules do it: e.g. https://github.com/yast/yast-bootloader/blob/master/.rubocop.yml#L3
src/clients/security.rb
Outdated
current = if levels.empty? | ||
:custom | ||
else | ||
levels.last # mimic the old implementation |
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.
this is in fact wrong, as levels contain pair of [key, level]
and old one use only key.
so how code can look like:
current = @Levels.select{ |_key, level| level == Security.Settings }.keys.last || :custom
src/clients/security.rb
Outdated
end | ||
false | ||
Security.modified |
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.
this in fact is not same as previous code as it can return true, when something else set Security.modified to true and here it is not set
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.
and why we return always false? there is a reason for 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.
I expect that logic is that we return false to indicate that this action do not change anything.
Security.Settings, | ||
"PASSWD_REMEMBER_HISTORY", | ||
Ops.get_string(options, "remember", "0") | ||
) |
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.
this looks like removed without replacemend.
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 think i covered the case in my change, no?
As I see, maybe the problem is that I'm converting the options["remember"] to int?
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.
yes, this code uses string, not sure how hard it is
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 changed it already to use strings. I think there is no problem, but consistence is good.
src/clients/security_auto.rb
Outdated
@param = Convert.to_map(WFM.Args(1)) | ||
if !Yast::WFM.Args.empty? | ||
@func = Yast::WFM.Args[0] | ||
if Yast::WFM.Args.length > 1 && Yast::WFM.Args[1].is_a?(Hash) |
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.
in fact second option is enough, because if length is 1 or less, then Yast::WFM.Args[1]
returns nil and nil.is_a?(Hash)
returns false
src/clients/security_auto.rb
Outdated
@@ -90,37 +90,37 @@ def main | |||
end | |||
@ret = Security.Import( | |||
Map.KeysToUpper( | |||
Convert.convert(@param, :from => "map", :to => "map <string, any>") | |||
Convert.convert(@param, from: "map", to: "map <string, any>") |
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.
in fact whole this convert can be dropeed as implementation do not distinct between map and specialized map.
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.
so simple @ret = Security.Import(Map.KeysToUpper(@param))
can be used
src/clients/security_auto.rb
Outdated
:from => "map", | ||
:to => "map <string, any>" | ||
from: "map", | ||
to: "map <string, any>" |
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.
same here
src/clients/security_auto.rb
Outdated
@@ -131,8 +131,8 @@ def main | |||
Builtins.y2milestone("Security auto finished") | |||
Builtins.y2milestone("----------------------------------------") | |||
|
|||
deep_copy(@ret) | |||
|
|||
# is it needed? |
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.
no, it is not needed as @ret is always assigned and from fresh copy.
src/include/security/dialogs.rb
Outdated
@@ -354,7 +352,7 @@ def DisplayHelpPopup(help_id) | |||
if help_id == "MANDATORY_SERVICES" | |||
missing = Security.MissingMandatoryServices | |||
|
|||
if missing != nil && missing != [] | |||
if !missing.nil? && missing != [] |
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.
in fact simple if missing && !missing.empty?
can be used.
src/include/security/dialogs.rb
Outdated
@@ -374,7 +371,7 @@ def DisplayHelpPopup(help_id) | |||
elsif help_id == "EXTRA_SERVICES" | |||
extra = Security.ExtraServices | |||
|
|||
if extra != nil && extra != [] | |||
if !extra.nil? && extra != [] |
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.
same here
src/include/security/dialogs.rb
Outdated
@@ -384,7 +381,7 @@ def DisplayHelpPopup(help_id) | |||
end | |||
end | |||
|
|||
if help != nil && help != "" | |||
if !help.nil? && help != "" |
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.
and here
src/include/security/routines.rb
Outdated
@@ -89,7 +89,7 @@ def settings2widget(_ID) | |||
# "Widget" == "IntField" | |||
if widget == "IntField" | |||
intval = Builtins.tointeger(value) | |||
intval = 0 if intval == nil | |||
intval = 0 if intval.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.
intval ||= 0
is simplier
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.
proposed by the rubycop :)
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 know, as my proposal won't work if intval will be boolean, but it is not, so it is safer and nicer.
src/include/security/routines.rb
Outdated
@@ -112,15 +112,15 @@ def settings2widget(_ID) | |||
# string|list it | |||
Builtins.y2debug("li=%1 (%2)", li, i) | |||
it = Ops.get(li, i) | |||
it = "" if it == nil | |||
it = "" if it.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.
it ||= ""
src/include/security/routines.rb
Outdated
else | ||
new = "no" | ||
end | ||
new = if ret == true |
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.
new = ret ? "yes" : "no"
src/modules/Security.rb
Outdated
@@ -268,9 +262,9 @@ def PollAbort | |||
end | |||
|
|||
# Abort function | |||
# @return blah blah lahjk | |||
# @return false |
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.
this is in fact not true, as first statement is return. it should be return true if operation should be aborted
src/modules/Security.rb
Outdated
def Abort | ||
return Builtins.eval(@AbortFunction) == true if @AbortFunction != nil | ||
return Builtins.eval(@AbortFunction) == true if !@AbortFunction.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.
return Builtins.eval(@AbortFunction) == true if @AbortFunction
Integer(val) | ||
rescue | ||
nil | ||
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.
why is this better then before?
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.
proposed by the rubycop :)
Integer(@Settings.fetch("kernel.sysrq", "0")) | ||
rescue | ||
nil | ||
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.
same here
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.
proposed by the rubycop :)
src/modules/Security.rb
Outdated
@@ -731,35 +733,30 @@ def Import(settings) | |||
# (For use by autoinstallation.) | |||
# @return [Hash] Dumped settings (later acceptable by Import ()) | |||
def Export | |||
Builtins.eval(@Settings) | |||
@Settings if @Settings.is_a?(Hash) |
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.
well, here is bug, that eval do copy. Also check for hash is not needed, so it should look like
deep_copy(@Settings)
src/modules/Security.rb
Outdated
Builtins.foreach(@do_not_test) do |key| | ||
settings = Builtins.remove(settings, key) | ||
end | ||
@do_not_test.each { |k| settings.delete k } |
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.
what about settings = settings - @do_not_test
?
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.
or even better settings -= @do_not_test
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.
hm.. settings is a hash, no? Plain hashes don't have the "-" method implemented, I'm not sure if I can do it.
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.
ok, if it is Hash, then delete is right approach
src/modules/Security.rb
Outdated
_("Current Security Level: %1"), | ||
Ops.get(@LevelsNames, Convert.to_string(current), "") | ||
) | ||
summary = _("Current Security Level: #{@LevelsNames[current]}") |
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.
no, this won't work, as _
method get extrapolated string, which is wrong. It have to look in ruby like
summary = _("Current Security Level: %s") % @LevelsNames[current]
src/clients/security_auto.rb
Outdated
|
||
# is it needed? | ||
deep_copy(@ret) | ||
# EOF |
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.
this is in fact wrong as you kill return value. I just said that deep_copy is not needed, so still you need to have here @ret
otherwise it return nil, causing bugs.
src/include/security/routines.rb
Outdated
@@ -112,7 +112,7 @@ def settings2widget(_ID) | |||
# string|list it | |||
Builtins.y2debug("li=%1 (%2)", li, i) | |||
it = Ops.get(li, i) | |||
it = "" if it.nil? | |||
it = ||= "" |
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.
this is wrong, it should be it ||=
I fixed almost all issues. Rubocop has just three complains related with useless assignment. Could you please evaluate them?
|
@vpereira OK, I evaluated them and result is:
|
Security.modified = true | ||
return true |
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.
is it valid? what is reason for this change?
@@ -19,14 +19,15 @@ | |||
# current contact information at www.suse.com. | |||
# ------------------------------------------------------------------------------ | |||
# | |||
|
|||
# rubocop:disable Style/MutableConstant |
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.
why it is needed? why not simply freeze array?
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, below we in fact modify it. It does not look correct for me.
src/modules/Security.rb
Outdated
YAML.load_file(srv_file) | ||
rescue | ||
{} | ||
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.
to be honest this looks horrible. why not use
srv_lists = {}
if srv_file
begin
srv_lists = YAML.load_file(srv_file)
rescue => e
log.warn "Failed to load yaml file: #{e.message}"
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.
proposed by rubycop ;)
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 know, but as I said it is really ugly and I think with my code rubocop will be also happy ;)
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.
thats matter of taste. I can use your approach. However I prefer the "functional" style returning and assigning the value of if.
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'm doing as you suggested @jreidinger
ok, I added few mote notes, that looks wrong for me. Otherwise it looks good. |
Ping, @vpereira could you look at the @jreidinger 's comments? |
hi, I will check it ASAP, sorry for the delay
…On Mon, Apr 3, 2017 at 3:10 PM, Ladislav Slezák ***@***.***> wrote:
Ping, @vpereira <https://github.com/vpereira> could you look at the
@jreidinger <https://github.com/jreidinger> 's comments?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#40 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AACSKrbylE6l0hH6ZlW3E4KaPcPH2DK4ks5rsO_HgaJpZM4JyiEN>
.
|
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.
only missing answer is one for https://github.com/yast/yast-security/pull/40/files#r103430182 to be sure change of return value is valid.
@vpereira @jreidinger it seems this PR is just waiting for the missing answer about a change or returned values. @vpereira do you plan to work on it ? if not, as it is only a cleanup and no bug related I would close it until we are really able to finish it |
hi @teclator , the communication in this PR didn't work so well.. some long period of silence between the commits makes hard for someone sporadically contributing to the codestream to come back and work on that. There are still a question from my side, without answer. Beside it, maybe some commits should be cherry-picked, since it was started in 2016 and probably we updated already our rubocop rules.. Please advice what should be done here. |
@vpereira up to you. I mean, you can rebase with master fixing the conflicts and check rubocop. If you do not have time or do not plan to work on it then taking in account that we are in RC phase and that it is just a cleanup and do not fix any bug at all I would close it. |
Closing it by now |
Hi, I refactored some autoconverted code. I didn't change any functionality. Just refactoring.