Added tests for CLI utils

This commit is contained in:
mattstypa 2018-09-25 11:09:20 -05:00
parent dbeeea4dbb
commit 34ad504a46
5 changed files with 157 additions and 16 deletions

View File

@ -1,3 +1,4 @@
/lib
/docs
/__tests__/fixtures/cli-utils.js
defaultConfig.stub.js

111
__tests__/cli.utils.test.js Normal file
View File

@ -0,0 +1,111 @@
import path from 'path'
import * as utils from '../src/cli/utils'
describe('cli utils', () => {
const fixture = utils.readFile(path.resolve(__dirname, 'fixtures/cli-utils.js'))
describe('parseCliParams', () => {
it('parses CLI parameters', () => {
const result = utils.parseCliParams(['a', 'b', '-c', 'd'])
expect(result).toEqual(['a', 'b'])
})
})
describe('parseCliOptions', () => {
it('parses CLI options', () => {
const result = utils.parseCliOptions(['a', '-b', 'c'], { test: ['b'] })
expect(result).toEqual({ test: ['c'] })
})
it('parses multiple types of options', () => {
const result = utils.parseCliOptions(['a', '-b', 'c', '--test', 'd', '-test', 'e'], {
test: ['test', 'b'],
})
expect(result).toEqual({ test: ['c', 'd', 'e'] })
})
it('ignores unknown options', () => {
const result = utils.parseCliOptions(['a', '-b', 'c'], {})
expect(result).toEqual({})
})
it('maps options', () => {
const result = utils.parseCliOptions(['a', '-b', 'c', '-d', 'e'], { test: ['b', 'd'] })
expect(result).toEqual({ test: ['c', 'e'] })
})
it('parses undefined options', () => {
const result = utils.parseCliOptions(['a'], { test: ['b'] })
expect(result).toEqual({ test: undefined })
})
it('parses flags', () => {
const result = utils.parseCliOptions(['a', '-b'], { test: ['b'] })
expect(result).toEqual({ test: [] })
})
it('accepts multiple values per option', () => {
const result = utils.parseCliOptions(['a', '-b', 'c', 'd', '-e', 'f', '-g', 'h'], {
test: ['b', 'g'],
})
expect(result).toEqual({ test: ['c', 'd', 'h'] })
})
})
describe('stripBlockComments', () => {
it('does not strip code', () => {
const result = utils.stripBlockComments(fixture)
expect(result).toEqual(expect.stringContaining('__code_no_comment__'))
expect(result).toEqual(expect.stringContaining('__code_comment_line__'))
expect(result).toEqual(expect.stringContaining('__code_comment_block__'))
expect(result).toEqual(expect.stringContaining('__code_comment_line_important__'))
expect(result).toEqual(expect.stringContaining('__code_comment_block_important__'))
})
it('strips block comments', () => {
const result = utils.stripBlockComments(fixture)
expect(result).not.toEqual(expect.stringContaining('__comment_block__'))
expect(result).not.toEqual(expect.stringContaining('__comment_block_multiline__'))
expect(result).not.toEqual(expect.stringContaining('__comment_block_code__'))
})
it('strips docblock comments', () => {
const result = utils.stripBlockComments(fixture)
expect(result).not.toEqual(expect.stringContaining('__comment_docblock__'))
})
it('does not strip line comments', () => {
const result = utils.stripBlockComments(fixture)
expect(result).toEqual(expect.stringContaining('__comment_line__'))
expect(result).toEqual(expect.stringContaining('__comment_line_important__'))
expect(result).toEqual(expect.stringContaining('__comment_line_code__'))
expect(result).toEqual(expect.stringContaining('__comment_line_important_code__'))
})
it('does not strip important block comments', () => {
const result = utils.stripBlockComments(fixture)
expect(result).toEqual(expect.stringContaining('__comment_block_important__'))
expect(result).toEqual(expect.stringContaining('__comment_block_multiline_important__'))
expect(result).toEqual(expect.stringContaining('__comment_block_important_code__'))
})
it('does not strip important docblock comments', () => {
const result = utils.stripBlockComments(fixture)
expect(result).toEqual(expect.stringContaining('__comment_docblock_important__'))
})
})
})

View File

@ -0,0 +1,29 @@
// __comment_line__
//! __comment_line_important__
/* __comment_block__ */
/*! __comment_block_important__ */
/*
__comment_block_multiline__
*/
/*!
__comment_block_multiline_important__
*/
/**
__comment_docblock__
*/
/**!
__comment_docblock_important__
*/
const __code_no_comment__ = 'test'
const __code_comment_line__ = 'test' // __comment_line_code__
const __code_comment_block__ = 'test' /* __comment_block_code__ */
const __code_comment_line_important__ = 'test' //! __comment_line_important_code__
const __code_comment_block_important__ = 'test' /*! __comment_block_important_code__ */

View File

@ -1,5 +1,4 @@
import chalk from 'chalk'
import stripComments from 'strip-comments'
import * as constants from '../constants'
import * as emoji from '../emoji'
@ -20,20 +19,6 @@ export const optionMap = {
noComments: ['no-comments'],
}
/**
* Strips block comments from input string. Consolidates multiple line breaks.
*
* @param {string} input
* @return {string}
*/
function stripBlockComments(input) {
return stripComments
.block(input, { keepProtected: true })
.replace(/\n\s*\n\s*\n/g, '\n\n') // Strip unnecessary line breaks
.trim()
.concat('\n')
}
/**
* Runs the command.
*
@ -55,7 +40,7 @@ export function run(cliParams, cliOptions) {
.replace('// let defaultConfig', 'let defaultConfig')
.replace("require('./plugins/container')", "require('tailwindcss/plugins/container')")
noComments && (stub = stripBlockComments(stub))
noComments && (stub = utils.stripBlockComments(stub))
utils.writeFile(file, stub)

View File

@ -1,6 +1,7 @@
import chalk from 'chalk'
import { ensureFileSync, existsSync, outputFileSync, readFileSync } from 'fs-extra'
import { findKey, mapValues, trimStart } from 'lodash'
import stripComments from 'strip-comments'
import * as emoji from './emoji'
import packageJson from '../../package.json'
@ -121,3 +122,17 @@ export function writeFile(path, content) {
return outputFileSync(path, content)
}
/**
* Strips block comments from input string. Consolidates multiple line breaks.
*
* @param {string} input
* @return {string}
*/
export function stripBlockComments(input) {
return stripComments
.block(input, { keepProtected: true })
.replace(/\n\s*\n\s*\n/g, '\n\n') // Strip unnecessary line breaks
.trim()
.concat('\n')
}