Skip to content

Commit

Permalink
feat(npm): adds possibility to specify an alternate NPM path for pack…
Browse files Browse the repository at this point in the history
…age finding
  • Loading branch information
ArnaudBuchholz committed Feb 26, 2024
1 parent 257099d commit d2c6fad
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 36 deletions.
2 changes: 2 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Check additional information below.
-b, --browser <command> [💻🔗🧪] Browser instantiation command (relative to cwd or use $/ for
provided ones) (default: "$/puppeteer.js")
--browser-args <argument...> [💻🔗🧪] Browser instantiation command parameters (use -- instead)
--alternate-npm-path <path> [💻🔗] Alternate NPM path to look for packages (priority: local,
alternate, global)
--no-npm-install [💻🔗🧪] Prevent any NPM install (execution may fail if a dependency is
missing)
-bt, --browser-close-timeout <timeout> [💻🔗🧪] Maximum waiting time for browser close (default: 2000)
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ui5-test-runner",
"version": "4.1.1",
"version": "4.2.0",
"description": "Standalone test runner for UI5",
"main": "index.js",
"bin": {
Expand Down Expand Up @@ -78,7 +78,7 @@
"standard": "^17.1.0",
"start-server-and-test": "^2.0.3",
"typescript": "^5.3.3",
"ui5-tooling-transpile": "^3.3.4"
"ui5-tooling-transpile": "^3.3.5"
},
"optionalDependencies": {
"fsevents": "^2.3.3"
Expand Down
3 changes: 2 additions & 1 deletion src/job-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ function buildAndCheckMode (job) {
'pageTimeout',
'browserCloseTimeout',
'failFast',
'keepAlive'
'keepAlive',
'alternateNpmPath'
])
return 'capabilities'
}
Expand Down
4 changes: 4 additions & 0 deletions src/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function getCommand (cwd) {
.option('-p, --parallel <count>', '[💻🔗🧪] Number of parallel tests executions', 2)
.option('-b, --browser <command>', '[💻🔗🧪] Browser instantiation command (relative to cwd or use $/ for provided ones)', '$/puppeteer.js')
.option('--browser-args <argument...>', '[💻🔗🧪] Browser instantiation command parameters (use -- instead)')
.option('--alternate-npm-path <path>', '[💻🔗] Alternate NPM path to look for packages (priority: local, alternate, global)')
.option('--no-npm-install', '[💻🔗🧪] Prevent any NPM install (execution may fail if a dependency is missing)')
.option('-bt, --browser-close-timeout <timeout>', '[💻🔗🧪] Maximum waiting time for browser close', timeout, 2000)
.option('-br, --browser-retry <count>', '[💻🔗🧪] Browser instantiation retries : if the command fails unexpectedly, it is re-executed (0 means no retry)', 1)
Expand Down Expand Up @@ -224,6 +225,9 @@ function finalize (job) {
if (job.cache) {
updateToAbsolute('cache')
}
if (job.alternateNpmPath) {
checkAccess({ path: job.alternateNpmPath, label: 'Alternate NPM path' })
}
job.mode = buildAndCheckMode(job)
if (job.mode === 'legacy') {
checkAccess({ path: job.webapp, label: 'webapp folder' })
Expand Down
56 changes: 33 additions & 23 deletions src/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,38 @@ function resolveDependencyPath (name) {
}
}

async function findDependencyPath (job, name) {
if (!localRoot) {
[localRoot, globalRoot] = await Promise.all([
npm(job, 'root'),
npm(job, 'root', '--global')
])
}
const localPath = join(localRoot, name)
if (await folderExists(localPath)) {
return [localPath, false]
}
if (job.alternateNpmPath) {
const alternatePath = join(job.alternateNpmPath, name)
if (await folderExists(alternatePath)) {
return [alternatePath, false]
}
}
const globalPath = join(globalRoot, name)
let justInstalled = false
if (!await folderExists(globalPath)) {
if (!job.npmInstall) {
throw UTRError.NPM_DEPENDENCY_NOT_FOUND(name)
}
const previousStatus = job.status
job.status = `Installing ${name}...`
await npm(job, 'install', name, '-g')
justInstalled = true
job.status = previousStatus
}
return [globalPath, justInstalled]
}

module.exports = {
resolveDependencyPath,

Expand All @@ -52,29 +84,7 @@ module.exports = {
} catch (e) {
}
if (!modulePath) {
if (!localRoot) {
[localRoot, globalRoot] = await Promise.all([
npm(job, 'root'),
npm(job, 'root', '--global')
])
}
const localPath = join(localRoot, name)
if (await folderExists(localPath)) {
modulePath = localPath
} else {
const globalPath = join(globalRoot, name)
if (!await folderExists(globalPath)) {
if (!job.npmInstall) {
throw UTRError.NPM_DEPENDENCY_NOT_FOUND(name)
}
const previousStatus = job.status
job.status = `Installing ${name}...`
await npm(job, 'install', name, '-g')
justInstalled = true
job.status = previousStatus
}
modulePath = globalPath
}
[modulePath, justInstalled] = await findDependencyPath(job, name)
}
const output = getOutput(job)
const installedPackage = require(join(modulePath, 'package.json'))
Expand Down
16 changes: 15 additions & 1 deletion src/npm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ describe('src/npm', () => {
beforeEach(() => {
job = fromObject(cwd, {
reportDir,
coverage: false
coverage: false,
alternateNpmPath: join(cwd, 'alternate-npm')
})
job.status = 'Testing'
output = getOutput(job)
Expand All @@ -45,6 +46,19 @@ describe('src/npm', () => {
expect(output.packageNotLatest).not.toHaveBeenCalled()
})

it('detects already installed package in alternate folder', async () => {
mock({
api: 'exec',
scriptPath: 'npm',
args: ['view', 'impossible', 'version'],
exec: async childProcess => childProcess.stdout.write('1.0.0\n'),
persist: true
})
const path = await resolvePackage(job, 'impossible')
expect(output.resolvedPackage).toHaveBeenCalledTimes(1)
expect(output.packageNotLatest).not.toHaveBeenCalled()
})

it('detects already installed global package (but warn as not the latest)', async () => {
mock({
api: 'exec',
Expand Down
3 changes: 3 additions & 0 deletions test/project/alternate-npm/impossible/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "1.0.0"
}

0 comments on commit d2c6fad

Please sign in to comment.