Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): Setup command which uses an npm package #8920

Merged
merged 15 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/cli-helpers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './lib'
export * from './lib/colors'
export * from './lib/paths'
export * from './lib/project'
export * from './lib/version'
export * from './auth/setupHelpers'

export * from './lib/installHelpers'
Expand Down
368 changes: 368 additions & 0 deletions packages/cli-helpers/src/lib/__tests__/version.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,368 @@
jest.mock('@redwoodjs/project-config', () => {
return {
getPaths: () => {
return {
base: '',
}
},
}
})
jest.mock('fs')

import fs from 'fs'

import { getCompatibilityData } from '../version'

const EXAMPLE_PACKUMENT = {
_id: '@scope/package-name',
_rev: 'a1b2c3a1b2c3a1b2c3a1b2c3a1b2c3a1b2c3',
name: '@scope/package-name',
'dist-tags': {
latest: '0.0.3',
},
versions: {
'0.0.1': {
name: '@scope/package-name',
version: '0.0.1',
main: 'index.js',
scripts: {
test: 'echo "Error: no test specified" && exit 1',
},
author: '',
license: 'ISC',
description: '',
dependencies: {
'some-package': '1.2.3',
},
_id: '@scope/[email protected]',
_nodeVersion: '18.16.0',
_npmVersion: '9.5.1',
dist: {
integrity: 'sha512-somehashvalue',
shasum: 'somehashvalue',
tarball: 'someurl',
fileCount: 8,
unpackedSize: 1024,
signatures: [
{
keyid: 'SHA256:somehashvalue',
sig: 'somehashvalue',
},
],
},
_npmUser: {
name: 'someuser',
email: 'someemail',
},
directories: {},
maintainers: [
{
name: 'someuser',
email: 'someemail',
},
],
_npmOperationalInternal: {
host: 'somes3',
tmp: 'sometmp',
},
_hasShrinkwrap: false,
},
'0.0.2': {
name: '@scope/package-name',
version: '0.0.2',
main: 'index.js',
scripts: {
test: 'echo "Error: no test specified" && exit 1',
},
author: '',
license: 'ISC',
description: '',
dependencies: {
'some-package': '1.2.3',
},
engines: {
redwoodjs: '^5.1.0',
},
_id: '@scope/[email protected]',
_nodeVersion: '20.2.0',
_npmVersion: '9.6.6',
dist: {
integrity: 'sha512-somehashvalue',
shasum: 'somehashvalue',
tarball: 'someurl',
fileCount: 8,
unpackedSize: 1024,
signatures: [
{
keyid: 'SHA256:somehashvalue',
sig: 'somehashvalue',
},
],
},
_npmUser: {
name: 'someuser',
email: 'someemail',
},
directories: {},
maintainers: [
{
name: 'someuser',
email: 'someemail',
},
],
_npmOperationalInternal: {
host: 'somes3',
tmp: 'sometmp',
},
_hasShrinkwrap: false,
},
'0.0.3': {
name: '@scope/package-name',
version: '0.0.3',
main: 'index.js',
scripts: {
test: 'echo "Error: no test specified" && exit 1',
},
author: '',
license: 'ISC',
description: '',
dependencies: {
'some-package': '1.2.3',
},
engines: {
redwoodjs: '^6.0.0',
},
_id: '@scope/[email protected]',
_nodeVersion: '20.2.0',
_npmVersion: '9.6.6',
dist: {
integrity: 'sha512-somehashvalue',
shasum: 'somehashvalue',
tarball: 'someurl',
fileCount: 8,
unpackedSize: 1024,
signatures: [
{
keyid: 'SHA256:somehashvalue',
sig: 'somehashvalue',
},
],
},
_npmUser: {
name: 'someuser',
email: 'someemail',
},
directories: {},
maintainers: [
{
name: 'someuser',
email: 'someemail',
},
],
_npmOperationalInternal: {
host: 'somes3',
tmp: 'sometmp',
},
_hasShrinkwrap: false,
},
},
time: {
created: '2023-05-10T12:10:52.090Z',
'0.0.1': '2023-05-10T12:10:52.344Z',
'0.0.2': '2023-07-15T19:45:25.905Z',
},
maintainers: [
{
name: 'someuser',
email: 'someemail',
},
],
license: 'ISC',
readme: 'ERROR: No README data found!',
readmeFilename: '',
author: {
name: 'someuser',
},
}

