Skip to content

Commit

Permalink
Added system tests to check activation for virtual environments
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartik Raj committed Dec 8, 2018
1 parent 1aa6c54 commit 061bc9f
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 60 deletions.
15 changes: 15 additions & 0 deletions build/ci/addEnvPath.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#Adds the virtual environment's executable path to json file

import json,sys
import os.path
key = sys.argv[1]

if os.path.isfile('$(ENV_PATHS_LOCATION)'):
with open('$(ENV_PATHS_LOCATION)', 'r') as read_file:
data = json.load(read_file)
else:
with open('$(ENV_PATHS_LOCATION)', 'w+') as read_file:
data = {}
with open('$(ENV_PATHS_LOCATION)', 'w') as outfile:
data[key] = sys.executable
json.dump(data, outfile, sort_keys=True, indent=4)
104 changes: 51 additions & 53 deletions build/ci/templates/virtual_env_tests.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
parameters:
name: 'VirtualEnv_Tests_Windows_py37'
PythonVersion: '3.7'
NodeVersion: '8.11.2'
NpmVersion: 'latest'
PoolName: 'Hosted VS2017'
MOCHA_CI_REPORTER_ID: '$(Build.SourcesDirectory)/build/ci/mocha-vsts-reporter.js'
MOCHA_CI_REPORTFILE: '$(Build.ArtifactStagingDirectory)/reports/junit-report.xml'
MOCHA_REPORTER_JUNIT: true
RunHygiene: true
UploadBinary: false
AzureStorageAccountName: 'vscodepythonci'
AzureStorageContainerName: 'vscode-python-ci'
EnvironmentExecutableFolder: 'Scripts'
DIRECTORY_FOR_VIRTUAL_ENVS: './envPaths.json'
ENV_PATHS_LOCATION: './src/tmp/envPaths.json'
TEST_FILES_SUFFIX: 'testvirtualenvs'
TestSuiteName: 'testSingleWorkspace'

jobs:
- job: ${{ parameters.name }}
Expand All @@ -25,17 +19,10 @@ jobs:
nodeVersion: ${{ parameters.NodeVersion }}
npmVersion: ${{ parameters.NpmVersion }}
pythonVersion: ${{ parameters.PythonVersion }}
mochaReportFile: ${{ parameters.MOCHA_CI_REPORTFILE }}
MOCHA_CI_REPORTER_ID: ${{ parameters.MOCHA_CI_REPORTER_ID }}
MOCHA_CI_REPORTFILE: ${{ parameters.MOCHA_CI_REPORTFILE }}
MOCHA_REPORTER_JUNIT: ${{ parameters.MOCHA_REPORTER_JUNIT }}
runHygiene: ${{ parameters.RunHygiene }}
uploadBinary: ${{ parameters.UploadBinary }}
azureStorageAcctName: ${{ parameters.AzureStorageAccountName }}
azureStorageContainerName: ${{ parameters.AzureStorageContainerName }}
plaform: ${{ parameters.Platform }}
environmentExecutableFolder: ${{ parameters.EnvironmentExecutableFolder }}
DIRECTORY_FOR_VIRTUAL_ENVS: ${{ parameters.DIRECTORY_FOR_VIRTUAL_ENVS }}
ENV_PATHS_LOCATION: ${{ parameters.ENV_PATHS_LOCATION }}
TEST_FILES_SUFFIX: ${{ parameters.TEST_FILES_SUFFIX }}
TestSuiteName: ${{ parameters.TestSuiteName }}

steps:
- bash: echo REQUESTED VARIABLE VALUES
Expand Down Expand Up @@ -92,42 +79,22 @@ jobs:
versionSpec: 3.7


