microsoft/hve-core

Public

mirrored fromhttps://github.com/microsoft/hve-coreAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/add-second-skill-package

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

scripts/tests/security/Invoke-PipAudit.Tests.ps1

162lines · modecode

1#Requires -Modules Pester
2# Copyright (c) Microsoft Corporation.
3# SPDX-License-Identifier: MIT
4
5BeforeAll {
6 # Stub external tools when not installed so Pester can mock them
7 if (-not (Get-Command uv -ErrorAction SilentlyContinue)) { function global:uv { } }
8 if (-not (Get-Command uvx -ErrorAction SilentlyContinue)) { function global:uvx { } }
9
10 . $PSScriptRoot/../../security/Invoke-PipAudit.ps1
11 Import-Module (Join-Path $PSScriptRoot '../../lib/Modules/CIHelpers.psm1') -Force
12
13 $mockPath = Join-Path $PSScriptRoot '../Mocks/GitMocks.psm1'
14 Import-Module $mockPath -Force
15
16 # CI helper mocks — suppress console output and enable assertions
17 Mock Write-Host {}
18 Mock Write-CIAnnotation {}
19 Mock Write-CIStepSummary {}
20}
21
22Describe 'Find-PythonProjects' -Tag 'Unit' {
23 Context 'Project discovery' {
24 It 'Finds Python projects with pyproject.toml' {
25 $testDir = Join-Path $TestDrive 'projects'
26 New-Item -ItemType Directory -Path "$testDir/skill-a" -Force | Out-Null
27 New-Item -ItemType File -Path "$testDir/skill-a/pyproject.toml" -Force | Out-Null
28
29 $projects = @(Find-PythonProjects -SearchPath $testDir)
30
31 $projects.Count | Should -Be 1
32 $projects[0] | Should -BeLike "*skill-a*"
33 }
34
35 It 'Excludes node_modules directories' {
36 $testDir = Join-Path $TestDrive 'nm-test'
37 New-Item -ItemType Directory -Path "$testDir/node_modules/pkg" -Force | Out-Null
38 New-Item -ItemType File -Path "$testDir/node_modules/pkg/pyproject.toml" -Force | Out-Null
39
40 $projects = Find-PythonProjects -SearchPath $testDir
41
42 $projects.Count | Should -Be 0
43 }
44
45 It 'Returns empty when no projects found' {
46 $testDir = Join-Path $TestDrive 'empty'
47 New-Item -ItemType Directory -Path $testDir -Force | Out-Null
48
49 $projects = Find-PythonProjects -SearchPath $testDir
50
51 $projects.Count | Should -Be 0
52 }
53
54 It 'Excludes paths matching exclude patterns' {
55 $testDir = Join-Path $TestDrive 'exclude-test'
56 New-Item -ItemType Directory -Path "$testDir/include-me" -Force | Out-Null
57 New-Item -ItemType File -Path "$testDir/include-me/pyproject.toml" -Force | Out-Null
58 New-Item -ItemType Directory -Path "$testDir/skip-me" -Force | Out-Null
59 New-Item -ItemType File -Path "$testDir/skip-me/pyproject.toml" -Force | Out-Null
60
61 $projects = @(Find-PythonProjects -SearchPath $testDir -Exclude @('skip-me'))
62
63 $projects.Count | Should -Be 1
64 $projects[0] | Should -BeLike "*include-me*"
65 }
66
67 It 'Finds multiple projects sorted' {
68 $testDir = Join-Path $TestDrive 'multi'
69 New-Item -ItemType Directory -Path "$testDir/z-skill" -Force | Out-Null
70 New-Item -ItemType File -Path "$testDir/z-skill/pyproject.toml" -Force | Out-Null
71 New-Item -ItemType Directory -Path "$testDir/a-skill" -Force | Out-Null
72 New-Item -ItemType File -Path "$testDir/a-skill/pyproject.toml" -Force | Out-Null
73
74 $projects = Find-PythonProjects -SearchPath $testDir
75
76 $projects.Count | Should -Be 2
77 $projects[0] | Should -BeLike "*a-skill*"
78 $projects[1] | Should -BeLike "*z-skill*"
79 }
80 }
81}
82
83Describe 'Invoke-PipAuditForProject' -Tag 'Unit' {
84 Context 'Audit execution' {
85 It 'Runs uv export and pip-audit for a project' {
86 $testDir = Join-Path $TestDrive 'audit-test'
87 $outputDir = Join-Path $TestDrive 'audit-output'
88 New-Item -ItemType Directory -Path $testDir -Force | Out-Null
89 New-Item -ItemType Directory -Path $outputDir -Force | Out-Null
90
91 Mock uv {}
92 Mock uvx {}
93 $global:LASTEXITCODE = 0
94
95 $result = Invoke-PipAuditForProject -ProjectPath $testDir -OutputPath $outputDir
96
97 $result | Should -Be $false
98 Should -Invoke uv -Times 1
99 Should -Invoke uvx -Times 1
100 }
101
102 It 'Returns true when vulnerabilities are found' {
103 $testDir = Join-Path $TestDrive 'vuln-test'
104 $outputDir = Join-Path $TestDrive 'vuln-output'
105 New-Item -ItemType Directory -Path $testDir -Force | Out-Null
106 New-Item -ItemType Directory -Path $outputDir -Force | Out-Null
107
108 Mock uv {}
109 Mock uvx { $global:LASTEXITCODE = 1 }
110
111 $result = Invoke-PipAuditForProject -ProjectPath $testDir -OutputPath $outputDir
112
113 $result | Should -Be $true
114 }
115 }
116}
117
118Describe 'Start-PipAudit' -Tag 'Unit' {
119 Context 'Orchestration' {
120 It 'Returns 0 when no projects found' {
121 Mock Find-PythonProjects { @() }
122
123 $result = Start-PipAudit -SearchPath $TestDrive -OutputPath (Join-Path $TestDrive 'out-none')
124
125 $result | Should -Be 0
126 Should -Invoke Write-Host -ParameterFilter { $Object -eq 'No Python projects found' }
127 }
128
129 It 'Audits all discovered projects and returns 0 when clean' {
130 $outputDir = Join-Path $TestDrive 'out-clean'
131 Mock Find-PythonProjects { @("$TestDrive/proj-a", "$TestDrive/proj-b") }
132 Mock Invoke-PipAuditForProject { $false }
133
134 $result = Start-PipAudit -SearchPath $TestDrive -OutputPath $outputDir
135
136 $result | Should -Be 0
137 Should -Invoke Invoke-PipAuditForProject -Times 2 -Exactly
138 }
139
140 It 'Returns 1 when vulnerabilities found and FailOnVulnerability set' {
141 $outputDir = Join-Path $TestDrive 'out-vuln-fail'
142 Mock Find-PythonProjects { @("$TestDrive/proj-a") }
143 Mock Invoke-PipAuditForProject { $true }
144
145 $result = Start-PipAudit -SearchPath $TestDrive -OutputPath $outputDir -FailOnVulnerability
146
147 $result | Should -Be 1
148 Should -Invoke Write-Host -ParameterFilter { $Object -like '::error::*' }
149 }
150
151 It 'Returns 0 when vulnerabilities found without FailOnVulnerability' {
152 $outputDir = Join-Path $TestDrive 'out-vuln-nofail'
153 Mock Find-PythonProjects { @("$TestDrive/proj-a") }
154 Mock Invoke-PipAuditForProject { $true }
155
156 $result = Start-PipAudit -SearchPath $TestDrive -OutputPath $outputDir
157
158 $result | Should -Be 0
159 Should -Not -Invoke Write-Host -ParameterFilter { $Object -like '::error::*' }
160 }
161 }
162}
163