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

Fix package pattern in json schema configuration registry #1429

Conversation

WangWeiLin-MV
Copy link
Contributor

@WangWeiLin-MV WangWeiLin-MV commented Jun 13, 2024

Fix microsoft/vcpkg/issues/39259 for package pattern with an optional trailing *.

Change

  • Add definitions/reserved-name for reuse
    • Vcpkg reserved name by ^(core|default)$
    • Win32 filesystem reserved name by a loose pattern ^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\\.[^.]+)*$, which include COM0 and LPT0. See documents
  • Add definitions/package-pattern to fix optional trailing *
  • Replace definitions/port-name with definitions/identifier
    • Pattern of port-name matches 'dot-separated identifier'. No relevant documents found, remove this definition.
    • Pattern of identifier matches 'lowercase with digits and dashes'
  • Append # to $schema
  • Add more unit tests
  • Add platform independent json schema e2e test step in pr.yaml

Test

Schema vcpkg.schema.json validated all ports vcpkg.json with json-schema-tests-dir/vcpkg-ports-json.test.ps1.

Schema vcpkg-configuration.schema.json tested pass with json-schema-tests-dir/json-schema-bvt.test.ps1

@WangWeiLin-MV WangWeiLin-MV force-pushed the docs/json-schema/configuration-registry-package-pattern branch from 498a54d to c9090c1 Compare June 13, 2024 10:46
@BillyONeal
Copy link
Member

Format failures fixed in #1433

@WangWeiLin-MV WangWeiLin-MV force-pushed the docs/json-schema/configuration-registry-package-pattern branch from 64800ac to e03dc04 Compare June 14, 2024 08:14
@WangWeiLin-MV
Copy link
Contributor Author

WangWeiLin-MV commented Jun 14, 2024

Schema vcpkg-configuration.schema.json tested cases pass in PowerShell 7.4.2

param(
    [string]$VcpkgSrcDir = (Join-Path $PSScriptRoot '../..' | Resolve-Path),
    [string]$TempWorkDir = (New-Item -ItemType Directory Temp:/$(New-Guid)).FullName
)
$ErrorActionPreference = 'Stop'
# . $PSScriptRoot/../end-to-end-tests-prelude.ps1

$VcpkgJsonSchema = @{
    Artifact      = 'artifact.schema.json'
    Configuration = 'vcpkg-configuration.schema.json'
    Definitions   = 'vcpkg-schema-definitions.schema.json'
    Port          = 'vcpkg.schema.json'
}

# remove `$id` in schema for error 'Test-Json: Cannot parse the JSON schema.'
$VcpkgJsonSchema.Values
| ForEach-Object {
    Copy-Item -Path (Join-path $VcpkgSrcDir 'docs' $_) -Destination $TempWorkDir
    Join-Path $TempWorkDir $_
}
| ForEach-Object {
    (Get-Content -Raw -Path $_) -replace '(?s)\n  "\$id".+?\n', "`n"
    | Set-Content -NoNewline -Path $_
}
| Out-Null

[scriptblock]$_Gen__ = {
    param(
        [Parameter(Mandatory)][bool]$Expected,
        [Parameter(Mandatory)][string[]]$PackageArray,
        [string]$Baseline = '0' * 40
    )
    return @(
        $Expected,
        @{
            registries = @(
                [pscustomobject]@{
                    kind       = 'git'
                    repository = ''
                    baseline   = $Baseline
                    packages   = $PackageArray
                }
            )
        }
    )
}

