-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): update package-manager utils to normalize yarn-path when m…
…igrating
- Loading branch information
1 parent
34bdc4f
commit 9c3bb8e
Showing
3 changed files
with
242 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,99 @@ | ||
jest.mock('fs'); | ||
import * as fs from 'fs'; | ||
import * as configModule from '../config/configuration'; | ||
import { detectPackageManager } from './package-manager'; | ||
import { | ||
detectPackageManager, | ||
modifyYarnRcToFitNewDirectory, | ||
modifyYarnRcYmlToFitNewDirectory, | ||
} from './package-manager'; | ||
|
||
describe('package-manager', () => { | ||
it('should detect package manager in nxJson', () => { | ||
jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({ | ||
cli: { | ||
packageManager: 'pnpm', | ||
}, | ||
describe('detectPackageManager', () => { | ||
it('should detect package manager in nxJson', () => { | ||
jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({ | ||
cli: { | ||
packageManager: 'pnpm', | ||
}, | ||
}); | ||
const packageManager = detectPackageManager(); | ||
expect(packageManager).toEqual('pnpm'); | ||
expect(fs.existsSync).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should detect yarn package manager from yarn.lock', () => { | ||
jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({}); | ||
(fs.existsSync as jest.Mock).mockReturnValueOnce(true); | ||
const packageManager = detectPackageManager(); | ||
expect(packageManager).toEqual('yarn'); | ||
expect(fs.existsSync).toHaveBeenNthCalledWith(1, 'yarn.lock'); | ||
}); | ||
|
||
it('should detect pnpm package manager from pnpm-lock.yaml', () => { | ||
jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({}); | ||
(fs.existsSync as jest.Mock).mockImplementation((path) => { | ||
return path === 'pnpm-lock.yaml'; | ||
}); | ||
const packageManager = detectPackageManager(); | ||
expect(packageManager).toEqual('pnpm'); | ||
expect(fs.existsSync).toHaveBeenCalledTimes(3); | ||
}); | ||
const packageManager = detectPackageManager(); | ||
expect(packageManager).toEqual('pnpm'); | ||
expect(fs.existsSync).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should detect yarn package manager from yarn.lock', () => { | ||
jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({}); | ||
(fs.existsSync as jest.Mock).mockReturnValueOnce(true); | ||
const packageManager = detectPackageManager(); | ||
expect(packageManager).toEqual('yarn'); | ||
expect(fs.existsSync).toHaveBeenNthCalledWith(1, 'yarn.lock'); | ||
it('should use npm package manager as default', () => { | ||
jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({}); | ||
(fs.existsSync as jest.Mock).mockReturnValue(false); | ||
const packageManager = detectPackageManager(); | ||
expect(packageManager).toEqual('npm'); | ||
expect(fs.existsSync).toHaveBeenCalledTimes(5); | ||
}); | ||
}); | ||
|
||
it('should detect pnpm package manager from pnpm-lock.yaml', () => { | ||
jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({}); | ||
(fs.existsSync as jest.Mock).mockImplementation((path) => { | ||
return path === 'pnpm-lock.yaml'; | ||
describe('modifyYarnRcYmlToFitNewDirectory', () => { | ||
it('should update paths properly', () => { | ||
expect( | ||
modifyYarnRcYmlToFitNewDirectory( | ||
'/home/user/projects/nx', | ||
'/tmp/virtual-nx-workspace', | ||
'yarnPath: ./bin/yarn.js' | ||
) | ||
).toEqual('yarnPath: ../../home/user/projects/nx/bin/yarn.js\n'); | ||
}); | ||
|
||
it('should update plugins appropriately', () => { | ||
expect( | ||
modifyYarnRcYmlToFitNewDirectory( | ||
'/home/user/projects/nx', | ||
'/tmp/virtual-nx-workspace', | ||
[ | ||
'enableProgressBars: false', | ||
'plugins:', | ||
' - ./scripts/yarn-plugin.js', | ||
' - path: .yarn/plugins/imported-plugin.js', | ||
' spec: imported-plugin', | ||
].join('\n') | ||
) | ||
).toEqual( | ||
[ | ||
'enableProgressBars: false', | ||
'', | ||
'plugins:', | ||
' - ../../home/user/projects/nx/scripts/yarn-plugin.js', | ||
' - path: ../../home/user/projects/nx/.yarn/plugins/imported-plugin.js', | ||
' spec: imported-plugin', | ||
'', | ||
].join('\n') | ||
); | ||
}); | ||
const packageManager = detectPackageManager(); | ||
expect(packageManager).toEqual('pnpm'); | ||
expect(fs.existsSync).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
it('should use npm package manager as default', () => { | ||
jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({}); | ||
(fs.existsSync as jest.Mock).mockReturnValue(false); | ||
const packageManager = detectPackageManager(); | ||
expect(packageManager).toEqual('npm'); | ||
expect(fs.existsSync).toHaveBeenCalledTimes(5); | ||
describe('modifyYarnRcToFitNewDirectory', () => { | ||
it('should update paths properly', () => { | ||
expect( | ||
modifyYarnRcToFitNewDirectory( | ||
'/home/user/projects/nx', | ||
'/tmp/virtual-nx-workspace', | ||
'yarn-path ./bin/yarn.js' | ||
) | ||
).toEqual('yarn-path "../../home/user/projects/nx/bin/yarn.js"'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters