pulumi/sdk/nodejs/npm/npm_test.go

69 lines
2.0 KiB
Go
Raw Normal View History

2023-05-24 17:18:22 +00:00
// Copyright 2016-2023, 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 (
"context"
"fmt"
2023-05-26 16:25:28 +00:00
"path/filepath"
2023-05-26 18:38:40 +00:00
"strings"
2023-05-24 17:18:22 +00:00
"testing"
"github.com/stretchr/testify/assert"
)
// This test checks that os.exec call for `npm install` is well constructed.
func TestNPMInstallCmd(t *testing.T) {
t.Parallel()
cases := []struct {
production bool
expectedArgs []string
}{
{
production: true,
2023-05-26 16:25:28 +00:00
expectedArgs: []string{"false", "install", "--loglevel=error", "--production"},
2023-05-24 17:18:22 +00:00
}, {
production: false,
2023-05-26 16:25:28 +00:00
expectedArgs: []string{"false", "install", "--loglevel=error"},
2023-05-24 17:18:22 +00:00
},
}
pkgManager := &npmManager{
executable: "false", // a fake path for testing.
2023-05-24 17:18:22 +00:00
}
ctx := context.Background()
for _, tc := range cases {
tc := tc
name := fmt.Sprintf("production=%v", tc.production)
t.Run(name, func(tt *testing.T) {
tt.Parallel()
command := pkgManager.installCmd(ctx, tc.production)
// Compare our expectations against observations.
expected := tc.expectedArgs
observed := command.Args
assert.ElementsMatch(t, expected, observed)
2023-05-26 18:38:40 +00:00
// Next, we check if the binary name matches our expectations.
// Trim the absolute path, since it's system dependent.
observedCommand := filepath.Base(command.Path)
// Trim the extension, which will appear on Windows systems.
if extension := filepath.Ext(observedCommand); extension != "" {
2023-05-29 13:16:23 +00:00
observedCommand = strings.TrimSuffix(observedCommand, extension)
2023-05-26 18:38:40 +00:00
}
assert.Equal(t, "false", observedCommand)
2023-05-24 17:18:22 +00:00
})
}
}