Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions src/providers/python_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,18 @@ export default class Python_controller {
} catch (error) {
throw new Error('Failed creating virtual python environment', {cause: error})
}
if(this.pathToPythonBin.includes("python3")) {
this.pathToPipBin = path.join(path.sep,this.pythonEnvDir,os.platform() === 'win32' ? "Scripts" : "bin",this.#decideIfWindowsOrLinuxPath("pip3"))
this.pathToPythonBin = path.join(path.sep,this.pythonEnvDir,os.platform() === 'win32' ? "Scripts" : "bin",this.#decideIfWindowsOrLinuxPath("python3"))
if(os.platform() === 'win32') {
let driveLetter = path.parse(process.cwd()).root
this.pathToPythonBin = `${driveLetter}${this.pathToPythonBin.substring(1)}`
this.pathToPipBin = `${driveLetter}${this.pathToPipBin.substring(1)}`
}
} else {
this.pathToPipBin = path.join(path.sep,this.pythonEnvDir,os.platform() === 'win32' ? "Scripts" : "bin",this.#decideIfWindowsOrLinuxPath("pip"));
this.pathToPythonBin = path.join(path.sep,this.pythonEnvDir,os.platform() === 'win32' ? "Scripts" : "bin",this.#decideIfWindowsOrLinuxPath("python"))
if(os.platform() === 'win32') {
let driveLetter = path.parse(process.cwd()).root
this.pathToPythonBin = `${driveLetter}${this.pathToPythonBin.substring(1)}`
this.pathToPipBin = `${driveLetter}${this.pathToPipBin.substring(1)}`
}
const isWindows = os.platform() === 'win32'
// venv always installs the unversioned `python` and `pip` names regardless of
// how the source interpreter is named: `Scripts\python.exe`/`Scripts\pip.exe`
// on Windows and `bin/python`/`bin/pip` on POSIX. A `python3` source must not
// be mapped to the nonexistent `Scripts\python3.exe` (TC-7485).
const envBinDir = isWindows ? "Scripts" : "bin"
this.pathToPipBin = path.join(path.sep, this.pythonEnvDir, envBinDir, this.#decideIfWindowsOrLinuxPath("pip"))
this.pathToPythonBin = path.join(path.sep, this.pythonEnvDir, envBinDir, this.#decideIfWindowsOrLinuxPath("python"))
if(isWindows) {
Comment thread
a-oren marked this conversation as resolved.
Resolved
let driveLetter = path.parse(process.cwd()).root
this.pathToPythonBin = `${driveLetter}${this.pathToPythonBin.substring(1)}`
this.pathToPipBin = `${driveLetter}${this.pathToPipBin.substring(1)}`
Comment thread
a-oren marked this conversation as resolved.
Resolved
}
// upgrade pip version to latest
try {
Expand Down
103 changes: 103 additions & 0 deletions test/providers/python_controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import os from 'node:os'
import path from 'node:path'

import { expect } from 'chai'
import esmock from 'esmock'

import Python_controller from '../../src/providers/python_controller.js'
import {
Expand All @@ -11,6 +12,41 @@ import {
readRequirementsFile
} from '../../src/providers/requirements_parser.js'

const REQUIREMENTS_MANIFEST = 'test/providers/tst_manifests/pip/pip_requirements_txt_no_ignore/requirements.txt'

/**
* Loads Python_controller with os.platform() and command invocation stubbed, then
* constructs a virtual-environment controller so the venv setup invocations can be
* inspected. Stubbing the parser keeps the test independent of the tree-sitter wasm
* artifacts.
* @param {string} platform - platform reported by os.platform()
* @param {string} sourcePython - configured source python binary
* @return {Promise<{controller: Python_controller, invocations: Array<{bin: string, args: string[]}>}>}
*/
async function constructVirtualEnvController(platform, sourcePython) {
const invocations = []
/** Records a command invocation instead of executing it. */
const recordInvocation = (bin, args) => {
invocations.push({ bin, args })
return ''
}
const { default: MockedPythonController } = await esmock('../../src/providers/python_controller.js', {
os: { default: { platform: () => platform }, EOL: '\n' },
'../../src/tools.js': {
environmentVariableIsPopulated: () => false,
getCustom: () => undefined,
invokeCommand: recordInvocation
},
'../../src/providers/requirements_parser.js': {
getParser: async () => null,
getRequirementQuery: async () => null,
getPinnedVersionQuery: async () => null
}
})
const controller = new MockedPythonController(false, 'pip3', sourcePython, REQUIREMENTS_MANIFEST)
return { controller, invocations }
}

suite('Python_controller BEST_EFFORTS validation', function() {
let originalBestEfforts

Expand Down Expand Up @@ -81,6 +117,73 @@ suite('Python_controller BEST_EFFORTS validation', function() {
})
})

suite('Python_controller virtual environment executables', function() {
// Windows venv executable names must not depend on how the source interpreter is named (TC-7485).
for (const sourcePython of ['python3', 'python']) {
/** Verifies that a Windows venv created from `sourcePython` upgrades pip via Scripts/python.exe. */
test(`upgrades pip via Scripts/python.exe on Windows for a ${sourcePython} source`, async function() {
// Given a Windows host and a configured source interpreter named sourcePython
const { controller, invocations } = await constructVirtualEnvController('win32', sourcePython)

// When the venv is prepared while the controller is constructed
const venvInvocation = invocations[0]
const pipUpgradeInvocation = invocations[1]

// Then the venv is created from the configured source interpreter
expect(invocations).to.have.lengthOf(2)
expect(venvInvocation.bin).to.equal(sourcePython)
expect(venvInvocation.args).to.deep.equal(['-m', 'venv', path.join(path.sep, 'tmp', 'trustify_da_env_js')])

// Then the executable paths are anchored to the venv directory that was created.
// Resolving the recorded target makes this match the controller's drive-prefixed
// Windows paths on a native host as well as under the mocked win32 platform.
const expectedVenvDir = path.resolve(venvInvocation.args[2])
expect(path.resolve(controller.pythonEnvDir)).to.equal(expectedVenvDir)
const expectedPythonBin = path.join(expectedVenvDir, 'Scripts', 'python.exe')
const expectedPipBin = path.join(expectedVenvDir, 'Scripts', 'pip.exe')

// Then the pip upgrade runs through that venv's Scripts/python.exe exactly
expect(pipUpgradeInvocation.args).to.deep.equal(['-m', 'pip', 'install', '--upgrade', 'pip'])
expect(pipUpgradeInvocation.bin).to.equal(expectedPythonBin)
expect(controller.pathToPythonBin).to.equal(expectedPythonBin)

// Then the venv pip path is exactly that venv's Scripts/pip.exe
expect(controller.pathToPipBin).to.equal(expectedPipBin)

// Then no invocation targets the nonexistent Scripts/python3.exe
expect(path.basename(pipUpgradeInvocation.bin)).to.not.equal('python3.exe')
expect(invocations.map(invocation => path.basename(invocation.bin))).to.not.include('python3.exe')
expect(invocations.map(invocation => path.basename(invocation.bin))).to.not.include('pip3.exe')
})
}

// The venv layout is independent of the source interpreter name, so POSIX
// environments always expose bin/python and bin/pip.
for (const sourcePython of ['python3', 'python']) {
/** Verifies that POSIX venvs expose bin/python and bin/pip regardless of the source interpreter name. */
test(`uses bin/python and bin/pip on non-Windows hosts for a ${sourcePython} source`, async function() {
// Given a POSIX host and a configured source interpreter named sourcePython
const { controller, invocations } = await constructVirtualEnvController('linux', sourcePython)

// When the venv is prepared while the controller is constructed
const venvInvocation = invocations[0]
const pipUpgradeInvocation = invocations[1]

// Then the venv is created from the configured source interpreter
expect(invocations).to.have.lengthOf(2)
expect(venvInvocation.bin).to.equal(sourcePython)
expect(venvInvocation.args[2]).to.equal(path.join(path.sep, 'tmp', 'trustify_da_env_js'))

// Then the pip upgrade runs through the venv bin/python exactly
expect(pipUpgradeInvocation.args).to.deep.equal(['-m', 'pip', 'install', '--upgrade', 'pip'])
expect(pipUpgradeInvocation.bin).to.equal(path.join(venvInvocation.args[2], 'bin', 'python'))
expect(controller.pathToPythonBin).to.equal(path.join(venvInvocation.args[2], 'bin', 'python'))

// Then the venv pip path is bin/pip
expect(controller.pathToPipBin).to.equal(path.join(venvInvocation.args[2], 'bin', 'pip'))
})
}
})
suite('Python_controller requirements file decoding', () => {
const requirementsText = 'blinker==1.9.0\r\ncertifi==2026.10.23\r\n'
let tempDir
Expand Down
Loading