Skip to content

[Issue]: Refactor extension scripts to use CIHelpers module #350

@WilliamBerryiii

Description

@WilliamBerryiii

Summary

Refactor Package-Extension.ps1 and Prepare-Extension.ps1 to use the shared CIHelpers.psm1 module, removing 8 inline CI platform detection and output patterns across both scripts.

Prerequisites

Problem

The VS Code extension packaging scripts contain inline CI patterns that duplicate functionality now available in CIHelpers.psm1.

Package-Extension.ps1 (6 patterns)

# Output variables (lines ~555-561)
if ($env:GITHUB_OUTPUT) {
    "version=$packageVersion" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8
    "vsix-file=$($vsixFile.Name)" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8
}

# Environment detection
if ($env:GITHUB_ACTIONS -eq 'true') { ... }
if ($env:TF_BUILD -eq 'True') { ... }

# Error annotations in catch blocks
Write-Output "::error::$($_.Exception.Message)"

Prepare-Extension.ps1 (2 patterns)

# Error annotation in catch block
catch {
    if ($env:GITHUB_ACTIONS -eq 'true') {
        Write-Output "::error::$($_.Exception.Message)"
    }
    throw
}

Pattern inventory (8 total):

Script Pattern Type Count CIHelpers Function
Package-Extension.ps1 Environment detection 2 Test-CIEnvironment, Get-CIPlatform
Package-Extension.ps1 Output variables 2 Set-CIOutput
Package-Extension.ps1 Annotations 2 Write-CIAnnotation
Prepare-Extension.ps1 Annotations 2 Write-CIAnnotation

Solution

Step 1: Add module imports

Add after the param() block in both scripts:

# Import shared CI helpers module
$ciHelpersPath = Join-Path $PSScriptRoot "../lib/Modules/CIHelpers.psm1"
Import-Module $ciHelpersPath -Force

Step 2: Refactor Package-Extension.ps1

Output variables:

# Before
if ($env:GITHUB_OUTPUT) {
    "version=$packageVersion" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8
    "vsix-file=$($vsixFile.Name)" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8
}

# After
Set-CIOutput -Name 'version' -Value $packageVersion
Set-CIOutput -Name 'vsix-file' -Value $vsixFile.Name

Catch blocks:

# Before
catch {
    if ($env:GITHUB_ACTIONS -eq 'true') {
        Write-Output "::error::$($_.Exception.Message)"
    }
    elseif ($env:TF_BUILD -eq 'True') {
        Write-Output "##vso[task.logissue type=error]$($_.Exception.Message)"
    }
    throw
}

# After
catch {
    Write-CIAnnotation -Type error -Message $_.Exception.Message
    throw
}

Step 3: Refactor Prepare-Extension.ps1

# Before
catch {
    if ($env:GITHUB_ACTIONS -eq 'true') {
        Write-Output "::error::$($_.Exception.Message)"
    }
    throw
}

# After
catch {
    Write-CIAnnotation -Type error -Message $_.Exception.Message
    throw
}

Testing Requirements

Update Package-Extension.Tests.ps1

BeforeAll {
    # Mock CIHelpers module
    Mock -ModuleName Package-Extension Get-CIPlatform { return 'Local' }
    Mock -ModuleName Package-Extension Test-CIEnvironment { return $false }
    Mock -ModuleName Package-Extension Set-CIOutput { }
    Mock -ModuleName Package-Extension Write-CIAnnotation { }
}

Describe 'CI Integration' {
    Context 'GitHub Actions environment' {
        BeforeEach {
            Mock Get-CIPlatform { return 'GitHub' }
            Mock Test-CIEnvironment { return $true }
        }

        It 'Sets version output variable' {
            # Run packaging
            Should -Invoke Set-CIOutput -ParameterFilter { $Name -eq 'version' }
        }

        It 'Sets vsix-file output variable' {
            # Run packaging
            Should -Invoke Set-CIOutput -ParameterFilter { $Name -eq 'vsix-file' }
        }
    }

    Context 'Error handling' {
        It 'Writes error annotation on failure' {
            # Trigger error condition
            Should -Invoke Write-CIAnnotation -ParameterFilter { $Type -eq 'error' }
        }
    }
}

Update Prepare-Extension.Tests.ps1

BeforeAll {
    Mock -ModuleName Prepare-Extension Write-CIAnnotation { }
}

Describe 'Error handling' {
    It 'Writes CI annotation on preparation failure' {
        # Trigger error
        Should -Invoke Write-CIAnnotation -ParameterFilter { $Type -eq 'error' }
    }
}

Coverage targets

Script Target Coverage
Package-Extension.ps1 80%+
Prepare-Extension.ps1 80%+

Run validation

# Run tests with coverage
Invoke-Pester -Path scripts/tests/extension/ -Output Detailed -CodeCoverage @(
    'scripts/extension/Package-Extension.ps1',
    'scripts/extension/Prepare-Extension.ps1'
)

# Verify no inline CI patterns remain
$patterns = '::error|::warning|##vso\[|GITHUB_OUTPUT|GITHUB_ACTIONS|TF_BUILD'
Select-String -Path scripts/extension/*.ps1 -Pattern $patterns | Should -BeNullOrEmpty

Acceptance Criteria

  • Both scripts import CIHelpers.psm1 module
  • All 8 inline CI patterns replaced with CIHelpers function calls
  • No $env:GITHUB_OUTPUT, $env:GITHUB_ACTIONS, $env:TF_BUILD checks remain
  • No ::error::, ##vso[ string patterns remain
  • Package-Extension.Tests.ps1 updated with CI integration tests
  • Prepare-Extension.Tests.ps1 updated with error annotation tests
  • Test coverage reaches 80%+ for both scripts
  • Extension packaging workflow remains functional
  • PSScriptAnalyzer passes with no errors

Files Changed

  • scripts/extension/Package-Extension.ps1
  • scripts/extension/Prepare-Extension.ps1
  • scripts/tests/extension/Package-Extension.Tests.ps1
  • scripts/tests/extension/Prepare-Extension.Tests.ps1

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions