[Ubuntu] Add verbosity during software report generation (#4811)

* Add verbosity during software report generation

* Fix Get-CpanVersion

* set default ValidateExitCode value to $true

* update java test

* use lower-case for parameters
This commit is contained in:
Aleksandr Chebotov
2021-12-28 11:16:20 +03:00
committed by GitHub
parent bb56fb7ef4
commit 92eeb55189
8 changed files with 107 additions and 44 deletions

View File

@@ -140,7 +140,7 @@ function Get-HomebrewVersion {
} }
function Get-CpanVersion { function Get-CpanVersion {
$result = Get-CommandResult "cpan --version" $result = Get-CommandResult "cpan --version" -ExpectExitCode @(25, 255)
$result.Output -match "version (?<version>\d+\.\d+) " | Out-Null $result.Output -match "version (?<version>\d+\.\d+) " | Out-Null
$cpanVersion = $Matches.version $cpanVersion = $Matches.version
return "cpan $cpanVersion" return "cpan $cpanVersion"

View File

@@ -3,7 +3,9 @@ param (
$OutputDirectory $OutputDirectory
) )
$ErrorActionPreference = "Stop" $global:ErrorActionPreference = "Stop"
$global:ErrorView = "NormalView"
Set-StrictMode -Version Latest
Import-Module MarkdownPS Import-Module MarkdownPS
Import-Module (Join-Path $PSScriptRoot "SoftwareReport.Android.psm1") -DisableNameChecking Import-Module (Join-Path $PSScriptRoot "SoftwareReport.Android.psm1") -DisableNameChecking
@@ -36,41 +38,41 @@ $markdown += New-MDHeader "Installed Software" -Level 2
$markdown += New-MDHeader "Language and Runtime" -Level 3 $markdown += New-MDHeader "Language and Runtime" -Level 3
$runtimesList = @( $runtimesList = @(
(Get-BashVersion), (Get-BashVersion),
(Get-CPPVersions), (Get-CPPVersions),
(Get-FortranVersions), (Get-FortranVersions),
(Get-ErlangVersion), (Get-ErlangVersion),
(Get-ErlangRebar3Version), (Get-ErlangRebar3Version),
(Get-MonoVersion), (Get-MonoVersion),
(Get-MsbuildVersion), (Get-MsbuildVersion),
(Get-NodeVersion), (Get-NodeVersion),
(Get-PerlVersion), (Get-PerlVersion),
(Get-PythonVersion), (Get-PythonVersion),
(Get-Python3Version), (Get-Python3Version),
(Get-RubyVersion), (Get-RubyVersion),
(Get-SwiftVersion), (Get-SwiftVersion),
(Get-JuliaVersion), (Get-JuliaVersion),
(Get-KotlinVersion), (Get-KotlinVersion),
(Get-ClangVersions), (Get-ClangVersions),
(Get-ClangFormatVersions) (Get-ClangFormatVersions)
) )
$markdown += New-MDList -Style Unordered -Lines ($runtimesList | Sort-Object) $markdown += New-MDList -Style Unordered -Lines ($runtimesList | Sort-Object)
$markdown += New-MDHeader "Package Management" -Level 3 $markdown += New-MDHeader "Package Management" -Level 3
$packageManagementList = @( $packageManagementList = @(
(Get-HomebrewVersion), (Get-HomebrewVersion),
(Get-CpanVersion), (Get-CpanVersion),
(Get-GemVersion), (Get-GemVersion),
(Get-MinicondaVersion), (Get-MinicondaVersion),
(Get-HelmVersion), (Get-HelmVersion),
(Get-NpmVersion), (Get-NpmVersion),
(Get-YarnVersion), (Get-YarnVersion),
(Get-PipxVersion), (Get-PipxVersion),
(Get-PipVersion), (Get-PipVersion),
(Get-Pip3Version), (Get-Pip3Version),
(Get-VcpkgVersion) (Get-VcpkgVersion)
) )
$markdown += New-MDList -Style Unordered -Lines ($packageManagementList | Sort-Object) $markdown += New-MDList -Style Unordered -Lines ($packageManagementList | Sort-Object)
@@ -273,4 +275,5 @@ $markdown += New-MDNewLine
$markdown += New-MDHeader "Installed apt packages" -Level 3 $markdown += New-MDHeader "Installed apt packages" -Level 3
$markdown += Get-AptPackages | New-MDTable $markdown += Get-AptPackages | New-MDTable
Test-BlankElement
$markdown | Out-File -FilePath "${OutputDirectory}/Ubuntu-Readme.md" $markdown | Out-File -FilePath "${OutputDirectory}/Ubuntu-Readme.md"

View File

@@ -182,7 +182,7 @@ function Get-NvmVersion {
function Get-PackerVersion { function Get-PackerVersion {
# Packer 1.7.1 has a bug and outputs version to stderr instead of stdout https://github.com/hashicorp/packer/issues/10855 # Packer 1.7.1 has a bug and outputs version to stderr instead of stdout https://github.com/hashicorp/packer/issues/10855
$result = (Get-CommandResult -Command "packer --version").Output $result = (Get-CommandResult "packer --version").Output
$packerVersion = [regex]::matches($result, "(\d+.){2}\d+").Value $packerVersion = [regex]::matches($result, "(\d+.){2}\d+").Value
return "Packer $packerVersion" return "Packer $packerVersion"
} }

View File

@@ -2,11 +2,27 @@ function Get-CommandResult {
param ( param (
[Parameter(Mandatory=$true)] [Parameter(Mandatory=$true)]
[string] $Command, [string] $Command,
[switch] $Multiline [int[]] $ExpectExitCode = 0,
[switch] $Multiline,
[bool] $ValidateExitCode = $true
) )
# Bash trick to suppress and show error output because some commands write to stderr (for example, "python --version") # Bash trick to suppress and show error output because some commands write to stderr (for example, "python --version")
$stdout = & bash -c "$Command 2>&1" $stdout = & bash -c "$Command 2>&1"
$exitCode = $LASTEXITCODE $exitCode = $LASTEXITCODE
if ($ValidateExitCode) {
if ($ExpectExitCode -notcontains $exitCode) {
try {
throw "StdOut: '$stdout' ExitCode: '$exitCode'"
} catch {
Write-Host $_.Exception.Message
Write-Host $_.ScriptStackTrace
exit $LASTEXITCODE
}
}
}
return @{ return @{
Output = If ($Multiline -eq $true) { $stdout } else { [string]$stdout } Output = If ($Multiline -eq $true) { $stdout } else { [string]$stdout }
ExitCode = $exitCode ExitCode = $exitCode

View File

@@ -110,3 +110,49 @@ function Get-AptSourceRepository {
return $sourceUrl return $sourceUrl
} }
function Test-BlankElement {
param(
[string] $Markdown
)
$splitByLines = $Markdown.Split("`n")
# Validate entry without version
$blankVersions = $splitByLines -match "^-" -notmatch "Image Version|MySQL Server|Vcpkg|\d\." | Out-String
# Validate tables with blank rows
$blankRows = ""
for($i = 0; $i -lt $splitByLines.Length; $i++) {
$addRows= $false
$table = @()
if ($splitByLines[$i].StartsWith("#") -and $splitByLines[$i+1].StartsWith("|")) {
$table += $splitByLines[$i,($i+1),($i+2)]
$i += 3
$current = $splitByLines[$i]
while ($current.StartsWith("|")) {
$isBlankRow = $current.Substring(1, $current.LastIndexOf("|") - 2).Split("|").Trim() -contains ""
if ($isBlankRow) {
$table += $current
$addRows = $true
}
$current = $splitByLines[++$i]
}
if ($addRows) {
$blankRows += $table | Out-String
}
}
}
# Display report
$isReport = $false
if ($blankVersions) {
Write-Host "Software list with blank version:`n${blankVersions}"
$isReport = $true
}
if ($blankRows) {
Write-Host "Tables with blank rows:`n${blankRows}"
$isReport = $true
}
if ($isReport) {
exit 1
}
}

View File

@@ -44,12 +44,12 @@ function Invoke-PesterTests {
function ShouldReturnZeroExitCode { function ShouldReturnZeroExitCode {
Param( Param(
[String] $ActualValue, [string] $ActualValue,
[switch] $Negate, [switch] $Negate,
[string] $Because # This parameter is unused but we need it to match Pester asserts signature [string] $Because # This parameter is unused but we need it to match Pester asserts signature
) )
$result = Get-CommandResult $ActualValue $result = Get-CommandResult $ActualValue -ValidateExitCode $false
[bool]$succeeded = $result.ExitCode -eq 0 [bool]$succeeded = $result.ExitCode -eq 0
if ($Negate) { $succeeded = -not $succeeded } if ($Negate) { $succeeded = -not $succeeded }
@@ -69,12 +69,12 @@ function ShouldReturnZeroExitCode {
function ShouldMatchCommandOutput { function ShouldMatchCommandOutput {
Param( Param(
[String] $ActualValue, [string] $ActualValue,
[String] $RegularExpression, [string] $RegularExpression,
[switch] $Negate [switch] $Negate
) )
$output = (Get-CommandResult $ActualValue).Output | Out-String $output = (Get-CommandResult $ActualValue -ValidateExitCode $false).Output | Out-String
[bool] $succeeded = $output -cmatch $RegularExpression [bool] $succeeded = $output -cmatch $RegularExpression
if ($Negate) { if ($Negate) {

View File

@@ -11,7 +11,7 @@ Describe "MongoDB" {
Describe "PostgreSQL" { Describe "PostgreSQL" {
It "PostgreSQL Service" { It "PostgreSQL Service" {
"sudo systemctl start postgresql" | Should -ReturnZeroExitCode "sudo systemctl start postgresql" | Should -ReturnZeroExitCode
(Get-CommandResult "pg_isready").Output | Should -Be "/var/run/postgresql:5432 - accepting connections" "pg_isready" | Should -MatchCommandOutput "/var/run/postgresql:5432 - accepting connections"
"sudo systemctl stop postgresql" | Should -ReturnZeroExitCode "sudo systemctl stop postgresql" | Should -ReturnZeroExitCode
} }

View File

@@ -29,8 +29,7 @@ Describe "Java" {
$gradleVariableValue | Should -BeLike "/usr/share/gradle-*" $gradleVariableValue | Should -BeLike "/usr/share/gradle-*"
$gradlePath = Join-Path $env:GRADLE_HOME "bin/gradle" $gradlePath = Join-Path $env:GRADLE_HOME "bin/gradle"
$result = Get-CommandResult "`"$GradlePath`" -version" "`"$GradlePath`" -version" | Should -ReturnZeroExitCode
$result.ExitCode | Should -Be 0
} }
It "Java <Version>" -TestCases $jdkVersions { It "Java <Version>" -TestCases $jdkVersions {
@@ -38,12 +37,11 @@ Describe "Java" {
$javaVariableValue | Should -Not -BeNullOrEmpty $javaVariableValue | Should -Not -BeNullOrEmpty
$javaPath = Join-Path $javaVariableValue "bin/java" $javaPath = Join-Path $javaVariableValue "bin/java"
$result = Get-CommandResult "`"$javaPath`" -version" "`"$javaPath`" -version" | Should -ReturnZeroExitCode
$result.ExitCode | Should -Be 0
if ($Version -eq 8) { if ($Version -eq 8) {
$Version = "1.${Version}" $Version = "1.${Version}"
} }
$result.Output | Should -Match ([regex]::Escape("openjdk version `"${Version}.")) "`"$javaPath`" -version" | Should -MatchCommandOutput ([regex]::Escape("openjdk version `"${Version}."))
} }
} }