Skip to content

Commit 42ccf27

Browse files
committed
improve tests, fix build
1 parent bec04fc commit 42ccf27

8 files changed

Lines changed: 214 additions & 76 deletions

File tree

packages/prettyhtml/cli/index.js

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,54 @@
33
'use strict'
44

55
const PassThrough = require('stream').PassThrough
6-
const notifier = require('update-notifier')
7-
const engine = require('unified-engine')
8-
const unified = require('unified')
96
const { basename } = require('path')
107
const pack = require('./../package')
11-
const prettier = require('prettier')
8+
const { configTransform, processResult } = require('./processor')
129
const args = require('./args')
13-
const { transform, processor } = require('./processor')
14-
15-
const extensions = ['html']
16-
17-
notifier({ pkg: pack }).notify()
18-
19-
const prettierConfig = prettier.resolveConfig.sync(process.cwd()) || {}
20-
const cli = args(prettierConfig)
10+
const prettier = require('prettier')
11+
const engine = require('unified-engine')
12+
const unified = require('unified')
13+
const notifier = require('update-notifier')
2114

22-
const settings = {
23-
processor: unified(),
24-
extensions: extensions,
25-
configTransform: transform,
26-
streamError: new PassThrough(), // sink errors
27-
rcName: '.prettyhtmlrc',
28-
packageField: 'prettyhtml',
29-
ignoreName: '.prettyhtmlignore',
30-
frail: false,
31-
defaultConfig: transform({ prettierConfig, flags: cli.flags })
15+
function getDefaultSettings() {
16+
const settings = {
17+
extensions: ['html'],
18+
streamError: new PassThrough(), // sink errors
19+
rcName: '.prettyhtmlrc',
20+
packageField: 'prettyhtml',
21+
ignoreName: '.prettyhtmlignore',
22+
frail: false
23+
}
24+
return settings
3225
}
3326

34-
const processResult = processor({ flags: cli.flags })
35-
36-
if (cli.flags.stdin === false) {
37-
if (cli.input.length === 0) {
38-
cli.showHelp()
27+
module.exports = { getDefaultSettings }
28+
29+
// this was run directly from the command line
30+
if (require.main === module) {
31+
notifier({ pkg: pack }).notify()
32+
33+
const prettierConfig = prettier.resolveConfig.sync(process.cwd()) || {}
34+
const cli = args(prettierConfig)
35+
const settings = getDefaultSettings()
36+
settings.configTransform = configTransform
37+
settings.defaultConfig = configTransform({ prettierConfig, cli })
38+
settings.processor = unified()
39+
40+
if (cli.flags.stdin === false) {
41+
if (cli.input.length === 0) {
42+
cli.showHelp()
43+
} else {
44+
settings.files = cli.input
45+
settings.output = true // Whether to overwrite the input files
46+
settings.out = false // Whether to write the processed file to streamOut
47+
48+
engine(settings, processResult({ cli }))
49+
}
3950
} else {
40-
settings.files = cli.input
41-
settings.output = true // Whether to overwrite the input files
42-
settings.out = false // Whether to write the processed file to streamOut
43-
44-
engine(settings, processResult)
45-
}
46-
} else {
47-
if (cli.input.length !== 0) {
48-
settings.output = basename(cli.input[0])
51+
if (cli.input.length !== 0) {
52+
settings.output = basename(cli.input[0])
53+
}
54+
engine(settings, processResult({ cli }))
4955
}
50-
engine(settings, processResult)
5156
}

packages/prettyhtml/cli/processor.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ const sortAttributes = require('@starptech/prettyhtml-sort-attributes')
44
const format = require('@starptech/prettyhtml-formatter')
55
const report = require('vfile-reporter')
66

7-
function processor({ flags }) {
8-
return function processResult(err, code, result) {
7+
function processResult({ cli }) {
8+
return (err, code, result) => {
99
const out = report(err || result.files, {
10-
quiet: flags.quiet,
11-
silent: flags.silent
10+
quiet: cli.flags.quiet,
11+
silent: cli.flags.silent
1212
})
1313

1414
if (out) {
@@ -19,7 +19,7 @@ function processor({ flags }) {
1919
}
2020
}
2121

22-
function transform({ prettierConfig, flags }) {
22+
function configTransform({ prettierConfig, cli }) {
2323
const plugins = [
2424
[
2525
parse,
@@ -33,30 +33,30 @@ function transform({ prettierConfig, flags }) {
3333
[
3434
format,
3535
{
36-
tabWidth: flags.tabWidth,
37-
useTabs: flags.useTabs,
38-
singleQuote: flags.singleQuote,
39-
usePrettier: flags.usePrettier,
36+
tabWidth: cli.flags.tabWidth,
37+
useTabs: cli.flags.useTabs,
38+
singleQuote: cli.flags.singleQuote,
39+
usePrettier: cli.flags.usePrettier,
4040
prettier: prettierConfig
4141
}
4242
]
4343
]
4444

45-
if (flags.sortAttributes || prettierConfig.sortAttributes) {
45+
if (cli.flags.sortAttributes || prettierConfig.sortAttributes) {
4646
plugins.push([sortAttributes, {}])
4747
}
4848

4949
plugins.push([
5050
stringify,
5151
{
52-
tabWidth: flags.tabWidth,
53-
printWidth: flags.printWidth,
54-
singleQuote: flags.singleQuote,
55-
wrapAttributes: flags.wrapAttributes
52+
tabWidth: cli.flags.tabWidth,
53+
printWidth: cli.flags.printWidth,
54+
singleQuote: cli.flags.singleQuote,
55+
wrapAttributes: cli.flags.wrapAttributes
5656
}
5757
])
5858

5959
return { plugins }
6060
}
6161

62-
module.exports = { transform, processor }
62+
module.exports = { configTransform, processResult }
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict'
2+
3+
var PassThrough = require('stream').PassThrough
4+
5+
module.exports = spy
6+
7+
function spy() {
8+
var stream = new PassThrough()
9+
var output = []
10+
var originalWrite = stream.write
11+
12+
stream.write = write
13+
14+
done.stream = stream
15+
16+
return done
17+
18+
function write(chunk, encoding, callback) {
19+
callback = typeof encoding === 'function' ? encoding : callback
20+
21+
if (typeof callback === 'function') {
22+
setImmediate(callback)
23+
}
24+
25+
output.push(chunk)
26+
}
27+
28+
function done() {
29+
stream.write = originalWrite
30+
31+
return output.join('')
32+
}
33+
}

packages/prettyhtml/test/cli.js

Lines changed: 100 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,106 @@
11
const test = require('ava')
2-
const path = require('path')
3-
const { writeFileSync, readFileSync } = require('fs')
4-
const { spawnSync } = require('child_process')
5-
6-
const inputFile = path.join(__dirname, 'fixtures', 'default', 'input.html')
7-
const outputFile = path.join(__dirname, 'fixtures', 'default', 'output.html')
8-
const inputData = readFileSync(inputFile)
9-
const cliPath = path.join(__dirname, '..', 'cli', 'index.js')
10-
11-
test('Should format with default settings', t => {
12-
writeFileSync(
13-
inputFile,
14-
'<form #heroForm (ngSubmit)="onSubmit(heroForm)"><input type="text" [(onChange)]="dede" name="test"><button [style.color]="isSpecial ? \'red\' : \'green\'"></button></form>'
2+
const { Readable } = require('stream')
3+
const { configTransform } = require('../cli/processor')
4+
const { getDefaultSettings } = require('../cli')
5+
const spy = require('../test-helpers/util')
6+
const args = require('../cli/args')
7+
const engine = require('unified-engine')
8+
const unified = require('unified')
9+
10+
test.cb('Should format with default settings', t => {
11+
const stream = new Readable()
12+
stream._read = () => {}
13+
stream.push(
14+
`<form #heroForm (ngSubmit)="onSubmit(heroForm)"><input type="text" [(onChange)]="dede" name="test" /><button [style.color]="isSpecial ? 'red' : 'green'"></button></form>`
15+
)
16+
stream.push(null)
17+
18+
const stderr = spy()
19+
const stdout = spy()
20+
21+
const prettierConfig = {}
22+
const cli = args(prettierConfig)
23+
const settings = getDefaultSettings({ prettierConfig, cli })
24+
settings.configTransform = configTransform
25+
settings.defaultConfig = configTransform({ prettierConfig, cli })
26+
settings.processor = unified()
27+
settings.streamIn = stream
28+
settings.streamError = stderr.stream
29+
settings.streamOut = stdout.stream
30+
31+
engine(
32+
{
33+
...settings,
34+
streamIn: stream,
35+
streamError: stderr.stream
36+
},
37+
(err, code, result) => {
38+
t.falsy(err)
39+
t.deepEqual([stderr(), code], ['<stdin>: no issues found\n', 0])
40+
t.snapshot(result.files[0])
41+
t.end()
42+
}
1543
)
16-
const argv = [cliPath, inputFile]
17-
const result = spawnSync('node', argv)
44+
})
45+
46+
test('Should use correct default settings when no prettier settings was provided', t => {
47+
const prettierConfig = {}
48+
const cli = args(prettierConfig)
49+
t.deepEqual(cli.flags, {
50+
printWidth: 80,
51+
quiet: false,
52+
silent: false,
53+
singleQuote: false,
54+
sortAttributes: false,
55+
stdin: false,
56+
tabWidth: 2,
57+
usePrettier: true,
58+
useTabs: false,
59+
wrapAttributes: false
60+
})
61+
})
62+
63+
test('Should use correct settings when prettier settings was provided', t => {
64+
const prettierConfig = {
65+
printWidth: 120,
66+
tabWidth: 4,
67+
singleQuote: true,
68+
useTabs: true
69+
}
70+
const cli = args(prettierConfig)
71+
t.deepEqual(cli.flags, {
72+
printWidth: 120,
73+
quiet: false,
74+
silent: false,
75+
// Dont let it override by prettier settings because `"` is best practice in HTML
76+
singleQuote: false,
77+
sortAttributes: false,
78+
stdin: false,
79+
tabWidth: 4,
80+
usePrettier: true,
81+
useTabs: true,
82+
wrapAttributes: false
83+
})
84+
})
85+
86+
test('Should transform config in unified pipes', t => {
87+
const prettierConfig = {}
88+
const cli = args(prettierConfig)
89+
const config = configTransform({ prettierConfig, cli })
90+
t.is(config.plugins.length, 3)
91+
})
1892

19-
t.is(result.status, 0)
20-
t.is(readFileSync(inputFile, 'utf8'), readFileSync(outputFile, 'utf8'))
93+
test('Should add sortAttributes plugin to the config', t => {
94+
const prettierConfig = { sortAttributes: true }
95+
const cli = args(prettierConfig)
96+
const config = configTransform({ prettierConfig, cli })
97+
t.is(config.plugins.length, 4)
2198
})
2299

23-
test.after.always('restore file', t => {
24-
writeFileSync(inputFile, inputData)
100+
test('Should add sortAttributes plugin to the config when it was passed as a flag', t => {
101+
const prettierConfig = {}
102+
const cli = args(prettierConfig)
103+
cli.flags.sortAttributes = true
104+
const config = configTransform({ prettierConfig, cli })
105+
t.is(config.plugins.length, 4)
25106
})

packages/prettyhtml/test/fixtures/default/input.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/prettyhtml/test/fixtures/default/output.html

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Snapshot report for `test/cli.js`
2+
3+
The actual snapshot is saved in `cli.js.snap`.
4+
5+
Generated by [AVA](https://ava.li).
6+
7+
## Should format with default settings
8+
9+
> Snapshot 1
10+
11+
VFile {
12+
contents: `<form #heroForm (ngSubmit)="onSubmit(heroForm)">␊
13+
<input type="text" [(onChange)]="dede" name="test">␊
14+
<button [style.color]="isSpecial ? 'red' : 'green'"></button>␊
15+
</form>␊
16+
`,
17+
cwd: 'D:\\Repositories\\prettyhtml\\packages\\prettyhtml',
18+
data: {
19+
unifiedEngineGiven: true,
20+
unifiedEngineStreamIn: true,
21+
},
22+
history: [],
23+
messages: [],
24+
}
459 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)