From 6bf62316f7c1d85c7194b74a8bc95e4eaa3d5cb9 Mon Sep 17 00:00:00 2001 From: Michael Kelley Date: Mon, 22 Aug 2022 22:04:28 -0700 Subject: [PATCH] Fix yarn package name parsing (#10) - Fix yarn package name parsing to account for beginning '@' - Add tests to test package name parsing function - Add tests to CI and Deploy workflows --- .github/scripts/Tests.ps1 | 1 + .github/workflows/CI.yml | 3 ++ .github/workflows/Deploy.yml | 3 ++ .gitignore | 2 +- build.ps1 | 4 +- .../Find-PackageNameWithoutVersion.ps1 | 8 ++++ src/Helpers/Get-Packages.ps1 | 7 ++-- tests/Helpers.Tests.ps1 | 38 +++++++++++++++++++ 8 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 .github/scripts/Tests.ps1 create mode 100644 src/Helpers/Find-PackageNameWithoutVersion.ps1 create mode 100644 tests/Helpers.Tests.ps1 diff --git a/.github/scripts/Tests.ps1 b/.github/scripts/Tests.ps1 new file mode 100644 index 0000000..66f3614 --- /dev/null +++ b/.github/scripts/Tests.ps1 @@ -0,0 +1 @@ +Invoke-Pester -Path (Join-Path $PSScriptRoot "..\..\tests") -Verbose \ No newline at end of file diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index cbcbebf..1df01b8 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -23,3 +23,6 @@ jobs: - name: Build module run: ./build.ps1 shell: pwsh + - name: Run tests + run: ./.github/scripts/Tests.ps1 + shell: pwsh diff --git a/.github/workflows/Deploy.yml b/.github/workflows/Deploy.yml index 1e3ed28..d4c00f2 100644 --- a/.github/workflows/Deploy.yml +++ b/.github/workflows/Deploy.yml @@ -21,6 +21,9 @@ jobs: - name: Build Crescendo module run: ./build.ps1 shell: pwsh + - name: Run tests + run: ./.github/scripts/Tests.ps1 + shell: pwsh - name: Publish PowerShell Module run: ./.github/scripts/Deploy.ps1 shell: pwsh diff --git a/.gitignore b/.gitignore index 738964b..8c1de63 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ obj/* output/* yarn.crescendo/* -sample/node_modules/* \ No newline at end of file +**/node_modules/* diff --git a/build.ps1 b/build.ps1 index fa189e2..b1a2419 100644 --- a/build.ps1 +++ b/build.ps1 @@ -57,6 +57,4 @@ $ManifestInfo = @{ ProjectUri = 'https://github.com/kelleyma49/yarn.crescendo' } -Update-ModuleManifest -Path (Join-Path $Output "yarn.crescendo.psd1") @ManifestInfo -Verbose - -# Invoke-Pester -Path "$PSScriptRoot\tests" +Update-ModuleManifest -Path (Join-Path $Output "yarn.crescendo.psd1") @ManifestInfo -Verbose \ No newline at end of file diff --git a/src/Helpers/Find-PackageNameWithoutVersion.ps1 b/src/Helpers/Find-PackageNameWithoutVersion.ps1 new file mode 100644 index 0000000..69ca16e --- /dev/null +++ b/src/Helpers/Find-PackageNameWithoutVersion.ps1 @@ -0,0 +1,8 @@ + +function Find-PackageNameWithoutVersion($name) { + $idx = $name.LastIndexOf('@') + if ($idx -gt 0) { + $name = $name.Substring(0, $idx) + } + $name +} \ No newline at end of file diff --git a/src/Helpers/Get-Packages.ps1 b/src/Helpers/Get-Packages.ps1 index bd91e5e..ed37d1b 100644 --- a/src/Helpers/Get-Packages.ps1 +++ b/src/Helpers/Get-Packages.ps1 @@ -4,6 +4,8 @@ param ( $CommandName, $CommandAst, $FakeBoundParameters ) +. (Join-Path $PSScriptRoot "Find-PackageNameWithoutVersion.ps1") + if ([string]::IsNullOrEmpty($WordToComplete)) { $json = & yarn list --json | ConvertFrom-Json } @@ -11,7 +13,6 @@ else { $json = & yarn list --pattern $WordToComplete --json | ConvertFrom-Json } $names = $json.data.trees | ForEach-Object { - $nameWithoutVersion = $_.name.Split('@')[0] - $nameWithoutVersion + Find-PackageNameWithoutVersion $_.name } -return $names \ No newline at end of file +return $names diff --git a/tests/Helpers.Tests.ps1 b/tests/Helpers.Tests.ps1 new file mode 100644 index 0000000..3085d00 --- /dev/null +++ b/tests/Helpers.Tests.ps1 @@ -0,0 +1,38 @@ +#Import-Module "$PSScriptRoot\..\yarn-crescendo\yarn.crescendo.psd1" -Force +BeforeAll { + . "$PSScriptRoot\..\src\Helpers\Find-PackageNameWithoutVersion.ps1" +} + +Describe "Find-PackageNameWithoutVersion" { + It "should return same string with -" { + Find-PackageNameWithoutVersion "some-package" | Should -Be "some-package" + } + + It "should return same string with ." { + Find-PackageNameWithoutVersion "example.com" | Should -Be "example.com" + } + + It "should return same string with _" { + Find-PackageNameWithoutVersion "under_score" | Should -Be "under_score" + } + + It "should return same string starting with numbers" { + Find-PackageNameWithoutVersion "123numeric" | Should -Be "123numeric" + } + + It "should return same string starting @" { + Find-PackageNameWithoutVersion "@npm/thingy" | Should -Be "@npm/thingy" + } + + It "should strip number" { + Find-PackageNameWithoutVersion "thingy@1.2.3" | Should -Be "thingy" + } + + It "should strip number with ^" { + Find-PackageNameWithoutVersion "thingy@^1.2.3" | Should -Be "thingy" + } + + It "should strip number" { + Find-PackageNameWithoutVersion "@npm/thingy@1.2.3" | Should -Be "@npm/thingy" + } +}