From 862bce837e2e3dcf1d8575c10b999b66bfce1dca Mon Sep 17 00:00:00 2001 From: David Mosher Date: Thu, 3 May 2018 13:42:45 -0400 Subject: [PATCH] add test harness for cli.js and stdout bugfix - when used on the CLI, lib/cli.js was writing the output of log statements to STDOUT; this caused consumers of the CLI who wanted to pipe the processed output (css) to other unix utilities to end up with invalid CSS due to the log messages appearing at the start and end of the files - this commit fixes this by replacing `console.log` with `console.warn` and `console.error`, which both write output to STDERR --- __tests__/cli.test.js | 21 +++++++++++++++++++++ __tests__/fixtures/tailwind-cli-input.css | 3 +++ __tests__/fixtures/tailwind-cli-output.css | 3 +++ src/cli.js | 10 +++++----- 4 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 __tests__/cli.test.js create mode 100644 __tests__/fixtures/tailwind-cli-input.css create mode 100644 __tests__/fixtures/tailwind-cli-output.css diff --git a/__tests__/cli.test.js b/__tests__/cli.test.js new file mode 100644 index 000000000..19364ca08 --- /dev/null +++ b/__tests__/cli.test.js @@ -0,0 +1,21 @@ +import { spawnSync } from 'child_process' +import fs from 'fs' +import path from 'path' + +function runCli(task, options) { + return spawnSync('node', [`${path.join(process.cwd(), 'lib/cli.js')}`, `${task}`, ...options]) +} + +function pathToFixture(fixture) { + return path.resolve(`${__dirname}/fixtures/${fixture}`) +} + +function readFixture(fixture) { + return fs.readFileSync(pathToFixture(fixture), 'utf8') +} + +test('stdout only contains processed output', () => { + const expected = readFixture('tailwind-cli-output.css') + const result = runCli('build', [pathToFixture('tailwind-cli-input.css')]) + expect(result.stdout.toString()).toEqual(expected) +}) diff --git a/__tests__/fixtures/tailwind-cli-input.css b/__tests__/fixtures/tailwind-cli-input.css new file mode 100644 index 000000000..563d20e99 --- /dev/null +++ b/__tests__/fixtures/tailwind-cli-input.css @@ -0,0 +1,3 @@ +body { + color: green; +} diff --git a/__tests__/fixtures/tailwind-cli-output.css b/__tests__/fixtures/tailwind-cli-output.css new file mode 100644 index 000000000..563d20e99 --- /dev/null +++ b/__tests__/fixtures/tailwind-cli-output.css @@ -0,0 +1,3 @@ +body { + color: green; +} diff --git a/src/cli.js b/src/cli.js index 18b25584e..5433800b6 100755 --- a/src/cli.js +++ b/src/cli.js @@ -20,7 +20,7 @@ function writeStrategy(options) { } function buildTailwind(inputFile, config, write) { - console.log('Building Tailwind!') + console.warn('Building Tailwind!') const input = fs.readFileSync(inputFile, 'utf8') @@ -28,9 +28,9 @@ function buildTailwind(inputFile, config, write) { .process(input, { from: inputFile }) .then(result => { write(result.css) - console.log('Finished building Tailwind!') + console.warn('Finished building Tailwind!') }) - .catch(error => console.log(error)) + .catch(error => console.error(error)) } const packageJson = require(path.resolve(__dirname, '../package.json')) @@ -48,7 +48,7 @@ program } if (fs.existsSync(destination)) { - console.log(`Destination ${destination} already exists, aborting.`) + console.error(`Destination ${destination} already exists, aborting.`) process.exit(1) } @@ -58,7 +58,7 @@ program destination, output.replace("require('./plugins/container')", "require('tailwindcss/plugins/container')") ) - console.log(`Generated Tailwind config: ${destination}`) + console.warn(`Generated Tailwind config: ${destination}`) process.exit() })