11import { Filename , ppath , xfs , npath } from '@yarnpkg/fslib' ;
22import { delimiter } from 'node:path' ;
33import process from 'node:process' ;
4+ import { setTimeout } from 'node:timers/promises' ;
45import { describe , beforeEach , it , expect , test } from 'vitest' ;
56
67import { Engine } from '../sources/Engine' ;
@@ -92,7 +93,6 @@ describe(`EnableCommand`, () => {
9293 await xfs . mktempPromise ( async cwd => {
9394 await xfs . writeFilePromise ( ppath . join ( cwd , `yarn` ) , `hello` ) ;
9495
95- process . env . PATH = `${ npath . fromPortablePath ( cwd ) } ${ delimiter } ${ process . env . PATH } ` ;
9696 await expect ( runCli ( cwd , [ `enable` , `--install-directory` , npath . fromPortablePath ( cwd ) ] ) ) . resolves . toMatchObject ( {
9797 stdout : `` ,
9898 stderr : `` ,
@@ -114,7 +114,6 @@ describe(`EnableCommand`, () => {
114114 ppath . join ( cwd , `yarn` ) ,
115115 ) ;
116116
117- process . env . PATH = `${ npath . fromPortablePath ( cwd ) } ${ delimiter } ${ process . env . PATH } ` ;
118117 await expect ( runCli ( cwd , [ `enable` , `--install-directory` , npath . fromPortablePath ( cwd ) ] ) ) . resolves . toMatchObject ( {
119118 stdout : `` ,
120119 stderr : expect . stringMatching ( / ^ y a r n i s a l r e a d y i n s t a l l e d i n .+ a n d p o i n t s t o a Y a r n S w i t c h i n s t a l l - s k i p p i n g \n $ / ) ,
@@ -125,4 +124,46 @@ describe(`EnableCommand`, () => {
125124 expect ( file ) . toBe ( `hello` ) ;
126125 } ) ;
127126 } ) ;
127+
128+ test . skipIf ( process . platform === `win32` ) ( `should not re-link if binaries are already correct` , async ( ) => {
129+ await xfs . mktempPromise ( async cwd => {
130+ await makeBin ( cwd , `corepack` as Filename ) ;
131+
132+ await expect ( runCli ( cwd , [ `enable` , `--install-directory` , npath . fromPortablePath ( cwd ) ] ) ) . resolves . toMatchObject ( {
133+ stdout : `` ,
134+ stderr : `` ,
135+ exitCode : 0 ,
136+ } ) ;
137+ const yarnStat1 = await xfs . lstatPromise ( ppath . join ( cwd , `yarn` ) ) ;
138+
139+ await setTimeout ( 10 ) ;
140+
141+ await expect ( runCli ( cwd , [ `enable` , `--install-directory` , npath . fromPortablePath ( cwd ) ] ) ) . resolves . toMatchObject ( {
142+ stdout : `` ,
143+ stderr : `` ,
144+ exitCode : 0 ,
145+ } ) ;
146+ const yarnStat2 = await xfs . lstatPromise ( ppath . join ( cwd , `yarn` ) ) ;
147+
148+ expect ( yarnStat2 . mtimeMs ) . toBe ( yarnStat1 . mtimeMs ) ;
149+ } ) ;
150+ } ) ;
151+
152+ test . skipIf ( process . platform === `win32` ) ( `should overwrite existing symlinks if they are incorrect` , async ( ) => {
153+ await xfs . mktempPromise ( async cwd => {
154+ await makeBin ( cwd , `corepack` as Filename ) ;
155+
156+ await xfs . writeFilePromise ( ppath . join ( cwd , `dummy-target` ) , `hello` ) ;
157+ await xfs . symlinkPromise ( ppath . join ( cwd , `dummy-target` ) , ppath . join ( cwd , `yarn` ) ) ;
158+
159+ await expect ( runCli ( cwd , [ `enable` , `--install-directory` , npath . fromPortablePath ( cwd ) ] ) ) . resolves . toMatchObject ( {
160+ stdout : `` ,
161+ stderr : `` ,
162+ exitCode : 0 ,
163+ } ) ;
164+
165+ const newLink = await xfs . readlinkPromise ( ppath . join ( cwd , `yarn` ) ) ;
166+ expect ( newLink ) . toContain ( `yarn.js` ) ;
167+ } ) ;
168+ } ) ;
128169} ) ;
0 commit comments