|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +const { existsSync, readFileSync, release } = vi.hoisted(() => ({ |
| 4 | + existsSync: vi.fn(() => false), |
| 5 | + readFileSync: vi.fn(() => ''), |
| 6 | + release: vi.fn(() => '6.1.0-generic'), |
| 7 | +})) |
| 8 | + |
| 9 | +vi.mock('node:fs', () => ({ existsSync, readFileSync })) |
| 10 | +vi.mock('node:os', () => ({ release })) |
| 11 | + |
| 12 | +/** Re-import so the module-level detection caches start empty. */ |
| 13 | +async function loadEnvironment() { |
| 14 | + vi.resetModules() |
| 15 | + return import('../../src/dev/environment') |
| 16 | +} |
| 17 | + |
| 18 | +const realPlatform = process.platform |
| 19 | + |
| 20 | +function stubPlatform(platform: NodeJS.Platform) { |
| 21 | + Object.defineProperty(process, 'platform', { value: platform, configurable: true }) |
| 22 | +} |
| 23 | + |
| 24 | +beforeEach(() => { |
| 25 | + existsSync.mockReturnValue(false) |
| 26 | + readFileSync.mockReturnValue('') |
| 27 | + release.mockReturnValue('6.1.0-generic') |
| 28 | +}) |
| 29 | + |
| 30 | +afterEach(() => { |
| 31 | + stubPlatform(realPlatform) |
| 32 | + vi.clearAllMocks() |
| 33 | +}) |
| 34 | + |
| 35 | +describe('isDocker', () => { |
| 36 | + it('should detect `/.dockerenv`', async () => { |
| 37 | + existsSync.mockReturnValue(true) |
| 38 | + const { isDocker } = await loadEnvironment() |
| 39 | + |
| 40 | + expect(isDocker()).toBe(true) |
| 41 | + }) |
| 42 | + |
| 43 | + it('should detect a docker cgroup', async () => { |
| 44 | + readFileSync.mockReturnValue('0::/docker/abc123') |
| 45 | + const { isDocker } = await loadEnvironment() |
| 46 | + |
| 47 | + expect(isDocker()).toBe(true) |
| 48 | + }) |
| 49 | + |
| 50 | + it('should cache the result', async () => { |
| 51 | + const { isDocker } = await loadEnvironment() |
| 52 | + |
| 53 | + expect(isDocker()).toBe(false) |
| 54 | + existsSync.mockReturnValue(true) |
| 55 | + expect(isDocker()).toBe(false) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should not detect docker on a plain host', async () => { |
| 59 | + const { isDocker } = await loadEnvironment() |
| 60 | + |
| 61 | + expect(isDocker()).toBe(false) |
| 62 | + }) |
| 63 | +}) |
| 64 | + |
| 65 | +describe('isWsl', () => { |
| 66 | + it('should detect `WSL_DISTRO_NAME`', async () => { |
| 67 | + const { isWsl } = await loadEnvironment() |
| 68 | + |
| 69 | + expect(isWsl('linux', { WSL_DISTRO_NAME: 'Ubuntu' })).toBe(true) |
| 70 | + }) |
| 71 | + |
| 72 | + it('should detect a microsoft kernel release', async () => { |
| 73 | + release.mockReturnValue('5.15.0-microsoft-standard-WSL2') |
| 74 | + const { isWsl } = await loadEnvironment() |
| 75 | + |
| 76 | + expect(isWsl('linux', {})).toBe(true) |
| 77 | + }) |
| 78 | + |
| 79 | + it('should detect microsoft in `/proc/version`', async () => { |
| 80 | + readFileSync.mockReturnValue('Linux version 5.15.0 (Microsoft@Microsoft.com)') |
| 81 | + const { isWsl } = await loadEnvironment() |
| 82 | + |
| 83 | + expect(isWsl('linux', {})).toBe(true) |
| 84 | + }) |
| 85 | + |
| 86 | + it('should be false off linux', async () => { |
| 87 | + release.mockReturnValue('5.15.0-microsoft-standard-WSL2') |
| 88 | + const { isWsl } = await loadEnvironment() |
| 89 | + |
| 90 | + expect(isWsl('darwin', { WSL_DISTRO_NAME: 'Ubuntu' })).toBe(false) |
| 91 | + expect(isWsl('win32', {})).toBe(false) |
| 92 | + }) |
| 93 | + |
| 94 | + it('should be false on plain linux', async () => { |
| 95 | + const { isWsl } = await loadEnvironment() |
| 96 | + |
| 97 | + expect(isWsl('linux', {})).toBe(false) |
| 98 | + }) |
| 99 | +}) |
| 100 | + |
| 101 | +describe('detectIsolatedEnvironment', () => { |
| 102 | + it('should prefer the container over WSL', async () => { |
| 103 | + existsSync.mockReturnValue(true) |
| 104 | + release.mockReturnValue('5.15.0-microsoft-standard-WSL2') |
| 105 | + const { detectIsolatedEnvironment } = await loadEnvironment() |
| 106 | + |
| 107 | + expect(detectIsolatedEnvironment()).toBe('the container') |
| 108 | + }) |
| 109 | + |
| 110 | + it('should report WSL', async () => { |
| 111 | + stubPlatform('linux') |
| 112 | + release.mockReturnValue('5.15.0-microsoft-standard-WSL2') |
| 113 | + const { detectIsolatedEnvironment } = await loadEnvironment() |
| 114 | + |
| 115 | + expect(detectIsolatedEnvironment()).toBe('WSL') |
| 116 | + }) |
| 117 | + |
| 118 | + it('should report nothing on a plain host', async () => { |
| 119 | + const { detectIsolatedEnvironment } = await loadEnvironment() |
| 120 | + |
| 121 | + expect(detectIsolatedEnvironment()).toBeUndefined() |
| 122 | + }) |
| 123 | +}) |
0 commit comments