Skip to content

Commit

Permalink
Add --format repo tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Feb 27, 2023
1 parent e50fff0 commit 7b00df9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/package-managers/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,15 @@ export const mockViewMany =
? mockReturnedVersions
: mockReturnedVersions[name]

const version = (isPackument(partialPackument) ? partialPackument.version : partialPackument) || ''
const version = isPackument(partialPackument) ? partialPackument.version : partialPackument

// if there is no version, hard exit
// otherwise getPackageProtected will swallow the error
if (!version) {
console.error(`No mock version supplied for ${name}`)
process.exit(1)
}

const time = (isPackument(partialPackument) && partialPackument.time?.[version]) || new Date().toISOString()
const packument: Packument = {
name,
Expand Down Expand Up @@ -387,7 +395,7 @@ async function viewMany(
}

// select each field from the result object
return keyValueBy(fields, field => {
const resultNormalized = keyValueBy(fields, field => {
let value = result[field]

// index into the result object to get the dist-tag
Expand All @@ -401,6 +409,8 @@ async function viewMany(
[field]: value,
}
})

return resultNormalized
}

/** Memoize viewMany for --deep performance. */
Expand Down
27 changes: 25 additions & 2 deletions test/format.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import chai, { expect } from 'chai'
import chaiString from 'chai-string'
import fs from 'fs/promises'
import os from 'os'
import path from 'path'
import spawn from 'spawn-please'
import stubNpmView from './helpers/stubNpmView'
Expand All @@ -11,8 +13,8 @@ process.env.NCU_TESTS = 'true'

const bin = path.join(__dirname, '../build/src/bin/cli.js')

describe('--format time', () => {
it('show publish time', async () => {
describe('format', () => {
it('--format time', async () => {
const timestamp = '2020-04-27T21:48:11.660Z'
const stub = stubNpmView(
{
Expand All @@ -32,4 +34,25 @@ describe('--format time', () => {
expect(output).contains(timestamp)
stub.restore()
})

it('--format repo', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const pkgFile = path.join(tempDir, 'package.json')
await fs.writeFile(
pkgFile,
JSON.stringify({
dependencies: {
'modern-diacritics': '^1.0.0',
},
}),
'utf-8',
)
try {
await spawn('npm', ['install'], { cwd: tempDir })
const output = await spawn('node', [bin, '--format', 'repo'], { cwd: tempDir })
output.should.include('https://github.com/Mitsunee/modern-diacritics')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})
})
35 changes: 34 additions & 1 deletion test/interactive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('--interactive', () => {
'ncu-test-v2': '2.0.0',
'ncu-test-tag': '1.1.0',
'ncu-test-return-version': '2.0.0',
'modern-diacritics': '99.9.9',
},
{ spawn: true },
)
Expand Down Expand Up @@ -106,7 +107,11 @@ describe('--interactive', () => {
await fs.writeFile(
pkgFile,
JSON.stringify({
dependencies: { 'ncu-test-v2': '1.0.0', 'ncu-test-return-version': '1.0.0', 'ncu-test-tag': '1.0.0' },
dependencies: {
'ncu-test-v2': '1.0.0',
'ncu-test-return-version': '1.0.0',
'ncu-test-tag': '1.0.0',
},
}),
'utf-8',
)
Expand Down Expand Up @@ -135,4 +140,32 @@ describe('--interactive', () => {
await fs.rm(tempDir, { recursive: true, force: true })
}
})

it('with --format repo', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const pkgFile = path.join(tempDir, 'package.json')
await fs.writeFile(
pkgFile,
JSON.stringify({
dependencies: {
'modern-diacritics': '^1.0.0',
},
}),
'utf-8',
)
try {
await spawn('npm', ['install'], { cwd: tempDir })
const output = await spawn('node', [bin, '--interactive', '--format', 'repo'], {
cwd: tempDir,
env: {
...process.env,
INJECT_PROMPTS: JSON.stringify([['modern-diacritics'], true]),
},
})

output.should.include('https://github.com/Mitsunee/modern-diacritics')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})
})

0 comments on commit 7b00df9

Please sign in to comment.