Skip to content

Commit

Permalink
Pauldorsch/fix support python m pip (unit tests) (#1223)
Browse files Browse the repository at this point in the history
* add support for python -m pip

* update pip command service to accept python exe

* swap so we use pip as default

* fixing remote build

* fix tests

* add unit tests
  • Loading branch information
pauld-msft committed Aug 8, 2024
1 parent 84e9308 commit 924c4ea
Showing 1 changed file with 95 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ public async Task PipCommandService_ReturnsFalseWhenPipExistsAsync()
(await service.PipExistsAsync()).Should().BeFalse();
}

[TestMethod]
public async Task PipCommandService_ReturnsTrueWhenPythonExistsAsync()
{
this.commandLineInvokationService.Setup(x => x.CanCommandBeLocatedAsync("pip", It.IsAny<IEnumerable<string>>(), "--version")).ReturnsAsync(false);
this.commandLineInvokationService.Setup(x => x.CanCommandBeLocatedAsync("python", It.IsAny<IEnumerable<string>>(), "-m", "pip", "--version")).ReturnsAsync(true);

var service = new PipCommandService(
this.commandLineInvokationService.Object,
this.pathUtilityService,
this.fileUtilityService.Object,
this.envVarService.Object,
this.logger.Object);

(await service.PipExistsAsync()).Should().BeTrue();
}

[TestMethod]
public async Task PipCommandService_ReturnsTrueWhenPipExistsForAPathAsync()
{
Expand Down Expand Up @@ -124,6 +140,42 @@ public async Task PipCommandService_BadVersion_ReturnsNullAsync()
semVer.Should().BeNull();
}

[TestMethod]
public async Task PipCommandService_BadPip_ReturnsPythonAsync()
{
this.commandLineInvokationService.Setup(x => x.CanCommandBeLocatedAsync(
It.Is<string>(x => x == "pip" || x == "python"),
It.IsAny<IEnumerable<string>>(),
It.Is<string[]>(x => x.Last() == "--version")))
.ReturnsAsync(false);

this.commandLineInvokationService.Setup(x => x.CanCommandBeLocatedAsync(
"python",
It.IsAny<IEnumerable<string>>(),
It.Is<string[]>(x => x.Last() == "--version")))
.ReturnsAsync(true);

this.commandLineInvokationService.Setup(x => x.ExecuteCommandAsync(
"python",
It.IsAny<IEnumerable<string>>(),
It.IsAny<DirectoryInfo>(),
It.IsAny<CancellationToken>(),
new string[] { "-m", "pip", "--version" }))
.ReturnsAsync(new CommandLineExecutionResult { ExitCode = 0, StdOut = "pip 24.1.2 from C:\\Python312\\site-packages\\pip (python 3.12)" });

var service = new PipCommandService(
this.commandLineInvokationService.Object,
this.pathUtilityService,
this.fileUtilityService.Object,
this.envVarService.Object,
this.logger.Object);

var semVer = await service.GetPipVersionAsync();
semVer.Major.Should().Be(24);
semVer.Minor.Should().Be(1);
semVer.Build.Should().Be(2);
}

[TestMethod]
public async Task PipCommandService_BadVersionString_ReturnsNullAsync()
{
Expand Down Expand Up @@ -265,6 +317,49 @@ public async Task PipCommandService_GeneratesReport_RequirementsTxt_CorrectlyAsy

var (report, reportFile) = await service.GenerateInstallationReportAsync(testPath);

ValidateRequirementsTxtReportFile(report, reportFile);

this.commandLineInvokationService.Verify();
}

[TestMethod]
public async Task PythonPipCommandService_GeneratesReport_RequirementsTxt_CorrectlyAsync()
{
var testPath = Path.Join(Directory.GetCurrentDirectory(), string.Join(Guid.NewGuid().ToString(), ".txt"));

this.commandLineInvokationService.Setup(x => x.CanCommandBeLocatedAsync("pip", It.IsAny<IEnumerable<string>>(), "--version")).ReturnsAsync(false);
this.commandLineInvokationService.Setup(x => x.CanCommandBeLocatedAsync("python", It.IsAny<IEnumerable<string>>(), "-m", "pip", "--version")).ReturnsAsync(true);

var service = new PipCommandService(
this.commandLineInvokationService.Object,
this.pathUtilityService,
this.fileUtilityService.Object,
this.envVarService.Object,
this.logger.Object);

this.commandLineInvokationService.Setup(x => x.ExecuteCommandAsync(
"python",
It.IsAny<IEnumerable<string>>(),
It.Is<DirectoryInfo>(d => d.FullName.Contains(Directory.GetCurrentDirectory(), StringComparison.OrdinalIgnoreCase)),
It.IsAny<CancellationToken>(),
It.Is<string[]>(s =>
s.Any(e => e.Contains("requirements.txt", StringComparison.OrdinalIgnoreCase))
&& s.Any(e => e.Equals("-m", StringComparison.OrdinalIgnoreCase)))))
.ReturnsAsync(new CommandLineExecutionResult { ExitCode = 0, StdErr = string.Empty, StdOut = string.Empty })
.Verifiable();

this.fileUtilityService.Setup(x => x.ReadAllTextAsync(It.IsAny<FileInfo>()))
.ReturnsAsync(TestResources.pip_report_single_pkg);

var (report, reportFile) = await service.GenerateInstallationReportAsync(testPath);

ValidateRequirementsTxtReportFile(report, reportFile);

this.commandLineInvokationService.Verify();
}

private static void ValidateRequirementsTxtReportFile(PipInstallationReport report, FileInfo reportFile)
{
// the file shouldn't exist since we're not writing to it in the test
reportFile.Should().NotBeNull();
reportFile.Exists.Should().Be(false);
Expand All @@ -284,8 +379,6 @@ public async Task PipCommandService_GeneratesReport_RequirementsTxt_CorrectlyAsy
report.InstallItems[0].Metadata.AuthorEmail.Should().Be("benjamin@python.org");
report.InstallItems[0].Metadata.Maintainer.Should().BeNullOrEmpty();
report.InstallItems[0].Metadata.MaintainerEmail.Should().BeNullOrEmpty();

this.commandLineInvokationService.Verify();
}

[TestMethod]
Expand Down

0 comments on commit 924c4ea

Please sign in to comment.