github.com/pulumi/pulumi/pkg@v1.14.1/npm/npm_test.go (about)

     1  // Copyright 2016-2020, Pulumi Corporation.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package npm
    16  
    17  import (
    18  	"io/ioutil"
    19  	"os"
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestNPMInstall(t *testing.T) {
    27  	testInstall(t, "npm")
    28  }
    29  
    30  func TestYarnInstall(t *testing.T) {
    31  	os.Setenv("PULUMI_PREFER_YARN", "true")
    32  	testInstall(t, "yarn")
    33  }
    34  
    35  func testInstall(t *testing.T, expectedBin string) {
    36  	// Skip during short test runs since this test involves downloading dependencies.
    37  	if testing.Short() {
    38  		t.Skip("Skipped in short test run")
    39  	}
    40  
    41  	// Create a new empty test directory and change the current working directory to it.
    42  	tempdir, _ := ioutil.TempDir("", "test-env")
    43  	defer os.RemoveAll(tempdir)
    44  	assert.NoError(t, os.Chdir(tempdir))
    45  
    46  	// Create a package directory to install dependencies into.
    47  	pkgdir := filepath.Join(tempdir, "package")
    48  	assert.NoError(t, os.Mkdir(pkgdir, 0700))
    49  
    50  	// Write out a minimal package.json file that has at least one dependency.
    51  	packageJSONFilename := filepath.Join(pkgdir, "package.json")
    52  	packageJSON := []byte(`{
    53  	    "name": "test-package",
    54  	    "dependencies": {
    55  	        "@pulumi/pulumi": "^1.0.0"
    56  	    }
    57  	}`)
    58  	assert.NoError(t, ioutil.WriteFile(packageJSONFilename, packageJSON, 0644))
    59  
    60  	// Install dependencies, passing nil for stdout and stderr, which connects
    61  	// them to the file descriptor for the null device (os.DevNull).
    62  	bin, err := Install(pkgdir, nil, nil)
    63  	assert.NoError(t, err)
    64  	assert.Equal(t, expectedBin, bin)
    65  }