Skip to content
Closed
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
8 changes: 8 additions & 0 deletions resources/NpmDsc/NpmDsc.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,14 @@
$inDesiredState = $this.Test()
if ($this.Ensure -eq [Ensure]::Present) {
if (-not $inDesiredState) {
# TODO: Handling owner/repo package references pointing to GH repositories requires accounting
# for git errors (missing git, failed authentication, etc.).
#
# See: https://docs.npmjs.com/cli/v11/commands/npm-install#description

Check warning on line 349 in resources/NpmDsc/NpmDsc.psm1

View workflow job for this annotation

GitHub Actions / Check Spelling

`npmjs` is not a recognized word. (unrecognized-spelling)
if (($this.Name -notmatch '^git(?:\+(?:ssh|https?|file))?://') -and $this.Name.Contains('/') -and -not $this.Name.StartsWith('@')) {
throw "The Set operation currently only supports packages specified as [<@scope>/]<name>. The given package looks like a GitHub repository: $($this.Name)."
}

Install-NpmPackage -PackageName $this.Name -Arguments $this.Arguments -Global $this.Global
}
} else {
Expand Down
41 changes: 41 additions & 0 deletions tests/NpmDsc/NpmDsc.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,47 @@ Describe 'NpmPackage' {
$finalState.InDesiredState | Should -Be $true
}

Context 'Package names' {
BeforeEach {
npm uninstall babel/helpers 2>$null | Out-Null || $(throw 'npm uninstall failed')
npm uninstall --global babel/helpers 2>$null | Out-Null || $(throw 'npm uninstall failed')
}

Context 'Present' {
It 'Unsupported if package name points to VCS repository (scope/name)' -Skip:(!$IsWindows) -ForEach @(
@{ isGlobal = $false }
@{ isGlobal = $true }
) {
$desiredState = @{
Name = 'babel/helpers'
Global = $isGlobal
Ensure = 'Present'
}

$message = 'The Set operation currently only supports packages specified as `[<@scope>/`]<name>. The given package looks like a GitHub repository: babel/helpers.'

{ Invoke-DscResource -Name NpmPackage -ModuleName NpmDsc -Method Set -Property $desiredState } | Should -Throw $message
}
}

Context 'Absent' {
It 'Supports any package name' -Skip:(!$IsWindows) -ForEach @(
@{ isGlobal = $false }
@{ isGlobal = $true }
) {
$desiredState = @{
Name = 'babel/helpers'
Global = $isGlobal
Ensure = 'Absent'
}

Invoke-DscResource -Name NpmPackage -ModuleName NpmDsc -Method Set -Property $desiredState
$desiredState = Invoke-DscResource -Name NpmPackage -ModuleName NpmDsc -Method Test -Property $desiredState
$desiredState.InDesiredState | Should -Be $true
}
}
}

It 'Performs whatif operation successfully' -Skip:(!$IsWindows) {
$whatIfState = @{
Name = 'react'
Expand Down
Loading