| 1 |
/* eslint max-lines: "off" */ |
| 2 |
|
| 3 |
"use strict"; |
| 4 |
|
| 5 |
const noop = require("es5-ext/function/noop") |
| 6 |
, { resolve } = require("path") |
| 7 |
, isModuleNotFoundError = require("../../is-module-not-found-error"); |
| 8 |
|
| 9 |
const { |
| 10 |
setup: setupFileLinks, |
| 11 |
teardown: teardownFileLinks |
| 12 |
} = require("../_lib/setup-playground-file-symlinks"); |
| 13 |
|
| 14 |
const { |
| 15 |
setup: setupDirLinks, |
| 16 |
teardown: teardownDirLinks |
| 17 |
} = require("../_lib/setup-playground-dir-symlinks"); |
| 18 |
|
| 19 |
const playgroundDir = resolve(__dirname, "../__playground"); |
| 20 |
|
| 21 |
module.exports = (t, a) => { |
| 22 |
a.deep(t(playgroundDir, "./foo"), { |
| 23 |
targetPath: resolve(`${ playgroundDir }/foo.js`), |
| 24 |
realPath: resolve(`${ playgroundDir }/foo.js`) |
| 25 |
}); |
| 26 |
a.deep(t(playgroundDir, "./foo.js"), { |
| 27 |
targetPath: resolve(`${ playgroundDir }/foo.js`), |
| 28 |
realPath: resolve(`${ playgroundDir }/foo.js`) |
| 29 |
}); |
| 30 |
try { |
| 31 |
t(playgroundDir, "./foo.json"); |
| 32 |
throw new Error("Unexpected"); |
| 33 |
} catch (error) { |
| 34 |
a(isModuleNotFoundError(error, "./foo.json"), true); |
| 35 |
} |
| 36 |
a(t(playgroundDir, "./foo.json", { silent: true }), null); |
| 37 |
a.deep(t(playgroundDir, "./other"), { |
| 38 |
targetPath: resolve(`${ playgroundDir }/other.js`), |
| 39 |
realPath: resolve(`${ playgroundDir }/other.js`) |
| 40 |
}); |
| 41 |
a.deep(t(playgroundDir, "./other/"), { |
| 42 |
targetPath: resolve(`${ playgroundDir }/other/index.js`), |
| 43 |
realPath: resolve(`${ playgroundDir }/other/index.js`) |
| 44 |
}); |
| 45 |
a.deep(t(playgroundDir, "./samename"), { |
| 46 |
targetPath: resolve(`${ playgroundDir }/samename`), |
| 47 |
realPath: resolve(`${ playgroundDir }/samename`) |
| 48 |
}); |
| 49 |
a.deep(t(playgroundDir, "./samename.js"), { |
| 50 |
targetPath: resolve(`${ playgroundDir }/samename.js`), |
| 51 |
realPath: resolve(`${ playgroundDir }/samename.js`) |
| 52 |
}); |
| 53 |
a.deep(t(playgroundDir, "./samename.json"), { |
| 54 |
targetPath: resolve(`${ playgroundDir }/samename.json`), |
| 55 |
realPath: resolve(`${ playgroundDir }/samename.json`) |
| 56 |
}); |
| 57 |
a.deep(t(playgroundDir, "./samename"), { |
| 58 |
targetPath: resolve(`${ playgroundDir }/samename`), |
| 59 |
realPath: resolve(`${ playgroundDir }/samename`) |
| 60 |
}); |
| 61 |
a.deep(t(playgroundDir, "./dir"), { |
| 62 |
targetPath: resolve(`${ playgroundDir }/dir/lorem.js`), |
| 63 |
realPath: resolve(`${ playgroundDir }/dir/lorem.js`) |
| 64 |
}); |
| 65 |
a.deep(t(playgroundDir, "./dir/lorem"), { |
| 66 |
targetPath: resolve(`${ playgroundDir }/dir/lorem.js`), |
| 67 |
realPath: resolve(`${ playgroundDir }/dir/lorem.js`) |
| 68 |
}); |
| 69 |
a.deep(t(playgroundDir, "./dir/subdir/bar"), { |
| 70 |
targetPath: resolve(`${ playgroundDir }/dir/subdir/bar.js`), |
| 71 |
realPath: resolve(`${ playgroundDir }/dir/subdir/bar.js`) |
| 72 |
}); |
| 73 |
a.deep(t(`${ playgroundDir }/dir`, "."), { |
| 74 |
targetPath: resolve(`${ playgroundDir }/dir/lorem.js`), |
| 75 |
realPath: resolve(`${ playgroundDir }/dir/lorem.js`) |
| 76 |
}); |
| 77 |
a.deep(t(`${ playgroundDir }/dir`, "./"), { |
| 78 |
targetPath: resolve(`${ playgroundDir }/dir/lorem.js`), |
| 79 |
realPath: resolve(`${ playgroundDir }/dir/lorem.js`) |
| 80 |
}); |
| 81 |
a.deep(t(`${ playgroundDir }/dir`, "./lorem"), { |
| 82 |
targetPath: resolve(`${ playgroundDir }/dir/lorem.js`), |
| 83 |
realPath: resolve(`${ playgroundDir }/dir/lorem.js`) |
| 84 |
}); |
| 85 |
a.deep(t(`${ playgroundDir }/dir`, "../other"), { |
| 86 |
targetPath: resolve(`${ playgroundDir }/other.js`), |
| 87 |
realPath: resolve(`${ playgroundDir }/other.js`) |
| 88 |
}); |
| 89 |
a.deep(t(`${ playgroundDir }/dir`, "../other/"), { |
| 90 |
targetPath: resolve(`${ playgroundDir }/other/index.js`), |
| 91 |
realPath: resolve(`${ playgroundDir }/other/index.js`) |
| 92 |
}); |
| 93 |
a.deep(t(`${ playgroundDir }/dir/subdir`, "../"), { |
| 94 |
targetPath: resolve(`${ playgroundDir }/dir/lorem.js`), |
| 95 |
realPath: resolve(`${ playgroundDir }/dir/lorem.js`) |
| 96 |
}); |
| 97 |
a.deep(t(`${ playgroundDir }/dir/subdir`, "../../foo"), { |
| 98 |
targetPath: resolve(`${ playgroundDir }/foo.js`), |
| 99 |
realPath: resolve(`${ playgroundDir }/foo.js`) |
| 100 |
}); |
| 101 |
|
| 102 |
a.deep(t(playgroundDir, "outer"), { |
| 103 |
targetPath: resolve(`${ playgroundDir }/node_modules/outer/raz.js`), |
| 104 |
realPath: resolve(`${ playgroundDir }/node_modules/outer/raz.js`) |
| 105 |
}); |
| 106 |
a.deep(t(playgroundDir, "outer/boo"), { |
| 107 |
targetPath: resolve(`${ playgroundDir }/node_modules/outer/boo.js`), |
| 108 |
realPath: resolve(`${ playgroundDir }/node_modules/outer/boo.js`) |
| 109 |
}); |
| 110 |
try { |
| 111 |
t(playgroundDir, "outer/boo.json"); |
| 112 |
throw new Error("Unexpected"); |
| 113 |
} catch (error) { |
| 114 |
a(isModuleNotFoundError(error, "outer/boo.json"), true); |
| 115 |
} |
| 116 |
a.deep(t(playgroundDir, "outer3"), { |
| 117 |
targetPath: resolve(`${ playgroundDir }/node_modules/outer3/index.js`), |
| 118 |
realPath: resolve(`${ playgroundDir }/node_modules/outer3/index.js`) |
| 119 |
}); |
| 120 |
a.deep(t(playgroundDir, "pkg-main-dir"), { |
| 121 |
targetPath: resolve(`${ playgroundDir }/node_modules/pkg-main-dir/lib/index.js`), |
| 122 |
realPath: resolve(`${ playgroundDir }/node_modules/pkg-main-dir/lib/index.js`) |
| 123 |
}); |
| 124 |
a.deep(t(playgroundDir, "broken-main"), { |
| 125 |
targetPath: resolve(`${ playgroundDir }/node_modules/broken-main/index.js`), |
| 126 |
realPath: resolve(`${ playgroundDir }/node_modules/broken-main/index.js`) |
| 127 |
}); |
| 128 |
try { |
| 129 |
t(playgroundDir, "nested/elo"); |
| 130 |
throw new Error("Unexpected"); |
| 131 |
} catch (error) { |
| 132 |
a(isModuleNotFoundError(error, "nested/elo"), true); |
| 133 |
} |
| 134 |
a.deep(t(`${ playgroundDir }/node_modules/outer`, "outer3"), { |
| 135 |
targetPath: resolve(`${ playgroundDir }/node_modules/outer3/index.js`), |
| 136 |
realPath: resolve(`${ playgroundDir }/node_modules/outer3/index.js`) |
| 137 |
}); |
| 138 |
try { |
| 139 |
t(`${ playgroundDir }/node_modules/outer`, "project/foo"); |
| 140 |
throw new Error("Unexpected"); |
| 141 |
} catch (error) { |
| 142 |
a(isModuleNotFoundError(error, "project/foo"), true); |
| 143 |
} |
| 144 |
a.deep(t(`${ playgroundDir }/node_modules/outer`, "nested/elo"), { |
| 145 |
targetPath: resolve(`${ playgroundDir }/node_modules/outer/node_modules/nested/elo.js`), |
| 146 |
realPath: resolve(`${ playgroundDir }/node_modules/outer/node_modules/nested/elo.js`) |
| 147 |
}); |
| 148 |
try { |
| 149 |
t(`${ playgroundDir }/node_modules/outer/node_modules/nested`, "project/foo"); |
| 150 |
throw new Error("Unexpected"); |
| 151 |
} catch (error) { |
| 152 |
a(isModuleNotFoundError(error, "project/foo"), true); |
| 153 |
} |
| 154 |
a.deep(t(`${ playgroundDir }/node_modules/outer/node_modules/nested`, "outer"), { |
| 155 |
targetPath: resolve(`${ playgroundDir }/node_modules/outer/raz.js`), |
| 156 |
realPath: resolve(`${ playgroundDir }/node_modules/outer/raz.js`) |
| 157 |
}); |
| 158 |
a.deep(t(`${ playgroundDir }/node_modules/outer/node_modules/nested`, "outer/boo"), { |
| 159 |
targetPath: resolve(`${ playgroundDir }/node_modules/outer/boo.js`), |
| 160 |
realPath: resolve(`${ playgroundDir }/node_modules/outer/boo.js`) |
| 161 |
}); |
| 162 |
a.deep(t(`${ playgroundDir }/node_modules/outer/node_modules/nested`, "outer3"), { |
| 163 |
targetPath: resolve(`${ playgroundDir }/node_modules/outer3/index.js`), |
| 164 |
realPath: resolve(`${ playgroundDir }/node_modules/outer3/index.js`) |
| 165 |
}); |
| 166 |
|
| 167 |
// Symlink tests |
| 168 |
return Promise.all([ |
| 169 |
setupFileLinks().then( |
| 170 |
() => { |
| 171 |
let testError, teardownPromise; |
| 172 |
try { |
| 173 |
a.deep(t(playgroundDir, "./valid-file-link"), { |
| 174 |
targetPath: resolve(`${ playgroundDir }/valid-file-link.js`), |
| 175 |
realPath: resolve(`${ playgroundDir }/file-link-target.js`) |
| 176 |
}); |
| 177 |
a.deep(t(playgroundDir, "./deep-file-link"), { |
| 178 |
targetPath: resolve(`${ playgroundDir }/deep-file-link.js`), |
| 179 |
realPath: resolve(`${ playgroundDir }/deep-file-link-target.js`) |
| 180 |
}); |
| 181 |
try { |
| 182 |
t(playgroundDir, "./invalid-file-link"); |
| 183 |
throw new Error("Unexpected"); |
| 184 |
} catch (error) { |
| 185 |
a(isModuleNotFoundError(error, "./invalid-file-link"), true); |
| 186 |
} |
| 187 |
a.deep(t(playgroundDir, "./invalid-file-link-with-a-fallback"), { |
| 188 |
targetPath: resolve( |
| 189 |
`${ playgroundDir }/invalid-file-link-with-a-fallback.json` |
| 190 |
), |
| 191 |
realPath: resolve( |
| 192 |
`${ playgroundDir }/invalid-file-link-with-a-fallback.json` |
| 193 |
) |
| 194 |
}); |
| 195 |
} catch (error) { |
| 196 |
testError = error; |
| 197 |
} finally { |
| 198 |
teardownPromise = teardownFileLinks(); |
| 199 |
} |
| 200 |
return teardownPromise.then(() => { if (testError) throw testError; }); |
| 201 |
}, |
| 202 |
error => { |
| 203 |
if (error.code === "EPERM") { |
| 204 |
process.stdout.write( |
| 205 |
"Warning: Could not test file symlinks due to not suffient " + |
| 206 |
"process permissions to create them\n" |
| 207 |
); |
| 208 |
return; |
| 209 |
} |
| 210 |
throw error; |
| 211 |
} |
| 212 |
), |
| 213 |
setupDirLinks().then(() => { |
| 214 |
let testError, teardownPromise; |
| 215 |
try { |
| 216 |
a.deep(t(playgroundDir, "./valid-dir-link"), { |
| 217 |
targetPath: resolve(`${ playgroundDir }/valid-dir-link/index.js`), |
| 218 |
realPath: resolve(`${ playgroundDir }/dir-link-target/index.js`) |
| 219 |
}); |
| 220 |
a.deep(t(playgroundDir, "./deep-dir-link"), { |
| 221 |
targetPath: resolve(`${ playgroundDir }/deep-dir-link/index.js`), |
| 222 |
realPath: resolve(`${ playgroundDir }/deep-dir-link-target/index.js`) |
| 223 |
}); |
| 224 |
try { |
| 225 |
t(playgroundDir, "./invalid-dir-link"); |
| 226 |
throw new Error("Unexpected"); |
| 227 |
} catch (error) { |
| 228 |
a(isModuleNotFoundError(error, "./invalid-dir-link"), true); |
| 229 |
} |
| 230 |
} catch (error) { |
| 231 |
testError = error; |
| 232 |
} finally { |
| 233 |
teardownPromise = teardownDirLinks(); |
| 234 |
} |
| 235 |
return teardownPromise.then(() => { if (testError) throw testError; }); |
| 236 |
}) |
| 237 |
]).then(noop); |
| 238 |
}; |