main.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import fs from 'fs';
  2. import path from 'path';
  3. import test from 'ava';
  4. import execa from 'execa';
  5. import slash from 'slash';
  6. import tempWrite from 'temp-write';
  7. process.chdir(__dirname);
  8. const main = (args, opts) => execa(path.join(__dirname, '../main.js'), args, opts);
  9. test('fix option', async t => {
  10. const filepath = await tempWrite('console.log()\n', 'x.js');
  11. await main(['--fix', filepath]);
  12. t.is(fs.readFileSync(filepath, 'utf8').trim(), 'console.log();');
  13. });
  14. test('fix option with stdin', async t => {
  15. const {stdout} = await main(['--fix', '--stdin'], {
  16. input: 'console.log()\n'
  17. });
  18. t.is(stdout.trim(), 'console.log();');
  19. });
  20. test('stdin-filename option with stdin', async t => {
  21. const {stdout} = await main(['--stdin', '--stdin-filename=unicorn-file'], {
  22. input: 'console.log()\n',
  23. reject: false
  24. });
  25. t.regex(stdout, /unicorn-file:/);
  26. });
  27. test('reporter option', async t => {
  28. const filepath = await tempWrite('console.log()\n', 'x.js');
  29. try {
  30. await main(['--reporter=compact', filepath]);
  31. } catch (err) {
  32. t.true(err.stdout.includes('Error - '));
  33. }
  34. });
  35. test('overrides fixture', async t => {
  36. const cwd = path.join(__dirname, 'fixtures/overrides');
  37. await t.notThrows(main([], {cwd}));
  38. });
  39. // #65
  40. test.failing('ignores fixture', async t => {
  41. const cwd = path.join(__dirname, 'fixtures/ignores');
  42. await t.throws(main([], {cwd}));
  43. });
  44. test('ignore files in .gitignore', async t => {
  45. const cwd = path.join(__dirname, 'fixtures/gitignore');
  46. const err = await t.throws(main(['--reporter=json'], {cwd}));
  47. const reports = JSON.parse(err.stdout);
  48. const files = reports
  49. .map(report => path.relative(cwd, report.filePath))
  50. .map(report => slash(report));
  51. t.deepEqual(files, ['index.js', 'test/bar.js']);
  52. });
  53. test('ignore explicit files when in .gitgnore', async t => {
  54. const cwd = path.join(__dirname, 'fixtures/gitignore');
  55. await t.notThrows(main(['test/foo.js', '--reporter=json'], {cwd}));
  56. });
  57. test('negative gitignores', async t => {
  58. const cwd = path.join(__dirname, 'fixtures/negative-gitignore');
  59. const err = await t.throws(main(['--reporter=json'], {cwd}));
  60. const reports = JSON.parse(err.stdout);
  61. const files = reports.map(report => path.relative(cwd, report.filePath));
  62. t.deepEqual(files, ['foo.js']);
  63. });
  64. test('supports being extended with a shareable config', async t => {
  65. const cwd = path.join(__dirname, 'fixtures/project');
  66. await t.notThrows(main([], {cwd}));
  67. });
  68. test('quiet option', async t => {
  69. const filepath = await tempWrite('// TODO: quiet\nconsole.log()\n', 'x.js');
  70. const err = await t.throws(main(['--quiet', '--reporter=json', filepath]));
  71. const [report] = JSON.parse(err.stdout);
  72. t.is(report.warningCount, 0);
  73. });
  74. test('init option', async t => {
  75. const filepath = await tempWrite('{}', 'package.json');
  76. await main(['--init'], {
  77. cwd: path.dirname(filepath)
  78. });
  79. const packageJson = fs.readFileSync(filepath, 'utf8');
  80. t.deepEqual(JSON.parse(packageJson).scripts, {test: 'xo'});
  81. });
  82. test('invalid node-engine option', async t => {
  83. const filepath = await tempWrite('console.log()\n', 'x.js');
  84. const err = await t.throws(main(['--node-version', 'v', filepath]));
  85. t.is(err.code, 1);
  86. });
  87. test('cli option takes precedence over config', async t => {
  88. const cwd = path.join(__dirname, 'fixtures/default-options');
  89. const input = 'console.log()\n';
  90. // Use config from package.json
  91. await t.notThrows(main(['--stdin'], {cwd, input}));
  92. // Override package.json config with cli flag
  93. await t.throws(main(['--semicolon=true', '--stdin'], {cwd, input}));
  94. // Use XO default (`true`) even if option is not set in package.json nor cli arg
  95. // i.e make sure absent cli flags are not parsed as `false`
  96. await t.throws(main(['--stdin'], {input}));
  97. });