# See src/vcpkg-test/registries.cpp "check valid package patterns"
@{
    $VcpkgJsonSchema.Configuration =
    @{
        'packages: ["a"]'                 = & $_Gen__ $true  @('a')
        'packages: empty'                 = & $_Gen__ $false [string[]]@('')
        'packages: blank'                 = & $_Gen__ $false @(' ')
        'packages: baseline 39'           = & $_Gen__ $false @('a')       ('0' * 39)
        'packages: hashtag ["*"]'         = & $_Gen__ $true  @('*')
        'packages: hashtag ["a*"]'        = & $_Gen__ $true  @('a*')
        'packages: hashtag ["*a"]'        = & $_Gen__ $false @('*a')
        'packages: hashtag ["b-*"]'       = & $_Gen__ $true  @('b-*')
        'packages: hashtag ["c-d-*"]'     = & $_Gen__ $true  @('c-d-*')
        'packages: hashtag dup ["a**"]'   = & $_Gen__ $false @('a**')
        'packages: hashtag dup ["b-**"]'  = & $_Gen__ $false @('b-**')
        'packages: hashtag dup ["c--*"]'  = & $_Gen__ $false @('c--*')
        'packages: hashtag dup ["d-*-*"]' = & $_Gen__ $false @('d-*-*')
        'packages: hashtag mid ["a*b"]'   = & $_Gen__ $false @('a*b')
        'packages: mix array ["a*","b"]'  = & $_Gen__ $true  @('a*', 'b')
        'packages: symbols ["a+"]'        = & $_Gen__ $false @('a+')
        'packages: symbols ["a?"]'        = & $_Gen__ $false @('a?')
    }
    $VcpkgJsonSchema.Port          =
    @{
        # test identifiers
        'port-name: "co"'                 = $true, @{name = 'co' }
        'port-name: "rapidjson"'          = $true, @{name = 'rapidjson' }
        'port-name: "boost-tuple"'        = $true, @{name = 'boost-tuple' }
        'port-name: "vcpkg-boost-helper"' = $true, @{name = 'vcpkg-boost-helper' }
        'port-name: "lpt"'                = $true, @{name = 'lpt' }
        'port-name: "com"'                = $true, @{name = 'com' }
        # reject invalid characters
        'port-name: ""'                   = $false, @{name = '' }
        'port-name: " "'                  = $false, @{name = ' ' }
        'port-name: "boost_tuple"'        = $false, @{name = 'boost_tuple' }
        'port-name: "boost.'              = $false, @{name = 'boost.' }
        'port-name: "boost.tuple"'        = $false, @{name = 'boost.tuple' }
        'port-name: "boost@1"'            = $false, @{name = 'boost@1' }
        'port-name: "boost#1"'            = $false, @{name = 'boost#1' }
        'port-name: "boost:x64-windows"'  = $false, @{name = 'boost:x64-windows' }
        # accept legacy
        'port-name: "all_modules"'        = $false, @{name = 'all_modules' } # removed in json-schema
        # reject reserved keywords
        'port-name: "prn"'                = $false, @{name = 'prn' }
        'port-name: "aux"'                = $false, @{name = 'aux' }
        'port-name: "nul"'                = $false, @{name = 'nul' }
        'port-name: "con"'                = $false, @{name = 'con' }
        'port-name: "core"'               = $false, @{name = 'core' }
        'port-name: "default"'            = $false, @{name = 'default' }
        'port-name: "lpt0"'               = $false, @{name = 'lpt0' }
        'port-name: "lpt9"'               = $false, @{name = 'lpt9' }
        'port-name: "com0"'               = $false, @{name = 'com0' }
        'port-name: "com9"'               = $false, @{name = 'com9' }
        # reject incomplete segments
        'port-name: "-a"'                 = $false, @{name = '-a' }
        'port-name: "a-"'                 = $false, @{name = 'a-' }
        'port-name: "a--"'                = $false, @{name = 'a--' }
        'port-name: "---"'                = $false, @{name = '---' }
    }
}.GetEnumerator()
| ForEach-Object {
    @{
        SchemaName = $_.Key
        JsonCases  = $_.Value.GetEnumerator() | ForEach-Object { @{Title = $_.Key; Expected = $_.Value[0]; Json = ConvertTo-Json -InputObject $_.Value[1] -Depth 5 -Compress } }
    }
}
| ForEach-Object {
    $_SchemaName = $_.SchemaName
    $_SchemaPath = Join-Path $TempWorkDir $_SchemaName
    $_.JsonCases | ForEach-Object {
        $_Title = $_.Title
        $_Expected = $_.Expected

        $_Actual = Test-Json -ea:0 -Json $_.Json -SchemaFile $_SchemaPath
        $_Result = $_.Expected -eq $_Actual ? 'Pass':'Fail'
        if ($_Result -eq 'Fail') {
            throw "$_SchemaName validate fail with $_Title, expected $_Expected"
        }
        [pscustomobject]@{
            SchemaName = $_SchemaName
            Title      = $_Title
            Expected   = $_Expected
            Actual     = $_Actual
            Result     = $_Result
        }
    }
}
| Sort-Object SchemaName, Title
| Format-Table

Remove-Item -Recurse -Force $TempWorkDir

Schema vcpkg.schema.json validated ports vcpkg.json pass in PowerShell 7.4.2

param(
    [string]$VcpkgSrcDir = (Join-Path $PSScriptRoot '../..' | Resolve-Path),
    [string]$VcpkgRegistryDir = $env:VCPKG_ROOT,
    [string]$TempWorkDir = (New-Item -ItemType Directory Temp:/$(New-Guid)).FullName
)
$ErrorActionPreference = 'Stop'
# . $PSScriptRoot/../end-to-end-tests-prelude.ps1

