11cb0ef41Sopenharmony_ciconst path = require('path') 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst ROOT = path.resolve(__dirname, '../..') 41cb0ef41Sopenharmony_ciconst BIN = path.join(ROOT, 'bin') 51cb0ef41Sopenharmony_ciconst LIB = path.join(ROOT, 'lib') 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci// since mock npm changes directories it can be hard to figure out the 81cb0ef41Sopenharmony_ci// correct path to mock something with tap since the directory will change 91cb0ef41Sopenharmony_ci// before/after npm is loaded. This helper replaces {BIN} and {LIB} with 101cb0ef41Sopenharmony_ci// the absolute path to those directories 111cb0ef41Sopenharmony_ciconst replace = (s) => { 121cb0ef41Sopenharmony_ci if (/^[./{]/.test(s)) { 131cb0ef41Sopenharmony_ci return s 141cb0ef41Sopenharmony_ci .replace(/^\{BIN\}/, BIN) 151cb0ef41Sopenharmony_ci .replace(/^\{LIB\}/, LIB) 161cb0ef41Sopenharmony_ci .replace(/^\{ROOT\}/, ROOT) 171cb0ef41Sopenharmony_ci } else { 181cb0ef41Sopenharmony_ci return require.resolve(s) 191cb0ef41Sopenharmony_ci } 201cb0ef41Sopenharmony_ci} 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ciconst tmock = (t, p, mocks = {}) => { 231cb0ef41Sopenharmony_ci const entries = Object.entries(mocks).map(([k, v]) => [replace(k), v]) 241cb0ef41Sopenharmony_ci return t.mock(replace(p), Object.fromEntries(entries)) 251cb0ef41Sopenharmony_ci} 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_cimodule.exports = tmock 28