| 1 | /*
|
|---|
| 2 | * Jake JavaScript build tool
|
|---|
| 3 | * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
|
|---|
| 4 | *
|
|---|
| 5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
|---|
| 6 | * you may not use this file except in compliance with the License.
|
|---|
| 7 | * You may obtain a copy of the License at
|
|---|
| 8 | *
|
|---|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0
|
|---|
| 10 | *
|
|---|
| 11 | * Unless required by applicable law or agreed to in writing, software
|
|---|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
|---|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|---|
| 14 | * See the License for the specific language governing permissions and
|
|---|
| 15 | * limitations under the License.
|
|---|
| 16 | *
|
|---|
| 17 | */
|
|---|
| 18 |
|
|---|
| 19 | const PROJECT_DIR = process.env.PROJECT_DIR;
|
|---|
| 20 |
|
|---|
| 21 | let assert = require('assert');
|
|---|
| 22 | let fs = require('fs');
|
|---|
| 23 | let path = require('path');
|
|---|
| 24 | let file = require(`${PROJECT_DIR}/lib/utils/file`);
|
|---|
| 25 | let existsSync = fs.existsSync || path.existsSync;
|
|---|
| 26 | let exec = require('child_process').execSync;
|
|---|
| 27 |
|
|---|
| 28 | suite('fileUtils', function () {
|
|---|
| 29 |
|
|---|
| 30 | test('mkdirP', function () {
|
|---|
| 31 | let expected = [
|
|---|
| 32 | ['foo'],
|
|---|
| 33 | ['foo', 'bar'],
|
|---|
| 34 | ['foo', 'bar', 'baz'],
|
|---|
| 35 | ['foo', 'bar', 'baz', 'qux']
|
|---|
| 36 | ];
|
|---|
| 37 | file.mkdirP('foo/bar/baz/qux');
|
|---|
| 38 | let res = exec('find foo').toString().trim().split('\n');
|
|---|
| 39 | for (let i = 0, ii = res.length; i < ii; i++) {
|
|---|
| 40 | assert.equal(path.join.apply(path, expected[i]), res[i]);
|
|---|
| 41 | }
|
|---|
| 42 | file.rmRf('foo');
|
|---|
| 43 | });
|
|---|
| 44 |
|
|---|
| 45 | test('rmRf', function () {
|
|---|
| 46 | file.mkdirP('foo/bar/baz/qux');
|
|---|
| 47 | file.rmRf('foo/bar');
|
|---|
| 48 | let res = exec('find foo').toString().trim().split('\n');
|
|---|
| 49 | assert.equal(1, res.length);
|
|---|
| 50 | assert.equal('foo', res[0]);
|
|---|
| 51 | fs.rmdirSync('foo');
|
|---|
| 52 | });
|
|---|
| 53 |
|
|---|
| 54 | test('rmRf with symlink subdir', function () {
|
|---|
| 55 | file.mkdirP('foo');
|
|---|
| 56 | file.mkdirP('bar');
|
|---|
| 57 | fs.writeFileSync('foo/hello.txt', 'hello, it\'s me');
|
|---|
| 58 | fs.symlinkSync('../foo', 'bar/foo'); file.rmRf('bar');
|
|---|
| 59 |
|
|---|
| 60 | // Make sure the bar directory was successfully deleted
|
|---|
| 61 | let barDeleted = false;
|
|---|
| 62 | try {
|
|---|
| 63 | fs.statSync('bar');
|
|---|
| 64 | } catch(err) {
|
|---|
| 65 | if(err.code == 'ENOENT') {
|
|---|
| 66 | barDeleted = true;
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 | assert.equal(true, barDeleted);
|
|---|
| 70 |
|
|---|
| 71 | // Make sure that the file inside the linked folder wasn't deleted
|
|---|
| 72 | let res = fs.readdirSync('foo');
|
|---|
| 73 | assert.equal(1, res.length);
|
|---|
| 74 | assert.equal('hello.txt', res[0]);
|
|---|
| 75 |
|
|---|
| 76 | // Cleanup
|
|---|
| 77 | fs.unlinkSync('foo/hello.txt');
|
|---|
| 78 | fs.rmdirSync('foo');
|
|---|
| 79 | });
|
|---|
| 80 |
|
|---|
| 81 | test('rmRf with symlinked dir', function () {
|
|---|
| 82 | file.mkdirP('foo');
|
|---|
| 83 | fs.writeFileSync('foo/hello.txt', 'hello!');
|
|---|
| 84 | fs.symlinkSync('foo', 'bar');
|
|---|
| 85 | file.rmRf('bar');
|
|---|
| 86 |
|
|---|
| 87 | // Make sure the bar directory was successfully deleted
|
|---|
| 88 | let barDeleted = false;
|
|---|
| 89 | try {
|
|---|
| 90 | fs.statSync('bar');
|
|---|
| 91 | } catch(err) {
|
|---|
| 92 | if(err.code == 'ENOENT') {
|
|---|
| 93 | barDeleted = true;
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 | assert.equal(true, barDeleted);
|
|---|
| 97 |
|
|---|
| 98 | // Make sure that the file inside the linked folder wasn't deleted
|
|---|
| 99 | let res = fs.readdirSync('foo');
|
|---|
| 100 | assert.equal(1, res.length);
|
|---|
| 101 | assert.equal('hello.txt', res[0]);
|
|---|
| 102 |
|
|---|
| 103 | // Cleanup
|
|---|
| 104 | fs.unlinkSync('foo/hello.txt');
|
|---|
| 105 | fs.rmdirSync('foo');
|
|---|
| 106 | });
|
|---|
| 107 |
|
|---|
| 108 | test('cpR with same name and different directory', function () {
|
|---|
| 109 | file.mkdirP('foo');
|
|---|
| 110 | fs.writeFileSync('foo/bar.txt', 'w00t');
|
|---|
| 111 | file.cpR('foo', 'bar');
|
|---|
| 112 | assert.ok(existsSync('bar/bar.txt'));
|
|---|
| 113 | file.rmRf('foo');
|
|---|
| 114 | file.rmRf('bar');
|
|---|
| 115 | });
|
|---|
| 116 |
|
|---|
| 117 | test('cpR with same to and from will throw', function () {
|
|---|
| 118 | assert.throws(function () {
|
|---|
| 119 | file.cpR('foo.txt', 'foo.txt');
|
|---|
| 120 | });
|
|---|
| 121 | });
|
|---|
| 122 |
|
|---|
| 123 | test('cpR rename via copy in directory', function () {
|
|---|
| 124 | file.mkdirP('foo');
|
|---|
| 125 | fs.writeFileSync('foo/bar.txt', 'w00t');
|
|---|
| 126 | file.cpR('foo/bar.txt', 'foo/baz.txt');
|
|---|
| 127 | assert.ok(existsSync('foo/baz.txt'));
|
|---|
| 128 | file.rmRf('foo');
|
|---|
| 129 | });
|
|---|
| 130 |
|
|---|
| 131 | test('cpR rename via copy in base', function () {
|
|---|
| 132 | fs.writeFileSync('bar.txt', 'w00t');
|
|---|
| 133 | file.cpR('bar.txt', 'baz.txt');
|
|---|
| 134 | assert.ok(existsSync('baz.txt'));
|
|---|
| 135 | file.rmRf('bar.txt');
|
|---|
| 136 | file.rmRf('baz.txt');
|
|---|
| 137 | });
|
|---|
| 138 |
|
|---|
| 139 | test('cpR keeps file mode', function () {
|
|---|
| 140 | fs.writeFileSync('bar.txt', 'w00t', {mode: 0o750});
|
|---|
| 141 | fs.writeFileSync('bar1.txt', 'w00t!', {mode: 0o744});
|
|---|
| 142 | file.cpR('bar.txt', 'baz.txt');
|
|---|
| 143 | file.cpR('bar1.txt', 'baz1.txt');
|
|---|
| 144 |
|
|---|
| 145 | assert.ok(existsSync('baz.txt'));
|
|---|
| 146 | assert.ok(existsSync('baz1.txt'));
|
|---|
| 147 | let bazStat = fs.statSync('baz.txt');
|
|---|
| 148 | let bazStat1 = fs.statSync('baz1.txt');
|
|---|
| 149 | assert.equal(0o750, bazStat.mode & 0o7777);
|
|---|
| 150 | assert.equal(0o744, bazStat1.mode & 0o7777);
|
|---|
| 151 |
|
|---|
| 152 | file.rmRf('bar.txt');
|
|---|
| 153 | file.rmRf('baz.txt');
|
|---|
| 154 | file.rmRf('bar1.txt');
|
|---|
| 155 | file.rmRf('baz1.txt');
|
|---|
| 156 | });
|
|---|
| 157 |
|
|---|
| 158 | test('cpR keeps file mode when overwriting with preserveMode', function () {
|
|---|
| 159 | fs.writeFileSync('bar.txt', 'w00t', {mode: 0o755});
|
|---|
| 160 | fs.writeFileSync('baz.txt', 'w00t!', {mode: 0o744});
|
|---|
| 161 | file.cpR('bar.txt', 'baz.txt', {silent: true, preserveMode: true});
|
|---|
| 162 |
|
|---|
| 163 | assert.ok(existsSync('baz.txt'));
|
|---|
| 164 | let bazStat = fs.statSync('baz.txt');
|
|---|
| 165 | assert.equal(0o755, bazStat.mode & 0o777);
|
|---|
| 166 |
|
|---|
| 167 | file.rmRf('bar.txt');
|
|---|
| 168 | file.rmRf('baz.txt');
|
|---|
| 169 | });
|
|---|
| 170 |
|
|---|
| 171 | test('cpR does not keep file mode when overwriting', function () {
|
|---|
| 172 | fs.writeFileSync('bar.txt', 'w00t', {mode: 0o766});
|
|---|
| 173 | fs.writeFileSync('baz.txt', 'w00t!', {mode: 0o744});
|
|---|
| 174 | file.cpR('bar.txt', 'baz.txt');
|
|---|
| 175 |
|
|---|
| 176 | assert.ok(existsSync('baz.txt'));
|
|---|
| 177 | let bazStat = fs.statSync('baz.txt');
|
|---|
| 178 | assert.equal(0o744, bazStat.mode & 0o777);
|
|---|
| 179 |
|
|---|
| 180 | file.rmRf('bar.txt');
|
|---|
| 181 | file.rmRf('baz.txt');
|
|---|
| 182 | });
|
|---|
| 183 |
|
|---|
| 184 | test('cpR copies file mode recursively', function () {
|
|---|
| 185 | fs.mkdirSync('foo');
|
|---|
| 186 | fs.writeFileSync('foo/bar.txt', 'w00t', {mode: 0o740});
|
|---|
| 187 | file.cpR('foo', 'baz');
|
|---|
| 188 |
|
|---|
| 189 | assert.ok(existsSync('baz'));
|
|---|
| 190 | let barStat = fs.statSync('baz/bar.txt');
|
|---|
| 191 | assert.equal(0o740, barStat.mode & 0o777);
|
|---|
| 192 |
|
|---|
| 193 | file.rmRf('foo');
|
|---|
| 194 | file.rmRf('baz');
|
|---|
| 195 | });
|
|---|
| 196 |
|
|---|
| 197 | test('cpR keeps file mode recursively', function () {
|
|---|
| 198 | fs.mkdirSync('foo');
|
|---|
| 199 | fs.writeFileSync('foo/bar.txt', 'w00t', {mode: 0o740});
|
|---|
| 200 | fs.mkdirSync('baz');
|
|---|
| 201 | fs.mkdirSync('baz/foo');
|
|---|
| 202 | fs.writeFileSync('baz/foo/bar.txt', 'w00t!', {mode: 0o755});
|
|---|
| 203 | file.cpR('foo', 'baz', {silent: true, preserveMode: true});
|
|---|
| 204 |
|
|---|
| 205 | assert.ok(existsSync('baz'));
|
|---|
| 206 | let barStat = fs.statSync('baz/foo/bar.txt');
|
|---|
| 207 | assert.equal(0o740, barStat.mode & 0o777);
|
|---|
| 208 |
|
|---|
| 209 | file.rmRf('foo');
|
|---|
| 210 | file.rmRf('baz');
|
|---|
| 211 | });
|
|---|
| 212 |
|
|---|
| 213 | test('cpR copies directory mode recursively', function () {
|
|---|
| 214 | fs.mkdirSync('foo', 0o755);
|
|---|
| 215 | fs.mkdirSync('foo/bar', 0o700);
|
|---|
| 216 | file.cpR('foo', 'bar');
|
|---|
| 217 |
|
|---|
| 218 | assert.ok(existsSync('foo'));
|
|---|
| 219 | let fooBarStat = fs.statSync('bar/bar');
|
|---|
| 220 | assert.equal(0o700, fooBarStat.mode & 0o777);
|
|---|
| 221 |
|
|---|
| 222 | file.rmRf('foo');
|
|---|
| 223 | file.rmRf('bar');
|
|---|
| 224 | });
|
|---|
| 225 |
|
|---|
| 226 | });
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|