From 1f1bb7598c8ebaf40318e5fdccdf3e2a375c8ebc Mon Sep 17 00:00:00 2001 From: Jamie Kinkead <41454626+jkisk@users.noreply.github.com> Date: Wed, 29 Jan 2020 16:02:44 -0800 Subject: [PATCH] Fix for windows policy-pack install (#3837) * Fix for windows policy-pack install * Add test for npm install --- pkg/npm/npm.go | 4 ++- pkg/npm/npm_test.go | 65 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 pkg/npm/npm_test.go diff --git a/pkg/npm/npm.go b/pkg/npm/npm.go index 5f8cd14bf..916455e1f 100644 --- a/pkg/npm/npm.go +++ b/pkg/npm/npm.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "os" "os/exec" + "path/filepath" "strings" uuid "github.com/satori/go.uuid" @@ -75,7 +76,8 @@ func Install(dir string, stdout, stderr io.Writer) (string, error) { } // Ensure the "node_modules" directory exists. - if _, err := os.Stat("node_modules"); os.IsNotExist(err) { + nodeModulesPath := filepath.Join(dir, "node_modules") + if _, err := os.Stat(nodeModulesPath); os.IsNotExist(err) { return bin, errors.Errorf("%s install reported success, but node_modules directory is missing", bin) } diff --git a/pkg/npm/npm_test.go b/pkg/npm/npm_test.go new file mode 100644 index 000000000..1f673bcc2 --- /dev/null +++ b/pkg/npm/npm_test.go @@ -0,0 +1,65 @@ +// 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 ( + "io/ioutil" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNPMInstall(t *testing.T) { + testInstall(t, "npm") +} + +func TestYarnInstall(t *testing.T) { + os.Setenv("PULUMI_PREFER_YARN", "true") + testInstall(t, "yarn") +} + +func testInstall(t *testing.T, expectedBin string) { + // Skip during short test runs since this test involves downloading dependencies. + if testing.Short() { + t.Skip("Skipped in short test run") + } + + // Create a new empty test directory and change the current working directory to it. + tempdir, _ := ioutil.TempDir("", "test-env") + defer os.RemoveAll(tempdir) + assert.NoError(t, os.Chdir(tempdir)) + + // Create a package directory to install dependencies into. + pkgdir := filepath.Join(tempdir, "package") + assert.NoError(t, os.Mkdir(pkgdir, 0700)) + + // Write out a minimal package.json file that has at least one dependency. + packageJSONFilename := filepath.Join(pkgdir, "package.json") + packageJSON := []byte(`{ + "name": "test-package", + "dependencies": { + "@pulumi/pulumi": "^1.0.0" + } + }`) + assert.NoError(t, ioutil.WriteFile(packageJSONFilename, packageJSON, 0644)) + + // Install dependencies, passing nil for stdout and stderr, which connects + // them to the file descriptor for the null device (os.DevNull). + bin, err := Install(pkgdir, nil, nil) + assert.NoError(t, err) + assert.Equal(t, expectedBin, bin) +}