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

[WIP] refactoring autoconverted ruby code into ruby code #40

Closed
wants to merge 7 commits into from

Conversation

vpereira
Copy link

@vpereira vpereira commented Sep 1, 2016

Hi, I refactored some autoconverted code. I didn't change any functionality. Just refactoring.

@mvidner
Copy link
Member

mvidner commented Sep 1, 2016

Hi, I refactored some autoconverted code. I didn't change any functionality. Just refactoring.

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?

@vpereira
Copy link
Author

vpereira commented Sep 1, 2016

sure, i used a rubocop configuration used by yast. I will attach it.


# do not force %r
Style/RegexpLiteral:
Enabled: false
Copy link
Member

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

current = if levels.empty?
:custom
else
levels.last # mimic the old implementation
Copy link
Member

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

end
false
Security.modified
Copy link
Member

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

Copy link
Author

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?

Copy link
Member

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")
)
Copy link
Member

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.

Copy link
Author

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?

Copy link
Member

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

Copy link
Author

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.

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

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

@@ -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>")
Copy link
Member

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.

Copy link
Member

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

:from => "map",
:to => "map <string, any>"
from: "map",
to: "map <string, any>"
Copy link
Member

Choose a reason for hiding this comment

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

same here

@@ -131,8 +131,8 @@ def main
Builtins.y2milestone("Security auto finished")
Builtins.y2milestone("----------------------------------------")

deep_copy(@ret)

# is it needed?
Copy link
Member

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.

@@ -354,7 +352,7 @@ def DisplayHelpPopup(help_id)
if help_id == "MANDATORY_SERVICES"
missing = Security.MissingMandatoryServices

if missing != nil && missing != []
if !missing.nil? && missing != []
Copy link
Member

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.

@@ -374,7 +371,7 @@ def DisplayHelpPopup(help_id)
elsif help_id == "EXTRA_SERVICES"
extra = Security.ExtraServices

if extra != nil && extra != []
if !extra.nil? && extra != []
Copy link
Member

Choose a reason for hiding this comment

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

same here

@@ -384,7 +381,7 @@ def DisplayHelpPopup(help_id)
end
end

if help != nil && help != ""
if !help.nil? && help != ""
Copy link
Member

Choose a reason for hiding this comment

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

and here

@@ -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?
Copy link
Member

Choose a reason for hiding this comment

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

intval ||= 0 is simplier

Copy link
Author

Choose a reason for hiding this comment

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

proposed by the rubycop :)

Copy link
Member

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.

@@ -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?
Copy link
Member

Choose a reason for hiding this comment

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

it ||= ""

else
new = "no"
end
new = if ret == true
Copy link
Member

Choose a reason for hiding this comment

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

new = ret ? "yes" : "no"

@@ -268,9 +262,9 @@ def PollAbort
end

# Abort function
# @return blah blah lahjk
# @return false
Copy link
Member

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

def Abort
return Builtins.eval(@AbortFunction) == true if @AbortFunction != nil
return Builtins.eval(@AbortFunction) == true if !@AbortFunction.nil?
Copy link
Member

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

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?

Copy link
Author

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

Choose a reason for hiding this comment

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

same here

Copy link
Author

Choose a reason for hiding this comment

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

proposed by the rubycop :)

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

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)

Builtins.foreach(@do_not_test) do |key|
settings = Builtins.remove(settings, key)
end
@do_not_test.each { |k| settings.delete k }
Copy link
Member

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 ?

Copy link
Member

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

Copy link
Author

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.

Copy link
Member

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

_("Current Security Level: %1"),
Ops.get(@LevelsNames, Convert.to_string(current), "")
)
summary = _("Current Security Level: #{@LevelsNames[current]}")
Copy link
Member

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]


# is it needed?
deep_copy(@ret)
# EOF
Copy link
Member

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.

@@ -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 = ||= ""
Copy link
Member

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 ||=

@vpereira
Copy link
Author

vpereira commented Oct 5, 2016

I fixed almost all issues. Rubocop has just three complains related with useless assignment. Could you please evaluate them?

src/clients/security.rb:180:7: W: Lint/UselessAssignment: Useless assignment to variable - current. 
      current = :custom
      ^^^^^^^
src/include/security/complex.rb:138:9: W: Lint/UselessAssignment: Useless assignment to variable - cur. 
        cur = UI.QueryWidget(Id(:rb), :CurrentButton)
        ^^^
src/include/security/dialogs.rb:289:9: W: Lint/UselessAssignment: Useless assignment to variable - current_value. 
        current_value = Ops.get(
        ^^^^^^^^^^^^^

@jreidinger
Copy link
Member

@vpereira OK, I evaluated them and result is:

  • src/clients/security.rb:180 - valid as you set custom few lines lower as default, so please remove it
  • src/include/security/complex.rb:138 - also valid as when cur is used, it is few lines below, so please remove it
  • src/include/security/dialogs.rb:289 - also valid, looks like old code, now it uses label_mapping for label instead of name, so also can be removed

Security.modified = true
return true
Copy link
Member

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

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?

Copy link
Member

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.

YAML.load_file(srv_file)
rescue
{}
end) : {}
Copy link
Member

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

Copy link
Author

Choose a reason for hiding this comment

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

proposed by rubycop ;)

Copy link
Member

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

Copy link
Author

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.

Copy link
Author

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

@jreidinger
Copy link
Member

ok, I added few mote notes, that looks wrong for me. Otherwise it looks good.

@lslezak
Copy link
Member

lslezak commented Apr 3, 2017

Ping, @vpereira could you look at the @jreidinger 's comments?

@vpereira
Copy link
Author

vpereira commented Apr 3, 2017 via email

@teclator teclator changed the title refactoring autoconverted ruby code into ruby code [WIP] refactoring autoconverted ruby code into ruby code Apr 6, 2017
Copy link
Member

@jreidinger jreidinger left a 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.

@teclator
Copy link
Contributor

teclator commented Apr 9, 2018

@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

@vpereira
Copy link
Author

vpereira commented Apr 9, 2018

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.

@teclator
Copy link
Contributor

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

@teclator teclator closed this Apr 11, 2018
@teclator teclator reopened this Apr 11, 2018
@teclator
Copy link
Contributor

Closing it by now

@teclator teclator closed this Apr 13, 2018
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