概要
たまに使う機会があるpowershell。
再利用できそうな処理はテストを書きたい
毎回ローカルのPCでテストを実行するのは面倒なので、github actionで自動テストを実行するように設定します。
開発環境
ソフトウェア名 | バージョン | 説明 |
---|---|---|
Windows | 10 | OS |
Powershell | 5.1.19041 | スクリプト言語およびキャラクターユーザーインターフェイス |
VSCode | 1.99.3 | IDE |
powershell | 2025.0.0 | VsCodeエクステンション Microsoft製 |
Pester | --- | Powershellのテストフレームワーク |
コード
GithubAction
.github/workflows/pester_ci.yml
name: CI
on:
push:
branches:
- main
pull_request:
jobs:
test:
runs-on: windows-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install Pester
run: |
Install-Module -Name Pester -Force -SkipPublisherCheck
- name: Run Pester Tests
run: |
Invoke-Pester .\Service\
プロダクトコード
# パソコンの情報を取得するクラス
#
class machineInfoService{
[String] $cpuName
[String] $modelName
[String] getMemorySize(){
Return Get-WmiObject -Class Win32_PhysicalMemory | %{ $_.Capacity} | Measure-Object -Sum | %{ ($_.sum /1024/1024/1024).toString()}
}
[String] getModelName(){
return (Get-WmiObject Win32_ComputerSystemProduct).name
}
[String] getCpuName(){
return @((Get-WmiObject Win32_Processor).Name)[0]
}
[String] getSerialNumber(){
return (Get-WmiObject Win32_BIOS).SerialNumber;
}
[String] getOS(){
$OS = (Get-WmiObject Win32_OperatingSystem).Caption
$SP = (Get-WmiObject Win32_OperatingSystem).ServicePackMajorVersion
if( $SP -ne 0 ){ $OS += "SP" + $SP }
return $OS
}
[String] getHostName(){
return hostname;
}
}
テストコード
using module ".\MachineInfoService.psm1"
Import-Module Pester
# 起動方法
# Invoke-Pester .\Service\MachineInfoServiceTest.Tests.ps1
Describe "MachineInfoService" {
Context "Get-MachineInfo" {
It "Serial番号取得のテスト" {
$result = [MachineInfoService]::New().getSerialNumber();
[System.Console]::WriteLine("TEST01:SERIALNumber is"+$result)
$result | Should -Not -BeNullOrEmpty
}
}
}
実行結果
SerialNumberに値が入っていることを確認できます。
参考
github コミット分