Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Introduce Resolve-ManifestInformation function #160

Merged
merged 1 commit into from
May 23, 2021
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
refactor: Introduce Resolve-ManifestInformation function
Support only local path (+versioned)

Small extraction of #137
  • Loading branch information
Ash258 committed May 23, 2021
commit 906b892b9d7739cfbff2863b67e232e6634670be
96 changes: 94 additions & 2 deletions lib/manifest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ function ConvertTo-Manifest {
}
}

# TODO: YAML
function appname_from_url($url) { return (Split-Path $url -Leaf) -replace '\.json$' }
function appname_from_url($url) { return (Split-Path $url -Leaf) -replace "\.($ALLOWED_MANIFEST_EXTENSION_REGEX)$" }

function manifest_path($app, $bucket, $version = $null) {
$name = sanitary_path $app
Expand Down Expand Up @@ -181,6 +180,99 @@ function New-VersionedManifest {
}
}

#region Resolve Helpers
$_br = '[/\\]'
$_archivedManifestRegex = "${_br}bucket${_br}old${_br}(?<manifestName>.+?)${_br}(?<manifestVersion>.+?)\.(?<manifestExtension>$ALLOWED_MANIFEST_EXTENSION_REGEX)$"
#endregion Resolve Helpers

function Get-LocalManifest {
<#
.SYNOPSIS
Get "metadata" about local manifest with support for archived manifests.
.PARAMETER Query
Specifies the file path where manifest is stored.
#>
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param([Parameter(Mandatory, ValueFromPipeline)] [String] $Query)

process {
try {
$manifest = ConvertFrom-Manifest -LiteralPath $Query
} catch {
throw [ScoopException] "File is not a valid manifest ($($_.Exception.Message))" # TerminatingError thrown
}

$localPath = Get-Item -LiteralPath $Query
$applicationName = $localPath.BaseName

# Check if archived version was provided
if ($localPath.FullName -match $_archivedManifestRegex) {
$applicationName = $Matches['manifestName']
}

return @{
'Name' = $applicationName
'Manifest' = $manifest
'Path' = $localPath
}
}
}

function Resolve-ManifestInformation {
<#
.SYNOPSIS
Find and parse manifest file according to search query. Return universal object with all relevant information about manifest.
.PARAMETER ApplicationQuery
Specifies the string used for looking for manifest.
.EXAMPLE
Resolve-ManifestInformation -ApplicationQuery 'pwsh'
Resolve-ManifestInformation -ApplicationQuery 'pwsh@7.2.0'
Resolve-ManifestInformation -ApplicationQuery 'Ash258/pwsh'
Resolve-ManifestInformation -ApplicationQuery 'Ash258/pwsh@6.1.3'
Resolve-ManifestInformation -ApplicationQuery '.\bucket\old\cosi\7.1.0.yaml'
Resolve-ManifestInformation -ApplicationQuery '.\cosi.yaml'
Resolve-ManifestInformation -ApplicationQuery 'https://raw.githubusercontent.com/Ash258/GithubActionsBucketForTesting/main/bucket/alfa.yaml'
Resolve-ManifestInformation -ApplicationQuery 'https://raw.githubusercontent.com/Ash258/GithubActionsBucketForTesting/main/bucket/old/alfa/0.0.15-12060.yaml'
#>
[CmdletBinding()]
[OutputType([PSCustomObject])]
param([Parameter(Mandatory, ValueFromPipeline)] [String] $ApplicationQuery)

process {
$manifest = $applicationName = $applicationVersion = $bucket = $localPath = $url = $calcBucket = $calcURL = $null

if (Test-Path -LiteralPath $ApplicationQuery) {
$res = Get-LocalManifest -Query $ApplicationQuery
$applicationName = $res.Name
$applicationVersion = $res.Manifest.version
$manifest = $res.Manifest
$localPath = $res.Path
} else {
throw 'Not supported way how to provide manifest'
}

debug $res

# TODO: Validate manifest object
if ($null -eq $manifest.version) {
debug $manifest
throw [ScoopException] 'Not a valid manifest' # TerminatingError thrown
}

return [Ordered] @{
'ApplicationName' = $applicationName
'Version' = $applicationVersion
'Bucket' = $bucket
'ManifestObject' = $manifest
'Url' = $url
'LocalPath' = $localPath
'CalculatedUrl' = $calcURL
'CalculatedBucket' = $calcBucket
}
}
}

