From c4bd4d565478a23b6693b5db1008921ff19ab45a Mon Sep 17 00:00:00 2001 From: Joshua Cotton Date: Sat, 25 Apr 2020 15:00:20 -0400 Subject: [PATCH 1/7] Close #7424. Allow / in relative paths for `using module` --- .../engine/parser/SymbolResolver.cs | 4 ++-- .../Language/Parser/UsingModule.Tests.ps1 | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 test/powershell/Language/Parser/UsingModule.Tests.ps1 diff --git a/src/System.Management.Automation/engine/parser/SymbolResolver.cs b/src/System.Management.Automation/engine/parser/SymbolResolver.cs index a453f0095ac..785296f9cb9 100644 --- a/src/System.Management.Automation/engine/parser/SymbolResolver.cs +++ b/src/System.Management.Automation/engine/parser/SymbolResolver.cs @@ -498,8 +498,8 @@ private Collection GetModulesFromUsingModule(UsingStatementAst usi return null; } - // case 1: relative path. Relative for file in the same folder should include .\ - bool isPath = fullyQualifiedNameStr.Contains(@"\"); + // case 1: relative path. Relative for file in the same folder should include .\ or ./ + bool isPath = fullyQualifiedNameStr.Contains(@"\") || fullyQualifiedNameStr.Contains("/"); if (isPath && !LocationGlobber.IsAbsolutePath(fullyQualifiedNameStr)) { string rootPath = Path.GetDirectoryName(_parser._fileName); diff --git a/test/powershell/Language/Parser/UsingModule.Tests.ps1 b/test/powershell/Language/Parser/UsingModule.Tests.ps1 new file mode 100644 index 00000000000..37ad5cb461b --- /dev/null +++ b/test/powershell/Language/Parser/UsingModule.Tests.ps1 @@ -0,0 +1,23 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +Describe "Using module" -Tag "CI" { + BeforeAll { + Push-Location $PSScriptRoot + New-Item tmp -ItemType Directory + } + AfterAll { + Remove-Item tmp -Recurse + Pop-Location + } + It 'correctly handles paths with ./' { + Set-Location ./tmp + + 'function Get-Foo { "hi from t.psm1" }' > t.psm1 + + 'using module ./t.psm1; Get-Foo' > t.ps1 + + Set-Location .. + { ./tmp/t.ps1 } | Should -Not -Throw + } +} From 3caf6efcefa64e26927aaad0d4496df735e36475 Mon Sep 17 00:00:00 2001 From: Joshua Cotton Date: Sun, 26 Apr 2020 12:03:55 -0400 Subject: [PATCH 2/7] Move test to correct test file --- .../Classes/scripting.Classes.using.tests.ps1 | 11 ++++++++- .../Language/Parser/UsingModule.Tests.ps1 | 23 ------------------- 2 files changed, 10 insertions(+), 24 deletions(-) delete mode 100644 test/powershell/Language/Parser/UsingModule.Tests.ps1 diff --git a/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 b/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 index 18c3661d5af..dc3b4dfeca7 100644 --- a/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 +++ b/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 @@ -339,7 +339,7 @@ using module Foo } } - Context 'Side by side' { + Context 'Side by side' { BeforeAll { # Add side-by-side module $newVersion = '3.4.5' @@ -523,6 +523,15 @@ using module FooForPaths Pop-Location } } + + It 'can be accessed by relative path with ./' { + $name = 'relative-forward-slash-paths' + "function Get-Foo { ""hi from $name.psm1"" }" | Set-Content "TestDrive:\modules\$name.psm1" + + "using module ./$name.psm1; Get-Foo" | Set-Content "TestDrive:\modules\$name.ps1" + + { & "TestDrive:\modules\$name.ps1" } | Should -Not -Throw + } } Context "module has non-terminating error handled with 'SilentlyContinue'" { diff --git a/test/powershell/Language/Parser/UsingModule.Tests.ps1 b/test/powershell/Language/Parser/UsingModule.Tests.ps1 deleted file mode 100644 index 37ad5cb461b..00000000000 --- a/test/powershell/Language/Parser/UsingModule.Tests.ps1 +++ /dev/null @@ -1,23 +0,0 @@ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. - -Describe "Using module" -Tag "CI" { - BeforeAll { - Push-Location $PSScriptRoot - New-Item tmp -ItemType Directory - } - AfterAll { - Remove-Item tmp -Recurse - Pop-Location - } - It 'correctly handles paths with ./' { - Set-Location ./tmp - - 'function Get-Foo { "hi from t.psm1" }' > t.psm1 - - 'using module ./t.psm1; Get-Foo' > t.ps1 - - Set-Location .. - { ./tmp/t.ps1 } | Should -Not -Throw - } -} From e98bc31c09ae91f73cde2190a9b0c69b936c929f Mon Sep 17 00:00:00 2001 From: Joshua Cotton Date: Sun, 26 Apr 2020 14:34:27 -0400 Subject: [PATCH 3/7] Resolve feedback Contains char instead of a string, and changing how the test checked for success --- .../engine/parser/SymbolResolver.cs | 2 +- .../Language/Classes/scripting.Classes.using.tests.ps1 | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/System.Management.Automation/engine/parser/SymbolResolver.cs b/src/System.Management.Automation/engine/parser/SymbolResolver.cs index 785296f9cb9..42091ba2e7b 100644 --- a/src/System.Management.Automation/engine/parser/SymbolResolver.cs +++ b/src/System.Management.Automation/engine/parser/SymbolResolver.cs @@ -499,7 +499,7 @@ private Collection GetModulesFromUsingModule(UsingStatementAst usi } // case 1: relative path. Relative for file in the same folder should include .\ or ./ - bool isPath = fullyQualifiedNameStr.Contains(@"\") || fullyQualifiedNameStr.Contains("/"); + bool isPath = fullyQualifiedNameStr.Contains('\\') || fullyQualifiedNameStr.Contains('/'); if (isPath && !LocationGlobber.IsAbsolutePath(fullyQualifiedNameStr)) { string rootPath = Path.GetDirectoryName(_parser._fileName); diff --git a/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 b/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 index dc3b4dfeca7..99ed193468a 100644 --- a/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 +++ b/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 @@ -526,11 +526,11 @@ using module FooForPaths It 'can be accessed by relative path with ./' { $name = 'relative-forward-slash-paths' - "function Get-Foo { ""hi from $name.psm1"" }" | Set-Content "TestDrive:\modules\$name.psm1" + 'function Get-TestString { "Worked" }' | Set-Content "TestDrive:\modules\$name.psm1" - "using module ./$name.psm1; Get-Foo" | Set-Content "TestDrive:\modules\$name.ps1" + "using module ./$name.psm1; Get-TestString" | Set-Content "TestDrive:\modules\$name.ps1" - { & "TestDrive:\modules\$name.ps1" } | Should -Not -Throw + & "TestDrive:\modules\$name.ps1" | Should -EQ "Worked" } } From 06e38b31f4287c5dace21c506a950e0f6caafa6b Mon Sep 17 00:00:00 2001 From: Joshua Cotton Date: Mon, 27 Apr 2020 19:44:05 -0400 Subject: [PATCH 4/7] Add regression test to ensure .\ still works for using module --- .../Language/Classes/scripting.Classes.using.tests.ps1 | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 b/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 index 99ed193468a..4b26fe9a205 100644 --- a/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 +++ b/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 @@ -532,6 +532,15 @@ using module FooForPaths & "TestDrive:\modules\$name.ps1" | Should -EQ "Worked" } + + It 'can be accessed by relative path with .\' { + $name = 'relative-backslash-paths' + 'function Get-TestString { "Worked" }' | Set-Content "TestDrive:\modules\$name.psm1" + + "using module .\$name.psm1; Get-TestString" | Set-Content "TestDrive:\modules\$name.ps1" + + & "TestDrive:\modules\$name.ps1" | Should -EQ "Worked" + } } Context "module has non-terminating error handled with 'SilentlyContinue'" { From e87ec6afb910780d0f9fe1518fefb29d6444f668 Mon Sep 17 00:00:00 2001 From: Joshua Cotton Date: Tue, 28 Apr 2020 19:23:10 -0400 Subject: [PATCH 5/7] Use BeExactly in Pester tests --- .../Language/Classes/scripting.Classes.using.tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 b/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 index 4b26fe9a205..3e6ea6b9773 100644 --- a/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 +++ b/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 @@ -530,7 +530,7 @@ using module FooForPaths "using module ./$name.psm1; Get-TestString" | Set-Content "TestDrive:\modules\$name.ps1" - & "TestDrive:\modules\$name.ps1" | Should -EQ "Worked" + & "TestDrive:\modules\$name.ps1" | Should -BeExactly "Worked" } It 'can be accessed by relative path with .\' { @@ -539,7 +539,7 @@ using module FooForPaths "using module .\$name.psm1; Get-TestString" | Set-Content "TestDrive:\modules\$name.ps1" - & "TestDrive:\modules\$name.ps1" | Should -EQ "Worked" + & "TestDrive:\modules\$name.ps1" | Should -BeExactly "Worked" } } From 3c3e3e501cc3db87af3d447f45940b6b82c828b2 Mon Sep 17 00:00:00 2001 From: Joshua Cotton Date: Wed, 29 Apr 2020 19:43:35 -0400 Subject: [PATCH 6/7] Refactor tests to use TestCases --- .../Classes/scripting.Classes.using.tests.ps1 | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 b/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 index 3e6ea6b9773..0d937bf247d 100644 --- a/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 +++ b/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 @@ -524,20 +524,12 @@ using module FooForPaths } } - It 'can be accessed by relative path with ./' { - $name = 'relative-forward-slash-paths' + It 'can be accessed by relative path with .' -TestCases @{Separator = '\'}, @{Separator = '/'} { + param([string]$Separator) + $name = 'relative-slash-paths' 'function Get-TestString { "Worked" }' | Set-Content "TestDrive:\modules\$name.psm1" - "using module ./$name.psm1; Get-TestString" | Set-Content "TestDrive:\modules\$name.ps1" - - & "TestDrive:\modules\$name.ps1" | Should -BeExactly "Worked" - } - - It 'can be accessed by relative path with .\' { - $name = 'relative-backslash-paths' - 'function Get-TestString { "Worked" }' | Set-Content "TestDrive:\modules\$name.psm1" - - "using module .\$name.psm1; Get-TestString" | Set-Content "TestDrive:\modules\$name.ps1" + "using module .$Separator$name.psm1; Get-TestString" | Set-Content "TestDrive:\modules\$name.ps1" & "TestDrive:\modules\$name.ps1" | Should -BeExactly "Worked" } From e9d51cbed5cf7d4af409462f20fc5cf3444d059a Mon Sep 17 00:00:00 2001 From: Joshua Cotton Date: Thu, 30 Apr 2020 13:08:23 -0400 Subject: [PATCH 7/7] Update TestCases formatting Co-authored-by: Ilya --- .../Language/Classes/scripting.Classes.using.tests.ps1 | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 b/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 index 0d937bf247d..8ea691888a5 100644 --- a/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 +++ b/test/powershell/Language/Classes/scripting.Classes.using.tests.ps1 @@ -524,7 +524,10 @@ using module FooForPaths } } - It 'can be accessed by relative path with .' -TestCases @{Separator = '\'}, @{Separator = '/'} { + It 'can be accessed by relative path with .' -TestCases @( + @{ Separator = '\' }, + @{ Separator = '/' } + ) { param([string]$Separator) $name = 'relative-slash-paths' 'function Get-TestString { "Worked" }' | Set-Content "TestDrive:\modules\$name.psm1" @@ -557,4 +560,3 @@ using module $testFile } } } -