Skip to content

Commit

Permalink
Add tests for inferPackageManager and safeParseURL
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacroldan committed Sep 20, 2024
1 parent 2796c12 commit 73aa5cd
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
24 changes: 24 additions & 0 deletions packages/cli-kit/src/public/common/url.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {isValidURL} from './url.js'
import {safeParseURL} from './url'
import {describe, expect, test} from 'vitest'

describe('isValidURL', () => {
Expand Down Expand Up @@ -34,3 +35,26 @@ describe('isValidURL', () => {
expect(got).toBe(false)
})
})

describe('safeParseURL', () => {
test('returns URL object for valid URL', () => {
const validURL = 'https://shopify.com/'
const result = safeParseURL(validURL)

expect(result).toBeInstanceOf(URL)
expect(result?.href).toBe(validURL)
})

test('returns undefined for invalid URL', () => {
const invalidURL = 'not a url'
const result = safeParseURL(invalidURL)

expect(result).toBeUndefined()
})

test('returns undefined for empty string', () => {
const result = safeParseURL('')

expect(result).toBeUndefined()
})
})
35 changes: 35 additions & 0 deletions packages/cli-kit/src/public/node/node-package-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@ import {
PackageJsonNotFoundError,
UnknownPackageManagerError,
checkForCachedNewVersion,
inferPackageManager,
PackageManager,
} from './node-package-manager.js'
import {captureOutput, exec} from './system.js'
import {inTemporaryDirectory, mkdir, touchFile, writeFile} from './fs.js'
import {joinPath, dirname, normalizePath} from './path.js'
import {inferPackageManagerForGlobalCLI} from './is-global.js'
import {cacheClear} from '../../private/node/conf-store.js'
import latestVersion from 'latest-version'
import {vi, describe, test, expect, beforeEach, afterEach} from 'vitest'

vi.mock('../../version.js')
vi.mock('./system.js')
vi.mock('latest-version')
vi.mock('./is-global')

const mockedExec = vi.mocked(exec)
const mockedCaptureOutput = vi.mocked(captureOutput)
Expand Down Expand Up @@ -992,3 +996,34 @@ describe('addNPMDependencies', () => {
})
})
})

describe('inferPackageManager', () => {
test('returns the package manager when a valid one is provided in options', () => {
expect(inferPackageManager('yarn')).toBe('yarn')
expect(inferPackageManager('npm')).toBe('npm')
expect(inferPackageManager('pnpm')).toBe('pnpm')
expect(inferPackageManager('bun')).toBe('bun')
})

test('ignores invalid package manager in options', () => {
const mockEnv = {npm_config_user_agent: 'npm/1.0.0'}
expect(inferPackageManager('invalid' as PackageManager, mockEnv)).toBe('npm')
})

test('infers package manager from user agent when not provided in options', () => {
const mockEnv = {npm_config_user_agent: 'yarn/1.22.0'}
expect(inferPackageManager(undefined, mockEnv)).toBe('yarn')
})

test('infers package manager from global CLI when not in options or user agent', () => {
const mockEnv = {}
vi.mocked(inferPackageManagerForGlobalCLI).mockReturnValue('pnpm')
expect(inferPackageManager(undefined, mockEnv)).toBe('pnpm')
})

test('defaults to npm when no other method succeeds', () => {
const mockEnv = {}
vi.mocked(inferPackageManagerForGlobalCLI).mockReturnValue('unknown')
expect(inferPackageManager(undefined, mockEnv)).toBe('npm')
})
})
16 changes: 14 additions & 2 deletions packages/cli-kit/src/public/node/node-package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -708,11 +708,23 @@ export async function writePackageJSON(directory: string, packageJSON: PackageJs
await writeFile(packagePath, JSON.stringify(packageJSON, null, 2))
}

export function inferPackageManager(optionsPackageManager: string | undefined): PackageManager {
/**
* Infers the package manager to be used based on the provided options and environment.
*
* This function determines the package manager in the following order of precedence:
* 1. Uses the package manager specified in the options, if valid.
* 2. Infers the package manager from the user agent string.
* 3. Infers the package manager used for the global CLI installation.
* 4. Defaults to 'npm' if no other method succeeds.
*
* @param optionsPackageManager - The package manager specified in the options (if any).
* @returns The inferred package manager as a PackageManager type.
*/
export function inferPackageManager(optionsPackageManager: string | undefined, env = process.env): PackageManager {
if (optionsPackageManager && packageManager.includes(optionsPackageManager as PackageManager)) {
return optionsPackageManager as PackageManager
}
const usedPackageManager = packageManagerFromUserAgent()
const usedPackageManager = packageManagerFromUserAgent(env)
if (usedPackageManager !== 'unknown') return usedPackageManager

const globalPackageManager = inferPackageManagerForGlobalCLI()
Expand Down

0 comments on commit 73aa5cd

Please sign in to comment.