Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Fix: sync API incorrectly required relativePath option
Browse files Browse the repository at this point in the history
- `options` should be optional for sync methods too.
- TODO: add API interface tests for other APIs

BREAKING: CLI argument `relative` renamed to `relativePath`
  • Loading branch information
James Ramsay committed Jan 30, 2016
1 parent fc593d2 commit a1dc8b5
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 18 deletions.
39 changes: 24 additions & 15 deletions src/hercule.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,18 @@ export function transcludeFile(...args) {
}


export function transcludeFileSync(input, { relativePath }, log) {
const options = {
cwd: __dirname,
timeout: SYNC_TIMEOUT,
};
const args = [input, '--reporter', 'json-err'];
const result = childProcess.spawnSync('../bin/hercule', args, options);
export function transcludeFileSync(...args) {
const input = args.shift();
const [options, log] = args;

const syncOptions = { cwd: __dirname, timeout: SYNC_TIMEOUT };
const syncArgs = [input, '--reporter', 'json-err'];

_.each(options, (optionValue, optionName) => {
syncArgs.push(`--${optionName}`, `${optionValue}`);
});

const result = childProcess.spawnSync('../bin/hercule', syncArgs, syncOptions);
const outputContent = result.stdout.toString();
const outputLogs = result.stderr.toString().split('\n');

Expand All @@ -86,14 +91,18 @@ export function transcludeFileSync(input, { relativePath }, log) {
}


export function transcludeStringSync(input, { relativePath }, log) {
const options = {
input,
cwd: __dirname,
timeout: SYNC_TIMEOUT,
};
const args = ['--relative', relativePath, '--reporter', 'json-err'];
const result = childProcess.spawnSync('../bin/hercule', args, options);
export function transcludeStringSync(...args) {
const input = args.shift();
const [options, log] = args;

const syncOptions = { input, cwd: __dirname, timeout: SYNC_TIMEOUT };
const syncArgs = ['--reporter', 'json-err'];

_.each(options, (optionValue, optionName) => {
syncArgs.push(`--${optionName}`, `${optionValue}`);
});

const result = childProcess.spawnSync('../bin/hercule', syncArgs, syncOptions);
const outputContent = result.stdout.toString();
const outputLogs = result.stderr.toString().split('\n');

Expand Down
5 changes: 3 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const parser = dashdash.createParser({
helpArg: 'FILE',
},
{
names: ['relative', 'r'],
names: ['relativePath', 'r'],
type: 'string',
help: 'Relative path. stdin will be parsed relative to this path.',
},
Expand Down Expand Up @@ -73,9 +73,10 @@ function main() {
if (opts._args.length === 0) {
// Reading input from stdin
inputStream = process.stdin;
options.relativePath = opts.relative;
options.relativePath = opts.relativePath;
} else {
// Reading input from file
// TODO: handle file error!
inputStream = fs.createReadStream(opts._args[0], { encoding: 'utf8' });
options.relativePath = path.dirname(opts._args[0]);
}
Expand Down
2 changes: 1 addition & 1 deletion test/bats
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}

@test "transcluding stdin" {
result="$(cat ./test/fixtures/local-link/index.md | ./bin/hercule --relative ./test/fixtures/local-link)"
result="$(cat ./test/fixtures/local-link/index.md | ./bin/hercule --relativePath ./test/fixtures/local-link)"
expected="Jackdaws love my big sphinx of quartz."
[ "$result" == "$expected" ]
}
21 changes: 21 additions & 0 deletions test/units/transcludeFileSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import test from 'ava';

import { transcludeFileSync } from '../../lib/hercule';

let major;
let minor;

[major, minor] = process.versions.node.split('.');

if (major < 1 && minor < 12) {
test.only(`synchronous support not available < 0.12`, (t) => {
t.pass();
});
}

test(`should transclude with only required arguments`, (t) => {
const input = '../test/fixtures/no-link/index.md';
const expected = 'The quick brown fox jumps over the lazy dog.\n';
const output = transcludeFileSync(input);
t.same(output, expected);
});
21 changes: 21 additions & 0 deletions test/units/transcludeStringSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import test from 'ava';

import { transcludeStringSync } from '../../lib/hercule';

let major;
let minor;

[major, minor] = process.versions.node.split('.');

if (major < 1 && minor < 12) {
test.only(`synchronous support not available < 0.12`, (t) => {
t.pass();
});
}

test(`should transclude with only required arguments`, (t) => {
const input = 'The quick brown fox jumps over the lazy dog.';
const expected = 'The quick brown fox jumps over the lazy dog.';
const output = transcludeStringSync(input);
t.same(output, expected);
});

0 comments on commit a1dc8b5

Please sign in to comment.