import * as assert from 'assert' ; import * as fse from 'fs-extra' ; import * as path from 'path' ; import { cpUtils , delay , ext , getGlobalSetting , getRandomHexString , pythonVenvSetting , updateGlobalSetting , venvUtils } from '../extension.bundle' ; import { longRunningTestsEnabled , testFolderPath } from './global.test' ; import { runWithSetting } from './runWithSetting' ; suite ( 'venvUtils' , ( ) => { const command = 'do a thing' ; const venvName = '.venv' ; const testFolder = path . join ( testFolderPath , 'venvUtils' ) ; let oldVenvValue ; suiteSetup ( async function ( this ) : Promise < void > { oldVenvValue = getGlobalSetting ( pythonVenvSetting ) ; await updateGlobalSetting ( pythonVenvSetting , venvName ) ; if ( longRunningTestsEnabled ) { this . timeout ( 60 * 1000 ) ; await fse . ensureDir ( testFolder ) ; const pyAlias = process . platform === 'win32' ? 'py' : 'python3' ; await cpUtils . executeCommand ( ext . outputChannel , testFolder , pyAlias , '-m' , 'venv' , venvName ) ; } } ) ; suiteTeardown ( async ( ) => { await updateGlobalSetting ( pythonVenvSetting , oldVenvValue ) ; } ) ; test ( 'venvExists true' , async function ( this ) : Promise < void > { if ( ! longRunningTestsEnabled ) { this . skip ( ) ; } assert . equal ( await venvUtils . venvExists ( venvName , testFolder ) , true ) ; } ) ; test ( 'venvExists false' , async ( ) => { assert . equal ( await venvUtils . venvExists ( 'nonExistentPath' , testFolder ) , false ) ; const fileName = 'notAVenvFile' ; await fse . ensureFile ( path . join ( testFolder , fileName ) ) ; assert . equal ( await venvUtils . venvExists ( fileName , testFolder ) , false ) ; const folderName = 'notAVenvFolder' ; await fse . ensureDir ( path . join ( testFolder , folderName ) ) ; assert . equal ( await venvUtils . venvExists ( folderName , testFolder ) , false ) ; } ) ; test ( 'runCommandInVenv' , async function ( this ) : Promise < void > { if ( ! longRunningTestsEnabled ) { this . skip ( ) ; } await venvUtils . runCommandInVenv ( 'python --version' , venvName , testFolder ) ; } ) ; test ( 'convertToVenvCommand Windows powershell' , async function ( this ) : Promise < void > { if ( process . platform !== 'win32' ) { this . skip ( ) ; } await runWithWindowsTerminal ( 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe' , ( ) => { assert . equal ( venvUtils . convertToVenvCommand ( command , testFolder ) , '.venv\\Scripts\\activate ; do a thing' ) ; assert . equal ( venvUtils . convertToVenvPythonCommand ( command , venvName , 'win32' ) , '.venv\\Scripts\\python -m do a thing' ) ; } ) ; } ) ; test ( 'convertToVenvCommand Windows pwsh' , async function ( this ) : Promise < void > { if ( process . platform !== 'win32' ) { this . skip ( ) ; } await runWithWindowsTerminal ( 'C:\\Program Files\\PowerShell\\7\\pwsh.exe' , ( ) => { assert . equal ( venvUtils . convertToVenvCommand ( command , testFolder ) , '.venv\\Scripts\\activate ; do a thing' ) ; assert . equal ( venvUtils . convertToVenvPythonCommand ( command , venvName , 'win32' ) , '.venv\\Scripts\\python -m do a thing' ) ; } ) ; } ) ; test ( 'convertToVenvCommand Windows cmd' , async function ( this ) : Promise < void > { if ( process . platform !== 'win32' ) { this . skip ( ) ; } await runWithWindowsTerminal ( 'C:\\Windows\\System32\\cmd.exe' , ( ) => { assert . equal ( venvUtils . convertToVenvCommand ( command , testFolder ) , '.venv\\Scripts\\activate && do a thing' ) ; assert . equal ( venvUtils . convertToVenvPythonCommand ( command , venvName , 'win32' ) , '.venv\\Scripts\\python -m do a thing' ) ; } ) ; } ) ; test ( 'convertToVenvCommand Windows git bash' , async function ( this ) : Promise < void > { if ( process . platform !== 'win32' ) { this . skip ( ) ; } await runWithWindowsTerminal ( 'C:\\Program Files\\Git\\bin\\bash.exe' , ( ) => { assert . equal ( venvUtils . convertToVenvCommand ( command , testFolder ) , '. .venv/Scripts/activate && do a thing' ) ; assert . equal ( venvUtils . convertToVenvPythonCommand ( command , venvName , 'win32' ) , '.venv/Scripts/python -m do a thing' ) ; } ) ; } ) ; test ( 'convertToVenvCommand Mac' , async function ( this ) : Promise < void > { if ( process . platform !== 'darwin' ) { this . skip ( ) ; } assert . equal ( venvUtils . convertToVenvCommand ( command , testFolder ) , '. .venv/bin/activate && do a thing' ) ; assert . equal ( venvUtils . convertToVenvPythonCommand ( command , venvName , 'darwin' ) , '.venv/bin/python -m do a thing' ) ; } ) ; test ( 'convertToVenvCommand Linux' , async function ( this ) : Promise < void > { if ( process . platform !== 'linux' ) { this . skip ( ) ; } assert . equal ( venvUtils . convertToVenvCommand ( command , testFolder ) , '. .venv/bin/activate && do a thing' ) ; assert . equal ( venvUtils . convertToVenvPythonCommand ( command , venvName , 'linux' ) , '.venv/bin/python -m do a thing' ) ; } ) ; } ) ; async function runWithWindowsTerminal ( terminalPath , callback ) { if ( ! ( await fse . pathExists ( terminalPath ) ) ) { throw new Error ( `Terminal path cannot be set because it does not exist: ${ terminalPath }` ) ; } const profileName = getRandomHexString ( ) ; const terminalProfiles = { [ profileName ] : { path : terminalPath } } ; await runWithSetting ( 'terminal.integrated.profiles.windows' , terminalProfiles , async ( ) => { await runWithSetting ( 'terminal.integrated.defaultProfile.windows' , profileName , async ( ) => { await delay ( 5 * 1000 ) ; callback ( ) ; } ) ; } ) ; }