|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +/*global jest, describe, pit, expect, afterEach*/ |
| 12 | + |
| 13 | +"use strict"; |
| 14 | + |
| 15 | +jest.autoMockOff(); |
| 16 | + |
| 17 | +var child_process = require('child_process'); |
| 18 | +var fs = require('fs'); |
| 19 | +var path = require('path'); |
| 20 | +var rimraf = require('rimraf'); |
| 21 | +var temp = require('temp'); |
| 22 | +require('es6-promise').polyfill(); |
| 23 | + |
| 24 | +function run(args, stdin) { |
| 25 | + return new Promise(resolve => { |
| 26 | + var docgen = child_process.spawn( |
| 27 | + path.join(__dirname, '../react-docgen.js'), |
| 28 | + args |
| 29 | + ); |
| 30 | + var stdout = ''; |
| 31 | + var stderr = ''; |
| 32 | + docgen.stdout.on('data', data => stdout += data); |
| 33 | + docgen.stderr.on('data', data => stderr += data); |
| 34 | + docgen.on('close', () => resolve([stdout, stderr])); |
| 35 | + if (stdin) { |
| 36 | + docgen.stdin.write(stdin); |
| 37 | + } |
| 38 | + docgen.stdin.end(); |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +var component = fs.readFileSync( |
| 43 | + path.join(__dirname, '../../example/components/Component.js') |
| 44 | +); |
| 45 | + |
| 46 | +describe('react-docgen CLI', () => { |
| 47 | + var tempDir; |
| 48 | + var tempComponents = []; |
| 49 | + var tempNoComponents = []; |
| 50 | + |
| 51 | + function createTempfiles(suffix, dir) { |
| 52 | + if (!tempDir) { |
| 53 | + tempDir = temp.mkdirSync(); |
| 54 | + } |
| 55 | + if (!dir) { |
| 56 | + dir = tempDir; |
| 57 | + } else { |
| 58 | + dir = path.join(tempDir, dir); |
| 59 | + try { |
| 60 | + fs.mkdirSync(dir); |
| 61 | + } catch(error) { |
| 62 | + if (error.message.indexOf('EEXIST') === -1) { |
| 63 | + throw error; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + if (!suffix) { |
| 68 | + suffix = 'js'; |
| 69 | + } |
| 70 | + |
| 71 | + var componentPath = path.join(dir, 'Component.' + suffix); |
| 72 | + var componentFile = fs.openSync(componentPath, 'w'); |
| 73 | + fs.writeSync(componentFile, component.toString()); |
| 74 | + fs.closeSync(componentFile); |
| 75 | + var noComponentPath = path.join(dir, 'NoComponent.' + suffix); |
| 76 | + var noComponentFile = fs.openSync(noComponentPath, 'w'); |
| 77 | + fs.writeSync(noComponentFile, '{}'); |
| 78 | + fs.closeSync(noComponentFile); |
| 79 | + |
| 80 | + tempComponents.push(componentPath); |
| 81 | + tempNoComponents.push(noComponentPath); |
| 82 | + |
| 83 | + return dir; |
| 84 | + } |
| 85 | + |
| 86 | + afterEach(() => { |
| 87 | + if (tempDir) { |
| 88 | + rimraf.sync(tempDir); |
| 89 | + } |
| 90 | + tempDir = null; |
| 91 | + tempComponents.length = 0; |
| 92 | + tempNoComponents.length = 0; |
| 93 | + }); |
| 94 | + |
| 95 | + pit('reads from stdin', () => { |
| 96 | + return run(null, component).then(([stdout, stderr]) => { |
| 97 | + expect(stdout.length > 0).toBe(true); |
| 98 | + expect(stderr.length).toBe(0); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + pit('reads files provided as command line arguments', () => { |
| 103 | + createTempfiles(); |
| 104 | + return run(tempComponents.concat(tempNoComponents)).then( |
| 105 | + ([stdout, stderr]) => { |
| 106 | + expect(stdout).toContain('Component'); |
| 107 | + expect(stderr).toContain('NoComponent'); |
| 108 | + } |
| 109 | + ); |
| 110 | + }); |
| 111 | + |
| 112 | + pit('reads directories provided as command line arguments', () => { |
| 113 | + var tempDir = createTempfiles(); |
| 114 | + return run([tempDir]).then(([stdout, stderr]) => { |
| 115 | + expect(stdout).toContain('Component'); |
| 116 | + expect(stderr).toContain('NoComponent'); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + pit('considers js and jsx by default', () => { |
| 121 | + var tempDir = createTempfiles(); |
| 122 | + createTempfiles('jsx'); |
| 123 | + createTempfiles('foo'); |
| 124 | + return run([tempDir]).then(([stdout, stderr]) => { |
| 125 | + expect(stdout).toContain('Component.js'); |
| 126 | + expect(stdout).toContain('Component.jsx'); |
| 127 | + expect(stdout).not.toContain('Component.foo'); |
| 128 | + |
| 129 | + expect(stderr).toContain('NoComponent.js'); |
| 130 | + expect(stderr).toContain('NoComponent.jsx'); |
| 131 | + expect(stderr).not.toContain('NoComponent.foo'); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + pit('considers files with the specified extension', () => { |
| 136 | + createTempfiles('foo'); |
| 137 | + createTempfiles('bar'); |
| 138 | + |
| 139 | + var verify = ([stdout, stderr]) => { |
| 140 | + expect(stdout).toContain('Component.foo'); |
| 141 | + expect(stdout).toContain('Component.bar'); |
| 142 | + |
| 143 | + expect(stderr).toContain('NoComponent.foo'); |
| 144 | + expect(stderr).toContain('NoComponent.bar'); |
| 145 | + }; |
| 146 | + |
| 147 | + |
| 148 | + return Promise.all([ |
| 149 | + run(['--extension=foo', '--extension=bar', tempDir]).then(verify), |
| 150 | + run(['-x', 'foo', '-x', 'bar', tempDir]).then(verify), |
| 151 | + ]); |
| 152 | + }); |
| 153 | + |
| 154 | + pit('ignores files in node_modules and __tests__ by default', () => { |
| 155 | + createTempfiles(null, 'node_modules'); |
| 156 | + createTempfiles(null, '__tests__'); |
| 157 | + |
| 158 | + return run([tempDir]).then(([stdout, stderr]) => { |
| 159 | + expect(stdout).toBe(''); |
| 160 | + expect(stderr).toBe(''); |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + pit('ignores specified folders', () => { |
| 165 | + createTempfiles(null, 'foo'); |
| 166 | + |
| 167 | + var verify = ([stdout, stderr]) => { |
| 168 | + expect(stdout).toBe(''); |
| 169 | + expect(stderr).toBe(''); |
| 170 | + }; |
| 171 | + |
| 172 | + return Promise.all([ |
| 173 | + run(['--ignore=foo', tempDir]).then(verify), |
| 174 | + run(['-i', 'foo', tempDir]).then(verify) |
| 175 | + ]); |
| 176 | + }); |
| 177 | + |
| 178 | + pit('writes to stdout', () => { |
| 179 | + return run(null, component).then(([stdout, stderr]) => { |
| 180 | + expect(stdout.length > 0).toBe(true); |
| 181 | + expect(stderr.length).toBe(0); |
| 182 | + }); |
| 183 | + }); |
| 184 | + |
| 185 | + pit('writes to stderr', () => { |
| 186 | + return run(null, '{}').then(([stdout, stderr]) => { |
| 187 | + expect(stderr.length > 0).toBe(true); |
| 188 | + expect(stdout.length).toBe(0); |
| 189 | + }); |
| 190 | + }); |
| 191 | + |
| 192 | + pit('writes to a file if provided', function() { |
| 193 | + var outFile = temp.openSync(); |
| 194 | + createTempfiles(); |
| 195 | + |
| 196 | + var verify = ([stdout, stderr]) => { |
| 197 | + expect(fs.readFileSync(outFile.path)).not.toBe(''); |
| 198 | + expect(stdout).toBe(''); |
| 199 | + }; |
| 200 | + |
| 201 | + return Promise.all([ |
| 202 | + run(['--out=' + outFile.path, tempDir]).then(verify), |
| 203 | + run(['-o', outFile.path, tempDir]).then(verify), |
| 204 | + ]); |
| 205 | + }); |
| 206 | + |
| 207 | +}); |
0 commit comments