aboutsummaryrefslogtreecommitdiff
path: root/node_modules/ncp/test/ncp.js
blob: bc6df223c6f2d3c9b27799b43f7a0fdc5e5a85bf (plain)
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
var assert = require('assert'),
    fs = require('fs'),
    path = require('path'),
    rimraf = require('rimraf'),
    readDirFiles = require('read-dir-files'),
    util = require('util'),
    ncp = require('../').ncp;



describe('ncp', function () {
  describe('regular files and directories', function () {
    var fixtures = path.join(__dirname, 'regular-fixtures'),
        src = path.join(fixtures, 'src'),
        out = path.join(fixtures, 'out');

    before(function (cb) {
      rimraf(out, function() {
        ncp(src, out, cb);
      });
    });

    describe('when copying a directory of files', function () {
      it('files are copied correctly', function (cb) {
        readDirFiles(src, 'utf8', function (srcErr, srcFiles) {
          readDirFiles(out, 'utf8', function (outErr, outFiles) {
            assert.ifError(srcErr);
            assert.deepEqual(srcFiles, outFiles);
            cb();
          });
        });
      });
    });

    describe('when copying files using filter', function () {
      before(function (cb) {
        var filter = function(name) {
          return name.substr(name.length - 1) != 'a';
        };
        rimraf(out, function () {
          ncp(src, out, {filter: filter}, cb);
        });
      });

      it('files are copied correctly', function (cb) {
        readDirFiles(src, 'utf8', function (srcErr, srcFiles) {
          function filter(files) {
            for (var fileName in files) {
              var curFile = files[fileName];
              if (curFile instanceof Object)
                return filter(curFile);
              if (fileName.substr(fileName.length - 1) == 'a')
                delete files[fileName];
            }
          }
          filter(srcFiles);
          readDirFiles(out, 'utf8', function (outErr, outFiles) {
            assert.ifError(outErr);
            assert.deepEqual(srcFiles, outFiles);
            cb();
          });
        });
      });
    });

    describe('when using clobber=false', function () {
      it('the copy is completed successfully', function (cb) {
        ncp(src, out, function() {
          ncp(src, out, {clobber: false}, function(err) {
            assert.ifError(err);
            cb();
          });
        });
      });
    });

    describe('when using transform', function () {
      it('file descriptors are passed correctly', function (cb) {
        ncp(src, out, {
           transform: function(read,write,file) {
              assert.notEqual(file.name, undefined);
              assert.strictEqual(typeof file.mode,'number');
              read.pipe(write);
           }
        }, cb);
      });
    });

    describe('when using rename', function() {
      it('output files are correctly redirected', function(cb) {
        ncp(src, out, {
          rename: function(target) {
            if(path.basename(target) == 'a') return path.resolve(path.dirname(target), 'z');
            return target;
          }
        }, function(err) {
          if(err) return cb(err);

          readDirFiles(src, 'utf8', function (srcErr, srcFiles) {
            readDirFiles(out, 'utf8', function (outErr, outFiles) {
              assert.ifError(srcErr);
              assert.deepEqual(srcFiles.a, outFiles.z);
              cb();
            });
          });
        });
      });
    });
  });

  describe('symlink handling', function () {
    var fixtures = path.join(__dirname, 'symlink-fixtures'),
        src = path.join(fixtures, 'src'),
        out = path.join(fixtures, 'out');

    beforeEach(function (cb) {
      rimraf(out, cb);
    });

    it('copies symlinks by default', function (cb) {
      ncp(src, out, function (err) {
        if (err) return cb(err);
        assert.equal(fs.readlinkSync(path.join(out, 'file-symlink')), 'foo');
        assert.equal(fs.readlinkSync(path.join(out, 'dir-symlink')), 'dir');
        cb();
      })
    });

    it('copies file contents when dereference=true', function (cb) {
      ncp(src, out, { dereference: true }, function (err) {
        var fileSymlinkPath = path.join(out, 'file-symlink');
        assert.ok(fs.lstatSync(fileSymlinkPath).isFile());
        assert.equal(fs.readFileSync(fileSymlinkPath), 'foo contents');

        var dirSymlinkPath = path.join(out, 'dir-symlink');
        assert.ok(fs.lstatSync(dirSymlinkPath).isDirectory());
        assert.deepEqual(fs.readdirSync(dirSymlinkPath), ['bar']);

        cb();
      });
    });
  });

  describe('broken symlink handling', function () {
    var fixtures = path.join(__dirname, 'broken-symlink-fixtures'),
        src = path.join(fixtures, 'src'),
        out = path.join(fixtures, 'out');

    beforeEach(function (cb) {
      rimraf(out, cb);
    });

    it('copies broken symlinks by default', function (cb) {
      ncp(src, out, function (err) {
        if (err) return cb(err);
        assert.equal(fs.readlinkSync(path.join(out, 'broken-symlink')), 'does-not-exist');
        cb();
      })
    });

    it('returns an error when dereference=true', function (cb) {
      ncp(src, out, {dereference: true}, function (err) {
        assert.equal(err.length, 1);
        assert.equal(err[0].code, 'ENOENT');
        cb();
      });
    });
  });

  describe('modified files copies', function () {
      var fixtures = path.join(__dirname, 'modified-files'),
          src = path.join(fixtures, 'src'),
          out = path.join(fixtures, 'out');

      it('if file not exists copy file to target', function(cb) {
          rimraf(out, function() {
              ncp(src, out, {modified: true, clobber: false}, function (err) {
                  assert.equal(fs.existsSync(out), true);
                  cb();
              });
          });
      });

      it('change source file mtime and copy', function(cb) {
          fs.utimesSync(src+"/a", new Date().getTime()/1000, new Date('2015-01-01 00:00:00').getTime()/1000);
          ncp(src, out, {modified: true, clobber: false}, function (err) {
              fs.stat(out+"/a", function(err, stats) {
                  assert.equal(stats.mtime.getTime(), new Date('2015-01-01 00:00:00').getTime());
                  cb();
              });
          });
      });

  });
});