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

Failure in Lint/IneffectiveAccessModifier #8424

Closed
tas50 opened this issue Jul 31, 2020 · 0 comments
Closed

Failure in Lint/IneffectiveAccessModifier #8424

tas50 opened this issue Jul 31, 2020 · 0 comments
Labels
bug

Comments

@tas50
Copy link
Contributor

@tas50 tas50 commented Jul 31, 2020

Expected behavior

Cop should run without error

Actual behavior

Cop fails when scanning code.

Steps to reproduce the problem

Scan:

begin
  require 'junos-ez/stdlib'
  require ' net/netconf/exception'
rescue LoadError
  msg = 'Could not load the junos-ez-stdlib gem...'
  msg << 'ensure you are using the Chef for Junos packages'
  Chef::Log.debug msg
end

module Netdev
  module Junos
    # Provides compatibility shim between netdev_*_junos
    # providers and the `junos-ez-stdlib` library.
    class ApiClient
      begin
        # All possible resources `junos-ez-stdlib`
        # is able to manage. This Hash is used for
        # validation and metaprogramming.
        KNOWN_RESOURCES = {
          l1_ports: ::Junos::Ez::L1ports,
          l2_ports: ::Junos::Ez::L2ports,
          ip_ports: ::Junos::Ez::IPports,
          vlans: ::Junos::Ez::Vlans,
          lag_ports: ::Junos::Ez::LAGports,
        }.freeze

        KNOWN_RESOURCES.each_pair do |resource, provider_module|
          # Create a child class for each logical resource type. This forces
          # us to be explicit on which type of resource to manage.
          c = Class.new(self)
          c.class_eval <<-EVAL
            def initialize(resource_name)
              super(:#{resource}, resource_name)
            end
          EVAL

          class_name = provider_module.to_s.split('::').last
          const_set class_name, c
        end
      rescue NameError
        # If the requires above didn't work our class
        # references are definitely not going to work!
        Chef::Log.debug 'Could not generate Netdev::Junos::ApiClient child classes.'
      end

      # The `Junos::Ez` providers expect certain values
      # to be symbolized or requests will fail.
      VALUES_TO_SYMBOLIZE = %w( auto up down half full active passive disabled ).freeze

      attr_reader :resource_type
      attr_reader :resource_name

      resource_name resource_name

      def initialize(resource_type, _resource_name)
        unless KNOWN_RESOURCES.keys.include?(resource_type)
          error_message = "Invalid resource type :#{resource_type}."
          error_message << " Try one of: :#{KNOWN_RESOURCES.keys.join(', :')}"
          raise error_message
        end

        @resource_type = resource_type
      end

      # Writes managed resource to the candidate configuration.
      def write!
        with_config_check do
          managed_resource.write!
          Chef::Log.debug("#{self} wrote managed resource to Junos candidate configuration")
        end
      end

      # Removes managed resource from the candidate configuration.
      def delete!
        with_config_check do
          managed_resource.delete!
          Chef::Log.debug("#{self} deleted managed resource from Junos candidate configuration")
        end
      end

      # Activate managed resource in candidate configuration.
      def activate!
        with_config_check do
          managed_resource.activate!
          Chef::Log.debug("#{self} activated managed resource in Junos candidate configuration")
        end
      end

      # Deactivate managed resource in candidate configuration.
      def deactivate!
        with_config_check do
          managed_resource.deactivate!
          Chef::Log.debug("#{self} deactivated managed resource in Junos candidate configuration")
        end
      end

      # Given a hash of new property values and old property values
      # determines which have changed. `nil` values are ignored.
      #
      # @param new_values [#Hash<Symbol, Object>]
      # @param current_values [#Hash<Symbol, Object>]
      #
      # @return [#Hash<Symbol, Object>] updated properties
      def updated_changed_properties(new_values, current_values)
        new_values.each_pair do |property_name, new_value|
          old_value = current_values[property_name]

          next unless !new_value.nil? && (old_value != new_value)
          Chef::Log.debug("#{self} property '#{property_name}' has changed to '#{new_value}'")

          if managed_resource.properties.include?(property_name)
            # junos-ez-stdlib prefers some values as symbols
            managed_resource[property_name] = if VALUES_TO_SYMBOLIZE.include?(new_value)
                                                new_value.to_sym
                                              else
                                                new_value
                                              end
          else
            error_message = "#{self} don't know how to manage property :#{property_name}."
            error_message << " Known properties include: :#{managed_resource.properties.join(', :')}"
            raise ArgumentError, error_message
          end
        end
        # return Hash of updated properties
        managed_resource.should
      end

      def managed_resource
        @managed_resource ||= begin
          transport.send(resource_type)[resource_name]
                              rescue Netconf::RpcError => e
                                Chef::Log.debug("Managed Resource #{resource_name} not found: #{e}")
                                nil
        end
      end

      def to_s
        "#{self.class}[#{resource_name}]"
      end

      protected

      def transport
        Junos::ApiTransport.instance
      end

      # If processing the block of code passed in is successful config
      # changes are automatically committed. If something goes wrong
      # changes are rolled back.

      # Validates the Junos candidate configuration after yielding to
      # the code passed to this method. If validation fails an exception
      # is raised so the Chef run is halted.
      def with_config_check
        # ensure a transaction has been opened
        transport.start_transaction! unless transport.transaction_open?

        yield

        # validate the candidate configuration
        if transport.commit?
          Chef::Log.debug("#{self} validated Junos candidate configuration")
        end
      rescue Netconf::RpcError => e
        Chef::Log.error(format_rpc_error(e))
        raise e
      end

      # Takes a `Netconf::RpcError` and extracts the request and response
      # XML and attempts to pretty format them using `nokogiri`. Although
      # this client does not have an explicit dependency on `nokogiri` it
      # should be available in the local Rubygem install as `junos-ez-stdlib`
      # does have a transitive dependency on `nokogiri`.
      #
      # @param rpc_error [Netconf::RpcError] the exception to format
      #
      def format_rpc_error(rpc_error)
        request = rpc_error.cmd
        response = rpc_error.rsp

        # attempt to pretty format the XML using Nokogiri. This library
        # should be available for our use as it is a depdency of the
        # `net-netconf` gem.
        begin
          require 'nokogiri'
          request = Nokogiri::XML(request.to_xml) { |doc| doc.noblanks }
          response = Nokogiri::XML(response.to_xml) { |doc| doc.noblanks }
        rescue LoadError
          Chef::Log.debug 'Could not load nokogiri gem, xml will not be formatted'
          # fall back to ugly xml
        end

        error_msg = <<-MSG
  #{self} error communicating with the Junos XML API...rolling back!

  JUNOS XML REQUEST:

  #{request.to_xml}

  JUNOS XML RESPONSE:

  #{response.to_xml}
        MSG

        error_msg
      end
    end
  end
end

Error:

Scanning /Users/tsmith/dev/the_world/netdev/libraries/_junos_api_client.rb
An error occurred while Lint/IneffectiveAccessModifier cop was inspecting /Users/tsmith/dev/the_world/netdev/libraries/_junos_api_client.rb:14:4.
wrong number of arguments (given 3, expected 1..2)
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/lint/ineffective_access_modifier.rb:99:in `ineffective_modifier'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/lint/ineffective_access_modifier.rb:113:in `block in ineffective_modifier'
/usr/local/lib/ruby/gems/2.7.0/gems/rubocop-ast-0.2.0/lib/rubocop/ast/node.rb:190:in `block in each_child_node'
/usr/local/lib/ruby/gems/2.7.0/gems/rubocop-ast-0.2.0/lib/rubocop/ast/node.rb:187:in `each'
/usr/local/lib/ruby/gems/2.7.0/gems/rubocop-ast-0.2.0/lib/rubocop/ast/node.rb:187:in `each_child_node'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/lint/ineffective_access_modifier.rb:102:in `ineffective_modifier'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/lint/ineffective_access_modifier.rb:73:in `check_node'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/lint/ineffective_access_modifier.rb:61:in `on_class'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/commissioner.rb:91:in `block (2 levels) in trigger_responding_cops'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/commissioner.rb:112:in `with_cop_error_handling'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/commissioner.rb:90:in `block in trigger_responding_cops'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/commissioner.rb:89:in `each'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/commissioner.rb:89:in `trigger_responding_cops'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/commissioner.rb:61:in `block (2 levels) in <class:Commissioner>'
/usr/local/lib/ruby/gems/2.7.0/gems/rubocop-ast-0.2.0/lib/rubocop/ast/traversal.rb:159:in `on_while'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/commissioner.rb:62:in `block (2 levels) in <class:Commissioner>'
/usr/local/lib/ruby/gems/2.7.0/gems/rubocop-ast-0.2.0/lib/rubocop/ast/traversal.rb:159:in `on_while'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/commissioner.rb:62:in `block (2 levels) in <class:Commissioner>'
/usr/local/lib/ruby/gems/2.7.0/gems/rubocop-ast-0.2.0/lib/rubocop/ast/traversal.rb:59:in `block in on_begin'
/usr/local/lib/ruby/gems/2.7.0/gems/rubocop-ast-0.2.0/lib/rubocop/ast/traversal.rb:59:in `each'
/usr/local/lib/ruby/gems/2.7.0/gems/rubocop-ast-0.2.0/lib/rubocop/ast/traversal.rb:59:in `on_begin'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/commissioner.rb:62:in `block (2 levels) in <class:Commissioner>'
/usr/local/lib/ruby/gems/2.7.0/gems/rubocop-ast-0.2.0/lib/rubocop/ast/traversal.rb:14:in `walk'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/commissioner.rb:74:in `investigate'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/team.rb:151:in `investigate_partial'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/team.rb:83:in `investigate'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:295:in `inspect_file'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:245:in `block in do_inspection_loop'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:277:in `block in iterate_until_no_changes'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:270:in `loop'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:270:in `iterate_until_no_changes'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:241:in `do_inspection_loop'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:121:in `block in file_offenses'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:146:in `file_offense_cache'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:120:in `file_offenses'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:111:in `process_file'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:90:in `block in each_inspected_file'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:89:in `each'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:89:in `reduce'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:89:in `each_inspected_file'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:78:in `inspect_files'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:39:in `run'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cli/command/execute_runner.rb:21:in `execute_runner'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cli/command/execute_runner.rb:13:in `run'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cli/command.rb:10:in `run'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cli/environment.rb:17:in `run'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cli.rb:65:in `run_command'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cli.rb:72:in `execute_runners'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cli.rb:41:in `run'
/Users/tsmith/dev/oss/rubocop/exe/rubocop:13:in `block in <top (required)>'
/usr/local/Cellar/ruby/2.7.1_2/lib/ruby/2.7.0/benchmark.rb:308:in `realtime'
/Users/tsmith/dev/oss/rubocop/exe/rubocop:12:in `<top (required)>'
/usr/local/lib/ruby/gems/2.7.0/bin/rubocop:23:in `load'
/usr/local/lib/ruby/gems/2.7.0/bin/rubocop:23:in `<top (required)>'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:63:in `load'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:63:in `kernel_load'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:28:in `run'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/cli.rb:476:in `exec'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor.rb:399:in `dispatch'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/cli.rb:30:in `dispatch'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/base.rb:476:in `start'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/cli.rb:24:in `start'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/exe/bundle:46:in `block in <top (required)>'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/friendly_errors.rb:123:in `with_friendly_errors'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/exe/bundle:34:in `<top (required)>'
/usr/local/opt/ruby/bin/bundle:23:in `load'
/usr/local/opt/ruby/bin/bundle:23:in `<main>'
.

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Lint/IneffectiveAccessModifier cop was inspecting /Users/tsmith/dev/the_world/netdev/libraries/_junos_api_client.rb:14:4.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop-hq/rubocop/issues

Mention the following information in the issue report:
0.88.0 (using Parser 2.7.1.4, rubocop-ast 0.2.0, running on ruby 2.7.1 x86_64-darwin19)
Finished in 0.1381979999714531 seconds

RuboCop version

master
@koic koic added the bug label Jul 31, 2020
koic added a commit to koic/rubocop that referenced this issue Jul 31, 2020
Fixes rubocop-hq#8424.

This PR fixes an error for `Lint/IneffectiveAccessModifier`
when there is `begin` before a method definition.
koic added a commit to koic/rubocop that referenced this issue Jul 31, 2020
Fixes rubocop-hq#8424.

This PR fixes an error for `Lint/IneffectiveAccessModifier`
when there is `begin...end` before a method definition.
koic added a commit to koic/rubocop that referenced this issue Jul 31, 2020
Fixes rubocop-hq#8424.

This PR fixes an error for `Lint/IneffectiveAccessModifier`
when there is `begin...end` before a method definition.
@koic koic closed this in #8427 Jul 31, 2020
koic added a commit that referenced this issue Jul 31, 2020
…ess_modifier

[Fix #8424] Fix an error for `Lint/IneffectiveAccessModifier`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

2 participants
You can’t perform that action at this time.