diff --git a/bin/graph.js b/bin/graph.js index 70c14fe6..8a7a0fdd 100644 --- a/bin/graph.js +++ b/bin/graph.js @@ -101,8 +101,12 @@ function main (args) { process.exit(0) } - if (!fs.existsSync(opts.outputDir)) { - fs.mkdirSync(opts.outputDir) + const fullOutputDir = opts.output + ? path.join(opts.outputDir, path.dirname(opts.output)) + : opts.outputDir + + if (!fs.existsSync(fullOutputDir)) { + fs.mkdirSync(fullOutputDir) } getStdin().then((txt) => { @@ -114,7 +118,7 @@ function main (args) { const write = (info, _, done) => { const itemName = getItemName(info) - const outPath = path.resolve(opts.outputDir, `${itemName}.${info.format}`) + const outPath = path.resolve(fullOutputDir, `${itemName}.${info.format}`) if (pipeToStdOut) { str(info.body) diff --git a/test/integration/orca_graph_test.js b/test/integration/orca_graph_test.js index 28d0585e..89272fff 100644 --- a/test/integration/orca_graph_test.js +++ b/test/integration/orca_graph_test.js @@ -1,12 +1,11 @@ const tap = require('tap') +const fs = require('fs') +const path = require('path') const { spawn } = require('child_process') const { paths } = require('../common') -const BASE_ARGS = [ - 'graph', - '--output-dir', paths.build, - '--verbose' -] +const BASE_ARGS = ['graph', '--verbose'] +const DUMMY_DATA = '{ "data": [{"y": [1,2,1]}] }' const _spawn = (t, args) => { const allArgs = args ? BASE_ARGS.concat(args) : BASE_ARGS @@ -46,3 +45,75 @@ tap.test('should log message when no input is given', t => { t.end() }) }) + +tap.test('should output to fig.png when --output is not set', t => { + const subprocess = _spawn(t, DUMMY_DATA) + const p = path.join(process.cwd(), 'fig.png') + + subprocess.on('close', code => { + t.same(code, 0) + t.ok(fs.existsSync(p)) + fs.unlinkSync(p) + t.end() + }) +}) + +tap.test('should respect --output when set (just filename case)', t => { + const subprocess = _spawn(t, [DUMMY_DATA, '--output=graph.png']) + const p = path.join(process.cwd(), 'graph.png') + + subprocess.on('close', code => { + t.same(code, 0) + t.ok(fs.existsSync(p)) + fs.unlinkSync(p) + t.end() + }) +}) + +tap.test('should respect --output when set (path/to/filename case)', t => { + const subprocess = _spawn(t, [DUMMY_DATA, '--output=build/tmp/graph.png']) + const p = path.join(process.cwd(), 'build', 'tmp', 'graph.png') + + subprocess.on('close', code => { + t.same(code, 0) + t.ok(fs.existsSync(p)) + fs.unlinkSync(p) + t.end() + }) +}) + +tap.test('should respect --output-dir when set', t => { + const subprocess = _spawn(t, [DUMMY_DATA, '--output-dir=build']) + const p = path.join(process.cwd(), 'build', 'fig.png') + + subprocess.on('close', code => { + t.same(code, 0) + t.ok(fs.existsSync(p)) + fs.unlinkSync(p) + t.end() + }) +}) + +tap.test('should respect --output (filename) and --output-dir when set', t => { + const subprocess = _spawn(t, [DUMMY_DATA, '--output-dir=build', '--output=graph.png']) + const p = path.join(process.cwd(), 'build', 'graph.png') + + subprocess.on('close', code => { + t.same(code, 0) + t.ok(fs.existsSync(p)) + fs.unlinkSync(p) + t.end() + }) +}) + +tap.test('should respect --output (path/to/filename) and --output-dir when set', t => { + const subprocess = _spawn(t, [DUMMY_DATA, '--output-dir=build', '--output=tmp/graph.png']) + const p = path.join(process.cwd(), 'build', 'tmp', 'graph.png') + + subprocess.on('close', code => { + t.same(code, 0) + t.ok(fs.existsSync(p)) + fs.unlinkSync(p) + t.end() + }) +})