Skip to content

Commit e50e93d

Browse files
authored
Add unit tests for XLSXTool and add system tests in CI (#130)
* Tracking *.pdf in git-lfs * Add tests for XLSXTool and restructure files/includes for testing * Remove redundant requires * Add tests to the build step of CI to run inspec_tools as an installed gem * Accept chef license agreement
1 parent d99d30f commit e50e93d

File tree

9 files changed

+44
-16
lines changed

9 files changed

+44
-16
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.xls filter=lfs diff=lfs merge=lfs -text
22
*.xlsx filter=lfs diff=lfs merge=lfs -text
3+
*.pdf filter=lfs diff=lfs merge=lfs -text

.github/workflows/build.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,14 @@ jobs:
4646
run: |
4747
inspec_tools --version
4848
inspec_tools --help
49+
inspec_tools xlsx2inspec -m examples/xlsx2inspec/mapping.cis.yml -x examples/cis.xlsx -p xlsx2inspec_test
50+
inspec_tools summary -j examples/sample_json/rhel-simp.json -o summary.json -c
51+
inspec_tools csv2inspec -c examples/csv2inspec/stig.csv -m examples/csv2inspec/mapping.yml -o csv2inspec_test
52+
inspec_tools xccdf2inspec -x examples/xccdf2inspec/xccdf.xml -a lib/data/attributes.yml -o xccdf2inspec_test
53+
inspec_tools pdf2inspec -p examples/CIS_Ubuntu_Linux_16.04_LTS_Benchmark_v1.0.0.pdf -o pdf2inspec_test
54+
inspec_tools inspec2csv -j examples/sample_json/rhel-simp.json -o inspec2csv_test.csv
55+
inspec_tools inspec2ckl -j examples/sample_json/rhel-simp.json -o inspec2ckl_test.ckl
56+
inspec_tools inspec2xccdf -j examples/sample_json/rhel-simp.json -a lib/data/attributes.yml -o inspec2xccdf_test.xml
57+
inspec_tools compliance -j examples/sample_json/single_control_results.json -f examples/sample_yaml/threshold.yaml
58+
env:
59+
CHEF_LICENSE: "accept"
Binary file not shown.

lib/inspec_tools.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ module InspecTools
1313
autoload :Inspec, 'inspec_tools/inspec'
1414
autoload :Summary, 'inspec_tools/summary'
1515
autoload :Threshold, 'inspec_tools/threshold'
16-
autoload :XLSXTool, 'inspec_tools/xlsx.rb'
16+
autoload :XLSXTool, 'inspec_tools/xlsx_tool'
1717
end

lib/inspec_tools/cli.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33

44
require 'inspec-objects'
55
require 'inspec'
6-
require_relative 'version'
7-
8-
require_relative '../utilities/inspec_util'
9-
require_relative '../utilities/csv_util'
106
require_relative './plugin_cli.rb'
117

128
# This tells the ruby cli app to use the same argument parsing as the plugin

lib/inspec_tools/plugin_cli.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
require 'yaml'
22
require 'json'
3-
43
require 'roo'
5-
require_relative 'version'
6-
74
require_relative '../utilities/inspec_util'
85
require_relative '../utilities/csv_util'
96

@@ -17,7 +14,7 @@ module InspecTools
1714
autoload :Inspec, 'inspec_tools/inspec'
1815
autoload :Summary, 'inspec_tools/summary'
1916
autoload :Threshold, 'inspec_tools/threshold'
20-
autoload :XLSXTool, 'inspec_tools/xlsx'
17+
autoload :XLSXTool, 'inspec_tools/xlsx_tool'
2118
end
2219

2320
# rubocop:disable Style/GuardClause

lib/inspec_tools/xlsx.rb renamed to lib/inspec_tools/xlsx_tool.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'word_wrap'
44
require 'yaml'
55
require 'digest'
6+
require 'roo'
67

78
require_relative '../utilities/inspec_util'
89

@@ -51,7 +52,7 @@ def get_cis_to_nist_control_mapping(spreadsheet)
5152
if row[3].is_a? Numeric
5253
cis_to_nist[row[3].to_s] = row[0]
5354
else
54-
cis2Nist[row[2].to_s] = row[0] unless (row[2] == '') || row[2].to_i.nil?
55+
cis_to_nist[row[2].to_s] = row[0] unless (row[2] == '') || row[2].to_i.nil?
5556
end
5657
end
5758
cis_to_nist
@@ -70,7 +71,7 @@ def insert_json_metadata
7071
@profile['attributes'] = []
7172
@profile['generator'] = {
7273
'name': 'inspec_tools',
73-
'version': VERSION
74+
'version': ::InspecTools::VERSION
7475
}
7576
end
7677

@@ -122,10 +123,10 @@ def apply_cis_and_nist_controls(control, cis_tags)
122123

123124
if cis_tags[:sub_section].nil? || cis_tags[:sub_section].blank?
124125
control['tags']['cis_controls'] << cis_tags[:section]
125-
control['tags']['nist'] << get_nist_control_for_cis([cis_tags[:section]])
126+
control['tags']['nist'] << get_nist_control_for_cis(cis_tags[:section])
126127
else
127128
control['tags']['cis_controls'] << "#{cis_tags[:section]}.#{cis_tags[:sub_section]}"
128-
control['tags']['nist'] << get_nist_control_for_cis([cis_tags[:section], cis_tags[:sub_section]])
129+
control['tags']['nist'] << get_nist_control_for_cis(cis_tags[:section], cis_tags[:sub_section])
129130
end
130131

131132
control['tags']['nist'] << LATEST_NIST_REV unless control['tags']['nist'].nil?
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require_relative '../test_helper'
2+
require 'roo'
3+
require 'yaml'
4+
5+
class XLSXToolTest < Minitest::Test
6+
def setup
7+
@xlsx = Roo::Spreadsheet.open('examples/cis.xlsx')
8+
@mapping = YAML.load_file('examples/xlsx2inspec/mapping.cis.yml')
9+
end
10+
11+
def test_that_xlsx_exists
12+
refute_nil ::InspecTools::XLSXTool
13+
end
14+
15+
def test_to_inspec
16+
xlsx = InspecTools::XLSXTool.new(@xlsx, @mapping, 'test')
17+
profile = xlsx.to_inspec('test')
18+
assert_equal profile['name'], 'test'
19+
assert_equal profile['generator'][:version], ::InspecTools::VERSION
20+
assert_equal profile['controls'].first['tags']['nist'], ["IA-2 (1)", "Rev_4"]
21+
assert_equal profile['controls'].first['tags']['cis_controls'], ["4.5", "Rev_7"]
22+
end
23+
end

test/unit/test_helper.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require 'simplecov'
22
SimpleCov.start
33
require 'minitest/autorun'
4-
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
5-
root = File.expand_path("../../", File.dirname(__FILE__))
6-
require "#{root}/lib/inspec_tools"
4+
$LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
5+
require 'inspec_tools'

0 commit comments

Comments
 (0)