Skip to content

Commit

Permalink
Merge branch 'use-flag-for-installing-dvc-globally' into update-dvc-i…
Browse files Browse the repository at this point in the history
…s-unavailable-text-content
  • Loading branch information
julieg18 committed Jun 13, 2023
2 parents 3cc4b47 + a3d5a4e commit 2d7c1e9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 70 deletions.
33 changes: 2 additions & 31 deletions extension/src/python/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('setupTestVenv', () => {
const envDir = '.env'
const cwd = __dirname

await setupTestVenv(__dirname, envDir, false, 'dvc')
await setupTestVenv(__dirname, envDir, 'dvc')

expect(mockedCreateProcess).toHaveBeenCalledTimes(3)
expect(mockedCreateProcess).toHaveBeenCalledWith({
Expand Down Expand Up @@ -53,7 +53,7 @@ describe('setupTestVenv', () => {
const envDir = '.env'
const cwd = __dirname

await setupTestVenv(__dirname, envDir, false, '-r', 'requirements.txt')
await setupTestVenv(__dirname, envDir, '-r', 'requirements.txt')

expect(mockedCreateProcess).toHaveBeenCalledTimes(3)
expect(mockedCreateProcess).toHaveBeenCalledWith({
Expand All @@ -74,33 +74,4 @@ describe('setupTestVenv', () => {
executable: join(cwd, envDir, 'Scripts', 'python.exe')
})
})

it('should use a user flag if the python env is global', async () => {
mockedCreateProcess.mockResolvedValue(mockedProcess)
mockedGetProcessPlatform.mockReturnValue('freebsd')

const envDir = '.env'
const cwd = __dirname

await setupTestVenv(__dirname, envDir, true, 'dvc')

expect(mockedCreateProcess).toHaveBeenCalledTimes(3)
expect(mockedCreateProcess).toHaveBeenCalledWith({
args: ['-m', 'venv', envDir],
cwd,
executable: 'python3'
})

expect(mockedCreateProcess).toHaveBeenCalledWith({
args: ['-m', 'pip', 'install', '--upgrade', '--user', 'pip', 'wheel'],
cwd,
executable: join(cwd, envDir, 'bin', 'python')
})

expect(mockedCreateProcess).toHaveBeenCalledWith({
args: ['-m', 'pip', 'install', '--upgrade', '--user', 'dvc'],
cwd,
executable: join(cwd, envDir, 'bin', 'python')
})
})
})
28 changes: 4 additions & 24 deletions extension/src/python/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ import { getVenvBinPath } from './path'
import { getProcessPlatform } from '../env'
import { exists } from '../fileSystem'
import { Logger } from '../common/logger'
import {
createProcess,
executeProcess,
Process,
ProcessOptions
} from '../process/execution'
import { createProcess, executeProcess, Process } from '../process/execution'

const sendOutput = (process: Process) => {
process.all?.on('data', chunk =>
Expand All @@ -20,21 +15,14 @@ const sendOutput = (process: Process) => {
export const installPackages = (
cwd: string,
pythonBin: string,
isGlobalEnv: boolean,
...args: string[]
): Process => {
const options: ProcessOptions = {
args: ['-m', 'pip', 'install', '--upgrade'],
const options = {
args: ['-m', 'pip', 'install', '--upgrade', ...args],
cwd,
executable: pythonBin
}

if (isGlobalEnv) {
options.args.push('--user')
}

options.args.push(...args)

return createProcess(options)
}

Expand All @@ -44,7 +32,6 @@ export const getDefaultPython = (): string =>
export const setupTestVenv = async (
cwd: string,
envDir: string,
isGlobalEnv: boolean,
...installArgs: string[]
) => {
if (!exists(join(cwd, envDir))) {
Expand All @@ -58,19 +45,12 @@ export const setupTestVenv = async (

const venvPython = getVenvBinPath(cwd, envDir, 'python')

const venvUpgrade = installPackages(
cwd,
venvPython,
isGlobalEnv,
'pip',
'wheel'
)
const venvUpgrade = installPackages(cwd, venvPython, 'pip', 'wheel')
await sendOutput(venvUpgrade)

const installRequestedPackages = installPackages(
cwd,
venvPython,
isGlobalEnv,
...installArgs
)
return sendOutput(installRequestedPackages)
Expand Down
23 changes: 20 additions & 3 deletions extension/src/setup/autoInstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const findPythonBinForInstall = async (): Promise<
)
}

const getProcessGlobalArgs = (isGlobal: boolean) => (isGlobal ? ['--user'] : [])

const showUpgradeProgress = (
root: string,
pythonBinPath: string,
Expand All @@ -32,7 +34,12 @@ const showUpgradeProgress = (
try {
await Toast.runCommandAndIncrementProgress(
async () => {
await installPackages(root, pythonBinPath, isGlobalEnv, 'dvc')
await installPackages(
root,
pythonBinPath,
...getProcessGlobalArgs(isGlobalEnv),
'dvc'
)
return 'Upgraded successfully'
},
progress,
Expand All @@ -56,7 +63,12 @@ const showInstallProgress = (
try {
await Toast.runCommandAndIncrementProgress(
async () => {
await installPackages(root, pythonBinPath, isGlobalEnv, 'dvclive')
await installPackages(
root,
pythonBinPath,
...getProcessGlobalArgs(isGlobalEnv),
'dvclive'
)
return 'DVCLive Installed'
},
progress,
Expand All @@ -69,7 +81,12 @@ const showInstallProgress = (
try {
await Toast.runCommandAndIncrementProgress(
async () => {
await installPackages(root, pythonBinPath, isGlobalEnv, 'dvc')
await installPackages(
root,
pythonBinPath,
...getProcessGlobalArgs(isGlobalEnv),
'dvc'
)
return 'DVC Installed'
},
progress,
Expand Down
17 changes: 5 additions & 12 deletions extension/src/test/suite/setup/autoInstall.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,11 @@ suite('Auto Install Test Suite', () => {
expect(mockInstallPackages).to.be.calledWithExactly(
cwd,
defaultPython,
false,
'dvc'
)
})

it('should pass the correct params to install function if python env is used and the active env is global', async () => {
it('should install with a user flag if python env is used and the active env is global', async () => {
bypassProgressCloseDelay()
const cwd = __dirname
stub(PythonExtension, 'getPythonExecutionDetails').resolves(['python'])
Expand All @@ -110,7 +109,7 @@ suite('Auto Install Test Suite', () => {
expect(mockInstallPackages).to.be.calledWithExactly(
cwd,
defaultPython,
true,
'--user',
'dvc'
)
})
Expand Down Expand Up @@ -138,7 +137,6 @@ suite('Auto Install Test Suite', () => {
expect(mockInstallPackages).to.be.calledWithExactly(
cwd,
defaultPython,
false,
'dvc'
)
})
Expand Down Expand Up @@ -202,18 +200,16 @@ suite('Auto Install Test Suite', () => {
expect(mockInstallPackages).to.be.calledWithExactly(
cwd,
defaultPython,
false,
'dvc'
)
expect(mockInstallPackages).to.be.calledWithExactly(
cwd,
defaultPython,
false,
'dvclive'
)
})

it('should pass the correct params to install function if python env is used and the active env is global', async () => {
it('should install with a user flag if python env is used and the active env is global', async () => {
bypassProgressCloseDelay()
const cwd = __dirname
stub(PythonExtension, 'getPythonExecutionDetails').resolves(['python'])
Expand All @@ -236,13 +232,13 @@ suite('Auto Install Test Suite', () => {
expect(mockInstallPackages).to.be.calledWithExactly(
cwd,
defaultPython,
true,
'--user',
'dvc'
)
expect(mockInstallPackages).to.be.calledWithExactly(
cwd,
defaultPython,
true,
'--user',
'dvclive'
)
})
Expand All @@ -268,7 +264,6 @@ suite('Auto Install Test Suite', () => {
expect(mockInstallPackages).to.be.calledWithExactly(
cwd,
defaultPython,
false,
'dvclive'
)
})
Expand Down Expand Up @@ -298,13 +293,11 @@ suite('Auto Install Test Suite', () => {
expect(mockInstallPackages).to.be.calledWithExactly(
cwd,
defaultPython,
false,
'dvclive'
)
expect(mockInstallPackages).to.be.calledWithExactly(
cwd,
defaultPython,
false,
'dvc'
)
})
Expand Down

0 comments on commit 2d7c1e9

Please sign in to comment.