- bash: |
echo "#Adds the virtual environment's executable path to json file
import json,sys
import os.path
key = sys.argv[1]
if os.path.isfile('$(DIRECTORY_FOR_VIRTUAL_ENVS)'):
with open('$(DIRECTORY_FOR_VIRTUAL_ENVS)', 'r') as read_file:
data = json.load(read_file)
else:
with open('$(DIRECTORY_FOR_VIRTUAL_ENVS)', 'w+') as read_file:
data = {}
with open('$(DIRECTORY_FOR_VIRTUAL_ENVS)', 'w') as outfile:
data[key] = sys.executable
json.dump(data, outfile, sort_keys=True, indent=4)" > addEnvPath.py
displayName: 'Create Environment paths adder'
- bash: |
pip install pipenv
pipenv run python addEnvPath.py pipenv
pipenv run python ./build/ci/addEnvPath.py pipenv
cat $(DIRECTORY_FOR_VIRTUAL_ENVS)
cat $(ENV_PATHS_LOCATION)
displayName: 'Create pipenv environment'
- bash: |
python -m venv .venv
.venv/$(environmentExecutableFolder)/python addEnvPath.py venv
.venv/$(environmentExecutableFolder)/python ./build/ci/addEnvPath.py venv
cat $(DIRECTORY_FOR_VIRTUAL_ENVS)
cat $(ENV_PATHS_LOCATION)
displayName: 'Create venv environment'
Expand All @@ -137,25 +104,56 @@ jobs:
python -m virtualenv .virtualenv
.virtualenv/$(environmentExecutableFolder)/python addEnvPath.py virtualenv
.virtualenv/$(environmentExecutableFolder)/python ./build/ci/addEnvPath.py virtualenv
cat $(DIRECTORY_FOR_VIRTUAL_ENVS)
cat $(ENV_PATHS_LOCATION)
displayName: 'Create virtualenv environment'
- task: CondaEnvironment@1
displayName: 'Create conda environment '
# - task: CondaEnvironment@1
# displayName: 'Create conda environment '
# inputs:
# packageSpecs: 'python=3.7'


# - bash: |
# python ./build/ci/addEnvPath.py conda

# source deactivate

# cat $(ENV_PATHS_LOCATION)

# displayName: 'Save conda environment executable'


- task: Npm@1
displayName: 'update npm'
inputs:
packageSpecs: 'python=3.7'
command: custom

verbose: true

- bash: |
python addEnvPath.py conda
customCommand: 'install -g npm@$(NpmVersion)'


- task: Npm@1
displayName: 'npm ci'
inputs:
command: custom

verbose: true

customCommand: ci


- task: Npm@1
displayName: 'run $(TestSuiteName)'
inputs:
command: custom

source deactivate
verbose: true

cat $(DIRECTORY_FOR_VIRTUAL_ENVS)
customCommand: 'run $(TestSuiteName)'

displayName: 'Save conda environment executable'

8 changes: 3 additions & 5 deletions build/ci/vscode-python-pr-validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ jobs:
NodeVersion: '8.11.2'
NpmVersion: 'latest'
PoolName: 'Hosted VS2017'
UploadBinary: true
Platform: 'Windows'
AzureStorageAccountName: 'vscodepythonci'
AzureStorageContainerName: 'vscode-python-ci'
EnvironmentExecutableFolder: 'Scripts'
DIRECTORY_FOR_VIRTUAL_ENVS: './envPaths.json'
ENV_PATHS_LOCATION: './src/tmp/envPaths.json'
TEST_FILES_SUFFIX: 'testvirtualenvs.ts'
TestSuiteName: 'testSingleWorkspace'


- job: 'System_Test_macOS'
Expand Down
2 changes: 1 addition & 1 deletion src/test/ciConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
// Constants that pertain to CI processes/tests only. No dependencies on vscode!
//

export const ENV_PATHS_LOCATION = process.env.ENV_PATHS_LOCATION;
export const IS_APPVEYOR = process.env.APPVEYOR === 'true';
export const IS_TRAVIS = process.env.TRAVIS === 'true';
export const IS_VSTS = process.env.TF_BUILD !== undefined;
Expand Down
7 changes: 6 additions & 1 deletion src/test/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type PythonSettingKeys = 'workspaceSymbols.enabled' | 'pythonPath' |
'unitTest.nosetestArgs' | 'unitTest.pyTestArgs' | 'unitTest.unittestArgs' |
'formatting.provider' | 'sortImports.args' |
'unitTest.nosetestsEnabled' | 'unitTest.pyTestEnabled' | 'unitTest.unittestEnabled' |
'envFile' | 'jediEnabled' | 'linting.ignorePatterns';
'envFile' | 'jediEnabled' | 'linting.ignorePatterns' | 'terminal.activateEnvironment';

