You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix cross-platform unit test path compatibility issues
Merge pull request awslabs#47 from austoonz/chore/fix-xplat-unit-testing
- Add Assert-PathEquals helper function to normalize path separators for cross-platform compatibility in unit tests
- Replace direct string path comparisons with Assert-PathEquals in Get-Handler.Tests.ps1 and Set-PSModulePath.Tests.ps1
- Remove Windows-specific read-only directory test that was causing cross-platform issues (not relevant for the Lambda runtime which is Linux only)
- Ensure all unit tests pass on Windows, Linux, and macOS by handling path separator differences transparently
The new Assert-PathEquals function normalizes both actual and expected paths to use forward slashes before comparison, allowing tests to pass regardless of the platform's native path separator behavior.
{ Assert-FileExists-Path $unreadableFile-ShouldBeFile } | Should -Not-Throw
447
447
}
448
448
}
449
-
}
449
+
}
450
+
451
+
Describe "Assert-PathEquals" {
452
+
Context "When paths are identical" {
453
+
It "Should pass for identical forward slash paths" {
454
+
{ Assert-PathEquals-Actual "/var/task/handler.ps1"-Expected "/var/task/handler.ps1" } | Should -Not-Throw
455
+
}
456
+
457
+
It "Should pass for identical backslash paths" {
458
+
{ Assert-PathEquals-Actual "C:\var\task\handler.ps1"-Expected "C:\var\task\handler.ps1" } | Should -Not-Throw
459
+
}
460
+
461
+
It "Should pass for identical relative paths" {
462
+
{ Assert-PathEquals-Actual "lib/utilities.ps1"-Expected "lib/utilities.ps1" } | Should -Not-Throw
463
+
}
464
+
465
+
It "Should pass for identical single file names" {
466
+
{ Assert-PathEquals-Actual "handler.ps1"-Expected "handler.ps1" } | Should -Not-Throw
467
+
}
468
+
}
469
+
470
+
Context "When paths differ only by separators" {
471
+
It "Should pass when actual uses backslashes and expected uses forward slashes" {
472
+
{ Assert-PathEquals-Actual "C:\var\task\handler.ps1"-Expected "C:/var/task/handler.ps1" } | Should -Not-Throw
473
+
}
474
+
475
+
It "Should pass when actual uses forward slashes and expected uses backslashes" {
476
+
{ Assert-PathEquals-Actual "/var/task/handler.ps1"-Expected "\var\task\handler.ps1" } | Should -Not-Throw
477
+
}
478
+
479
+
It "Should pass for mixed separators in both paths" {
480
+
{ Assert-PathEquals-Actual "C:\var/task\handler.ps1"-Expected "C:/var\task/handler.ps1" } | Should -Not-Throw
481
+
}
482
+
483
+
It "Should pass for nested directory paths with different separators" {
484
+
{ Assert-PathEquals-Actual "lib\modules\aws\tools\handler.ps1"-Expected "lib/modules/aws/tools/handler.ps1" } | Should -Not-Throw
485
+
}
486
+
}
487
+
488
+
Context "When paths are genuinely different" {
489
+
It "Should throw for different file names" {
490
+
{ Assert-PathEquals-Actual "/var/task/handler.ps1"-Expected "/var/task/different.ps1" } | Should -Throw -ExpectedMessage "*Expected path '/var/task/different.ps1' but got '/var/task/handler.ps1'*"
491
+
}
492
+
493
+
It "Should throw for different directory paths" {
494
+
{ Assert-PathEquals-Actual "/var/task/handler.ps1"-Expected "/var/runtime/handler.ps1" } | Should -Throw -ExpectedMessage "*Expected path '/var/runtime/handler.ps1' but got '/var/task/handler.ps1'*"
495
+
}
496
+
497
+
It "Should throw for different number of path segments" {
498
+
{ Assert-PathEquals-Actual "/var/task/lib/handler.ps1"-Expected "/var/task/handler.ps1" } | Should -Throw -ExpectedMessage "*Expected path '/var/task/handler.ps1' but got '/var/task/lib/handler.ps1'*"
499
+
}
500
+
501
+
It "Should throw for completely different paths" {
502
+
{ Assert-PathEquals-Actual "C:\Windows\System32\file.txt"-Expected "/usr/local/bin/script.sh" } | Should -Throw -ExpectedMessage "*Expected path '/usr/local/bin/script.sh' but got 'C:\Windows\System32\file.txt'*"
503
+
}
504
+
}
505
+
506
+
Context "When using custom Because parameter" {
507
+
It "Should include custom reason in error message" {
508
+
{ Assert-PathEquals-Actual "/var/task/wrong.ps1"-Expected "/var/task/handler.ps1"-Because "the script file path should match the handler configuration" } | Should -Throw -ExpectedMessage "*because the script file path should match the handler configuration*"
509
+
}
510
+
511
+
It "Should include custom reason with normalized paths" {
512
+
{ Assert-PathEquals-Actual "C:\var\task\wrong.ps1"-Expected "/var/task/handler.ps1"-Because "cross-platform paths should be normalized" } | Should -Throw -ExpectedMessage "*because cross-platform paths should be normalized*"
513
+
}
514
+
}
515
+
516
+
Context "When handling edge cases" {
517
+
It "Should handle PowerShell parameter validation for empty strings" {
518
+
# PowerShell parameter validation prevents empty strings for mandatory parameters
519
+
{ Assert-PathEquals-Actual ""-Expected "" } | Should -Throw -ExpectedMessage "*Cannot bind argument to parameter*"
520
+
}
521
+
522
+
It "Should handle PowerShell parameter validation for empty actual path" {
523
+
{ Assert-PathEquals-Actual ""-Expected "/var/task/handler.ps1" } | Should -Throw -ExpectedMessage "*Cannot bind argument to parameter*"
524
+
}
525
+
526
+
It "Should handle paths with trailing separators" {
527
+
{ Assert-PathEquals-Actual "/var/task/"-Expected "/var/task" } | Should -Throw -ExpectedMessage "*Expected path '/var/task' but got '/var/task/'*"
528
+
}
529
+
530
+
It "Should handle paths with multiple consecutive separators" {
531
+
{ Assert-PathEquals-Actual "/var//task/handler.ps1"-Expected "/var/task/handler.ps1" } | Should -Throw -ExpectedMessage "*Expected path '/var/task/handler.ps1' but got '/var//task/handler.ps1'*"
532
+
}
533
+
534
+
It "Should handle paths with dots" {
535
+
{ Assert-PathEquals-Actual "/var/task/./handler.ps1"-Expected "/var/task/handler.ps1" } | Should -Throw -ExpectedMessage "*Expected path '/var/task/handler.ps1' but got '/var/task/./handler.ps1'*"
536
+
}
537
+
538
+
It "Should handle paths with parent directory references" {
539
+
{ Assert-PathEquals-Actual "/var/task/../task/handler.ps1"-Expected "/var/task/handler.ps1" } | Should -Throw -ExpectedMessage "*Expected path '/var/task/handler.ps1' but got '/var/task/../task/handler.ps1'*"
0 commit comments