Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
110 changes: 110 additions & 0 deletions eng/common-tests/Verify-Links-ReplaceGithubLink.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
Import-Module Pester

BeforeAll {
# Default shared regex pattern with optional .git suffix
$script:defaultRegex = '^(https://github\.com/Azure/azure-sdk-for-java(?:\.git)?/(?:blob|tree)/)main(/.*)$'

# Helper function to test link replacement logic directly
# This mirrors the logic in ReplaceGithubLink function from Verify-Links.ps1
function Test-ReplaceGithubLink {
param(
[string]$originLink,
[string]$branchReplaceRegex,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot default the shared regex here instead of duplicating it in all the tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in commit 92bdf41. The shared regex pattern with optional .git suffix is now defined as $defaultRegex in the BeforeAll block and reused throughout the tests.

[string]$branchReplacementName
)

if (!$branchReplacementName -or !$branchReplaceRegex) {
return $originLink
}
$ReplacementPattern = "`${1}$branchReplacementName`$2"
return $originLink -replace $branchReplaceRegex, $ReplacementPattern
}
}

Describe "ReplaceGithubLink" {
Context "When repository URI does not end with .git" {
It "Should replace branch name in GitHub link without .git suffix" {
$originalLink = "https://github.com/Azure/azure-sdk-for-java/blob/main/README.md"
$expectedLink = "https://github.com/Azure/azure-sdk-for-java/blob/abc123def/README.md"

$result = Test-ReplaceGithubLink -originLink $originalLink -branchReplaceRegex '^(https://github\.com/Azure/azure-sdk-for-java/(?:blob|tree)/)main(/.*)$' -branchReplacementName "abc123def"
$result | Should -Be $expectedLink
}

It "Should replace branch name with tree path" {
$originalLink = "https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/core"
$expectedLink = "https://github.com/Azure/azure-sdk-for-java/tree/abc123def/sdk/core"

$result = Test-ReplaceGithubLink -originLink $originalLink -branchReplaceRegex '^(https://github\.com/Azure/azure-sdk-for-java/(?:blob|tree)/)main(/.*)$' -branchReplacementName "abc123def"
$result | Should -Be $expectedLink
}
}

Context "When repository URI ends with .git" {
It "Should replace branch name in GitHub link with .git suffix" {
$originalLink = "https://github.com/Azure/azure-sdk-for-java.git/blob/main/README.md"
$expectedLink = "https://github.com/Azure/azure-sdk-for-java.git/blob/abc123def/README.md"

$result = Test-ReplaceGithubLink -originLink $originalLink -branchReplaceRegex '^(https://github\.com/Azure/azure-sdk-for-java\.git/(?:blob|tree)/)main(/.*)$' -branchReplacementName "abc123def"
$result | Should -Be $expectedLink
}

It "Should replace branch name with tree path and .git suffix" {
$originalLink = "https://github.com/Azure/azure-sdk-for-java.git/tree/main/sdk/core"
$expectedLink = "https://github.com/Azure/azure-sdk-for-java.git/tree/abc123def/sdk/core"

$result = Test-ReplaceGithubLink -originLink $originalLink -branchReplaceRegex '^(https://github\.com/Azure/azure-sdk-for-java\.git/(?:blob|tree)/)main(/.*)$' -branchReplacementName "abc123def"
$result | Should -Be $expectedLink
}
}

Context "When regex handles optional .git suffix" {
It "Should replace branch name whether .git is present or not" {
# Test without .git
$originalLinkWithoutGit = "https://github.com/Azure/azure-sdk-for-java/blob/main/README.md"
$expectedLink = "https://github.com/Azure/azure-sdk-for-java/blob/abc123def/README.md"

$resultWithoutGit = Test-ReplaceGithubLink -originLink $originalLinkWithoutGit -branchReplaceRegex $defaultRegex -branchReplacementName "abc123def"
$resultWithoutGit | Should -Be $expectedLink

# Test with .git
$originalLinkWithGit = "https://github.com/Azure/azure-sdk-for-java.git/blob/main/README.md"
$expectedLinkWithGit = "https://github.com/Azure/azure-sdk-for-java.git/blob/abc123def/README.md"

$resultWithGit = Test-ReplaceGithubLink -originLink $originalLinkWithGit -branchReplaceRegex $defaultRegex -branchReplacementName "abc123def"
$resultWithGit | Should -Be $expectedLinkWithGit
}
}

Context "When link does not match regex pattern" {
It "Should return original link unchanged" {
$originalLink = "https://github.com/SomeOrg/SomeRepo/blob/main/README.md"

$result = Test-ReplaceGithubLink -originLink $originalLink -branchReplaceRegex $defaultRegex -branchReplacementName "abc123def"
$result | Should -Be $originalLink
}

It "Should return original link when branch name doesn't match" {
$originalLink = "https://github.com/Azure/azure-sdk-for-java/blob/feature-branch/README.md"

$result = Test-ReplaceGithubLink -originLink $originalLink -branchReplaceRegex $defaultRegex -branchReplacementName "abc123def"
$result | Should -Be $originalLink
}
}

Context "When replacement parameters are not set" {
It "Should return original link when branchReplacementName is empty" {
$originalLink = "https://github.com/Azure/azure-sdk-for-java/blob/main/README.md"

$result = Test-ReplaceGithubLink -originLink $originalLink -branchReplaceRegex $defaultRegex -branchReplacementName ""
$result | Should -Be $originalLink
}

It "Should return original link when branchReplaceRegex is empty" {
$originalLink = "https://github.com/Azure/azure-sdk-for-java/blob/main/README.md"

$result = Test-ReplaceGithubLink -originLink $originalLink -branchReplaceRegex "" -branchReplacementName "abc123def"
$result | Should -Be $originalLink
}
}
}
2 changes: 1 addition & 1 deletion eng/common/pipelines/templates/steps/verify-links.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ parameters:
Recursive: $false
CheckLinkGuidance: $true
Urls: '(Get-ChildItem -Path ./ -Recurse -Include *.md)'
BranchReplaceRegex: "^(${env:SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI}/(?:blob|tree)/)$(DefaultBranch)(/.*)$"
BranchReplaceRegex: "^(${env:SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI}(?:\\.git)?/(?:blob|tree)/)$(DefaultBranch)(/.*)$"
BranchReplacementName: "${env:SYSTEM_PULLREQUEST_SOURCECOMMITID}"
Condition: succeeded() # If you want to run on failure for the link checker, set it to `Condition: succeededOrFailed()`.

Expand Down