$VcpkgJsonSchema = @{
    Artifact      = 'artifact.schema.json'
    Configuration = 'vcpkg-configuration.schema.json'
    Definitions   = 'vcpkg-schema-definitions.schema.json'
    Port          = 'vcpkg.schema.json'
}

# remove `$id` in schema for error 'Test-Json: Cannot parse the JSON schema.'
$VcpkgJsonSchema.Values
| ForEach-Object {
    Copy-Item -Path (Join-path $VcpkgSrcDir 'docs' $_) -Destination $TempWorkDir
    Join-Path $TempWorkDir $_
}
| ForEach-Object {
    (Get-Content -Raw -Path $_) -replace '(?s)\n  "\$id".+?\n', "`n"
    | Set-Content -NoNewline -Path $_
}
| Out-Null

$VcpkgPortSchemaPath = Join-Path $TempWorkDir $VcpkgJsonSchema.Port

Get-ChildItem -Directory -Path (Join-Path $VcpkgRegistryDir 'ports')
| ForEach-Object -Parallel {
    $PortName = $_.Name
    $PortDir = $_.FullName
    $PortJsonPath = Join-Path $PortDir 'vcpkg.json'
    $Schema = $using:VcpkgPortSchemaPath

    $_Actual = Test-Json -ea:0 -LiteralPath $PortJsonPath -SchemaFile $Schema
    [pscustomobject]@{
        PortName = $PortName
        Actual   = $_Actual
    }
}
| ForEach-Object { Write-Host $_; $_ }
| Where-Object Actual -EQ $false
| ForEach-Object { Write-Error $_; $_ }

Remove-Item -Recurse -Force $TempWorkDir

@WangWeiLin-MV WangWeiLin-MV marked this pull request as ready for review June 14, 2024 10:09
@WangWeiLin-MV WangWeiLin-MV force-pushed the docs/json-schema/configuration-registry-package-pattern branch from e03dc04 to 5b68e3c Compare June 14, 2024 10:52
@BillyONeal
Copy link
Member

Add definitions/package-pattern to fix optional trailing *

Is this actually accepted by the product code?

@BillyONeal
Copy link
Member

  • Pattern of port-name matches 'dot-separated identifier'. No relevant documents found, remove this definition.

This was done to give better intellisense behavior / descriptive text. But I think it's fine to remove it.

@WangWeiLin-MV WangWeiLin-MV marked this pull request as draft June 18, 2024 01:37
@WangWeiLin-MV WangWeiLin-MV force-pushed the docs/json-schema/configuration-registry-package-pattern branch from 5b68e3c to 3237f31 Compare June 18, 2024 06:40
@WangWeiLin-MV
Copy link
Contributor Author

match ‘boost*’ and ‘boost-*’

Is this actually accepted by the product code?

There are some places to parse the pattern:

And there are some test cases covering it:

@WangWeiLin-MV WangWeiLin-MV force-pushed the docs/json-schema/configuration-registry-package-pattern branch from 3237f31 to 374a145 Compare June 18, 2024 09:29
@WangWeiLin-MV WangWeiLin-MV force-pushed the docs/json-schema/configuration-registry-package-pattern branch 12 times, most recently from 55a31ac to 9b04d30 Compare June 24, 2024 09:00
@WangWeiLin-MV WangWeiLin-MV marked this pull request as ready for review June 24, 2024 09:14
Copy link
Member

@BillyONeal BillyONeal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This 'request changes' is only for the nonstandard tests being in end-to-end-tests-dir, all other comments in here are extreme nitpicks you can take or leave.

.github/workflows/build.yaml Outdated Show resolved Hide resolved
src/vcpkg/configuration.cpp Outdated Show resolved Hide resolved
@BillyONeal
Copy link
Member

Oh, and thanks for the fixes!

@WangWeiLin-MV WangWeiLin-MV force-pushed the docs/json-schema/configuration-registry-package-pattern branch from 9b04d30 to ea20077 Compare July 9, 2024 10:18
@WangWeiLin-MV WangWeiLin-MV force-pushed the docs/json-schema/configuration-registry-package-pattern branch from ea20077 to c143456 Compare July 9, 2024 10:27
Copy link
Member

@BillyONeal BillyONeal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

@BillyONeal BillyONeal merged commit 998c893 into microsoft:main Jul 25, 2024
6 checks passed
@WangWeiLin-MV WangWeiLin-MV deleted the docs/json-schema/configuration-registry-package-pattern branch July 26, 2024 02:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

vcpkg-configuration.json validation failure with wildcards
3 participants