-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(test-console-render): destructure & async/await (#3980)
* refactor(test-console-render): async/await * refactor(test-console-render): destructure * style: asyncArrow
- Loading branch information
Showing
1 changed file
with
98 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,118 +1,130 @@ | ||
'use strict'; | ||
|
||
const fs = require('hexo-fs'); | ||
const pathFn = require('path'); | ||
const { mkdirs, readFile, rmdir, unlink, writeFile } = require('hexo-fs'); | ||
const { join } = require('path'); | ||
const Promise = require('bluebird'); | ||
|
||
describe('render', () => { | ||
const Hexo = require('../../../lib/hexo'); | ||
const hexo = new Hexo(pathFn.join(__dirname, 'render_test'), {silent: true}); | ||
const hexo = new Hexo(join(__dirname, 'render_test'), {silent: true}); | ||
const render = require('../../../lib/plugins/console/render').bind(hexo); | ||
|
||
before(() => fs.mkdirs(hexo.base_dir).then(() => hexo.init())); | ||
before(async () => { | ||
await mkdirs(hexo.base_dir); | ||
hexo.init(); | ||
}); | ||
|
||
after(() => fs.rmdir(hexo.base_dir)); | ||
after(() => rmdir(hexo.base_dir)); | ||
|
||
const body = [ | ||
'foo: 1', | ||
'bar:', | ||
' boo: 2' | ||
].join('\n'); | ||
|
||
it('relative path', () => { | ||
const src = pathFn.join(hexo.base_dir, 'test.yml'); | ||
const dest = pathFn.join(hexo.base_dir, 'result.json'); | ||
|
||
return fs.writeFile(src, body).then(() => render({_: ['test.yml'], output: 'result.json'})).then(() => fs.readFile(dest)).then(result => { | ||
JSON.parse(result).should.eql({ | ||
foo: 1, | ||
bar: { | ||
boo: 2 | ||
} | ||
}); | ||
|
||
return Promise.all([ | ||
fs.unlink(src), | ||
fs.unlink(dest) | ||
]); | ||
it('relative path', async () => { | ||
const src = join(hexo.base_dir, 'test.yml'); | ||
const dest = join(hexo.base_dir, 'result.json'); | ||
|
||
await writeFile(src, body); | ||
await render({_: ['test.yml'], output: 'result.json'}); | ||
const result = await readFile(dest); | ||
JSON.parse(result).should.eql({ | ||
foo: 1, | ||
bar: { | ||
boo: 2 | ||
} | ||
}); | ||
|
||
await Promise.all([ | ||
unlink(src), | ||
unlink(dest) | ||
]); | ||
}); | ||
|
||
it('absolute path', () => { | ||
const src = pathFn.join(hexo.base_dir, 'test.yml'); | ||
const dest = pathFn.join(hexo.base_dir, 'result.json'); | ||
|
||
return fs.writeFile(src, body).then(() => render({_: [src], output: 'result.json'})).then(() => fs.readFile(dest)).then(result => { | ||
JSON.parse(result).should.eql({ | ||
foo: 1, | ||
bar: { | ||
boo: 2 | ||
} | ||
}); | ||
|
||
return Promise.all([ | ||
fs.unlink(src), | ||
fs.unlink(dest) | ||
]); | ||
it('absolute path', async () => { | ||
const src = join(hexo.base_dir, 'test.yml'); | ||
const dest = join(hexo.base_dir, 'result.json'); | ||
|
||
await writeFile(src, body); | ||
await render({_: [src], output: 'result.json'}); | ||
|
||
const result = await readFile(dest); | ||
JSON.parse(result).should.eql({ | ||
foo: 1, | ||
bar: { | ||
boo: 2 | ||
} | ||
}); | ||
|
||
await Promise.all([ | ||
unlink(src), | ||
unlink(dest) | ||
]); | ||
}); | ||
|
||
it('absolute output', () => { | ||
const src = pathFn.join(hexo.base_dir, 'test.yml'); | ||
const dest = pathFn.join(hexo.base_dir, 'result.json'); | ||
|
||
return fs.writeFile(src, body).then(() => render({_: ['test.yml'], output: dest})).then(() => fs.readFile(dest)).then(result => { | ||
JSON.parse(result).should.eql({ | ||
foo: 1, | ||
bar: { | ||
boo: 2 | ||
} | ||
}); | ||
|
||
return Promise.all([ | ||
fs.unlink(src), | ||
fs.unlink(dest) | ||
]); | ||
it('absolute output', async () => { | ||
const src = join(hexo.base_dir, 'test.yml'); | ||
const dest = join(hexo.base_dir, 'result.json'); | ||
|
||
await writeFile(src, body); | ||
await render({_: ['test.yml'], output: dest}); | ||
|
||
const result = await readFile(dest); | ||
JSON.parse(result).should.eql({ | ||
foo: 1, | ||
bar: { | ||
boo: 2 | ||
} | ||
}); | ||
|
||
await Promise.all([ | ||
unlink(src), | ||
unlink(dest) | ||
]); | ||
}); | ||
|
||
// it('output'); missing-unit-test | ||
|
||
it('engine', () => { | ||
const src = pathFn.join(hexo.base_dir, 'test'); | ||
const dest = pathFn.join(hexo.base_dir, 'result.json'); | ||
|
||
return fs.writeFile(src, body).then(() => render({_: ['test'], output: 'result.json', engine: 'yaml'})).then(() => fs.readFile(dest)).then(result => { | ||
JSON.parse(result).should.eql({ | ||
foo: 1, | ||
bar: { | ||
boo: 2 | ||
} | ||
}); | ||
|
||
return Promise.all([ | ||
fs.unlink(src), | ||
fs.unlink(dest) | ||
]); | ||
it('engine', async () => { | ||
const src = join(hexo.base_dir, 'test'); | ||
const dest = join(hexo.base_dir, 'result.json'); | ||
|
||
await writeFile(src, body); | ||
await render({_: ['test'], output: 'result.json', engine: 'yaml'}); | ||
|
||
const result = await readFile(dest); | ||
JSON.parse(result).should.eql({ | ||
foo: 1, | ||
bar: { | ||
boo: 2 | ||
} | ||
}); | ||
|
||
await Promise.all([ | ||
unlink(src), | ||
unlink(dest) | ||
]); | ||
}); | ||
|
||
it('pretty', () => { | ||
const src = pathFn.join(hexo.base_dir, 'test.yml'); | ||
const dest = pathFn.join(hexo.base_dir, 'result.json'); | ||
|
||
return fs.writeFile(src, body).then(() => render({_: ['test.yml'], output: 'result.json', pretty: true})).then(() => fs.readFile(dest)).then(result => { | ||
result.should.eql(JSON.stringify({ | ||
foo: 1, | ||
bar: { | ||
boo: 2 | ||
} | ||
}, null, ' ')); | ||
|
||
return Promise.all([ | ||
fs.unlink(src), | ||
fs.unlink(dest) | ||
]); | ||
}); | ||
it('pretty', async () => { | ||
const src = join(hexo.base_dir, 'test.yml'); | ||
const dest = join(hexo.base_dir, 'result.json'); | ||
|
||
await writeFile(src, body); | ||
await render({_: ['test.yml'], output: 'result.json', pretty: true}); | ||
|
||
const result = await readFile(dest); | ||
result.should.eql(JSON.stringify({ | ||
foo: 1, | ||
bar: { | ||
boo: 2 | ||
} | ||
}, null, ' ')); | ||
|
||
await Promise.all([ | ||
unlink(src), | ||
unlink(dest) | ||
]); | ||
}); | ||
}); |