|
| 1 | +import { join } from 'path'; |
1 | 2 | import Tag from '../../../lib/extend/tag';
|
2 | 3 | import chai from 'chai';
|
| 4 | +import Hexo from '../../../lib/hexo'; |
| 5 | +import defaultConfig from '../../../lib/hexo/default_config'; |
| 6 | +import posts from '../../../lib/plugins/processor/post'; |
| 7 | +import Filter from '../../../lib/extend/filter'; |
| 8 | +import renderPostFilter from '../../../lib/plugins/filter/before_generate/render_post'; |
| 9 | +import { mkdirs, rmdir, writeFile } from 'hexo-fs'; |
| 10 | +// @ts-ignore |
| 11 | +import Promise from 'bluebird'; |
3 | 12 | const should = chai.should();
|
4 | 13 |
|
| 14 | +type PostParams = Parameters<ReturnType<typeof posts>['process']> |
| 15 | +type PostReturn = ReturnType<ReturnType<typeof posts>['process']> |
| 16 | + |
5 | 17 | describe('Tag', () => {
|
6 | 18 | const tag = new Tag();
|
7 | 19 |
|
| 20 | + const baseDir = join(__dirname, 'post_test'); |
| 21 | + const hexo = new Hexo(baseDir); |
| 22 | + const post = posts(hexo); |
| 23 | + const process: (...args: PostParams) => Promise<PostReturn> = Promise.method(post.process.bind(hexo)); |
| 24 | + const { source } = hexo; |
| 25 | + const { File } = source; |
| 26 | + |
| 27 | + function newFile(options) { |
| 28 | + const { path } = options; |
| 29 | + |
| 30 | + options.path = (options.published ? '_posts' : '_drafts') + '/' + path; |
| 31 | + options.source = join(source.base, options.path); |
| 32 | + |
| 33 | + options.params = { |
| 34 | + published: options.published, |
| 35 | + path, |
| 36 | + renderable: options.renderable |
| 37 | + }; |
| 38 | + |
| 39 | + return new File(options); |
| 40 | + } |
| 41 | + |
| 42 | + before(async () => { |
| 43 | + await mkdirs(baseDir); |
| 44 | + hexo.init(); |
| 45 | + }); |
| 46 | + |
| 47 | + beforeEach(() => { hexo.config = Object.assign({}, defaultConfig); }); |
| 48 | + |
| 49 | + after(() => rmdir(baseDir)); |
| 50 | + |
8 | 51 | it('register()', async () => {
|
9 | 52 | const tag = new Tag();
|
10 | 53 |
|
@@ -180,4 +223,75 @@ describe('Tag', () => {
|
180 | 223 | spy.should.eql(true);
|
181 | 224 | });
|
182 | 225 | });
|
| 226 | + |
| 227 | + it('tag should get right locals', async () => { |
| 228 | + let count = 0; |
| 229 | + hexo.extend.filter = new Filter(); |
| 230 | + hexo.extend.tag = new Tag(); |
| 231 | + hexo.extend.tag.register('series', () => { |
| 232 | + count = hexo.locals.get('posts').length; |
| 233 | + return ''; |
| 234 | + }, {ends: false}); |
| 235 | + hexo.extend.filter.register('before_generate', renderPostFilter.bind(hexo)); |
| 236 | + |
| 237 | + const body1 = [ |
| 238 | + 'title: "test1"', |
| 239 | + 'date: 2023-09-03 16:59:42', |
| 240 | + 'tags: foo', |
| 241 | + '---', |
| 242 | + '{% series %}' |
| 243 | + ].join('\n'); |
| 244 | + |
| 245 | + const file = newFile({ |
| 246 | + path: 'test1.html', |
| 247 | + published: true, |
| 248 | + type: 'create', |
| 249 | + renderable: true |
| 250 | + }); |
| 251 | + |
| 252 | + const body2 = [ |
| 253 | + '---', |
| 254 | + 'title: test2', |
| 255 | + 'date: 2023-09-03 16:59:46', |
| 256 | + 'tags: foo', |
| 257 | + '---' |
| 258 | + ]; |
| 259 | + |
| 260 | + const file2 = newFile({ |
| 261 | + path: 'test2.html', |
| 262 | + published: true, |
| 263 | + type: 'create', |
| 264 | + renderable: true |
| 265 | + }); |
| 266 | + |
| 267 | + const body3 = [ |
| 268 | + 'title: test3', |
| 269 | + 'date: 2023-09-03 16:59:49', |
| 270 | + 'tags: foo', |
| 271 | + '---' |
| 272 | + ]; |
| 273 | + |
| 274 | + const file3 = newFile({ |
| 275 | + path: 'test3.html', |
| 276 | + published: true, |
| 277 | + type: 'create', |
| 278 | + renderable: true |
| 279 | + }); |
| 280 | + |
| 281 | + await Promise.all([ |
| 282 | + writeFile(file.source, body1), |
| 283 | + writeFile(file2.source, body2), |
| 284 | + writeFile(file3.source, body3) |
| 285 | + ]); |
| 286 | + |
| 287 | + await Promise.all([ |
| 288 | + process(file), |
| 289 | + process(file2), |
| 290 | + process(file3) |
| 291 | + ]); |
| 292 | + |
| 293 | + await hexo._generate({ cache: false }); |
| 294 | + |
| 295 | + count.should.eql(3); |
| 296 | + }); |
183 | 297 | });
|
0 commit comments