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

mssql_session - Handling cases where the data is nil #2752

Merged
merged 2 commits into from
Mar 1, 2018
Merged

mssql_session - Handling cases where the data is nil #2752

merged 2 commits into from
Mar 1, 2018

Conversation

frezbo
Copy link
Contributor

@frezbo frezbo commented Feb 27, 2018

same o/p where the code failed:

inspec> sql = mssql_session()
=> MSSQL session
inspec> data = sql.query("SELECT who.name AS [Principal Name] FROM sys.server_permissions what INNER JOIN sys.server_principals who ON who.principal_id = what.grantee_principal_id WHERE what.permission_name = 'Alter any endpoint' AND who.name NOT LIKE '##MS%##' AND who.type_desc <> 'SERVER_ROLE'  ORDER BY  who.name  ;")
=> SQL ResultSet
inspec> s = data.row(0).column('Principal Name')
=> #<DatabaseHelper::SQLColumn:0x00005566f360da10 @name="Principal Name", @row=nil>
inspec> s.value
NoMethodError: undefined method `[]' for nil:NilClass
from /home/frezbo/.gem/ruby/gems/inspec-2.0.17/lib/utils/database_helpers.rb:13:in `value'
inspec> 

@frezbo frezbo requested a review from a team as a code owner February 27, 2018 18:18
@clintoncwolfe clintoncwolfe changed the title Handling cases where the data is nil mssql_session - Handling cases where the data is nil Feb 27, 2018
Copy link
Contributor

@jquick jquick left a comment

Choose a reason for hiding this comment

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

Thanks @frezbo ! This is a great addition. Do you mind adding a unit test for this functionality?

You will want to create a file in: test/unit/utils/database_helpers_test.rb

Something like this should work:

# encoding: utf-8

require 'helper'

describe DatabaseHelper do
  describe DatabaseHelper::SQLColumn do
    def column(row = { 'test' => 'value' })
      DatabaseHelper::SQLColumn.new(row, 'test')
    end

    it 'has a valid column value' do
      column.value.must_equal 'value'
    end

    it 'returns empty value when nil row' do
      column(nil).value.must_equal ''
    end
  end
end

@frezbo
Copy link
Contributor Author

frezbo commented Mar 1, 2018

sure, let me bake that up and test

Signed-off-by: Noel Georgi <[email protected]>
Copy link
Contributor

@jquick jquick left a comment

Choose a reason for hiding this comment

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

Thanks @frezbo !

Copy link
Contributor

@jerryaldrichiii jerryaldrichiii left a comment

Choose a reason for hiding this comment

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

Looks good to me, great work @frezbo!

@jquick jquick merged commit dd033fb into inspec:master Mar 1, 2018
@clintoncwolfe clintoncwolfe added Type: Bug Feature not working as expected and removed bug labels Apr 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Feature not working as expected
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants