Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
fix: check for semver satisfaction of yvm config (#489)
Browse files Browse the repository at this point in the history
* test: add coverage for semver matchingin current

* fix: check for semver satisfaction of yvm config
  • Loading branch information
iamogbz authored Nov 12, 2019
1 parent f83d5a8 commit 136d7bc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
4 changes: 3 additions & 1 deletion src/commands/current.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import semver from 'semver'

import log from 'util/log'
import { getYarnPathEntries, isYvmPath } from 'util/path'
import { NOT_AVAILABLE, SYSTEM } from 'util/alias'
Expand All @@ -21,7 +23,7 @@ export const current = async ({ shell } = {}) => {
}
const rcVersion = getRcFileVersion()
if (rcVersion !== null) {
if (versionInUse === rcVersion) {
if (semver.satisfies(versionInUse, rcVersion)) {
log('Your .yvmrc version matches your PATH version, good job!')
} else {
log(
Expand Down
73 changes: 46 additions & 27 deletions test/commands/current.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fs, vol } from 'memfs'
import { vol } from 'memfs'

import * as path from 'util/path'
import * as version from 'util/version'
Expand All @@ -7,10 +7,8 @@ import { current } from 'commands/current'

const getYarnPathEntries = jest.spyOn(path, 'getYarnPathEntries')
const isYvmPath = jest.spyOn(path, 'isYvmPath')

const getVersionInUse = jest
.spyOn(version, 'getVersionInUse')
.mockResolvedValue('1.7.0')
const getRcFileVersion = jest.spyOn(version, 'getRcFileVersion')
const getVersionInUse = jest.spyOn(version, 'getVersionInUse')

describe('yvm current command', () => {
const mockYvmPath = '/User/tophat/.yvm'
Expand All @@ -20,6 +18,10 @@ describe('yvm current command', () => {
vol.fromJSON({ '.yvmrc': '1.13.0' })
})

afterEach(() => {
jest.resetAllMocks()
})

afterAll(() => {
jest.restoreAllMocks()
})
Expand All @@ -33,34 +35,51 @@ describe('yvm current command', () => {
})

it('fails to find yarn version if yvm not installed', async () => {
getVersionInUse.mockResolvedValueOnce('1.17.0')
getYarnPathEntries.mockReturnValueOnce(['/usr/local/bin/yarn'])
expect(await current()).toBe(2)
expect(log.default).toHaveBeenCalledWith(
expect.stringContaining(`Yarn was NOT installed by yvm`),
)
})

it('Succeeds if yvm version matches .yvmrc', async () => {
const version = fs.readFileSync('.yvmrc', 'utf8').trim()
isYvmPath.mockReturnValueOnce(true)
getVersionInUse.mockResolvedValueOnce(version)
getYarnPathEntries.mockReturnValueOnce([
`${mockYvmPath}/versions/v${version}/bin`,
])
expect(await current()).toBe(0)
expect(log.default).toHaveBeenCalledWith(
expect.stringContaining(`version matches your PATH version`),
)
})
it.each([
['1.13.0', '1.13.0'],
['>=1.16.0', '1.17.0'],
['^1.18.0', '1.19.2'],
])(
'Succeeds if yvm yarn version matches yvm config version',
async (rcVersion, version) => {
isYvmPath.mockReturnValueOnce(true)
getRcFileVersion.mockReturnValueOnce(rcVersion)
getVersionInUse.mockResolvedValueOnce(version)
getYarnPathEntries.mockReturnValueOnce([
`${mockYvmPath}/versions/v${version}/bin`,
])
expect(await current()).toBe(0)
expect(log.default).toHaveBeenCalledWith(
expect.stringContaining(`version matches your PATH version`),
)
},
)

it('Succeeds if yvm version does not match .yvmrc', async () => {
isYvmPath.mockReturnValueOnce(true)
getYarnPathEntries.mockReturnValueOnce([
`${mockYvmPath}/versions/v0.0.0/bin`,
])
expect(await current()).toBe(0)
expect(log.default).toHaveBeenCalledWith(
expect.stringContaining(`don't match`),
)
})
it.each([
['1.13.0', '1.12.0'],
['>=1.16.0', '1.15.0'],
['^1.18.0', '2.0.0'],
])(
'Succeeds even if yarn version does not match config version',
async (rcVersion, version) => {
isYvmPath.mockReturnValueOnce(true)
getRcFileVersion.mockReturnValueOnce(rcVersion)
getVersionInUse.mockResolvedValueOnce(version)
getYarnPathEntries.mockReturnValueOnce([
`${mockYvmPath}/versions/v${version}/bin`,
])
expect(await current()).toBe(0)
expect(log.default).toHaveBeenCalledWith(
expect.stringContaining(`don't match`),
)
},
)
})

0 comments on commit 136d7bc

Please sign in to comment.