Skip to content
3 changes: 2 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
{
"name": "PowerShell",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/powershell:lts-debian-11",
"image": "mcr.microsoft.com/powershell:lts-ubuntu-22.04",

"features": {
"ghcr.io/devcontainers/features/common-utils:2": {},
"ghcr.io/devcontainers/features/powershell:1": {},
Expand Down
3 changes: 2 additions & 1 deletion SfHelper.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ $START = Get-ChildItem -Path $MODULE_PATH -Filter start.ps1 -Recurse
if($START | Test-Path){ . $($START | Get-Item).FullName }

#Get public and private function definition files.
$Include = @( Get-ChildItem -Path $MODULE_PATH\include\*.ps1 -Recurse -ErrorAction SilentlyContinue )
$Public = @( Get-ChildItem -Path $MODULE_PATH\public\*.ps1 -Recurse -ErrorAction SilentlyContinue )
$Private = @( Get-ChildItem -Path $MODULE_PATH\private\*.ps1 -Recurse -ErrorAction SilentlyContinue )

#Dot source the files
Foreach($import in @($Public + $Private))
Foreach($import in @($Include + $Public + $Private))
{
Try
{
Expand Down
3 changes: 2 additions & 1 deletion Test/Test.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ Write-Information -Message ("Loading {0} ..." -f ($PSCommandPath | Split-Path -L
$MODULE_PATH = $PSScriptRoot

#Get public and private function definition files.
$Include = @( Get-ChildItem -Path $MODULE_PATH\include\*.ps1 -Recurse -ErrorAction SilentlyContinue )
$Public = @( Get-ChildItem -Path $MODULE_PATH\public\*.ps1 -Recurse -ErrorAction SilentlyContinue )
$Private = @( Get-ChildItem -Path $MODULE_PATH\private\*.ps1 -Recurse -ErrorAction SilentlyContinue )

#Dot source the files
Foreach($import in @($Public + $Private))
Foreach($import in @($Include + $Public + $Private))
{
Try
{
Expand Down
28 changes: 28 additions & 0 deletions Test/include/config.mock.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@


function Mock_Config{
param(
[Parameter(Position=0)][string] $key = "config",
[Parameter(Position=1)][object] $Config
)

$MOCK_CONFIG_PATH = "test_config_path"

# Remove mock config path if exists
if(Test-Path $MOCK_CONFIG_PATH){
Remove-Item -Path $MOCK_CONFIG_PATH -ErrorAction SilentlyContinue -Recurse -Force
}

# create mock config path
New-Item -Path $MOCK_CONFIG_PATH -ItemType Directory -Force

# if $config is not null save it to a file
if($null -ne $Config){
$configfile = Join-Path -Path $MOCK_CONFIG_PATH -ChildPath "$key.json"
$Config | ConvertTo-Json -Depth 10 | Set-Content $configfile
}

# Mock invoke call
MockCallToString "Invoke-GetConfigRootPath" -OutString $MOCK_CONFIG_PATH

}
9 changes: 9 additions & 0 deletions Test/include/database.mock.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function Mock_Database([switch]$ResetDatabase){

MockCallToString "Invoke-GetDatabaseStorePath" -OutString "test_database_path"

if($ResetDatabase){
Reset-DatabaseStore
}

}
23 changes: 20 additions & 3 deletions Test/private/InvokeCommandMock.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Managing dependencies
$MODULE_INVOKATION_TAG = "project-migration-module"
$MODULE_INVOKATION_TAG_MOCK = "project-migration-module_Mock"
$MODULE_INVOKATION_TAG = "SfHelperModule"
$MODULE_INVOKATION_TAG_MOCK = "SfHelperModule-Mock"
$ROOT = $PSScriptRoot | Split-Path -Parent
$MOCK_PATH = $ROOT | Join-Path -ChildPath 'private' -AdditionalChildPath 'mocks'

Expand Down Expand Up @@ -136,11 +136,28 @@

$result = Invoke-Expression -Command $Command

$result | ConvertTo-Json -Depth 100 | Out-File -FilePath $filePath
$json = $result | ConvertTo-Json -Depth 100

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
$json | Out-File -FilePath $filePath

Write-Host $FileName
} Export-ModuleMember -Function Save-InvokeAsMockFile

function Save-InvokeAsMockFileJson{

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Save-InvokeAsMockFileJson' does not have a help comment. Note

The cmdlet 'Save-InvokeAsMockFileJson' does not have a help comment.
param(
[Parameter(Mandatory=$true)] [string]$Command,
[Parameter(Mandatory=$true)] [string]$FileName
)

$filePath = Get-MockFileFullPath -fileName $fileName

$result = Invoke-Expression -Command $Command

Check warning

Code scanning / PSScriptAnalyzer

Invoke-Expression is used. Please remove Invoke-Expression from script and find other options instead. Warning

Invoke-Expression is used. Please remove Invoke-Expression from script and find other options instead.

$result | Out-File -FilePath $filePath

Write-Host $FileName

Check warning

Code scanning / PSScriptAnalyzer

File 'InvokeCommandMock.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information. Warning

File 'InvokeCommandMock.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information.
} Export-ModuleMember -Function Save-InvokeAsMockFileJson

function Assert-MockFileNotfound{
param(
[Parameter(Mandatory=$true,Position=0)] [string]$FileName
Expand Down
31 changes: 31 additions & 0 deletions Test/private/mocks/sf-data-query-account.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"status": 0,
"result": {
"records": [
{
"attributes": {
"type": "Account",
"url": "/services/data/v63.0/sobjects/Account/0010V00002KIWkaQAH"
},
"Id": "0010V00002KIWkaQAH",
"Name": "Contoso",
"OwnerId": "0053o000008Skv3AAC",
"Industry": "Retail",
"Account_Owner__c": "<a href=\"/0053o000008Skv3\" target=\"_self\">Oana Dinca</a> [<a href=\"/0010V00002KIWka/a?sendMail=1&amp;retURL=%2F0010V00002KIWka\" target=\"_self\">Change</a>]",
"Account_Segment__c": "Enterprise",
"Account_Owner_Role__c": "EMEA - Enterprise Sales Manager - South EMEA",
"Account_Tier__c": "Tier 1",
"Potential_Seats__c": 998,
"Country_Name__c": "Spain",
"Current_Seats__c": 600,
"Current_ARR_10__c": 116928,
"Salesforce_Record_URL__c": "https://github.my.salesforce.com/0010V00002KIWkaQAH",
"Potential_Seats_Manual__c": 1500,
"Website": "www.contos.es",
"PhotoUrl": "/services/images/photo/0010V00002KIWkaQAH"
}
],
"totalSize": 1,
"done": true
}
}
2 changes: 1 addition & 1 deletion Test/public/Get-SfObjectIdFromUrl.test.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Test_GetSfAccount{
function Test_GetSfAccountFromUrl{

. $PSScriptRoot/../../private/auxiliarfunctions.ps1

Expand Down
37 changes: 37 additions & 0 deletions Test/public/sfDataQuery.test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function Test_GetSfAccount{

Reset-InvokeCommandMock
Mock_Database -ResetDatabase
$mockAttrib = @{attributes = @("Potential_Seats_Manual__c","Website","PhotoUrl")}
Mock_Config -Config $mockAttrib

$dbstore = Invoke-MyCommand -Command GetDatabaseStorePath
Assert-AreEqual -Expected "test_database_path" -Presented $dbstore

$attrib = "Id,Name,OwnerId,Industry,Account_Owner__c,Account_Segment__c,Account_Owner_Role__c,Account_Tier__c,Potential_Seats__c,Country_Name__c,Current_Seats__c,Current_ARR_10__c,Salesforce_Record_URL__c,Potential_Seats_Manual__c,Website,PhotoUrl"
$type = "Account"
$id = "0010V00002KIWkaQAH"

$command = 'sf data query --query "SELECT {attributes} FROM {type} WHERE Id=''{id}''" -r=json'
$command = $command -replace "{attributes}", $attrib
$command = $command -replace "{type}", $type
$command = $command -replace "{id}", $id
MockCall -Command $command -filename "sf-data-query-account.json"

# Act with out cache
$result = get-sfAccount https://github.lightning.force.com/lightning/r/Account/0010V00002KIWkaQAH/view
Assert-AreEqual -Expected $Id -Presented $result.Id
$dbfiles = Get-ChildItem $dbstore
Assert-Count -Expected 1 -Presented $dbfiles
Assert-IsTrue -Condition ($dbfiles[0].Name -like "sfDataQuery*-$type-$id-*.json")

# Remove sf data

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
Reset-InvokeCommandMock
Mock_Database
Mock_Config -Config $mockAttrib

# Act with cache
$result = Get-SfAccount https://github.lightning.force.com/lightning/r/Account/0010V00002KIWkaQAH/view
Assert-AreEqual -Expected $Id -Presented $result.Id

}
93 changes: 93 additions & 0 deletions include/config.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Configuration management module

Set-MyInvokeCommandAlias -Alias GetConfigRootPath -Command "Invoke-GetConfigRootPath"

$moduleName = Get-ModuleName
$CONFIG_ROOT = [System.Environment]::GetFolderPath('UserProfile') | Join-Path -ChildPath ".helpers" -AdditionalChildPath $moduleName, "config"

# Create the config root if it does not exist
if(-Not (Test-Path $CONFIG_ROOT)){
New-Item -Path $CONFIG_ROOT -ItemType Directory
}

function Invoke-GetConfigRootPath {

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Invoke-GetConfigRootPath' does not have a help comment. Note

The cmdlet 'Invoke-GetConfigRootPath' does not have a help comment.
[CmdletBinding()]
param()

$configRoot = $CONFIG_ROOT
return $configRoot
} Export-ModuleMember -Function Invoke-GetConfigRootPath

function GetConfigFile {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)][string]$Key
)

$configRoot = Invoke-MyCommand -Command GetConfigRootPath
$path = Join-Path -Path $configRoot -ChildPath "$Key.json"
return $path
}

function Test-Configuration {
[CmdletBinding()]
param(
[Parameter(Position = 0)][string]$Key = "config"
)

$path = GetConfigFile -Key $Key

return Test-Path $path
}

function Get-Configuration {
[CmdletBinding()]
param(
[Parameter(Position = 0)][string]$Key = "config"
)

$path = GetConfigFile -Key $Key

if(-Not (Test-Configuration -Key $Key)){
return $null
}

try{
$ret = Get-Content $path | ConvertFrom-Json -ErrorAction Stop
return $ret
}
catch{
Write-Warning "Error reading configuration ($Key) file: $($path). $($_.Exception.Message)"
return $null
}
}

function Save-Configuration {
[CmdletBinding()]
param(
[Parameter(Position = 0)][string]$Key = "config",
[Parameter(Mandatory = $true, Position = 1)][Object]$Config
)

$path = GetConfigFile -Key $Key

try {
$Config | ConvertTo-Json -Depth 10 | Set-Content $path -ErrorAction Stop
}
catch {
Write-Warning "Error saving configuration ($Key) to file: $($path). $($_.Exception.Message)"
return $false

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Save-Configuration' returns an object of type 'System.Boolean' but this type is not declared in the OutputType attribute. Note

The cmdlet 'Save-Configuration' returns an object of type 'System.Boolean' but this type is not declared in the OutputType attribute.
}

return $true

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Save-Configuration' returns an object of type 'System.Boolean' but this type is not declared in the OutputType attribute. Note

The cmdlet 'Save-Configuration' returns an object of type 'System.Boolean' but this type is not declared in the OutputType attribute.
}










Loading
Loading