Skip to content

Commit

Permalink
fix: tests and cleanup of update message for major version
Browse files Browse the repository at this point in the history
  • Loading branch information
Jolg42 committed Sep 2, 2021
1 parent eabe5f3 commit 02cccab
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 58 deletions.
@@ -1,19 +1,45 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should display a update message w/ dev tag 1`] = `
exports[`update available message dev tag - major 1`] = `
┌─────────────────────────────────────────────────────────┐
│ Update available 2.6.1 -> 3.0.1 │
│ │
│ This is a major update - please follow the guide at │
│ https://pris.ly/d/major-version-upgrade │
│ │
│ Run the following to update │
│ npm i --save-dev prisma@dev │

This comment has been minimized.

Copy link
@janpio

janpio Sep 2, 2021

Member

Seems we lost the leadings 2 spaces here :( Probably when I extracted the string. We should add those back.

This comment has been minimized.

Copy link
@Jolg42

Jolg42 Sep 2, 2021

Author Member

Done!

This comment has been minimized.

Copy link
@janpio

janpio Sep 2, 2021

Member

Ah found it: 02cccab#r55827427

This comment has been minimized.

Copy link
@Jolg42

Jolg42 Sep 2, 2021

Author Member

Commenting in the PR is better 😉

│ npm i @prisma/client@dev │
└─────────────────────────────────────────────────────────┘
`;

exports[`update available message dev tag - minor 1`] = `
┌─────────────────────────────────────────────────────────┐
│ Update available 2.6.1 -> 2.16.0 │
│ Run the following to update │
│ npm i --save-dev prisma@dev │
│ npm i @prisma/client@dev │
│ npm i --save-dev prisma@dev │
│ npm i @prisma/client@dev │
└─────────────────────────────────────────────────────────┘
`;

exports[`update available message latest tag - major 1`] = `
┌─────────────────────────────────────────────────────────┐
│ Update available 2.6.1 -> 3.0.0 │
│ │
│ This is a major update - please follow the guide at │
│ https://pris.ly/d/major-version-upgrade │
│ │
│ Run the following to update │
│ npm i --save-dev prisma@latest │
│ npm i @prisma/client@latest │
└─────────────────────────────────────────────────────────┘
`;

