|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const fs = require('fs'); |
| 5 | +const URL = require('url').URL; |
| 6 | +const util = require('util'); |
| 7 | + |
| 8 | +function runPathTests(withSlash, withoutSlash, slashedPath, realName) { |
| 9 | + |
| 10 | + if (!slashedPath) { |
| 11 | + slashedPath = withSlash; |
| 12 | + } else if (typeof slashedPath === 'boolean') { |
| 13 | + realName = slashedPath; |
| 14 | + slashedPath = withSlash; |
| 15 | + } |
| 16 | + |
| 17 | + common.expectsError( |
| 18 | + () => { |
| 19 | + fs.readFile(withSlash, common.mustNotCall()); |
| 20 | + }, |
| 21 | + { |
| 22 | + code: 'ERR_PATH_IS_DIRECTORY', |
| 23 | + type: Error, |
| 24 | + message: 'The argument "path" must not end with "/". ' + |
| 25 | + `Received ${util.inspect(slashedPath)}` |
| 26 | + }); |
| 27 | + |
| 28 | + common.expectsError( |
| 29 | + () => { |
| 30 | + fs.readFileSync(withSlash, { flag: 'r' }); |
| 31 | + }, |
| 32 | + { |
| 33 | + code: 'ERR_PATH_IS_DIRECTORY', |
| 34 | + type: Error, |
| 35 | + message: 'The argument "path" must not end with "/". ' + |
| 36 | + `Received ${util.inspect(slashedPath)}` |
| 37 | + }); |
| 38 | + |
| 39 | + common.expectsError( |
| 40 | + () => { |
| 41 | + fs.open(withSlash, 'r', common.mustNotCall()); |
| 42 | + }, |
| 43 | + { |
| 44 | + code: 'ERR_PATH_IS_DIRECTORY', |
| 45 | + type: Error, |
| 46 | + message: 'The argument "path" must not end with "/". ' + |
| 47 | + `Received ${util.inspect(slashedPath)}` |
| 48 | + }); |
| 49 | + |
| 50 | + common.expectsError( |
| 51 | + () => { |
| 52 | + fs.openSync(withSlash, 'r'); |
| 53 | + }, |
| 54 | + { |
| 55 | + code: 'ERR_PATH_IS_DIRECTORY', |
| 56 | + type: Error, |
| 57 | + message: 'The argument "path" must not end with "/". ' + |
| 58 | + `Received ${util.inspect(slashedPath)}` |
| 59 | + }); |
| 60 | + |
| 61 | + common.expectsError( |
| 62 | + () => { |
| 63 | + fs.appendFile(withSlash, 'test data', common.mustNotCall()); |
| 64 | + }, |
| 65 | + { |
| 66 | + code: 'ERR_PATH_IS_DIRECTORY', |
| 67 | + type: Error, |
| 68 | + message: 'The argument "path" must not end with "/". ' + |
| 69 | + `Received ${util.inspect(slashedPath)}` |
| 70 | + }); |
| 71 | + |
| 72 | + common.expectsError( |
| 73 | + () => { |
| 74 | + fs.appendFileSync(withSlash, 'test data'); |
| 75 | + }, |
| 76 | + { |
| 77 | + code: 'ERR_PATH_IS_DIRECTORY', |
| 78 | + type: Error, |
| 79 | + message: 'The argument "path" must not end with "/". ' + |
| 80 | + `Received ${util.inspect(slashedPath)}` |
| 81 | + }); |
| 82 | + |
| 83 | + common.expectsError( |
| 84 | + () => { |
| 85 | + fs.createReadStream(withSlash); |
| 86 | + }, |
| 87 | + { |
| 88 | + code: 'ERR_PATH_IS_DIRECTORY', |
| 89 | + type: Error, |
| 90 | + message: 'The argument "path" must not end with "/". ' + |
| 91 | + `Received ${util.inspect(slashedPath)}` |
| 92 | + }); |
| 93 | + |
| 94 | + |
| 95 | + common.expectsError( |
| 96 | + () => { |
| 97 | + fs.createWriteStream(withSlash); |
| 98 | + }, |
| 99 | + { |
| 100 | + code: 'ERR_PATH_IS_DIRECTORY', |
| 101 | + type: Error, |
| 102 | + message: 'The argument "path" must not end with "/". ' + |
| 103 | + `Received ${util.inspect(slashedPath)}` |
| 104 | + }); |
| 105 | + |
| 106 | + common.expectsError( |
| 107 | + () => { |
| 108 | + fs.truncate(withSlash, 4, common.mustNotCall()); |
| 109 | + }, |
| 110 | + { |
| 111 | + code: 'ERR_PATH_IS_DIRECTORY', |
| 112 | + type: Error, |
| 113 | + message: 'The argument "path" must not end with "/". ' + |
| 114 | + `Received ${util.inspect(slashedPath)}` |
| 115 | + }); |
| 116 | + |
| 117 | + common.expectsError( |
| 118 | + () => { |
| 119 | + fs.truncateSync(withSlash, 4); |
| 120 | + }, |
| 121 | + { |
| 122 | + code: 'ERR_PATH_IS_DIRECTORY', |
| 123 | + type: Error, |
| 124 | + message: 'The argument "path" must not end with "/". ' + |
| 125 | + `Received ${util.inspect(slashedPath)}` |
| 126 | + }); |
| 127 | + |
| 128 | + common.expectsError( |
| 129 | + () => { |
| 130 | + fs.writeFile(withSlash, 'test data', common.mustNotCall()); |
| 131 | + }, |
| 132 | + { |
| 133 | + code: 'ERR_PATH_IS_DIRECTORY', |
| 134 | + type: Error, |
| 135 | + message: 'The argument "path" must not end with "/". ' + |
| 136 | + `Received ${util.inspect(slashedPath)}` |
| 137 | + }); |
| 138 | + |
| 139 | + common.expectsError( |
| 140 | + () => { |
| 141 | + fs.writeFileSync(withSlash, 'test data'); |
| 142 | + }, |
| 143 | + { |
| 144 | + code: 'ERR_PATH_IS_DIRECTORY', |
| 145 | + type: Error, |
| 146 | + message: 'The argument "path" must not end with "/". ' + |
| 147 | + `Received ${util.inspect(slashedPath)}` |
| 148 | + }); |
| 149 | + |
| 150 | + common.expectsError( |
| 151 | + () => { |
| 152 | + fs.copyFile(withSlash, withoutSlash, common.mustNotCall()); |
| 153 | + }, |
| 154 | + { |
| 155 | + code: 'ERR_PATH_IS_DIRECTORY', |
| 156 | + type: Error, |
| 157 | + message: `The argument "${realName ? 'src' : 'path'}" must not end ` + |
| 158 | + `with "/". Received ${util.inspect(slashedPath)}` |
| 159 | + }); |
| 160 | + |
| 161 | + common.expectsError( |
| 162 | + () => { |
| 163 | + fs.copyFile(withoutSlash, withSlash, common.mustNotCall()); |
| 164 | + }, |
| 165 | + { |
| 166 | + code: 'ERR_PATH_IS_DIRECTORY', |
| 167 | + type: Error, |
| 168 | + message: `The argument "${realName ? 'dest' : 'path'}" must not end ` + |
| 169 | + `with "/". Received ${util.inspect(slashedPath)}` |
| 170 | + }); |
| 171 | + |
| 172 | + common.expectsError( |
| 173 | + () => { |
| 174 | + fs.copyFileSync(withSlash, withoutSlash); |
| 175 | + }, |
| 176 | + { |
| 177 | + code: 'ERR_PATH_IS_DIRECTORY', |
| 178 | + type: Error, |
| 179 | + message: `The argument "${realName ? 'src' : 'path'}" must not end ` + |
| 180 | + `with "/". Received ${util.inspect(slashedPath)}` |
| 181 | + }); |
| 182 | + |
| 183 | + common.expectsError( |
| 184 | + () => { |
| 185 | + fs.copyFileSync(withoutSlash, withSlash); |
| 186 | + }, |
| 187 | + { |
| 188 | + code: 'ERR_PATH_IS_DIRECTORY', |
| 189 | + type: Error, |
| 190 | + message: `The argument "${realName ? 'dest' : 'path'}" must not end ` + |
| 191 | + `with "/". Received ${util.inspect(slashedPath)}` |
| 192 | + }); |
| 193 | +} |
| 194 | + |
| 195 | +const path = '/tmp/test'; |
| 196 | +const stringWithSlash = common.isWindows ? `file:///c:${path}/` : |
| 197 | + `file://${path}/`; |
| 198 | +const stringWithoutSlash = common.isWindows ? `file:///c:${path}` : |
| 199 | + `file://${path}`; |
| 200 | +const urlWithSlash = new URL(stringWithSlash); |
| 201 | +const urlWithoutSlash = new URL(stringWithoutSlash); |
| 202 | +const U8ABufferWithSlash = new Uint8Array(Buffer.from(stringWithSlash)); |
| 203 | +const U8ABufferWithoutSlash = new Uint8Array(Buffer.from(stringWithoutSlash)); |
| 204 | +runPathTests(urlWithSlash, urlWithoutSlash, `${path}/`); |
| 205 | +runPathTests(stringWithSlash, stringWithoutSlash, true); |
| 206 | +runPathTests(U8ABufferWithSlash, U8ABufferWithoutSlash, true); |
0 commit comments