def setUp(self): # Open the temporary configuration file and write the contents of # SALT_LINT_CONFIG to the temporary file. fp = tempfile.NamedTemporaryFile() fp.write(SALT_LINT_CONFIG.encode()) fp.seek(0) self.fp = fp # Specify the temporary file as if it was passed as a command line # argument. self.config = Configuration(dict(c=self.fp.name))
def test_file_positive(self): path = 'tests/test-extension-success.sls' runner = Runner(self.collection, path, Configuration()) self.assertEqual([], runner.run()) path = 'tests/test-extension-failure' runner = Runner(self.collection, path, Configuration()) self.assertEqual([], runner.run()) path = 'tests/test-extension-failure.extra.jinja' runner = Runner(self.collection, path, Configuration()) self.assertEqual([], runner.run())
class TestConfig(unittest.TestCase): def setUp(self): # Open the temporary configuration file and write the contents of # SALT_LINT_CONFIG to the temporary file. fp = tempfile.NamedTemporaryFile() fp.write(SALT_LINT_CONFIG.encode()) fp.seek(0) self.fp = fp # Specify the temporary file as if it was passed as a command line # argument. self.config = Configuration(dict(c=self.fp.name)) def tearDown(self): # Close the temporary configuration file. self.fp.close() def test_config(self): # Check 'verbosity' self.assertEqual(self.config.verbosity, 1) # Check 'skip_list' self.assertIn('208', self.config.skip_list) self.assertNotIn('200', self.config.skip_list) # Check 'exclude_paths' self.assertIn('exclude_this_file', self.config.exclude_paths) self.assertEqual(3, len(self.config.exclude_paths)) def test_is_file_ignored(self): # Check basic ignored rules per file self.assertTrue( self.config.is_file_ignored('tests/test-extension-failure', 'formatting')) self.assertFalse( self.config.is_file_ignored('tests/test-extension-failure', '210')) # Check more complex ignored rules per file using gitwildmatches self.assertTrue( self.config.is_file_ignored('tests/other/test.jinja', 'formatting')) self.assertFalse( self.config.is_file_ignored('test.jinja', 'formatting'))
def test_runner_exclude_paths(self): """ Check if all the excluded paths from the configuration are passed to the runner. """ exclude_paths = ['first.sls', 'second.sls'] config = Configuration(dict(exclude_paths=exclude_paths)) runner = Runner([], 'init.sls', config) self.assertTrue( any(path in runner.exclude_paths for path in exclude_paths))
def __init__(self, config=Configuration()): self.rules = [] self.config = config
def test_file_negative(self): path = 'tests/test-extension-failure.extra.sls' runner = Runner(self.collection, path, Configuration()) self.assertEqual(1, len(runner.run()))
def _call_runner(self, path): runner = Runner(self.collection, path, Configuration()) return runner.run()
def run(args=None): """Run the linter and return the exit code.""" parser = init_argument_parser() options = parser.parse_args(args if args is not None else sys.argv[1:]) stdin_filename = None file_names = set(options.files) checked_files = set() # Read input from STDIN if not sys.stdin.isatty(): with tempfile.NamedTemporaryFile('w', suffix='.sls', delete=False) as stdin_tmpfile: stdin_tmpfile.write(sys.stdin.read()) stdin_filename = stdin_tmpfile.name file_names.add(stdin_filename) # Read, parse and validate the configuration options_dict = vars(options) try: config = Configuration(options_dict) except SaltLintConfigError as exc: print(exc) return 2 # Show a help message on the screen if not file_names and not (options.listrules or options.listtags): parser.print_help(file=sys.stderr) return 1 # Collect the rules from the configuration collection = RulesCollection(config) for rulesdir in config.rulesdirs: collection.extend( RulesCollection.create_from_directory(rulesdir, config)) # Show the rules listing if options.listrules: print(collection) return 0 # Show the tags listing if options.listtags: print(collection.listtags()) return 0 formatter = initialize_formatter(config) problems = [] for file_name in file_names: runner = Runner(collection, file_name, config, checked_files) problems.extend(runner.run()) # Delete stdin temporary file if stdin_filename: os.unlink(stdin_filename) if problems: sorted_problems = sort_problems(problems) formatter.process(sorted_problems) return 2 return 0
def run(args=None): """Run the linter and return the exit code.""" # Wrap `sys.stdout` in an object that automatically encodes an unicode # string into utf-8, in Python 2 only. The default encoding for Python 3 # is already utf-8. if sys.version_info[0] < 3: sys.stdout = codecs.getwriter('utf-8')(sys.stdout) parser = init_argument_parser() options = parser.parse_args(args if args is not None else sys.argv[1:]) stdin_file = None file_names = set(options.files) checked_files = set() # Read input from STDIN if not sys.stdin.isatty(): stdin_file = tempfile.NamedTemporaryFile('w', suffix='.sls', delete=False) stdin_file.write(sys.stdin.read()) stdin_file.flush() file_names.add(stdin_file.name) # Read, parse and validate the configuration options_dict = vars(options) try: config = Configuration(options_dict) except SaltLintConfigError as exc: print(exc) return 2 # Show a help message on the screen if not file_names and not (options.listrules or options.listtags): parser.print_help(file=sys.stderr) return 1 # Collect the rules from the configuration collection = RulesCollection(config) for rulesdir in config.rulesdirs: collection.extend(RulesCollection.create_from_directory(rulesdir, config)) # Show the rules listing if options.listrules: print(collection) return 0 # Show the tags listing if options.listtags: print(collection.listtags()) return 0 formatter = initialize_formatter(config) problems = [] for file_name in file_names: runner = Runner(collection, file_name, config, checked_files) problems.extend(runner.run()) # Delete stdin temporary file if stdin_file: os.unlink(stdin_file.name) if problems: sorted_problems = sort_problems(problems) formatter.process(sorted_problems) return 2 return 0
def test_file_negative(self): path = 'tests/test-extension-failure' runner = Runner(self.collection, path, Configuration()) errors = runner.run() self.assertEqual(1, len(errors))