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

Commit

Permalink
fix: exception when reading yvm config fails, close #926 (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah authored Feb 12, 2021
1 parent d20b1bd commit d0be0af
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ install: build-production install-local

.PHONY: install-watch
install-watch: node_modules clean_webpack_build
$(WEBPACK_BUILD_DEV_WATCH) --env.INSTALL=true
$(WEBPACK_BUILD_DEV_WATCH) --env INSTALL=true

# -------------- Linting --------------

Expand Down
6 changes: 3 additions & 3 deletions src/shell/yvm.fish
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function yvm
end
if [ -z "$NEW_FISH_USER_PATHS" ]
yvm_err "Could not get new path from yvm"
exit 1
return 1
else
yvm_set_fish_user_paths $NEW_FISH_USER_PATHS
set -l new_version (yarn --version)
Expand All @@ -36,7 +36,7 @@ function yvm
end
if [ -z "$NEW_FISH_USER_PATHS" ]
yvm_err "Could not remove yvm from system path"
exit 1
return 1
else
yvm_set_fish_user_paths $NEW_FISH_USER_PATHS
end
Expand Down Expand Up @@ -76,7 +76,7 @@ function yvm
if not type -q "node"
yvm_err "%s\n" "YVM Could not automatically set yarn version."
yvm_err "%s\n" "Please ensure your YVM env variables and sourcing are set below sourcing node/nvm in your fish config file"
exit 1
return 1
end
yvm_shim
end
Expand Down
6 changes: 3 additions & 3 deletions src/shell/yvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ yvm() {
NEW_PATH=$(yvm_call_node_script get-new-path ${PROVIDED_VERSION})
if [ -z "${NEW_PATH}" ]; then
yvm_err "Could not get new path from yvm"
exit 1
return 1
else
yvm_set_user_path $NEW_PATH
yvm_echo "Now using yarn version $(yarn --version)"
Expand All @@ -28,7 +28,7 @@ yvm() {
NEW_PATH=$(yvm_call_node_script get-old-path)
if [ -z "${NEW_PATH}" ]; then
yvm_err "Could not remove yvm from system path"
exit 1
return 1
else
yvm_set_user_path $NEW_PATH
fi
Expand Down Expand Up @@ -68,7 +68,7 @@ yvm() {
if ! type "node" >/dev/null; then
yvm_err "YVM Could not find node executable."
yvm_err "Please ensure your YVM env variables and sourcing are set below sourcing node/nvm in your .zshrc or .bashrc"
exit 1
return 1
fi
yvm_shim
}
Expand Down
15 changes: 10 additions & 5 deletions src/util/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,17 @@ export const getRcFileVersion = () => {
'.yarnversion',
],
})
const result = explorer.search()
if (!result || result.isEmpty || !result.config) {
return null
try {
const result = explorer.search()
if (!result || result.isEmpty || !result.config) {
return null
}
log.info(`Found config ${result.filepath}`)
return String(result.config)
} catch (error) {
log.error('An error occurred trying to read the version file.')
throw error
}
log.info(`Found config ${result.filepath}`)
return String(result.config)
}

export const getVersionInUse = memoize(async () => {
Expand Down
31 changes: 31 additions & 0 deletions test/util/version.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,37 @@ describe('yvm config version', () => {
expect.stringContaining('Unable to resolve'),
)
})
it('Logs error when version discovery fails', async () => {
const mockVersion = '1.9.2'
vol.fromJSON({ [yvmPath]: {} })
await setDefaultVersion({
version: mockVersion,
})

jest.spyOn(log, 'error')
vol.fromJSON({
'package.json': JSON.stringify({
engines: { yarn: 'v1.0.0' },
}),
})

const [version1] = await getSplitVersionAndArgs()
expect(log.error).not.toBeCalled()
expect(version1).toEqual('1.0.0')

vol.fromJSON({
'package.json': `corrupted prefix${JSON.stringify({
engines: { yarn: 'v1.0.0' },
})}`,
})

await getSplitVersionAndArgs().catch(() => {})
expect(log.error).toHaveBeenCalledWith(
expect.stringContaining(
'An error occurred trying to read the version file.',
),
)
})
it('Uses default version when no config available', async () => {
const mockVersion = '1.9.2'
vol.fromJSON({ [yvmPath]: {} })
Expand Down

0 comments on commit d0be0af

Please sign in to comment.