function parse_json {
param([Parameter(Mandatory, ValueFromPipeline)] [System.IO.FileInfo] $Path)

Expand Down
63 changes: 62 additions & 1 deletion test/Shovel-Manifest.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
. "$PSScriptRoot\..\lib\core.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1"

Describe 'Resolve-ManifestInformation' -Tag 'Scoop' {
Describe 'Manifests operations' -Tag 'Scoop' {
BeforeAll {
$working_dir = (setup_working 'manifest' | Resolve-Path).Path
Move-Item "$working_dir\*.*" "$working_dir\bucket"
Expand Down Expand Up @@ -75,4 +75,65 @@ Describe 'Resolve-ManifestInformation' -Tag 'Scoop' {
{ New-VersionedManifest -LiteralPat $path -Version '22222' 6>$null } | Should -Throw "Invalid manifest '$path'"
$path = $null
}

Describe 'Resolve-ManifestInformation' {
Describe 'Get-LocalManifest' {
It 'Full path' {
$result = Resolve-ManifestInformation "$working_dir\bucket\pwsh.json"
$result.ApplicationName | Should -Be 'pwsh'
$result.Version | Should -Be '7.1.3'
$result.ManifestObject.checkver | Should -Be 'github'
$result.LocalPath | Should -Be "$working_dir\bucket\pwsh.json"
$result = $null

# Mix of dividers
$result = Resolve-ManifestInformation "$working_dir\bucket/cosi.yaml"
$result.ApplicationName | Should -Be 'cosi'
$result.Version | Should -Be '7.1.3'
$result.ManifestObject.checkver | Should -Be 'github'
$result.LocalPath | Should -Be "$working_dir\bucket\cosi.yaml"
$result = $null
}

It 'Full versioned path' {
$result = Resolve-ManifestInformation "$working_dir\bucket\old\pwsh\6.2.3.yml"
$result.ApplicationName | Should -Be 'pwsh'
$result.Version | Should -Be '6.2.3'
$result.ManifestObject.bin | Should -Be 'pwsh.exe'
$result.LocalPath | Should -Be "$working_dir\bucket\old\pwsh\6.2.3.yml"
$result = $null

$result = Resolve-ManifestInformation "$working_dir\bucket\old\cosi\7.1.0.yaml"
$result.ApplicationName | Should -Be 'cosi'
$result.Version | Should -Be '7.1.0'
$result.ManifestObject.bin | Should -Be 'pwsh.exe'
$result.LocalPath | Should -Be "$working_dir\bucket\old\cosi\7.1.0.yaml"
$result = $null

$result = Resolve-ManifestInformation "$working_dir\bucket/old\cosi/7.1.0.yaml"
$result.ApplicationName | Should -Be 'cosi'
$result.Version | Should -Be '7.1.0'
$result.ManifestObject.bin | Should -Be 'pwsh.exe'
$result.LocalPath | Should -Be "$working_dir\bucket\old\cosi\7.1.0.yaml"
$result = $null

$result = Resolve-ManifestInformation "$($working_dir -replace '\\', '/')/bucket/old/cosi/7.1.0.yaml"
$result.ApplicationName | Should -Be 'cosi'
$result.Version | Should -Be '7.1.0'
$result.ManifestObject.bin | Should -Be 'pwsh.exe'
$result = $null
}

It 'Error handling' {
{ manifest_path 'invalid_wget' 'main' | Resolve-ManifestInformation } | Should -Throw 'Not a valid manifest'
( { manifest_path 'broken_wget' 'main' | Resolve-ManifestInformation } | Should -Throw -Passthru).Exception.Message | Should -BeLike 'File is not a valid manifest*'
}
}

It 'Not supported query' {
{ Resolve-ManifestInformation '@@cosi@@' } | Should -Throw 'Not supported way how to provide manifest'
{ Resolve-ManifestInformation '@1.2.5.8' } | Should -Throw 'Not supported way how to provide manifest'
{ Resolve-ManifestInformation 'ftp://test.json' } | Should -Throw 'Not supported way how to provide manifest'
}
}
}