Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 8aafc84

Browse files
authored
Add CI as github action. (#47)
* Add CI as github action. Required small adjustments in build.ps1. Also created a cron job for weekly builds. * fix name of upload test results. * Add skip publisher check to pester install. * Change tests to not use PS7 commands for path combination. Also mock PSStyle variable. * Drop support for Windows PowerShell. It's not possible with the use of System.Text.Json. Add another test result file to gitignore. * Remove mock of PSStyle variable.
1 parent 8d57878 commit 8aafc84

File tree

6 files changed

+104
-25
lines changed

6 files changed

+104
-25
lines changed

.github/workflows/ci-test.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: TextUtility CI Tests
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
ci:
11+
name: Build and Test
12+
strategy:
13+
matrix:
14+
os: [ windows-latest, macos-latest, ubuntu-latest ]
15+
runs-on: ${{ matrix.os }}
16+
env:
17+
DOTNET_NOLOGO: true
18+
DOTNET_GENERATE_ASPNET_CERTIFICATE: false
19+
steps:
20+
- name: Checkout repository
21+
uses: actions/checkout@v4
22+
23+
- name: Install dotnet
24+
uses: actions/setup-dotnet@v4
25+
with:
26+
cache: true
27+
cache-dependency-path: '**/*.csproj'
28+
29+
- name: Install PSResources
30+
run: ./build.ps1 -Bootstrap
31+
shell: pwsh
32+
33+
- name: Build
34+
run: ./build.ps1 -Configuration Release
35+
shell: pwsh
36+
37+
- name: Test
38+
run: ./build.ps1 -Test -NoBuild
39+
shell: pwsh
40+
41+
- name: Upload test results
42+
uses: actions/upload-artifact@v4
43+
if: always()
44+
with:
45+
name: TextUtility-tests-${{ matrix.os }}
46+
path: testResults.xml

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ out/
66
project.lock.json
77
.DS_Store
88
Microsoft.PowerShell.TextUtility.xml
9+
testResults.xml
910
# VSCode directories that are not at the repository root
1011
/**/.vscode/

.pipelines/TextUtility-Official.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ name: TextUtility-ModuleBuild-$(Build.BuildId)
22
trigger: none
33
pr: none
44

5+
schedules:
6+
- cron: '0 3 * * 1'
7+
displayName: Weekly Build
8+
branches:
9+
include:
10+
- master
11+
always: true
12+
13+
parameters:
14+
- name: 'publishToPowerShellGallery'
15+
displayName: 'Publish module to PowerShell gallery'
16+
type: boolean
17+
default: false
18+
519
variables:
620
BuildConfiguration: Release
721
DOTNET_NOLOGO: true
@@ -106,6 +120,7 @@ extends:
106120
files_to_sign: "**/*.nupkg"
107121
- stage: release
108122
dependsOn: build
123+
condition: ${{ parameters.publishToPowerShellGallery }}
109124
variables:
110125
version: $[ stageDependencies.build.main.outputs['package.version'] ]
111126
drop: $(Pipeline.Workspace)/drop_build_main

build.ps1

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ param (
3030

3131
[Parameter()]
3232
[switch]
33-
$GetPackageVersion
33+
$GetPackageVersion,
34+
35+
[Parameter(ParameterSetName="bootstrap")]
36+
[switch]
37+
$Bootstrap
3438

3539
)
3640

@@ -89,29 +93,55 @@ function Export-Module
8993
function Test-Module {
9094
try {
9195
$PSVersionTable | Out-String -Stream | Write-Verbose -Verbose
92-
$pesterInstallations = Get-Module -ListAvailable -Name Pester
93-
if ($pesterInstallations.Version -notcontains "4.10.1") {
94-
Install-Module -Name Pester -RequiredVersion 4.10.1 -Force -Scope CurrentUser
95-
}
9696
$importTarget = "Import-Module ${PSScriptRoot}/out/${ModuleName}"
9797
$importPester = "Import-Module Pester -Max 4.10.1"
98-
$invokePester = "Invoke-Pester -OutputFormat NUnitXml -EnableExit -OutputFile ../Microsoft.PowerShell.TextUtility.xml"
99-
$command = "${importTarget}; ${importPester}; ${invokePester}"
98+
$invokePester = "Invoke-Pester -OutputFormat NUnitXml -EnableExit -OutputFile ../testResults.xml"
99+
$sb = [scriptblock]::Create("${importTarget}; ${importPester}; ${invokePester}")
100100
Push-Location $testRoot
101-
pwsh -noprofile -command $command
101+
# calculate the shell to run rather than hardcoding it.
102+
$PSEXE = (Get-Process -id $PID).MainModule.FileName
103+
& $PSEXE -noprofile -command $sb
102104
}
103105
finally {
104106
Pop-Location
105107
}
106108
}
107109

110+
function Invoke-Bootstrap
111+
{
112+
$neededPesterModule = Get-Module -Name Pester -ListAvailable | Where-Object { $_.Version -eq "4.10.1" }
113+
$neededPesterVersion = [version]"4.10.1"
114+
if ($neededPesterModule.Version -eq $neededPesterVersion)
115+
{
116+
Write-Verbose -Verbose -Message "Required pester version $neededPesterVersion is available."
117+
return
118+
}
119+
120+
Write-Verbose -Verbose -Message "Attempting install of Pester version ${neededPesterVersion}."
121+
Install-Module -Name Pester -Scope CurrentUser -RequiredVersion 4.10.1 -Force -SkipPublisherCheck
122+
$neededPesterModule = Get-Module -Name Pester -ListAvailable | Where-Object { $_.Version -eq $neededPesterVersion }
123+
if ($neededPesterModule.Version -ne $neededPesterVersion)
124+
{
125+
throw "Pester install failed"
126+
}
127+
128+
Write-Verbose -Verbose -Message "Pester version $neededPesterVersion installed."
129+
return
130+
}
131+
132+
108133
try {
109134
Push-Location "$PSScriptRoot/src/code"
110135
$script:moduleInfo = Get-ModuleInfo
111136
if ($GetPackageVersion) {
112137
return $moduleInfo.ModuleVersion
113138
}
114139

140+
if ($Bootstrap) {
141+
Invoke-Bootstrap
142+
return
143+
}
144+
115145
$outPath = "$PSScriptRoot/out/${moduleName}"
116146
if ($Clean) {
117147
if (Test-Path $outPath) {
@@ -132,19 +162,6 @@ try {
132162

133163
if ($Test) {
134164
Test-Module
135-
136-
<#
137-
$script = [ScriptBlock]::Create("try {
138-
Import-Module '${repoRoot}/out/${moduleName}/'
139-
Import-Module -Name Pester -Max 4.99
140-
Push-Location '${repoRoot}/test'
141-
Invoke-Pester
142-
}
143-
finally {
144-
Pop-Location
145-
}")
146-
pwsh -c $script
147-
#>
148165
return
149166
}
150167

src/Microsoft.PowerShell.TextUtility.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
CompanyName = 'Microsoft Corporation'
1111
Copyright = '(c) Microsoft Corporation. All rights reserved.'
1212
Description = "This module contains cmdlets to help with manipulating or reading text."
13-
PowerShellVersion = '5.1'
13+
PowerShellVersion = '7.0'
1414
FormatsToProcess = @('Microsoft.PowerShell.TextUtility.format.ps1xml')
1515
CmdletsToExport = @(
1616
'Compare-Text','ConvertFrom-Base64','ConvertTo-Base64',"ConvertFrom-TextTable"

test/TextTableParser.Tests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ Describe "Test text table parser" {
138138

139139
It "Should create proper json from '<FileName>' " -testCases $testCases {
140140
param ($FileName, $convertArgs, $rows, $Results)
141-
$Path = Join-Path $PSScriptRoot assets $FileName
141+
$Path = [io.path]::Combine($PSScriptRoot, "assets", $FileName)
142142
# do not alter convertArgs directly as it is a reference rather than a copy
143143
$localArgs = $convertArgs.Clone()
144144
$localArgs['AsJson'] = $true
@@ -158,7 +158,7 @@ Describe "Test text table parser" {
158158
Context "Test PSObject output" {
159159
It "Should create proper psobject from '<FileName>' " -testCases $testCases {
160160
param ($FileName, $convertArgs, $rows, $Results )
161-
$Path = Join-Path $PSScriptRoot assets $FileName
161+
$Path = [io.path]::Combine($PSScriptRoot, "assets", $FileName)
162162
$result = Get-Content $Path | ConvertFrom-TextTable @convertArgs
163163
$result | Should -BeOfType System.Management.Automation.PSObject
164164
$result.Count | Should -Be $Rows
@@ -180,7 +180,7 @@ Describe "Test text table parser" {
180180
@{ Name = "Property_04"; Value = "34567890123456789" }
181181
@{ Name = "Property_05"; Value = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" }
182182
)
183-
$testFile = Join-Path $PSScriptRoot assets "columns.01.txt"
183+
$testFile = [io.path]::Combine($PSScriptRoot, "assets", "columns.01.txt")
184184
$result = Get-Content $testFile | ConvertFrom-TextTable -ColumnOffset 0,5,13,23,40 -noheader
185185
$line = 0
186186
$testCases = $result.ForEach({@{Result = $_; Line = $line++}})

0 commit comments

Comments
 (0)