Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Test/Test.psm1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Write-Information -Message ("Loading {0} ..." -f ($PSCommandPath | Split-Path -LeafBase)) -InformationAction continue
Write-Information -MessageData ("Loading {0} ..." -f ($PSCommandPath | Split-Path -LeafBase)) -InformationAction continue

#Module path is where resides the RootModule file. This file. :)
$MODULE_PATH = $PSScriptRoot
Expand Down
23 changes: 22 additions & 1 deletion Test/helper/module.helper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function Get-Ps1FullPath{
)

# If folderName is not empty
if($FolderName -ne $null){
if($null -ne $FolderName){
$folder = Get-ModuleFolder -FolderName $FolderName -ModuleRootPath $ModuleRootPath
$path = $folder | Join-Path -ChildPath $Name
} else {
Expand Down Expand Up @@ -102,6 +102,27 @@ function Get-ModuleName{
return $MODULE_NAME
}

<#
.SYNOPSIS
Gets the full path of a specific module folder.

.DESCRIPTION
Gets the full path of a specific module folder based on the folder name and optionally the module root path.

.PARAMETER FolderName
The name of the folder to get the path for. Must be one of the valid folder names.

.PARAMETER ModuleRootPath
The root path of the module. If not provided, uses the default module root path.

.EXAMPLE
Get-ModuleFolder -FolderName "Public"
Gets the path to the Public folder of the current module.

.EXAMPLE
Get-ModuleFolder -FolderName "Private" -ModuleRootPath "C:\MyModule"
Gets the path to the Private folder of the specified module.
#>
function Get-ModuleFolder{
[CmdletBinding()]
param(
Expand Down
28 changes: 27 additions & 1 deletion Test/include/invokeCommand.mock.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ function MockCall{
Set-InvokeCommandMock -Alias $command -Command "Get-MockFileContent -filename $filename"
}

function MockCallAsync{
param(
[Parameter(Position=0)][string] $command,
[Parameter(Position=1)][string] $filename
)

Assert-MockFileNotfound $fileName

$moduleTest = $PSScriptRoot | Split-Path -Parent | Convert-Path

Set-InvokeCommandMock -Alias $command -Command "Import-Module $moduleTest ; Get-MockFileContent -filename $filename"
}

function MockCallJson{
param(
[Parameter(Position=0)][string] $command,
Expand All @@ -69,6 +82,20 @@ function MockCallJson{
Set-InvokeCommandMock -Alias $command -Command "Get-MockFileContentJson -filename $filename"
}

function MockCallJsonAsync{
param(
[Parameter(Position=0)][string] $command,
[Parameter(Position=1)][string] $filename

)

Assert-MockFileNotfound $fileName

$moduleTest = $PSScriptRoot | Split-Path -Parent | Convert-Path

Set-InvokeCommandMock -Alias $command -Command "Import-Module $moduleTest ; Get-MockFileContentJson -filename $filename"
}

function Get-MockFileFullPath{
param(
[parameter(Mandatory,Position=0)][string] $fileName
Expand Down Expand Up @@ -130,7 +157,6 @@ function MockCallToObject{
Set-Variable -Name $varName -Value $OutObject -Scope Global

Set-InvokeCommandMock -Alias $command -Command "(Get-Variable -Name $varName -Scope Global).Value"

}

function MockCallToNull{
Expand Down
117 changes: 117 additions & 0 deletions Test/private/mocks/clearProjectV2ItemFieldValue.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
{
"data": {
"updateProjectV2ItemFieldValue": {
"projectV2Item": {
"content": {
"__typename": "DraftIssue",
"id": "DI_lADOBCrGTM4ActQazgFYYPQ",
"body": "",
"title": "kk text"
},
"id": "PVTI_lADOBCrGTM4ActQazgMuXXc",
"fieldValues": {
"nodes": [
{
"__typename": "ProjectV2ItemFieldUserValue",
"users": {
"nodes": [
{
"login": "rulasg"
}
]
},
"field": {
"__typename": "ProjectV2Field",
"id": "PVTF_lADOBCrGTM4ActQazgSkYnA",
"name": "Assignees",
"dataType": "ASSIGNEES"
}
},
{
"__typename": "ProjectV2ItemFieldTextValue",
"text": "kk text",
"field": {
"__typename": "ProjectV2Field",
"id": "PVTF_lADOBCrGTM4ActQazgSkYm8",
"name": "Title",
"dataType": "TITLE"
}
},
{
"__typename": "ProjectV2ItemFieldSingleSelectValue",
"name": "Todo",
"field": {
"__typename": "ProjectV2SingleSelectField",
"id": "PVTSSF_lADOBCrGTM4ActQazgSkYnE",
"name": "Status",
"dataType": "SINGLE_SELECT",
"options": [
{
"id": "f75ad846",
"name": "Todo"
},
{
"id": "47fc9ee4",
"name": "In Progress"
},
{
"id": "98236657",
"name": "Done"
}
]
}
},
{
"__typename": "ProjectV2ItemFieldSingleSelectValue",
"name": "Nice⭐️",
"field": {
"__typename": "ProjectV2SingleSelectField",
"id": "PVTSSF_lADOBCrGTM4ActQazgSl5ME",
"name": "Severity",
"dataType": "SINGLE_SELECT",
"options": [
{
"id": "2d190e20",
"name": "Critical⭐️⭐️⭐️⭐️"
},
{
"id": "94d0bf94",
"name": "Important⭐️⭐️⭐️"
},
{
"id": "ea848251",
"name": "Needed⭐️⭐️"
},
{
"id": "c99f6d55",
"name": "Nice⭐️"
}
]
}
},
{
"__typename": "ProjectV2ItemFieldNumberValue",
"number": 8.0,
"field": {
"__typename": "ProjectV2Field",
"id": "PVTF_lADOBCrGTM4ActQazgSl5WM",
"name": "UserStories",
"dataType": "NUMBER"
}
},
{
"__typename": "ProjectV2ItemFieldNumberValue",
"number": 666.0,
"field": {
"__typename": "ProjectV2Field",
"id": "PVTF_lADOBCrGTM4ActQazgSkglc",
"name": "TimeTracker",
"dataType": "NUMBER"
}
}
]
}
}
}
}
}
113 changes: 113 additions & 0 deletions Test/public/project_items_staged.test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,59 @@
Assert-AreEqual -Expected $fileTitleValue2 -Presented $item2.$fieldTitle2
}

function Test_CommitProjectItemsStaged_SUCCESS_Emptyfield{
Reset-InvokeCommandMock
Mock_DatabaseRoot

$Owner = "SomeOrg" ; $ProjectNumber = 164

# Item id 10
# Name Value
# ---- -----
# id PVTI_lADOBCrGTM4ActQazgMuXXc
# number
# Severity Nice⭐️
# Status Todo
# TimeTracker 890
# Comment This
# body some content in body
# Assignees rulasg
# UserStories 8
# Title A draft in the project
# Priority 🥵High
# url
# type DraftIssue

$itemId1 = "PVTI_lADOBCrGTM4ActQazgMuXXc"
$fieldComment1 = "Comment"
$fieldPriority1 = "Priority"

# Edit-ProjectItem will call Get-Project with SkipItems
# This test is to confirm the sync works with the project and items
# Cache the project with items
MockCall_GitHubOrgProjectWithFields -Owner $owner -ProjectNumber $projectNumber -FileName 'projectV2.json'
$null = Get-Project -Owner $Owner -ProjectNumber $ProjectNumber

MockCallJson -FileName 'updateProjectV2ItemFieldValue.json' -Command 'Invoke-GitHubClearItemValues -ProjectId PVT_kwDOBCrGTM4ActQa -ItemId PVTI_lADOBCrGTM4ActQazgMuXXc -FieldId PVTF_lADOBCrGTM4ActQazgSl5GU'
MockCallJson -FileName 'updateProjectV2ItemFieldValue.json' -Command 'Invoke-GitHubClearItemValues -ProjectId PVT_kwDOBCrGTM4ActQa -ItemId PVTI_lADOBCrGTM4ActQazgMuXXc -FieldId PVTSSF_lADOBCrGTM4ActQazgSl5LY'

Edit-ProjectItem -Owner $owner -ProjectNumber $projectNumber $itemId1 $fieldComment1 ""
Edit-ProjectItem -Owner $owner -ProjectNumber $projectNumber $itemId1 $fieldPriority1 ""

$result = Sync-ProjectItemStaged -Owner $Owner -ProjectNumber $ProjectNumber

# Return true
Assert-IsTrue -Condition $result

# Staged list is empty
$staged = Get-ProjectItemStaged -Owner $Owner -ProjectNumber $ProjectNumber
Assert-IsNull -Object $staged

$item1 = Get-ProjectItem -Owner $Owner -ProjectNumber $ProjectNumber -ItemId $itemId1
Assert-StringIsNullOrEmpty -Presented $item1.$fieldComment1
Assert-StringIsNullOrEmpty -Presented $item1.$fieldPriority1
}

function Test_CommitProjectItemsStagedAsync_SUCCESS{
Reset-InvokeCommandMock
Mock_DatabaseRoot
Expand Down Expand Up @@ -339,4 +392,64 @@

Assert-NotImplemented

}

function Test_Sync_ProjectDatabaseAsync_ClearValues{
Reset-InvokeCommandMock
Mock_DatabaseRoot

$Owner = "SomeOrg" ; $ProjectNumber = 164

$moduleRootPath = $PSScriptRoot | Split-Path -Parent | Split-Path -Parent | Convert-Path

$projectId = "PVT_kwDOBCrGTM4ActQa"
$itemId1 = "PVTI_lADOBCrGTM4ActQazgMuXXc"
$fieldComment1 = "Comment" ; $fieldComment1Id = "PVTF_lADOBCrGTM4ActQazgSl5GU"
$fieldPriority1 = "Priority" ; $fieldPriority1Id ="PVTSSF_lADOBCrGTM4ActQazgSl5LY"

# Edit-ProjectItem will call Get-Project with SkipItems
# This test is to confirm the sync works with the project and items
# Cache the project with items
MockCall_GitHubOrgProjectWithFields -Owner $owner -ProjectNumber $projectNumber -FileName 'projectV2.json'
$null = Get-Project -Owner $Owner -ProjectNumber $ProjectNumber

# Mock clear command for empty comment field (using async alias)
$clearCommand = 'Import-Module {projecthelper} ; Invoke-GitHubClearItemValues -ProjectId {ProjectId} -ItemId {ItemId} -FieldId {FieldId}'
$clearCommand = $clearCommand -replace '{projecthelper}', $moduleRootPath
$clearCommand = $clearCommand -replace '{ProjectId}', $projectId
$clearCommand = $clearCommand -replace '{ItemId}', $itemId1
$clearCommand = $clearCommand -replace '{FieldId}', $fieldComment1Id
MockCallJsonAsync -Command $clearCommand -FileName "clearProjectV2ItemFieldValue.json"

# Mock clear command for empty priority field (using async alias)
$clearCommand = 'Import-Module {projecthelper} ; Invoke-GitHubClearItemValues -ProjectId {ProjectId} -ItemId {ItemId} -FieldId {FieldId}'
$clearCommand = $clearCommand -replace '{projecthelper}', $moduleRootPath
$clearCommand = $clearCommand -replace '{ProjectId}', $projectId
$clearCommand = $clearCommand -replace '{ItemId}', $itemId1
$clearCommand = $clearCommand -replace '{FieldId}', $fieldPriority1Id
MockCallJsonAsync -Command $clearCommand -FileName "clearProjectV2ItemFieldValue.json"

# Stage the values - clear comment (empty string) and update title
Edit-ProjectItem -Owner $owner -ProjectNumber $projectNumber $itemId1 $fieldComment1 ""
Edit-ProjectItem -Owner $owner -ProjectNumber $projectNumber $itemId1 $fieldPriority1 ""

Start-MyTranscript
$result = Sync-ProjectItemStagedAsync -Owner $Owner -ProjectNumber $ProjectNumber -SyncBatchSize 2
$transcript = Stop-MyTranscript

Check warning

Code scanning / PSScriptAnalyzer

The variable 'transcript' is assigned but never used. Warning

The variable 'transcript' is assigned but never used.

# Return true
Assert-IsTrue -Condition $result

# Verify clear and update commands were called by checking mock invocations
# The transcript should show both operations

# Staged list should be empty after successful sync
$staged = Get-ProjectItemStaged -Owner $Owner -ProjectNumber $ProjectNumber
Assert-IsNull -Object $staged

# Verify the values in the database
$item1 = Get-ProjectItem -Owner $Owner -ProjectNumber $ProjectNumber -ItemId $itemId1
Assert-StringIsNullOrEmpty -Presented $item1.$fieldComment1
Assert-StringIsNullOrEmpty -Presented $item1.$fieldPriority1

}
23 changes: 22 additions & 1 deletion helper/module.helper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function Get-Ps1FullPath{
)

# If folderName is not empty
if($FolderName -ne $null){
if($null -ne $FolderName){
$folder = Get-ModuleFolder -FolderName $FolderName -ModuleRootPath $ModuleRootPath
$path = $folder | Join-Path -ChildPath $Name
} else {
Expand Down Expand Up @@ -102,6 +102,27 @@ function Get-ModuleName{
return $MODULE_NAME
}

<#
.SYNOPSIS
Gets the full path of a specific module folder.

.DESCRIPTION
Gets the full path of a specific module folder based on the folder name and optionally the module root path.

.PARAMETER FolderName
The name of the folder to get the path for. Must be one of the valid folder names.

.PARAMETER ModuleRootPath
The root path of the module. If not provided, uses the default module root path.

.EXAMPLE
Get-ModuleFolder -FolderName "Public"
Gets the path to the Public folder of the current module.

.EXAMPLE
Get-ModuleFolder -FolderName "Private" -ModuleRootPath "C:\MyModule"
Gets the path to the Private folder of the specified module.
#>
function Get-ModuleFolder{
[CmdletBinding()]
param(
Expand Down
Loading
Loading