lint-files.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import path from 'path';
  2. import test from 'ava';
  3. import fn from '..';
  4. process.chdir(__dirname);
  5. test('only accepts whitelisted extensions', async t => {
  6. // Markdown files will always produce linter errors and will not be going away
  7. const mdGlob = path.join(__dirname, '..', '*.md');
  8. // No files should be linted = no errors
  9. const noOptionsResults = await fn.lintFiles(mdGlob, {});
  10. t.is(noOptionsResults.errorCount, 0);
  11. // Markdown files linted (with no plugin for it) = errors
  12. const moreExtensionsResults = await fn.lintFiles(mdGlob, {extensions: ['md']});
  13. t.true(moreExtensionsResults.errorCount > 0);
  14. });
  15. test('ignores dirs for empty extensions', async t => {
  16. {
  17. const glob = path.join(__dirname, 'fixtures/nodir/*');
  18. const results = await fn.lintFiles(glob, {extensions: ['', 'js']});
  19. const {results: [fileResult]} = results;
  20. // Only `fixtures/nodir/noextension` should be linted
  21. const expected = 'fixtures/nodir/noextension'.split('/').join(path.sep);
  22. const actual = path.relative(__dirname, fileResult.filePath);
  23. t.is(actual, expected);
  24. t.is(results.errorCount, 1);
  25. }
  26. {
  27. const glob = path.join(__dirname, 'fixtures/nodir/nested/*');
  28. const results = await fn.lintFiles(glob);
  29. const {results: [fileResult]} = results;
  30. // Ensure `nodir/nested` **would** report if globbed
  31. const expected = 'fixtures/nodir/nested/index.js'.split('/').join(path.sep);
  32. const actual = path.relative(__dirname, fileResult.filePath);
  33. t.is(actual, expected);
  34. t.is(results.errorCount, 1);
  35. }
  36. });
  37. test.serial('cwd option', async t => {
  38. const {results} = await fn.lintFiles('**/*', {cwd: 'fixtures/cwd'});
  39. const paths = results.map(r => path.relative(__dirname, r.filePath));
  40. paths.sort();
  41. t.deepEqual(paths, [path.join('fixtures', 'cwd', 'unicorn.js')]);
  42. });
  43. test('do not lint gitignored files', async t => {
  44. const cwd = path.join(__dirname, 'fixtures/gitignore');
  45. const glob = path.posix.join(cwd, '**/*');
  46. const ignored = path.resolve('fixtures/gitignore/test/foo.js');
  47. const {results} = await fn.lintFiles(glob, {cwd});
  48. t.is(results.some(r => r.filePath === ignored), false);
  49. });
  50. test('do not lint gitignored files in file with negative gitignores', async t => {
  51. const cwd = path.join(__dirname, 'fixtures/negative-gitignore');
  52. const glob = path.posix.join(cwd, '*');
  53. const ignored = path.resolve('fixtures/negative-gitignore/bar.js');
  54. const {results} = await fn.lintFiles(glob, {cwd});
  55. t.is(results.some(r => r.filePath === ignored), false);
  56. });
  57. test('lint negatively gitignored files', async t => {
  58. const cwd = path.join(__dirname, 'fixtures/negative-gitignore');
  59. const glob = path.posix.join(cwd, '*');
  60. const negative = path.resolve('fixtures/negative-gitignore/foo.js');
  61. const {results} = await fn.lintFiles(glob, {cwd});
  62. t.is(results.some(r => r.filePath === negative), true);
  63. });
  64. test('do not lint inapplicable negatively gitignored files', async t => {
  65. const cwd = path.join(__dirname, 'fixtures/negative-gitignore');
  66. const glob = path.posix.join(cwd, 'bar.js');
  67. const negative = path.resolve('fixtures/negative-gitignore/foo.js');
  68. const {results} = await fn.lintFiles(glob, {cwd});
  69. t.is(results.some(r => r.filePath === negative), false);
  70. });
  71. test('multiple negative patterns should act as positive patterns', async t => {
  72. const cwd = path.join(__dirname, 'fixtures', 'gitignore-multiple-negation');
  73. const {results} = await fn.lintFiles('**/*', {cwd});
  74. const paths = results.map(r => path.basename(r.filePath));
  75. paths.sort();
  76. t.deepEqual(paths, ['!!unicorn.js', '!unicorn.js']);
  77. });