11import { ppath , xfs , npath } from '@yarnpkg/fslib' ;
22import process from 'node:process' ;
3+ import { parseEnv } from 'node:util' ;
34import { describe , beforeEach , it , expect } from 'vitest' ;
45
56import { runCli } from './_runCli' ;
@@ -11,7 +12,7 @@ beforeEach(async () => {
1112} ) ;
1213
1314describe ( `UseCommand` , ( ) => {
14- it ( `should set the package manager in the current project` , async ( ) => {
15+ it ( `should update the "packageManager" field in the current project` , async ( ) => {
1516 await xfs . mktempPromise ( async cwd => {
1617 await xfs . writeJsonPromise ( ppath . join ( cwd , `package.json` ) , {
1718 packageManager :
`[email protected] ` , @@ -32,6 +33,85 @@ describe(`UseCommand`, () => {
3233 } ) ;
3334 } ) ;
3435
36+ it ( `should update .corepack.env if present and contains definition for pm version` , async t => {
37+ // Skip that test on Node.js 18.x as it lacks support for .env files.
38+ if ( process . version . startsWith ( `v18.` ) ) t . skip ( ) ;
39+
40+ await Promise . all ( [
41+ `COREPACK_DEV_ENGINES_YARN=1.1.0\n` ,
42+ `\nCOREPACK_DEV_ENGINES_YARN=1.1.0\n` ,
43+ `COREPACK_DEV_ENGINES_YARN=1.1.0` ,
44+ `\nCOREPACK_DEV_ENGINES_YARN=1.1.0` ,
45+ `FOO=bar\nCOREPACK_DEV_ENGINES_YARN=1.1.0\n` ,
46+ `FOO=bar\nCOREPACK_DEV_ENGINES_YARN=1.1.0` ,
47+ ] . map ( originalEnv => xfs . mktempPromise ( async cwd => {
48+ const pJSONContent = {
49+ devEngines : { packageManager : { name : `yarn` , version : `1.x` } } ,
50+ license : `MIT` ,
51+ } ;
52+ await xfs . writeJsonPromise ( ppath . join ( cwd , `package.json` ) , pJSONContent ) ;
53+ await xfs . writeFilePromise ( ppath . join ( cwd , `.corepack.env` ) , originalEnv ) ;
54+
55+ await expect ( runCli ( cwd , [ `use` , `[email protected] ` ] ) ) . resolves . toMatchObject ( { 56+ exitCode : 0 ,
57+ stdout :
expect . stringContaining ( `Installing [email protected] in the project...` ) , 58+ stderr : `` ,
59+ } ) ;
60+
61+ try {
62+ await expect ( xfs . readFilePromise ( ppath . join ( cwd , `.corepack.env` ) , `utf-8` ) . then ( parseEnv ) ) . resolves . toMatchObject ( {
63+ COREPACK_DEV_ENGINES_YARN : `1.22.4+sha512.a1833b862fe52169bd6c2a033045a07df5bc6a23595c259e675fed1b2d035ab37abe6ce309720abb6636d68f03615054b6292dc0a70da31c8697fda228b50d18` ,
64+ } ) ;
65+ } catch ( cause ) {
66+ throw new Error ( JSON . stringify ( originalEnv ) , { cause} ) ;
67+ }
68+ // It should not have touched package.json.
69+ await expect ( xfs . readJsonPromise ( ppath . join ( cwd , `package.json` ) ) ) . resolves . toStrictEqual ( pJSONContent ) ;
70+
71+ await expect ( runCli ( cwd , [ `yarn` , `--version` ] ) ) . resolves . toMatchObject ( {
72+ exitCode : 0 ,
73+ stdout : `1.22.4\n` ,
74+ } ) ;
75+ } ) ) ) ;
76+ } ) ;
77+
78+ it ( `should update .other.env if present` , async t => {
79+ // Skip that test on Node.js 18.x as it lacks support for .env files.
80+ if ( process . version . startsWith ( `v18.` ) ) t . skip ( ) ;
81+
82+ await Promise . all ( [
83+ `COREPACK_DEV_ENGINES_YARN=1.1.0\n` ,
84+ `\nCOREPACK_DEV_ENGINES_YARN=1.1.0\n` ,
85+ `COREPACK_DEV_ENGINES_YARN=1.1.0` ,
86+ `\nCOREPACK_DEV_ENGINES_YARN=1.1.0` ,
87+ `FOO=bar\nCOREPACK_DEV_ENGINES_YARN=1.1.0\n` ,
88+ `FOO=bar\nCOREPACK_DEV_ENGINES_YARN=1.1.0` ,
89+ ] . map ( originalEnv => xfs . mktempPromise ( async cwd => {
90+ await xfs . writeJsonPromise ( ppath . join ( cwd , `package.json` ) , {
91+ devEngines : { packageManager : { name : `yarn` , version : `1.x` } } ,
92+ } ) ;
93+ await xfs . writeFilePromise ( ppath . join ( cwd , `.other.env` ) , `COREPACK_DEV_ENGINES_YARN=1.0.0\n` ) ;
94+
95+ process . env . COREPACK_ENV_FILE = `.other.env` ;
96+ await expect ( runCli ( cwd , [ `use` , `[email protected] ` ] ) ) . resolves . toMatchObject ( { 97+ exitCode : 0 ,
98+ } ) ;
99+
100+ try {
101+ await expect ( xfs . readFilePromise ( ppath . join ( cwd , `.other.env` ) , `utf-8` ) . then ( parseEnv ) ) . resolves . toMatchObject ( {
102+ COREPACK_DEV_ENGINES_YARN : `1.22.4+sha512.a1833b862fe52169bd6c2a033045a07df5bc6a23595c259e675fed1b2d035ab37abe6ce309720abb6636d68f03615054b6292dc0a70da31c8697fda228b50d18` ,
103+ } ) ;
104+ } catch ( cause ) {
105+ throw new Error ( JSON . stringify ( originalEnv ) , { cause} ) ;
106+ }
107+
108+ await expect ( runCli ( cwd , [ `yarn` , `--version` ] ) ) . resolves . toMatchObject ( {
109+ exitCode : 0 ,
110+ stdout : `1.22.4\n` ,
111+ } ) ;
112+ } ) ) ) ;
113+ } ) ;
114+
35115 it ( `should create a package.json if absent` , async ( ) => {
36116 await xfs . mktempPromise ( async cwd => {
37117 await expect ( runCli ( cwd , [ `use` , `[email protected] ` ] ) ) . resolves . toMatchObject ( {
0 commit comments