UNPKG

2.99 kBJavaScriptView Raw
1/* eslint-env mocha */
2'use strict';
3
4var fs = require('fs');
5var touch = require('touch');
6var assert = require('assert');
7var join = require('path').join;
8
9var Metalsmith = require('metalsmith');
10var changed = require('../');
11
12const FIXTURES = join(__dirname, 'fixtures');
13
14describe('metalsmith-changed', function () {
15 beforeEach(function () {
16 // remove files in build dir
17 var generatedFiles = ['asdf.md', 'fdsa.md'];
18 generatedFiles.map(f => {
19 try {
20 fs.unlinkSync(join(FIXTURES, 'build', f))
21 } catch (e) {}
22 });
23 });
24
25 it('should add `metalsmith-changed-ctimes.json`', function (done) {
26 Metalsmith(FIXTURES)
27 .clean(true)
28 .use(changed())
29 .build(function (err, files) {
30 var ctimes = 'metalsmith-changed-ctimes.json';
31 var ctimesFound = Object.keys(files).indexOf(ctimes) !== -1;
32 assert(ctimesFound, ctimes + ' not found');
33 done(err);
34 });
35 });
36
37 it('should build all files on force: true', function (done) {
38 Metalsmith(FIXTURES)
39 .clean(false)
40 .use(changed({
41 force: true
42 }))
43 .build(function (err, files) {
44 // three files including ctimes.json
45 assert.equal(Object.keys(files).length, 3);
46 done(err);
47 });
48 });
49
50 it('should work with [array, of, patterns]', function (done) {
51 Metalsmith(FIXTURES)
52 .clean(false)
53 .use(changed({
54 forcePattern: ['asdf', '*.md']
55 }))
56 .build(function (err, files) {
57 assert.equal(Object.keys(files).length, 2);
58 done(err);
59 });
60 });
61
62 it('should only build files which has new ctime', function (done) {
63 touch.sync(join(FIXTURES, '/src/changed.md'));
64
65 Metalsmith(FIXTURES)
66 .clean(false)
67 .use(changed())
68 .build(function (err, files) {
69 assert.equal(Object.keys(files).length, 2);
70 done(err);
71 });
72 });
73
74 it('should not remove files missing `file.stats.ctime`', function (done) {
75 /**
76 * Typically files created through other plugins.
77 */
78
79 Metalsmith(FIXTURES)
80 .clean(false)
81 .use(function (files) {
82 files['asdf.md'] = {
83 title: 'asdf',
84 contents: '1234'
85 };
86 files['fdsa.md'] = {
87 title: 'asdf',
88 contents: '1234',
89 stats: {
90 ctime: new Date(1970, 1)
91 }
92 };
93 })
94 .use(changed())
95 .build(function (err, files) {
96 assert.equal(Object.keys(files).length, 3);
97 done(err);
98 });
99 });
100
101 it('should force build all files when metafiles are changed', function (done) {
102
103 Metalsmith(FIXTURES)
104 .clean(false)
105 .use(function (files) {
106 files['metafile.json'] = {
107 title: 'asdf',
108 contents: '1234'
109 };
110 })
111 .use(changed({
112 forceAllPattern: 'metafile.json'
113 }))
114 .build(function (err, files) {
115 assert.equal(Object.keys(files).length, 4);
116 done(err);
117 });
118 });
119});