exports[`should display a update message w/o tag 1`] = `
exports[`update available message latest tag - minor 1`] = `
┌─────────────────────────────────────────────────────────┐
│ Update available 2.6.1 -> 2.16.0 │
│ Run the following to update │
npm i --save-dev prisma
npm i @prisma/client
│ npm i --save-dev prisma@latest
│ npm i @prisma/client@latest
└─────────────────────────────────────────────────────────┘
`;
94 changes: 65 additions & 29 deletions packages/cli/src/__tests__/update-message.test.ts
Expand Up @@ -4,38 +4,74 @@ import { consoleContext, Context } from './__helpers__/context'

const ctx = Context.new().add(consoleContext()).assemble()

it('should display a update message w/ dev tag', () => {
printUpdateMessage({
status: 'ok',
// @ts-ignore
data: {
previous_version: '2.6.1',
current_version: '2.16.0',
package: 'prisma',
release_tag: 'dev',
},
describe('update available message', () => {
it('dev tag - minor', () => {
printUpdateMessage({
status: 'ok',
// @ts-ignore
data: {
previous_version: '2.6.1',

This comment has been minimized.

Copy link
@janpio

janpio Sep 2, 2021

Member

Technically this is wrong - the version would need to be 2.6.1-dev.15 or something like that, right?

This comment has been minimized.

Copy link
@Jolg42

Jolg42 Sep 2, 2021

Author Member

right, should be the same but better to update the test

current_version: '2.16.0',
package: 'prisma',
release_tag: 'dev',
},
})
const message = ctx.mocked['console.error'].mock.calls[0][0]
expect(message).toContain('npm i --save-dev prisma@dev')
expect(message).toContain('npm i @prisma/client@dev')
expect(message).toMatchSnapshot()
})
// @ts-ignore
const message = ctx.mocked['console.error'].mock.calls[0][0]

// process.stdout.write(message + "\n")
expect(message).toMatchSnapshot()
})
it('dev tag - major', () => {
printUpdateMessage({
status: 'ok',
// @ts-ignore
data: {
previous_version: '2.6.1',
current_version: '3.0.1',
package: 'prisma',
release_tag: 'dev',
},
})
const message = ctx.mocked['console.error'].mock.calls[0][0]
expect(message).toContain('This is a major update')
expect(message).toContain('npm i --save-dev prisma@dev')
expect(message).toContain('npm i @prisma/client@dev')
expect(message).toMatchSnapshot()
})

it('should display a update message w/o tag', () => {
printUpdateMessage({
status: 'ok',
// @ts-ignore
data: {
previous_version: '2.6.1',
current_version: '2.16.0',
package: 'prisma',
release_tag: 'latest',
},
it('latest tag - minor', () => {
printUpdateMessage({
status: 'ok',
// @ts-ignore
data: {
previous_version: '2.6.1',
current_version: '2.16.0',
package: 'prisma',
release_tag: 'latest',
},
})
const message = ctx.mocked['console.error'].mock.calls[0][0]
expect(message).toContain('npm i --save-dev prisma@latest')
expect(message).toContain('npm i @prisma/client@latest')
expect(message).toMatchSnapshot()
})
// @ts-ignore
const message = ctx.mocked['console.error'].mock.calls[0][0]

// process.stdout.write(message + "\n")
expect(message).toMatchSnapshot()
it('latest tag - major', () => {
printUpdateMessage({
status: 'ok',
// @ts-ignore
data: {
previous_version: '2.6.1',
current_version: '3.0.0',
package: 'prisma',
release_tag: 'latest',
},
})
const message = ctx.mocked['console.error'].mock.calls[0][0]
expect(message).toContain('This is a major update')
expect(message).toContain('npm i --save-dev prisma@latest')
expect(message).toContain('npm i @prisma/client@latest')
expect(message).toMatchSnapshot()
})
})
66 changes: 43 additions & 23 deletions packages/cli/src/utils/printUpdateMessage.ts
Expand Up @@ -10,30 +10,47 @@ export function printUpdateMessage(checkResult: {
}): void {
let boxHeight = 4
let majorText = ''
if (checkResult.data.previous_version.split('.')[0] < checkResult.data.current_version.split('.')[0]) {
majorText = `\nThis is a major update - please follow the guide at\nhttps://pris.ly/d/major-version-upgrade\n\n`
boxHeight = boxHeight + 4
}
let boxText = `\n${chalk.blue('Update available')} ${
checkResult.data.previous_version
} -> ${checkResult.data.current_version}\n${majorText}Run the following to update
${chalk.bold(
makeInstallCommand(checkResult.data.package, checkResult.data.release_tag),
)}
${chalk.bold(
makeInstallCommand('@prisma/client', checkResult.data.release_tag, {
canBeGlobal: false,
canBeDev: false,
}),
)}`
console.error(
drawBox({
height: boxHeight,
width: 59,
str: boxText,
horizontalPadding: 2,
}),

const currentVersionInstalled = checkResult.data.previous_version
const latestVersionAvailable = checkResult.data.current_version

const prismaCLICommand = makeInstallCommand(
checkResult.data.package,
checkResult.data.release_tag,
)
const prismaClientCommand = makeInstallCommand(
'@prisma/client',
checkResult.data.release_tag,
{
canBeGlobal: false,
canBeDev: false,
},
)

try {
const [majorInstalled] = currentVersionInstalled.split('.')
const [majorLatest] = latestVersionAvailable.split('.')

if (majorInstalled < majorLatest) {
majorText = `\nThis is a major update - please follow the guide at\nhttps://pris.ly/d/major-version-upgrade\n\n`
boxHeight = boxHeight + 4
}
} catch (e) { }

const boxText = `\n${chalk.blue(
'Update available',
)} ${currentVersionInstalled} -> ${latestVersionAvailable}\n${majorText}Run the following to update
${chalk.bold(prismaCLICommand)}

This comment has been minimized.

Copy link
@janpio

janpio Sep 2, 2021

Member

Ah no, here the spaces are now missing

${chalk.bold(prismaClientCommand)}`

const boxedMessage = drawBox({
height: boxHeight,
width: 59,
str: boxText,
horizontalPadding: 2,
})

console.error(boxedMessage)
}

function makeInstallCommand(
Expand Down Expand Up @@ -63,7 +80,10 @@ function makeInstallCommand(
} else {
command = `npm i ${packageName}`
}

// always output tag (so major upgrades work)
// see https://www.npmjs.com/package/prisma?activeTab=versions
// could be latest, dev, patch-dev, integration

This comment has been minimized.

Copy link
@janpio

janpio Sep 2, 2021

Member

I am not sure if checkpoint actually supports anything else than latest and dev - so the upgrade messages for the other might actually be misleading now that we add the tag all the time :(

This comment has been minimized.

Copy link
@janpio

janpio Sep 2, 2021

Member

Yeah, I do not think this can ever return patch-dev or integration based on the code that returns the tag in Checkpoint Server: https://github.com/prisma/checkpoint.prisma.io/blob/6ed053092fc472ae817e491a8f0800f0a8955c9b/src/internals/npm/index.ts#L95-L117

command += `@${tag}`

return command
Expand Down

0 comments on commit 02cccab

Please sign in to comment.