describe('version compatibility detection', () => {
beforeEach(() => {
jest.spyOn(global, 'fetch').mockImplementation(() => {
return {
json: () => {
return EXAMPLE_PACKUMENT
},
} as any
})

jest.spyOn(fs, 'readFileSync').mockImplementation(() => {
return JSON.stringify({
devDependencies: {
'@redwoodjs/core': '^6.0.0',
},
})
})
})

test('throws for some fetch related error', async () => {
// Mock the fetch function to throw an error
jest.spyOn(global, 'fetch').mockImplementation(() => {
throw new Error('Some fetch related error')
})
await expect(
getCompatibilityData('some-package', 'latest')
).rejects.toThrowErrorMatchingInlineSnapshot(`"Some fetch related error"`)

// Mock the json parsing to throw an error
jest.spyOn(global, 'fetch').mockImplementation(() => {
return {
json: () => {
throw new Error('Some json parsing error')
},
} as any
})

await expect(
getCompatibilityData('some-package', 'latest')
).rejects.toThrowErrorMatchingInlineSnapshot(`"Some json parsing error"`)
})

test('throws for some packument related error', async () => {
jest.spyOn(global, 'fetch').mockImplementation(() => {
return {
json: () => {
return {
error: 'Some packument related error',
}
},
} as any
})

await expect(
getCompatibilityData('some-package', 'latest')
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Some packument related error"`
)
})

test('throws if preferred version is not found', async () => {
await expect(
getCompatibilityData('@scope/package-name', '0.0.4')
).rejects.toThrowErrorMatchingInlineSnapshot(
`"The package '@scope/package-name' does not have a version '0.0.4'"`
)
})

test('throws if preferred tag is not found', async () => {
await expect(
getCompatibilityData('@scope/package-name', 'next')
).rejects.toThrowErrorMatchingInlineSnapshot(
`"The package '@scope/package-name' does not have a tag 'next'"`
)
})

test('throws if no redwoodjs engine is found', async () => {
await expect(
getCompatibilityData('@scope/package-name', '0.0.1')
).rejects.toThrowErrorMatchingInlineSnapshot(
`"The package '@scope/package-name' does not specify a RedwoodJS compatibility version/range"`
)
})

test('throws if no latest version could be found', async () => {
jest.spyOn(global, 'fetch').mockImplementation(() => {
return {
json: () => {
return {
...EXAMPLE_PACKUMENT,
'dist-tags': {},
}
},
} as any
})

await expect(
getCompatibilityData('@scope/package-name', 'latest')
).rejects.toThrowErrorMatchingInlineSnapshot(
`"The package '@scope/package-name' does not have a tag 'latest'"`
)
})

test('returns the preferred version if it is compatible', async () => {
expect(await getCompatibilityData('@scope/package-name', '0.0.3')).toEqual({
preferred: {
tag: 'latest',
version: '0.0.3',
},
compatible: {
tag: 'latest',
version: '0.0.3',
},
})
})

test('returns the latest compatible version if the preferred version is not compatible', async () => {
expect(await getCompatibilityData('@scope/package-name', '0.0.2')).toEqual({
preferred: {
tag: undefined,
version: '0.0.2',
},
compatible: {
tag: 'latest',
version: '0.0.3',
},
})
})

test('returns the latest compatible version when given a tag', async () => {
expect(await getCompatibilityData('@scope/package-name', 'latest')).toEqual(
{
preferred: {
tag: 'latest',
version: '0.0.3',
},
compatible: {
tag: 'latest',
version: '0.0.3',
},
}
)

jest.spyOn(fs, 'readFileSync').mockImplementation(() => {
return JSON.stringify({
devDependencies: {
'@redwoodjs/core': '5.2.0',
},
})
})

expect(await getCompatibilityData('@scope/package-name', 'latest')).toEqual(
{
preferred: {
tag: 'latest',
version: '0.0.3',
},
compatible: {
tag: undefined,
version: '0.0.2',
},
}
)
})

test('throws if no compatible version could be found', async () => {
jest.spyOn(fs, 'readFileSync').mockImplementation(() => {
return JSON.stringify({
devDependencies: {
'@redwoodjs/core': '7.0.0',
},
})
})

expect(
getCompatibilityData('@scope/package-name', 'latest')
).rejects.toThrowErrorMatchingInlineSnapshot(
`"No compatible version of '@scope/package-name' was found"`
)
})
})
Loading