-
Notifications
You must be signed in to change notification settings - Fork 8
Implements switchVersionRequirement
#302
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
Changes from all commits
d79eeee
8f09762
c69c367
dd6fa44
54cf9ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| use serde::Deserialize; | ||
| use zpm_semver::{Range, Version}; | ||
| use zpm_utils::{IoResultExt, Path}; | ||
|
|
||
| use crate::errors::Error; | ||
|
|
||
| #[derive(Deserialize, Default)] | ||
| #[serde(rename_all = "camelCase")] | ||
| struct PartialRcSettings { | ||
| #[serde(default)] | ||
| switch_version_requirement: Option<Range>, | ||
| } | ||
|
|
||
| fn load_switch_version_requirement() -> Result<Option<Range>, Error> { | ||
| let home_dir | ||
| = Path::home_dir()? | ||
| .ok_or(Error::MissingHomeFolder)?; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing home directory causes unnecessary validation failureMedium Severity When no home directory exists (common in Docker containers, some CI environments), Reviewed by Cursor Bugbot for commit d79eeee. Configure here. |
||
|
|
||
| let rc_filename | ||
| = std::env::var("YARN_RC_FILENAME") | ||
| .unwrap_or_else(|_| ".yarnrc.yml".to_string()); | ||
|
|
||
| let rc_path | ||
| = home_dir.with_join_str(&rc_filename); | ||
|
|
||
| let text | ||
| = rc_path.fs_read_text() | ||
| .ok_missing()?; | ||
|
|
||
| let Some(text) = text else { | ||
| return Ok(None); | ||
| }; | ||
|
|
||
| if text.is_empty() { | ||
| return Ok(None); | ||
| } | ||
|
|
||
| let partial: PartialRcSettings | ||
| = serde_yaml::from_str(&text)?; | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| Ok(partial.switch_version_requirement) | ||
| } | ||
|
|
||
| pub fn validate_yarn_version(version: &Version) -> Result<(), Error> { | ||
| let Some(requirement) = load_switch_version_requirement()? else { | ||
| return Ok(()); | ||
| }; | ||
|
|
||
| if !requirement.check_ignore_rc(version) { | ||
| return Err(Error::SwitchVersionMismatch { | ||
| requirement, | ||
| actual: version.clone(), | ||
| }); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| pub mod config; | ||
| pub mod daemons; | ||
| mod errors; | ||
| mod http; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ use std::process::ExitCode; | |
|
|
||
| mod cache; | ||
| mod commands; | ||
| mod config; | ||
| mod cwd; | ||
| mod daemons; | ||
| mod errors; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import {Filename, ppath, xfs} from '@yarnpkg/fslib'; | ||
|
|
||
| describe(`Features`, () => { | ||
| describe(`Yarn Switch`, () => { | ||
| describe(`switchVersionRequirement`, () => { | ||
| test( | ||
| `it should succeed when no config file exists`, | ||
| makeTemporaryEnv({ | ||
| packageManager: `yarn@6.0.0`, | ||
| }, async ({path, runSwitch}) => { | ||
| await expect(runSwitch(`--version`)).resolves.toMatchObject({ | ||
| code: 0, | ||
| }); | ||
| }), | ||
| ); | ||
|
|
||
| test( | ||
| `it should succeed when the config file is empty`, | ||
| makeTemporaryEnv({ | ||
| packageManager: `yarn@6.0.0`, | ||
| }, async ({path, runSwitch}) => { | ||
| const homePath = ppath.dirname(path); | ||
|
|
||
| await xfs.writeFilePromise(ppath.join(homePath, Filename.rc), ``); | ||
|
|
||
| await expect(runSwitch(`--version`)).resolves.toMatchObject({ | ||
| code: 0, | ||
| }); | ||
| }), | ||
| ); | ||
|
|
||
| test( | ||
| `it should succeed when the version matches the requirement`, | ||
| makeTemporaryEnv({ | ||
| packageManager: `yarn@6.0.0`, | ||
| }, async ({path, runSwitch}) => { | ||
| const homePath = ppath.dirname(path); | ||
|
|
||
| await xfs.writeJsonPromise(ppath.join(homePath, Filename.rc), { | ||
| switchVersionRequirement: `>=6.0.0`, | ||
| }); | ||
|
|
||
| await expect(runSwitch(`--version`)).resolves.toMatchObject({ | ||
| code: 0, | ||
| }); | ||
| }), | ||
| ); | ||
|
|
||
| test( | ||
| `it should fail when the version does not match the requirement`, | ||
| makeTemporaryEnv({ | ||
| packageManager: `yarn@6.0.0`, | ||
| }, async ({path, runSwitch}) => { | ||
| const homePath = ppath.dirname(path); | ||
|
|
||
| await xfs.writeJsonPromise(ppath.join(homePath, Filename.rc), { | ||
| switchVersionRequirement: `>=99.0.0`, | ||
| }); | ||
|
|
||
| await expect(runSwitch(`--version`)).rejects.toMatchObject({ | ||
| code: 1, | ||
| stdout: expect.stringContaining(`does not satisfy the required range`), | ||
| }); | ||
| }), | ||
| ); | ||
|
|
||
| test( | ||
| `it should fail to start a daemon when the version does not match the requirement`, | ||
| makeTemporaryEnv({ | ||
| packageManager: `yarn@6.0.0`, | ||
| }, async ({path, runSwitch}) => { | ||
| const homePath = ppath.dirname(path); | ||
|
|
||
| await xfs.writeJsonPromise(ppath.join(homePath, Filename.rc), { | ||
| switchVersionRequirement: `>=99.0.0`, | ||
| }); | ||
|
|
||
| await expect(runSwitch(`switch`, `daemon`, `--start`)).rejects.toMatchObject({ | ||
| code: 1, | ||
| stdout: expect.stringContaining(`does not satisfy the required range`), | ||
| }); | ||
| }), | ||
| ); | ||
| }); | ||
| }); | ||
| }); |


Uh oh!
There was an error while loading. Please reload this page.