- 
                Notifications
    You must be signed in to change notification settings 
- Fork 3.4k
          chore: Refactor {launch} from @packages/launcher
          #32687
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Draft
      
      
            cacieprins
  wants to merge
  9
  commits into
  develop
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
chore/launcher-launch-refactor
  
      
      
   
  
    
  
  
  
 
  
      
    base: develop
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +277
        
        
          −64
        
        
          
        
      
    
  
  
     Draft
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            9 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      c238b66
              
                chore: tests for launcher/launch, refactor process spawning for reada…
              
              
                cacieprins f8f52fe
              
                how did darwin.spec get lost? oh well
              
              
                cacieprins 8809e4f
              
                Merge branch 'develop' into chore/launcher-launch-refactor
              
              
                cacieprins 241c7fe
              
                Merge branch 'develop' into chore/launcher-launch-refactor
              
              
                cacieprins 5ff8d81
              
                Merge branch 'develop' into chore/launcher-launch-refactor
              
              
                cacieprins fca8c2a
              
                fix XDarwin ref
              
              
                cacieprins 0feca29
              
                Merge branch 'develop' into chore/launcher-launch-refactor
              
              
                cacieprins 9da5e3e
              
                fix ref to launcher
              
              
                cacieprins 115cbde
              
                Merge branch 'develop' into chore/launcher-launch-refactor
              
              
                cacieprins File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or 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 was deleted.
      
      Oops, something went wrong.
      
    
  
            File renamed without changes.
          
    
            File renamed without changes.
          
    
  
    
      This file contains hidden or 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 hidden or 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 | 
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import Debug from 'debug' | ||
| import type * as cp from 'child_process' | ||
| import type { FoundBrowser } from '@packages/types' | ||
| import type { Readable } from 'stream' | ||
| import { PlatformFactory } from './platforms/PlatformFactory' | ||
|  | ||
| export const debug = Debug('cypress:launcher:browsers') | ||
|  | ||
| /** starts a found browser and opens URL if given one */ | ||
| export type LaunchedBrowser = cp.ChildProcessByStdio<null, Readable, Readable> | ||
|  | ||
| // NOTE: For Firefox, geckodriver is used to launch the browser | ||
| export function launch ( | ||
| browser: FoundBrowser, | ||
| url: string, | ||
| args: string[] = [], | ||
| browserEnv = {}, | ||
| ) { | ||
| debug('launching browser %o', { browser, url }) | ||
|  | ||
| // We shouldn't need to check this, because FoundBrowser.path is | ||
| // not optional. | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lots of places explicitly coerce unknown POJOs to  | ||
| if (!browser.path) { | ||
| throw new Error(`Browser ${browser.name} is missing path`) | ||
| } | ||
|  | ||
| return PlatformFactory.select().launch(browser, url, args, browserEnv) | ||
| } | ||
  
    
      This file contains hidden or 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 | 
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // this file is named XDarwin because intellisense gets confused with '../darwin/' | ||
| import { ChildProcess, spawn } from 'child_process' | ||
| import { Platform } from './Platform' | ||
| import type { FoundBrowser } from '@packages/types' | ||
| import os from 'os' | ||
|  | ||
| export class Darwin extends Platform { | ||
| launch (browser: FoundBrowser, url: string, args: string[], env: Record<string, string> = {}): ChildProcess { | ||
| if (os.arch() === 'arm64') { | ||
| const proc = spawn( | ||
| 'arch', | ||
| [browser.path, url, ...args], { | ||
| ...Platform.defaultSpawnOpts, | ||
| env: { | ||
| ARCHPREFERENCE: 'arm64,x86_64', | ||
| ...Platform.defaultSpawnOpts.env, | ||
| ...env, | ||
| }, | ||
| }, | ||
| ) | ||
|  | ||
| this.addDebugListeners(proc, browser) | ||
|  | ||
| return proc | ||
| } | ||
|  | ||
| return super.launch(browser, url, args, env) | ||
| } | ||
| } | 
  
    
      This file contains hidden or 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 | 
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { Platform } from './Platform' | ||
|  | ||
| export class Linux extends Platform { | ||
| } | 
  
    
      This file contains hidden or 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 | 
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import type { FoundBrowser } from '@packages/types' | ||
| import { ChildProcess, spawn, SpawnOptions } from 'child_process' | ||
| import Debug from 'debug' | ||
|  | ||
| export const debug = Debug('cypress:launcher:browsers') | ||
|  | ||
| export abstract class Platform { | ||
| launch (browser: FoundBrowser, url: string, args: string[], env: Record<string, string> = {}): ChildProcess { | ||
| debug('launching browser %o', { browser, url, args, env }) | ||
|  | ||
| const proc = spawn(browser.path, [url, ...args], { | ||
| ...Platform.defaultSpawnOpts, | ||
| env: { | ||
| ...Platform.defaultSpawnOpts.env, | ||
| ...env, | ||
| }, | ||
| }) | ||
|  | ||
| this.addDebugListeners(proc, browser) | ||
|  | ||
| return proc | ||
| } | ||
|  | ||
| protected addDebugListeners (proc: ChildProcess, browser: FoundBrowser) { | ||
| proc.stdout?.on('data', (buf) => { | ||
| debug('%s stdout: %s', browser.name, String(buf).trim()) | ||
| }) | ||
|  | ||
| proc.stderr?.on('data', (buf) => { | ||
| debug('%s stderr: %s', browser.name, String(buf).trim()) | ||
| }) | ||
|  | ||
| proc.on('exit', (code, signal) => { | ||
| debug('%s exited: %o', browser.name, { code, signal }) | ||
| }) | ||
| } | ||
|  | ||
| static get defaultSpawnOpts (): SpawnOptions { | ||
| return { | ||
| stdio: ['ignore', 'pipe', 'pipe'], | ||
| env: { | ||
| ...process.env, | ||
| }, | ||
| } | ||
| } | ||
| } | 
  
    
      This file contains hidden or 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 | 
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import os from 'os' | ||
| import type { Platform } from './Platform' | ||
| import { Darwin } from './Darwin' | ||
| import { Linux } from './Linux' | ||
| import { Windows } from './Windows' | ||
|  | ||
| export class PlatformFactory { | ||
| static select (): Platform { | ||
| switch (os.platform()) { | ||
| case 'darwin': | ||
| return new Darwin() | ||
| case 'linux': | ||
| return new Linux() | ||
| case 'win32': | ||
| return new Windows() | ||
| default: | ||
| throw new Error(`Unsupported platform: ${os.platform()} ${os.arch()}`) | ||
| } | ||
| } | ||
| } | 
  
    
      This file contains hidden or 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 | 
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { Platform } from './Platform' | ||
|  | ||
| export class Windows extends Platform { | ||
| } | 
  
    
      This file contains hidden or 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 hidden or 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 hidden or 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 | 
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import { describe, it, expect, vi, Mocked } from 'vitest' | ||
| import type { FoundBrowser } from '@packages/types' | ||
| import { launch } from '../../lib/launch' | ||
| import os from 'os' | ||
| import { spawn, ChildProcess } from 'child_process' | ||
| import EventEmitter from 'events' | ||
|  | ||
| vi.mock('os', async (importActual) => { | ||
| const actual: typeof os = await importActual() | ||
|  | ||
| return { | ||
| default: { | ||
| ...actual, | ||
| platform: vi.fn(), | ||
| arch: vi.fn(), | ||
| }, | ||
| } | ||
| }) | ||
|  | ||
| vi.mock('child_process', async (importActual) => { | ||
| const actual = await importActual() | ||
|  | ||
| return { | ||
| // @ts-expect-error | ||
| ...actual, | ||
| spawn: vi.fn(), | ||
| } | ||
| }) | ||
|  | ||
| describe('launch', () => { | ||
| let browser: FoundBrowser | ||
| let url: string | ||
| let args: string[] | ||
| let browserEnv: Record<string, string> | ||
| let launchedBrowser: Mocked<ChildProcess> | ||
|  | ||
| let arch: ReturnType<typeof os.arch> | ||
| let platform: ReturnType<typeof os.platform> | ||
|  | ||
| beforeEach(() => { | ||
| browser = { | ||
| name: 'chrome', | ||
| version: '100.0.0', | ||
| path: 'chrome', | ||
| family: 'chromium', | ||
| channel: 'stable', | ||
| displayName: 'Chrome', | ||
| } | ||
|  | ||
| url = 'https://www.somedomain.test' | ||
| args = ['--headless'] | ||
| browserEnv = {} | ||
|  | ||
| launchedBrowser = { | ||
| on: vi.fn() as any, | ||
| // these are streams, but we don't need to test | ||
| // stream logic - they do need to implement event | ||
| // emission though, because of addDebugListeners | ||
| // @ts-expect-error | ||
| stdout: new EventEmitter(), | ||
| // @ts-expect-error | ||
| stderr: new EventEmitter(), | ||
| kill: vi.fn(), | ||
| } | ||
|  | ||
| vi.mocked(os.arch).mockImplementation(() => arch) | ||
| vi.mocked(os.platform).mockImplementation(() => platform) | ||
|  | ||
| vi.mocked(spawn).mockReturnValue(launchedBrowser) | ||
| }) | ||
|  | ||
| afterEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|  | ||
| it('throws when browser.path is missing', () => { | ||
| browser.path = undefined | ||
|  | ||
| expect(() => launch(browser, url, args, browserEnv)).toThrow('Browser chrome is missing path') | ||
| }) | ||
|  | ||
| describe('when darwin arm64', () => { | ||
| beforeEach(() => { | ||
| arch = 'arm64' | ||
| platform = 'darwin' | ||
| }) | ||
|  | ||
| it('launches a browser', () => { | ||
| const proc = launch(browser, url, args, browserEnv) | ||
|  | ||
| expect(spawn).toHaveBeenCalledWith( | ||
| 'arch', | ||
| [browser.path, url, ...args], | ||
| expect.objectContaining({ | ||
| stdio: ['ignore', 'pipe', 'pipe'], | ||
| env: expect.objectContaining({ | ||
| ...browserEnv, | ||
| ARCHPREFERENCE: 'arm64,x86_64', | ||
| }), | ||
| }), | ||
| ) | ||
|  | ||
| expect(proc).toBe(launchedBrowser) | ||
| }) | ||
| }) | ||
|  | ||
| for (const [testArch, testPlatform] of [ | ||
| ['x64', 'darwin'], | ||
| ['x64', 'linux'], | ||
| ['arm64', 'linux'], | ||
| ['x64', 'win32'], | ||
| ['arm64', 'win32'], | ||
| ]) { | ||
| describe(`when ${testPlatform} ${testArch}`, () => { | ||
| beforeEach(() => { | ||
| arch = testArch as typeof arch | ||
| platform = testPlatform as typeof platform | ||
| }) | ||
|  | ||
| it('launches a browser', () => { | ||
| const proc = launch(browser, url, args, browserEnv) | ||
|  | ||
| expect(spawn).toHaveBeenCalledWith( | ||
| browser.path, | ||
| [url, ...args], | ||
| expect.objectContaining({ | ||
| stdio: ['ignore', 'pipe', 'pipe'], | ||
| env: expect.any(Object), | ||
| }), | ||
| ) | ||
|  | ||
| expect(proc).toBe(launchedBrowser) | ||
| }) | ||
| }) | ||
| } | ||
| }) | 
  
    
      This file contains hidden or 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
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These were renamed because intellisense was complaining that
/lib/darwin/index.tswas too similar to/lib/platforms/Darwin.ts🤷