File: watch.js

package info (click to toggle)
node-gulp 4.0.2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 3,716 kB
  • sloc: javascript: 13,511; makefile: 94; sh: 12
file content (208 lines) | stat: -rw-r--r-- 5,476 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'use strict';

/* eslint-disable no-use-before-define */

var fs = require('fs');
var path = require('path');

var expect = require('expect');
var rimraf = require('rimraf');
var mkdirp = require('mkdirp');

var gulp = require('../');

var outpath = path.join(__dirname, './out-fixtures');

var tempFileContent = 'A test generated this file and it is safe to delete';

function createTempFile(path) {
  fs.writeFileSync(path, tempFileContent);
}

function updateTempFile(path) {
  setTimeout(function() {
    fs.appendFileSync(path, ' changed');
  }, 125);
}

describe.skip('gulp.watch()', function() {
  beforeEach(rimraf.bind(null, outpath));
  beforeEach(mkdirp.bind(null, outpath));
  afterEach(rimraf.bind(null, outpath));

  it('should call the function when file changes: no options', function(done) {
    var tempFile = path.join(outpath, 'watch-func.txt');

    createTempFile(tempFile);

    var watcher = gulp.watch('watch-func.txt', { cwd: outpath }, function(cb) {
      watcher.close();
      cb();
      done();
    });

    updateTempFile(tempFile);
  });

  it('should execute the gulp.parallel tasks', function(done) {
    var tempFile = path.join(outpath, 'watch-func.txt');

    createTempFile(tempFile);

    gulp.task('test', function(cb) {
      watcher.close();
      cb();
      done();
    });

    var watcher = gulp.watch('watch-func.txt', { cwd: outpath }, gulp.parallel('test'));

    updateTempFile(tempFile);
  });

  it('should work with destructuring', function(done) {
    var tempFile = path.join(outpath, 'watch-func.txt');
    var watch = gulp.watch;
    var parallel = gulp.parallel;
    var task = gulp.task;
    createTempFile(tempFile);

    task('test', function(cb) {
      watcher.close();
      cb();
      done();
    });

    var watcher = watch('watch-func.txt', { cwd: outpath }, parallel('test'));

    updateTempFile(tempFile);
  });

  it('should not call the function when no file changes: no options', function(done) {
    var tempFile = path.join(outpath, 'watch-func.txt');

    createTempFile(tempFile);

    var watcher = gulp.watch('watch-func.txt', { cwd: outpath }, function() {
      // TODO: proper fail here
      expect('Watcher erroneously called');
    });

    setTimeout(function() {
      watcher.close();
      done();
    }, 10);
  });

  it('should call the function when file changes: w/ options', function(done) {
    var tempFile = path.join(outpath, 'watch-func-options.txt');

    createTempFile(tempFile);

    var watcher = gulp.watch('watch-func-options.txt', { cwd: outpath }, function(cb) {
      watcher.close();
      cb();
      done();
    });

    updateTempFile(tempFile);
  });

  it('should not drop options when no callback specified', function(done) {
    var tempFile = path.join(outpath, 'watch-func-nodrop-options.txt');
    // By passing a cwd option, ensure options are not lost to gaze
    var relFile = '../watch-func-nodrop-options.txt';
    var cwd = path.join(outpath, '/subdir');

    createTempFile(tempFile);

    var watcher = gulp.watch(relFile, { cwd: cwd })
      .on('change', function(filepath) {
        expect(filepath).toExist();
        expect(path.resolve(cwd, filepath)).toEqual(path.resolve(tempFile));
        watcher.close();
        done();
      });

    updateTempFile(tempFile);
  });

  it('should work without options or callback', function(done) {
    // TODO: check we return watcher?
    gulp.watch('x');
    done();
  });

  it('should run many tasks: w/ options', function(done) {
    var tempFile = path.join(outpath, 'watch-task-options.txt');
    var a = 0;

    createTempFile(tempFile);

    gulp.task('task1', function(cb) {
      a++;
      cb();
    });
    gulp.task('task2', function(cb) {
      a += 10;
      expect(a).toEqual(11);
      watcher.close();
      cb();
      done();
    });

    var watcher = gulp.watch('watch-task-options.txt', { cwd: outpath }, gulp.series('task1', 'task2'));

    updateTempFile(tempFile);
  });

  it('should run many tasks: no options', function(done) {
    var tempFile = path.join(outpath, 'watch-many-tasks-no-options.txt');
    var a = 0;

    createTempFile(tempFile);

    gulp.task('task1', function(cb) {
      a++;
      cb();
    });
    gulp.task('task2', function(cb) {
      a += 10;
      expect(a).toEqual(11);
      watcher.close();
      cb();
      done();
    });

    var watcher = gulp.watch('./test/out-fixtures/watch-many-tasks-no-options.txt', gulp.series('task1', 'task2'));

    updateTempFile(tempFile);
  });

  it('should throw an error: passed parameter (string) is not a function', function(done) {
    var filename = 'empty.txt';
    var tempFile = path.join(outpath, filename);

    createTempFile(tempFile);
    try {
      gulp.watch(filename, { cwd: outpath }, 'task1');
    } catch (err) {
      expect(err.message).toEqual('watching ' + filename +  ': watch task has to be a function (optionally generated by using gulp.parallel or gulp.series)');
      done();
    }
  });

  it('should throw an error: passed parameter (array) is not a function', function(done) {
    var filename = 'empty.txt';
    var tempFile = path.join(outpath, filename);

    createTempFile(tempFile);
    try {
      gulp.watch(filename, { cwd: outpath }, ['task1']);
    } catch (err) {
      expect(err.message).toEqual('watching ' + filename +  ': watch task has to be a function (optionally generated by using gulp.parallel or gulp.series)');
      done();
    }
  });

});