Pauldorsch/fix support python m pip (unit tests) (#1223)

* 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
This commit is contained in:
Paul Dorsch 2024-08-08 17:06:32 -04:00 коммит произвёл GitHub
Родитель 84e9308790
Коммит 924c4ea498
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
1 изменённых файлов: 95 добавлений и 2 удалений

Просмотреть файл

@ -67,6 +67,22 @@ public class PipCommandServiceTests
(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()
{
@ -124,6 +140,42 @@ public class PipCommandServiceTests
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()
{
@ -265,6 +317,49 @@ public class PipCommandServiceTests
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);
@ -284,8 +379,6 @@ public class PipCommandServiceTests
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]