Skip to content

Commit

Permalink
Output File CLI Option (#1511)
Browse files Browse the repository at this point in the history
* --output-file CLI argument added

Co-authored-by: Kevin DeJong <[email protected]>
  • Loading branch information
Tro95 and kddejong authored May 21, 2020
1 parent 906c24c commit 661bd87
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/cfnlint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ def main():
LOGGER.debug('Completed linting of file: %s', str(filename))

matches_output = formatter.print_matches(matches, rules)

if matches_output:
print(matches_output)
if args.output_file:
with open(args.output_file, 'w') as output_file:
output_file.write(matches_output)
else:
print(matches_output)

return cfnlint.core.get_exit_code(matches)
except cfnlint.core.CfnLintExitException as e:
LOGGER.error(str(e))
Expand Down
10 changes: 10 additions & 0 deletions src/cfnlint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ def __call__(self, parser, namespace, values, option_string=None):
action='store_true'
)

standard.add_argument(
'--output-file', type=str, default=None,
help='Writes the output to the specified file, ideal for producing reports'
)

return parser


Expand Down Expand Up @@ -655,3 +660,8 @@ def config_file(self):
def build_graph(self):
""" build_graph """
return self._get_argument_value('build_graph', False, False)

@property
def output_file(self):
""" output_file """
return self._get_argument_value('output_file', False, True)
4 changes: 4 additions & 0 deletions src/cfnlint/data/CfnLintCli/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
"description": "Path to spec file to override with",
"type": "string"
},
"output_file": {
"description": "Path to the file to write the main output to",
"type": "string"
},
"regions": {
"description": "Regions to test against",
"items": {
Expand Down
12 changes: 12 additions & 0 deletions test/unit/module/config/test_cli_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ def test_create_parser_default_param(self):
self.assertEqual(config.cli_args.template_alt, [])
self.assertEqual(config.cli_args.regions, ['us-east-1', 'us-west-2'])

def test_stdout(self):
"""Test success run"""

config = cfnlint.config.CliArgs(['-t', 'template1.yaml'])
self.assertIsNone(config.cli_args.output_file)

def test_output_file(self):
"""Test success run"""

config = cfnlint.config.CliArgs(['-t', 'template1.yaml', '--output-file', 'test_output.txt'])
self.assertEqual(config.cli_args.output_file, 'test_output.txt')

def test_create_parser_exend(self):
"""Test success run"""

Expand Down
30 changes: 30 additions & 0 deletions test/unit/module/config/test_config_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,36 @@ def test_config_precedence(self, yaml_mock):
# template file wins over config file
self.assertEqual(config.ignore_checks, ['W3001'])

@patch('cfnlint.config.ConfigFileArgs._read_config', create=True)
def test_config_file_output(self, yaml_mock):
""" Test precedence in """

yaml_mock.side_effect = [
{
"output_file": "test_output.txt"
},
{}
]
config = cfnlint.config.ConfigMixIn([])

# Config file wins
self.assertEqual(config.output_file, 'test_output.txt')

@patch('cfnlint.config.ConfigFileArgs._read_config', create=True)
def test_config_file_output_mixin(self, yaml_mock):
""" Test precedence in """

yaml_mock.side_effect = [
{
"output_file": "test_output.txt"
},
{}
]
config = cfnlint.config.ConfigMixIn(['--output-file', 'test_output_2.txt'])

# CLI args win
self.assertEqual(config.output_file, 'test_output_2.txt')

@patch('cfnlint.config.ConfigFileArgs._read_config', create=True)
def test_config_default_region(self, yaml_mock):
""" Test precedence in """
Expand Down
8 changes: 8 additions & 0 deletions test/unit/module/core/test_run_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ def test_template_config(self, yaml_mock):
'test/fixtures/templates/good/core/config_parameters.yaml'])
self.assertEqual(args.update_documentation, False)
self.assertEqual(args.update_specs, False)
self.assertEqual(args.output_file, None)

def test_output_file(self):
filename = 'test/fixtures/templates/good/core/config_parameters.yaml'
(args, _, _,) = cfnlint.core.get_args_filenames([
'--template', filename, '--output-file', 'test_output.txt'])

self.assertEqual(args.output_file, 'test_output.txt')

@patch('cfnlint.config.ConfigFileArgs._read_config', create=True)
def test_positional_template_parameters(self, yaml_mock):
Expand Down

0 comments on commit 661bd87

Please sign in to comment.