pulumi/sdk/nodejs/npm/manager_test.go

369 lines
11 KiB
Go
Raw Permalink Normal View History

// Copyright 2016-2020, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package npm
import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"crypto/sha1" //nolint:gosec // this is what NPM wants
"encoding/hex"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
Use pnpm as package manager if we find a pnpm-lock.yaml file (#15456) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description Initial support pnpm. Note that this does not support pnpm workspaces yet. This also does not handle passing the package manager through from `pulumi new`. Once a user manually runs pnpm, creating a pnpm-lock.yaml, we'll detect that and pnpm. Fixes https://github.com/pulumi/pulumi/issues/15455 ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-21 13:41:21 +00:00
"os/exec"
"path/filepath"
"strconv"
"testing"
Fix merge failures #2 (#15543) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes https://github.com/pulumi/pulumi/issues/15528 See https://github.com/pulumi/pulumi/pull/15540 & https://github.com/pulumi/pulumi/pull/15531 Re-creating this as a PR with `ci/test` label so we can get it merged. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. --> --------- Co-authored-by: Justin Van Patten <jvp@justinvp.com> Co-authored-by: Anton Tayanovskyy <anton@pulumi.com> Co-authored-by: Thomas Gummerer <t.gummerer@gmail.com>
2024-02-29 21:06:24 +00:00
ptesting "github.com/pulumi/pulumi/sdk/v3/go/common/testing"
"github.com/pulumi/pulumi/sdk/v3/go/common/testing/iotest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// chdir temporarily changes the current directory of the program.
// It restores it to the original directory when the test is done.
func chdir(t *testing.T, dir string) {
cwd, err := os.Getwd()
require.NoError(t, err)
require.NoError(t, os.Chdir(dir)) // Set directory
t.Cleanup(func() {
require.NoError(t, os.Chdir(cwd)) // Restore directory
restoredDir, err := os.Getwd()
if assert.NoError(t, err) {
assert.Equal(t, cwd, restoredDir)
}
})
}
Use pnpm as package manager if we find a pnpm-lock.yaml file (#15456) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description Initial support pnpm. Note that this does not support pnpm workspaces yet. This also does not handle passing the package manager through from `pulumi new`. Once a user manually runs pnpm, creating a pnpm-lock.yaml, we'll detect that and pnpm. Fixes https://github.com/pulumi/pulumi/issues/15455 ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-21 13:41:21 +00:00
//nolint:paralleltest // changes working directory
func TestNPMInstall(t *testing.T) {
t.Run("development", func(t *testing.T) {
testInstall(t, "npm", false /*production*/)
})
t.Run("production", func(t *testing.T) {
testInstall(t, "npm", true /*production*/)
})
}
Use pnpm as package manager if we find a pnpm-lock.yaml file (#15456) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description Initial support pnpm. Note that this does not support pnpm workspaces yet. This also does not handle passing the package manager through from `pulumi new`. Once a user manually runs pnpm, creating a pnpm-lock.yaml, we'll detect that and pnpm. Fixes https://github.com/pulumi/pulumi/issues/15455 ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-21 13:41:21 +00:00
//nolint:paralleltest // changes working directory
func TestYarnInstall(t *testing.T) {
t.Run("development", func(t *testing.T) {
testInstall(t, "yarn", false /*production*/)
})
t.Run("production", func(t *testing.T) {
testInstall(t, "yarn", true /*production*/)
})
}
Use pnpm as package manager if we find a pnpm-lock.yaml file (#15456) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description Initial support pnpm. Note that this does not support pnpm workspaces yet. This also does not handle passing the package manager through from `pulumi new`. Once a user manually runs pnpm, creating a pnpm-lock.yaml, we'll detect that and pnpm. Fixes https://github.com/pulumi/pulumi/issues/15455 ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-21 13:41:21 +00:00
//nolint:paralleltest // changes working directory
func TestPnpmInstall(t *testing.T) {
t.Run("development", func(t *testing.T) {
testInstall(t, "pnpm", false /*production*/)
})
t.Run("production", func(t *testing.T) {
testInstall(t, "pnpm", true /*production*/)
})
}
func TestResolvePackageManager(t *testing.T) {
t.Parallel()
for _, tt := range []struct {
name string
pm PackageManagerType
Use pnpm as package manager if we find a pnpm-lock.yaml file (#15456) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description Initial support pnpm. Note that this does not support pnpm workspaces yet. This also does not handle passing the package manager through from `pulumi new`. Once a user manually runs pnpm, creating a pnpm-lock.yaml, we'll detect that and pnpm. Fixes https://github.com/pulumi/pulumi/issues/15455 ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-21 13:41:21 +00:00
lockFiles []string
expected string
Use pnpm as package manager if we find a pnpm-lock.yaml file (#15456) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description Initial support pnpm. Note that this does not support pnpm workspaces yet. This also does not handle passing the package manager through from `pulumi new`. Once a user manually runs pnpm, creating a pnpm-lock.yaml, we'll detect that and pnpm. Fixes https://github.com/pulumi/pulumi/issues/15455 ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-21 13:41:21 +00:00
}{
{"defaults to npm", AutoPackageManager, []string{}, "npm"},
{"picks npm", NpmPackageManager, []string{}, "npm"},
{"picks yarn", YarnPackageManager, []string{}, "yarn"},
{"picks pnpm", PnpmPackageManager, []string{}, "pnpm"},
{"picks npm based on lockfile", AutoPackageManager, []string{"npm"}, "npm"},
{"picks yarn based on lockfile", AutoPackageManager, []string{"yarn"}, "yarn"},
{"picks pnpm based on lockfile", AutoPackageManager, []string{"pnpm"}, "pnpm"},
{"yarn > pnpm > npm", AutoPackageManager, []string{"yarn", "pnpm", "npm"}, "yarn"},
{"pnpm > npm", AutoPackageManager, []string{"pnpm", "npm"}, "pnpm"},
Use pnpm as package manager if we find a pnpm-lock.yaml file (#15456) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description Initial support pnpm. Note that this does not support pnpm workspaces yet. This also does not handle passing the package manager through from `pulumi new`. Once a user manually runs pnpm, creating a pnpm-lock.yaml, we'll detect that and pnpm. Fixes https://github.com/pulumi/pulumi/issues/15455 ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-21 13:41:21 +00:00
} {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
for _, lockFile := range tt.lockFiles {
writeLockFile(t, dir, lockFile)
}
pm, err := ResolvePackageManager(tt.pm, dir)
Use pnpm as package manager if we find a pnpm-lock.yaml file (#15456) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description Initial support pnpm. Note that this does not support pnpm workspaces yet. This also does not handle passing the package manager through from `pulumi new`. Once a user manually runs pnpm, creating a pnpm-lock.yaml, we'll detect that and pnpm. Fixes https://github.com/pulumi/pulumi/issues/15455 ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-21 13:41:21 +00:00
require.NoError(t, err)
require.Equal(t, tt.expected, pm.Name())
Use pnpm as package manager if we find a pnpm-lock.yaml file (#15456) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description Initial support pnpm. Note that this does not support pnpm workspaces yet. This also does not handle passing the package manager through from `pulumi new`. Once a user manually runs pnpm, creating a pnpm-lock.yaml, we'll detect that and pnpm. Fixes https://github.com/pulumi/pulumi/issues/15455 ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-21 13:41:21 +00:00
})
}
}
func TestPack(t *testing.T) {
t.Parallel()
packageJSON := []byte(`{
"name": "test-package",
"version": "1.0"
}`)
for _, pm := range []string{"npm", "yarn", "pnpm"} {
pm := pm
t.Run(pm, func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
writeLockFile(t, dir, pm)
packageJSONFilename := filepath.Join(dir, "package.json")
require.NoError(t, os.WriteFile(packageJSONFilename, packageJSON, 0o600))
stderr := new(bytes.Buffer)
artifact, err := Pack(context.Background(), AutoPackageManager, dir, stderr)
Use pnpm as package manager if we find a pnpm-lock.yaml file (#15456) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description Initial support pnpm. Note that this does not support pnpm workspaces yet. This also does not handle passing the package manager through from `pulumi new`. Once a user manually runs pnpm, creating a pnpm-lock.yaml, we'll detect that and pnpm. Fixes https://github.com/pulumi/pulumi/issues/15455 ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-21 13:41:21 +00:00
require.NoError(t, err)
// check that the artifact contains a package.json
b, err := gzip.NewReader(bytes.NewReader((artifact)))
require.NoError(t, err)
tr := tar.NewReader(b)
for {
h, err := tr.Next()
if err == io.EOF {
require.Fail(t, "package.json not found")
break
}
require.NoError(t, err)
if h.Name == "package/package.json" {
break
}
}
})
}
}
func TestPackInvalidPackageJSON(t *testing.T) {
t.Parallel()
// Missing a version field
packageJSON := []byte(`{
"name": "test-package"
}`)
for _, tt := range []struct{ packageManager, expectedErrorMessage string }{
{"npm", "Invalid package, must have name and version"},
{"yarn", "Package doesn't have a version"},
{"pnpm", "Package version is not defined in the package.json"},
} {
tt := tt
t.Run(tt.packageManager, func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
writeLockFile(t, dir, tt.packageManager)
packageJSONFilename := filepath.Join(dir, "package.json")
require.NoError(t, os.WriteFile(packageJSONFilename, packageJSON, 0o600))
stderr := new(bytes.Buffer)
_, err := Pack(context.Background(), AutoPackageManager, dir, stderr)
Use pnpm as package manager if we find a pnpm-lock.yaml file (#15456) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description Initial support pnpm. Note that this does not support pnpm workspaces yet. This also does not handle passing the package manager through from `pulumi new`. Once a user manually runs pnpm, creating a pnpm-lock.yaml, we'll detect that and pnpm. Fixes https://github.com/pulumi/pulumi/issues/15455 ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-21 13:41:21 +00:00
exitErr := new(exec.ExitError)
require.ErrorAs(t, err, &exitErr)
assert.NotZero(t, exitErr.ExitCode())
require.Contains(t, stderr.String(), tt.expectedErrorMessage)
})
}
}
// writeLockFile writes a mock lockfile for the selected package manager
func writeLockFile(t *testing.T, dir string, packageManager string) {
t.Helper()
switch packageManager {
case "npm":
writeFile(t, filepath.Join(dir, "package-lock.json"), "{\"lockfileVersion\": 2}")
case "yarn":
writeFile(t, filepath.Join(dir, "yarn.lock"), "# yarn lockfile v1")
case "pnpm":
writeFile(t, filepath.Join(dir, "pnpm-lock.yaml"), "lockfileVersion: '6.0'")
}
}
func testInstall(t *testing.T, packageManager string, production bool) {
// To test this functionality without actually hitting NPM,
// we'll spin up a local HTTP server that implements a subset
// of the NPM registry API.
//
// We'll tell NPM to use this server with a ~/.npmrc file
// containing the line:
//
// registry = <srv.URL>
//
Use pnpm as package manager if we find a pnpm-lock.yaml file (#15456) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description Initial support pnpm. Note that this does not support pnpm workspaces yet. This also does not handle passing the package manager through from `pulumi new`. Once a user manually runs pnpm, creating a pnpm-lock.yaml, we'll detect that and pnpm. Fixes https://github.com/pulumi/pulumi/issues/15455 ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-21 13:41:21 +00:00
// Pnpm reads the same .npmrc file.
//
// Similarly, we'll tell Yarn to use this server with a
// ~/.yarnrc file containing the line:
//
// registry "<srv.URL>"
home := t.TempDir()
t.Setenv("HOME", home)
registryURL := fakeNPMRegistry(t)
writeFile(t, filepath.Join(home, ".npmrc"),
"registry="+registryURL)
writeFile(t, filepath.Join(home, ".yarnrc"),
"registry "+strconv.Quote(registryURL))
// Create a new empty test directory and change the current working directory to it.
tempdir := t.TempDir()
chdir(t, tempdir)
// Create a package directory to install dependencies into.
pkgdir := filepath.Join(tempdir, "package")
all: Reformat with gofumpt Per team discussion, switching to gofumpt. [gofumpt][1] is an alternative, stricter alternative to gofmt. It addresses other stylistic concerns that gofmt doesn't yet cover. [1]: https://github.com/mvdan/gofumpt See the full list of [Added rules][2], but it includes: - Dropping empty lines around function bodies - Dropping unnecessary variable grouping when there's only one variable - Ensuring an empty line between multi-line functions - simplification (`-s` in gofmt) is always enabled - Ensuring multi-line function signatures end with `) {` on a separate line. [2]: https://github.com/mvdan/gofumpt#Added-rules gofumpt is stricter, but there's no lock-in. All gofumpt output is valid gofmt output, so if we decide we don't like it, it's easy to switch back without any code changes. gofumpt support is built into the tooling we use for development so this won't change development workflows. - golangci-lint includes a gofumpt check (enabled in this PR) - gopls, the LSP for Go, includes a gofumpt option (see [installation instrutions][3]) [3]: https://github.com/mvdan/gofumpt#installation This change was generated by running: ```bash gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error) ``` The following files were manually tweaked afterwards: - pkg/cmd/pulumi/stack_change_secrets_provider.go: one of the lines overflowed and had comments in an inconvenient place - pkg/cmd/pulumi/destroy.go: `var x T = y` where `T` wasn't necessary - pkg/cmd/pulumi/policy_new.go: long line because of error message - pkg/backend/snapshot_test.go: long line trying to assign three variables in the same assignment I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 16:36:39 +00:00
assert.NoError(t, os.Mkdir(pkgdir, 0o700))
// Write out a minimal package.json file that has at least one dependency.
packageJSONFilename := filepath.Join(pkgdir, "package.json")
packageJSON := []byte(`{
"name": "test-package",
"license": "MIT",
"dependencies": {
"@pulumi/pulumi": "latest"
}
}`)
all: Reformat with gofumpt Per team discussion, switching to gofumpt. [gofumpt][1] is an alternative, stricter alternative to gofmt. It addresses other stylistic concerns that gofmt doesn't yet cover. [1]: https://github.com/mvdan/gofumpt See the full list of [Added rules][2], but it includes: - Dropping empty lines around function bodies - Dropping unnecessary variable grouping when there's only one variable - Ensuring an empty line between multi-line functions - simplification (`-s` in gofmt) is always enabled - Ensuring multi-line function signatures end with `) {` on a separate line. [2]: https://github.com/mvdan/gofumpt#Added-rules gofumpt is stricter, but there's no lock-in. All gofumpt output is valid gofmt output, so if we decide we don't like it, it's easy to switch back without any code changes. gofumpt support is built into the tooling we use for development so this won't change development workflows. - golangci-lint includes a gofumpt check (enabled in this PR) - gopls, the LSP for Go, includes a gofumpt option (see [installation instrutions][3]) [3]: https://github.com/mvdan/gofumpt#installation This change was generated by running: ```bash gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error) ``` The following files were manually tweaked afterwards: - pkg/cmd/pulumi/stack_change_secrets_provider.go: one of the lines overflowed and had comments in an inconvenient place - pkg/cmd/pulumi/destroy.go: `var x T = y` where `T` wasn't necessary - pkg/cmd/pulumi/policy_new.go: long line because of error message - pkg/backend/snapshot_test.go: long line trying to assign three variables in the same assignment I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 16:36:39 +00:00
assert.NoError(t, os.WriteFile(packageJSONFilename, packageJSON, 0o600))
Use pnpm as package manager if we find a pnpm-lock.yaml file (#15456) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description Initial support pnpm. Note that this does not support pnpm workspaces yet. This also does not handle passing the package manager through from `pulumi new`. Once a user manually runs pnpm, creating a pnpm-lock.yaml, we'll detect that and pnpm. Fixes https://github.com/pulumi/pulumi/issues/15455 ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-21 13:41:21 +00:00
writeLockFile(t, pkgdir, packageManager)
// Install dependencies, passing nil for stdout and stderr, which connects
// them to the file descriptor for the null device (os.DevNull).
Fix merge failures #2 (#15543) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes https://github.com/pulumi/pulumi/issues/15528 See https://github.com/pulumi/pulumi/pull/15540 & https://github.com/pulumi/pulumi/pull/15531 Re-creating this as a PR with `ci/test` label so we can get it merged. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. --> --------- Co-authored-by: Justin Van Patten <jvp@justinvp.com> Co-authored-by: Anton Tayanovskyy <anton@pulumi.com> Co-authored-by: Thomas Gummerer <t.gummerer@gmail.com>
2024-02-29 21:06:24 +00:00
ptesting.YarnInstallMutex.Lock()
defer ptesting.YarnInstallMutex.Unlock()
out := iotest.LogWriter(t)
bin, err := Install(context.Background(), AutoPackageManager, pkgdir, production, out, out)
assert.NoError(t, err)
Use pnpm as package manager if we find a pnpm-lock.yaml file (#15456) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description Initial support pnpm. Note that this does not support pnpm workspaces yet. This also does not handle passing the package manager through from `pulumi new`. Once a user manually runs pnpm, creating a pnpm-lock.yaml, we'll detect that and pnpm. Fixes https://github.com/pulumi/pulumi/issues/15455 ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-21 13:41:21 +00:00
assert.Equal(t, packageManager, bin)
}
// fakeNPMRegistry starts up an HTTP server that implements a subset of the NPM registry API
// that is sufficient for the tests in this file.
// The server will shut down when the test is complete.
//
// The server responds with fake information about a single package:
// @pulumi/pulumi.
//
// See https://github.com/npm/registry/blob/master/docs/REGISTRY-API.md for
// details on the protocol.
func fakeNPMRegistry(t testing.TB) string {
t.Helper()
// The server needs the tarball's SHA-1 hash so we'll build it in
// advance.
tarball, tarballSHA1 := tarballOf(t,
// The bare minimum files needed by NPM.
"package/package.json", `{
"name": "@pulumi/pulumi",
"license": "MIT"
}`)
var srv *httptest.Server
// Separate assignment so we can access srv.URL in the handler.
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Logf("[fakeNPMRegistry] %v %v", r.Method, r.URL.Path)
if r.Method != http.MethodGet {
// We only expect GET requests.
http.NotFound(w, r)
return
}
switch r.URL.Path {
case "/@pulumi/pulumi":
tarballURL := srv.URL + "/@pulumi/pulumi/-/pulumi-3.0.0.tgz"
fmt.Fprintf(w, `{
"name": "@pulumi/pulumi",
"dist-tags": {"latest": "3.0.0"},
"versions": {
"3.0.0": {
"name": "@pulumi/pulumi",
"version": "3.0.0",
"dist": {
"tarball": %q,
"shasum": %q
}
}
}
}`, tarballURL, tarballSHA1)
case "/@pulumi/pulumi/-/pulumi-3.0.0.tgz":
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Length", strconv.Itoa(len(tarball)))
_, err := w.Write(tarball)
if !assert.NoError(t, err) {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
default:
w.WriteHeader(http.StatusNotFound)
}
}))
t.Cleanup(srv.Close)
return srv.URL
}
// tarballOf constructs a .tar.gz archive containing the given files
// and returns the raw bytes and the SHA-1 hash of the archive.
//
// The files are specified as a list of pairs of paths and contents.
// The paths are relative to the root of the archive.
func tarballOf(t testing.TB, pairs ...string) (data []byte, sha string) {
t.Helper()
require.True(t, len(pairs)%2 == 0, "pairs must be a list of path/contents pairs")
var buff bytes.Buffer // raw .tar.gz bytes
hash := sha1.New() //nolint:gosec // this is what NPM wants
// Order of which writer wraps which is important here.
// .tar.gz means we need .gz to be the innermost writer.
gzipw := gzip.NewWriter(io.MultiWriter(&buff, hash))
tarw := tar.NewWriter(gzipw)
for i := 0; i < len(pairs); i += 2 {
path, contents := pairs[i], pairs[i+1]
require.NoError(t, tarw.WriteHeader(&tar.Header{
Name: path,
Mode: 0o600,
Size: int64(len(contents)),
}), "WriteHeader(%q)", path)
_, err := tarw.Write([]byte(contents))
require.NoError(t, err, "WriteContents(%q)", path)
}
// Closing the writers will flush them and write the final tarball bytes.
require.NoError(t, tarw.Close())
require.NoError(t, gzipw.Close())
return buff.Bytes(), hex.EncodeToString(hash.Sum(nil))
}
// writeFile creates a file at the given path with the given contents.
func writeFile(t testing.TB, path, contents string) {
t.Helper()
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o700))
require.NoError(t, os.WriteFile(path, []byte(contents), 0o600))
}