|
| 1 | +var test = require('tap').test, |
| 2 | + path = require('path'), |
| 3 | + os = require('os'), |
| 4 | + exec = require('child_process').exec, |
| 5 | + tmp = require('tmp'), |
| 6 | + fs = require('fs-extra'); |
| 7 | + |
| 8 | +function documentation(args, options, callback, parseJSON) { |
| 9 | + if (!callback) { |
| 10 | + callback = options; |
| 11 | + options = {}; |
| 12 | + } |
| 13 | + |
| 14 | + if (!options.cwd) { |
| 15 | + options.cwd = __dirname; |
| 16 | + } |
| 17 | + |
| 18 | + options.maxBuffer = 1024 * 1024; |
| 19 | + |
| 20 | + args.unshift(path.join(__dirname, '../bin/documentation.js')); |
| 21 | + |
| 22 | + exec(args.join(' '), options, callback); |
| 23 | +} |
| 24 | + |
| 25 | +test('readme command', function (group) { |
| 26 | + var fixtures = path.join(__dirname, 'fixture/readme'); |
| 27 | + var sourceFile = path.join(fixtures, 'index.js'); |
| 28 | + |
| 29 | + tmp.dir({unsafeCleanup: true}, function (err, d) { |
| 30 | + group.error(err); |
| 31 | + fs.copySync(path.join(fixtures, 'README.input.md'), path.join(d, 'README.md')); |
| 32 | + fs.copySync(path.join(fixtures, 'index.js'), path.join(d, 'index.js')); |
| 33 | + |
| 34 | + // run tests after setting up temp dir |
| 35 | + |
| 36 | + group.test('--diff-only: changes needed', function (t) { |
| 37 | + t.error(err); |
| 38 | + var before = fs.readFileSync(path.join(d, 'README.md'), 'utf-8'); |
| 39 | + documentation(['readme index.js --diff-only -s API'], {cwd: d}, function (err, stdout, stderr) { |
| 40 | + var after = fs.readFileSync(path.join(d, 'README.md'), 'utf-8'); |
| 41 | + t.ok(err); |
| 42 | + t.notEqual(err.code, 0, 'exit nonzero'); |
| 43 | + t.same(after, before, 'readme unchanged'); |
| 44 | + t.end(); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + var expectedFile = path.join(fixtures, 'README.output.md'); |
| 49 | + var expected = fs.readFileSync(expectedFile, 'utf-8'); |
| 50 | + |
| 51 | + group.test('updates README.md', function (t) { |
| 52 | + documentation(['readme index.js -s API'], {cwd: d}, function (err, stdout) { |
| 53 | + t.error(err); |
| 54 | + var actual = fs.readFileSync(path.join(d, 'README.md'), 'utf-8'); |
| 55 | + t.same(actual, expected, 'generated readme output'); |
| 56 | + t.end(); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + group.test('--readme-file', function (t) { |
| 61 | + fs.copySync(path.join(fixtures, 'README.input.md'), path.join(d, 'other.md')); |
| 62 | + documentation(['readme index.js -s API --readme-file other.md'], {cwd: d}, function (err, stdout) { |
| 63 | + t.error(err); |
| 64 | + var actual = fs.readFileSync(path.join(d, 'other.md'), 'utf-8'); |
| 65 | + t.same(actual, expected, 'generated readme output'); |
| 66 | + t.end(); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + group.test('--diff-only: changes NOT needed', function (t) { |
| 71 | + t.error(err); |
| 72 | + fs.copySync(path.join(fixtures, 'README.output.md'), path.join(d, 'uptodate.md')); |
| 73 | + documentation(['readme index.js --diff-only -s API --readme-file uptodate.md'], |
| 74 | + {cwd: d}, function (err, stdout, stderr) { |
| 75 | + t.error(err); |
| 76 | + t.match(stdout, 'is up to date.'); |
| 77 | + t.end(); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + group.test('requires -s option', function (t) { |
| 82 | + documentation(['readme index.js'], {cwd: d}, function (err, stdout, stderr) { |
| 83 | + t.ok(err); |
| 84 | + t.ok(err.code !== 0, 'exit nonzero'); |
| 85 | + t.match(stderr, 'Missing required argument: s'); |
| 86 | + t.end(); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + group.end(); |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
0 commit comments