def test_process_files__invalid_config(self): options = {'config': 'tests/fixtures/eslint/invalid.json'} tool = Eslint(self.problems, options, root_dir) tool.process_files([FILE_WITH_ERRORS]) problems = self.problems.all() self.assertEqual(1, len(problems), 'Invalid config should report an error') error = problems[0] self.assertIn('Your eslint configuration output the following error', error.body) self.assertIn("couldn't find", error.body) self.assertIn('config "invalid-rules"', error.body)
def test_execute_fixer(self): tool = Eslint( self.problems, { 'config': 'tests/fixtures/eslint/recommended_config.json', 'fixer': True, }, root_dir) original = read_file(FILE_WITH_FIXER_ERRORS) tool.execute_fixer([FILE_WITH_FIXER_ERRORS]) updated = read_and_restore_file(FILE_WITH_FIXER_ERRORS, original) assert original != updated, 'File content should change.' eq_(0, len(self.problems.all()), 'No errors should be recorded')
def test_process_files_with_config(self): options = {'config': 'tests/fixtures/eslint/config.json'} tool = Eslint(self.problems, options, root_dir) tool.process_files([FILE_WITH_ERRORS]) problems = self.problems.all(FILE_WITH_ERRORS) msg = ("'foo' is assigned a value but never used. (no-unused-vars)\n" "'bar' is not defined. (no-undef)\n" "Missing semicolon. (semi)") expected = [Comment(FILE_WITH_ERRORS, 2, 2, msg)] eq_(expected, problems)
def test_process_files__deprecated_option(self): options = {'config': 'tests/fixtures/eslint/deprecatedoption.json'} tool = Eslint(self.problems, options, root_dir) tool.process_files([FILE_WITH_ERRORS]) problems = self.problems.all() self.assertGreater(len(problems), 0, 'Invalid config should report an error') error = problems[0] self.assertIn('Your eslint configuration output the following error', error.body) self.assertIn('DeprecationWarning', error.body) style_error = problems[1] self.assertNotIn('DeprecationWarning', style_error.body)
def test_process_files_with_ignore_file_config(self): eslint_dir = os.path.join(root_dir, 'tests', 'fixtures', 'eslint') options = {'config': './config.json'} ignore_file = 'ignore.js' tool = Eslint(self.problems, options, eslint_dir) tool.process_files([ignore_file]) errors = self.problems.all(ignore_file) assert len(errors) == 1, errors error = errors[0] assert 'File ignored' in error.body, error assert error.line == Comment.FIRST_LINE_IN_DIFF assert error.position == Comment.FIRST_LINE_IN_DIFF
def test_execute_fixer__no_problems_remain(self): tool = Eslint(self.problems, { 'config': 'tests/fixtures/eslint/recommended_config.json', 'fixer': True }, root_dir) # The fixture file can have all problems fixed by eslint original = read_file(FILE_WITH_FIXER_ERRORS) tool.execute_fixer([FILE_WITH_FIXER_ERRORS]) tool.process_files([FILE_WITH_FIXER_ERRORS]) read_and_restore_file(FILE_WITH_FIXER_ERRORS, original) eq_(0, len(self.problems.all()), 'All errors should be autofixed')
def test_execute__install_plugins(self): custom_dir = root_dir + '/tests/fixtures/eslint_custom' tool = Eslint(self.problems, { 'config': 'config.json', 'install_plugins': True, 'fixer': True }, custom_dir) target = 'has_errors.js' tool.process_files([target]) problems = self.problems.all() eq_(2, len(problems), 'Should find errors') assert_in('Unexpected var', problems[0].body) ok_(docker.image_exists('nodejs'), 'original image is present') assert_not_in('eslint', docker.images(), 'no eslint image remains')
def test_execute__install_plugins_cleanup_image_on_failure(self): custom_dir = root_dir + '/tests/fixtures/eslint_custom' tool = Eslint(self.problems, { 'config': 'invalid.json', 'install_plugins': True, 'fixer': True }, custom_dir) target = 'has_errors.js' tool.process_files([target]) problems = self.problems.all() eq_(1, len(problems)) assert_in('Cannot find module', problems[0].body) ok_(docker.image_exists('nodejs'), 'original image is present') assert_not_in('eslint', docker.images(), 'no eslint image remains')
def test_execute_fixer__install_plugins(self): custom_dir = root_dir + '/tests/fixtures/eslint_custom' tool = Eslint(self.problems, { 'config': 'config.json', 'install_plugins': True, 'fixer': True }, custom_dir) target = 'tests/fixtures/eslint_custom/fixer_errors.js' # The fixture file can have all problems fixed by eslint original = read_file(target) tool.execute_fixer(['fixer_errors.js']) tool.process_files(['fixer_errors.js']) read_and_restore_file(target, original) eq_(0, len(self.problems.all()), 'All errors should be autofixed') assert_not_in('eslint', docker.images(), 'no eslint image remains')
def test_execute__install_plugins_cleanup_image_on_failure(self): custom_dir = root_dir + '/tests/fixtures/eslint_custom' tool = Eslint(self.problems, { 'config': 'invalid.json', 'install_plugins': True, 'fixer': True }, custom_dir) target = 'has_errors.js' tool.process_files([target]) problems = self.problems.all() self.assertEqual(1, len(problems)) self.assertIn('invalid-rules', problems[0].body) self.assertIn('output the following error', problems[0].body) self.assertTrue(docker.image_exists('eslint'), 'original image is present') for image in docker.images(): self.assertNotIn('eslint-', image, 'no eslint image remains')
def setUp(self): self.problems = Problems() self.tool = Eslint(self.problems)
def test_process_files_uses_default_config(self): tool = Eslint(self.problems, options={}) tool.process_files([FILE_WITH_ERRORS]) problems = self.problems.all(FILE_WITH_ERRORS) eq_(2, len(problems), 'With no config file there should be no errors.')
def setUp(self): self.problems = Problems() options = {'config': 'tests/fixtures/eslint/recommended_config.json'} self.tool = Eslint(self.problems, options)
def test_process_files_uses_default_config(self): tool = Eslint(self.problems, options={}, base_path=root_dir) tool.process_files([FILE_WITH_ERRORS]) problems = self.problems.all(FILE_WITH_ERRORS) self.assertEqual(2, len(problems), 'With no config file there should be errors.')
def test_has_fixer__enabled(self): tool = Eslint(self.problems, {'fixer': True}, root_dir) self.assertEqual(True, tool.has_fixer())
def test_has_fixer__not_enabled(self): tool = Eslint(self.problems, {}) self.assertEqual(False, tool.has_fixer())
def test_has_fixer__not_enabled(self): tool = Eslint(self.problems, {}) eq_(False, tool.has_fixer())