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
8 changes: 7 additions & 1 deletion Python/Product/PythonTools/pip_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,13 @@ def install_from_ensurepip(ensurepip):
sys.stdout.flush()

# We can bootstrap with ensurepip, but then have to upgrade to the latest
ensurepip.bootstrap(upgrade=True, default_pip=True)
if sys.platform == 'cli':
ensurepip.bootstrap(upgrade=True, default_pip=True)
else:
# In-process ensurepip disables pip configuration for the pip upgrade below.
subprocess.check_call(
EXECUTABLE + ["-m", "ensurepip", "--upgrade", "--default-pip"]
)

# Latest version doesn't work on IronPython, so don't upgrade
if sys.platform != 'cli':
Expand Down
115 changes: 115 additions & 0 deletions Python/Tests/VSInterpretersTests/PipDownloaderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Python Tools for Visual Studio
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABILITY OR NON-INFRINGEMENT.
//
// See the Apache License 2.0 for the specific language governing permissions and
// limitations under the License.

using System;
using System.IO;
using System.Linq;
using Microsoft.PythonTools.Infrastructure;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestUtilities;

namespace VSInterpretersTests {
[TestClass]
public class PipDownloaderTests {
[TestMethod, Priority(UnitTestPriority.P0)]
public void EnsurePipPreservesPipConfiguration() {
var python = PythonPaths.LatestVersion;
python.AssertInstalled("Unable to run test because Python 3 must be installed");

var pipDownloader = Path.GetFullPath(Path.Combine(
TestData.Root,
"..",
"Product",
"PythonTools",
"pip_downloader.py"
));

const string sentinel = "sentinel-pip-config.ini";
const string probe = @"
import os
import runpy
import sys
from unittest.mock import patch

module = runpy.run_path(sys.argv[1])
calls = []

class MutatingEnsurePip:
@staticmethod
def bootstrap(**kwargs):
os.environ[""PIP_CONFIG_FILE""] = os.devnull

def record_call(args):
calls.append(args)

os.environ[""PIP_CONFIG_FILE""] = sys.argv[2]
with patch.object(module[""subprocess""], ""check_call"", side_effect=record_call):
module[""install_from_ensurepip""](MutatingEnsurePip)

print(""RESULT:"" + os.environ[""PIP_CONFIG_FILE""])
for call in calls:
print(""CALL:"" + ""|"".join(call))
";

string probePath;
using (FileUtils.TemporaryTextFile(out probePath, probe))
using (var output = ProcessOutput.RunHiddenAndCapture(
python.InterpreterPath,
probePath,
pipDownloader,
sentinel
)) {
Assert.IsTrue(output.Wait(TimeSpan.FromSeconds(30)), "Probe did not exit promptly");

var diagnostics = string.Join(
Environment.NewLine,
output.StandardOutputLines.Concat(output.StandardErrorLines)
);
Assert.AreEqual(0, output.ExitCode, diagnostics);

var lines = output.StandardOutputLines
.Where(line => line.StartsWith("RESULT:") || line.StartsWith("CALL:"))
.ToArray();
Assert.AreEqual(3, lines.Length, diagnostics);
Assert.AreEqual("RESULT:" + sentinel, lines[0]);
var ensurePipCall = lines[1].Substring("CALL:".Length).Split('|');
var pipCall = lines[2].Substring("CALL:".Length).Split('|');
Assert.AreEqual(ensurePipCall[0], pipCall[0]);
CollectionAssert.AreEqual(
new[] {
"-m",
"ensurepip",
"--upgrade",
"--default-pip"
},
ensurePipCall.Skip(1).ToArray()
);
CollectionAssert.AreEqual(
new[] {
"-m",
"pip",
"install",
"-U",
"pip",
"setuptools",
"wheel"
},
pipCall.Skip(1).ToArray()
);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
</ItemGroup>
<ItemGroup>
<Compile Include="PipDownloaderTests.cs" />
<Compile Include="PipRequirementsUtilsTests.cs" />
<Compile Include="StringListReaderTests.cs" />
<Compile Include="VSInterpretersTests.cs" />
Expand Down
Loading