async function disposePythonSettings() {
if (!IS_SMOKE_TEST) {
Expand Down Expand Up @@ -76,6 +76,11 @@ export async function setPythonPathInWorkspaceRoot(pythonPath: string) {
return retryAsync(setPythonPathInWorkspace)(undefined, vscode.ConfigurationTarget.Workspace, pythonPath);
}

export async function restorePythonPathInWorkspaceRoot() {
const vscode = require('vscode') as typeof import('vscode');
return retryAsync(setPythonPathInWorkspace)(undefined, vscode.ConfigurationTarget.Workspace, PYTHON_PATH);
}

export const resetGlobalPythonPathSetting = async () => retryAsync(restoreGlobalPythonPathSetting)();

function getWorkspaceRoot() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict';
// tslint:disable:max-func-body-length no-invalid-this no-any

import { expect } from 'chai';
import * as fs from 'fs-extra';
import * as path from 'path';
import * as vscode from 'vscode';
import { ENV_PATHS_LOCATION } from '../../../ciConstants';
import { PYTHON_PATH, restorePythonPathInWorkspaceRoot, setPythonPathInWorkspaceRoot, updateSetting, waitForCondition } from '../../../common';
import { EXTENSION_ROOT_DIR_FOR_TESTS } from '../../../constants';
import { sleep } from '../../../core';
import { initialize, initializeTest } from '../../../initialize';

suite('Activation of Environments in Terminal', () => {
const file = path.join(EXTENSION_ROOT_DIR_FOR_TESTS, 'src', 'testMultiRootWkspc', 'smokeTests', 'testExecInTerminal.py');
const outputFile = path.join(EXTENSION_ROOT_DIR_FOR_TESTS, 'src', 'testMultiRootWkspc', 'smokeTests', 'testExecInTerminal.log');
const envPathsLocation = ENV_PATHS_LOCATION || path.join(EXTENSION_ROOT_DIR_FOR_TESTS, 'src', 'tmp', 'envPaths.json');
const waitTimeForActivation = 5000;
type EnvPath = {
venvPath: string;
pipenvPath: string;
virtualEnvPath: string;
};
let envPaths: EnvPath;
suiteSetup(async () => {
envPaths = await fs.readJson(envPathsLocation);
await initialize();
});
setup(async () => {
await initializeTest();
await cleanUp();
});
teardown(cleanUp);
suiteTeardown(revertSettings);
async function revertSettings() {
await updateSetting('terminal.activateEnvironment', undefined , vscode.workspace.workspaceFolders[0].uri, vscode.ConfigurationTarget.WorkspaceFolder);
await restorePythonPathInWorkspaceRoot();
}
async function cleanUp() {
if (await fs.pathExists(outputFile)) {
await fs.unlink(outputFile);
}
}
async function testActivation(envPath){
await updateSetting('terminal.activateEnvironment', true, vscode.workspace.workspaceFolders[0].uri, vscode.ConfigurationTarget.WorkspaceFolder);
await setPythonPathInWorkspaceRoot(envPath);
const terminal = vscode.window.createTerminal();
await sleep(waitTimeForActivation);
terminal.sendText(`python ${file}`, true);
await waitForCondition(() => fs.pathExists(outputFile), 5_000, '\'testExecInTerminal.log\' file not created');
const content = await fs.readFile(outputFile, 'utf-8');

expect(content).to.equal(envPath);
}
async function testNonActivation(){
await updateSetting('terminal.activateEnvironment', false, vscode.workspace.workspaceFolders[0].uri, vscode.ConfigurationTarget.WorkspaceFolder);
const terminal = vscode.window.createTerminal();
terminal.sendText(`python ${file}`, true);
await waitForCondition(() => fs.pathExists(outputFile), 5_000, '\'testExecInTerminal.log\' file not created');
const content = await fs.readFile(outputFile, 'utf-8');
expect(content).to.not.equal(PYTHON_PATH);
}
test('Should not activate', async () => {
await testNonActivation();
});
test('Should activate with venv', async () => {
await testActivation(envPaths.venvPath);
});
test('Should activate with pipenv', async () => {
await testActivation(envPaths.pipenvPath);
});
test('Should activate with virtualenv', async () => {
await testActivation(envPaths.virtualEnvPath);
});
});
6 changes: 6 additions & 0 deletions src/tmp/envPaths.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"condaPath": "C:\\Users\\karraj\\AppData\\Local\\Continuum\\anaconda3\\envs\\.conda\\python.exe",
"pipenvPath": "C:\\Users\\karraj\\.virtualenvs\\test-mFICMEuQ\\Scripts\\python.exe",
"venvPath": "C:\\Users\\karraj\\Desktop\\vscode-python\\src\\test\\.venv\\Scripts\\python.exe",
"virtualEnvPath": "C:\\Users\\karraj\\Desktop\\.virtualenv\\Scripts\\python.exe"
}

0 comments on commit 061bc9f

Please sign in to comment.