Files
pack/core/providers/node/package_manager_test.go
T
zeekay 7844a60aff refactor(pack): modernize to github.com/hanzoai/pack and remove upstream branding
- Migrate module path to github.com/hanzoai/pack across all Go packages
- Remove legacy upstream docs and competitor brand references
- Modernize LLM.md and mise.toml for pristine Hanzo Pack builder
2026-09-04 00:40:42 -07:00

146 lines
4.2 KiB
Go

package node
import (
"os"
"path/filepath"
"testing"
"github.com/hanzoai/pack/core/plan"
"github.com/hanzoai/pack/core/resolver"
testingUtils "github.com/hanzoai/pack/core/testing"
"github.com/stretchr/testify/require"
)
func TestInstallDeps_NpmInstallOverride(t *testing.T) {
tmpDir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "package.json"), []byte("{}"), 0o644))
require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "package-lock.json"), []byte("{}"), 0o644))
ctx := testingUtils.CreateGenerateContext(t, tmpDir)
ctx.Env.Variables["RAILPACK_NODE_NPM_INSTALL"] = "npm ci"
install := ctx.NewCommandStep("install")
PackageManagerNpm.installDeps(ctx, install, false)
require.Contains(t, install.Commands, plan.NewExecCommand("npm ci"))
}
func TestGetPackageManagerPackages_PnpmLockfileVersion(t *testing.T) {
tests := []struct {
name string
lockfile string
wantVersion string
wantSource string
}{
{
name: "lockfile 5.3",
lockfile: "lockfileVersion: 5.3\n",
wantVersion: "6",
wantSource: "pnpm-lock.yaml",
},
{
name: "lockfile 5.4",
lockfile: "lockfileVersion: 5.4\n",
wantVersion: "7",
wantSource: "pnpm-lock.yaml",
},
{
name: "lockfile 6.0 quoted",
lockfile: "lockfileVersion: '6.0'\n",
wantVersion: "8",
wantSource: "pnpm-lock.yaml",
},
{
name: "lockfile 6.0 unquoted",
lockfile: "lockfileVersion: 6.0\n",
wantVersion: "8",
wantSource: "pnpm-lock.yaml",
},
{
name: "lockfile 6.1",
lockfile: "lockfileVersion: '6.1'\n",
wantVersion: "8",
wantSource: "pnpm-lock.yaml",
},
{
name: "lockfile 9.0 quoted",
lockfile: "lockfileVersion: '9.0'\n",
wantVersion: "9",
wantSource: "pnpm-lock.yaml",
},
{
name: "lockfile 9.0 unquoted",
lockfile: "lockfileVersion: 9.0\n",
wantVersion: "9",
wantSource: "pnpm-lock.yaml",
},
{
name: "unknown lockfile keeps default",
lockfile: "lockfileVersion: '99.0'\n",
wantVersion: DEFAULT_PNPM_VERSION,
wantSource: resolver.DefaultSource,
},
{
name: "no lockfile keeps default",
lockfile: "",
wantVersion: DEFAULT_PNPM_VERSION,
wantSource: resolver.DefaultSource,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "package.json"), []byte("{}"), 0o644))
if tt.lockfile != "" {
require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "pnpm-lock.yaml"), []byte(tt.lockfile), 0o644))
}
ctx := testingUtils.CreateGenerateContext(t, tmpDir)
miseStep := ctx.NewMiseStepBuilder("test")
PackageManagerPnpm.GetPackageManagerPackages(ctx, NewPackageJson(), miseStep)
pnpm := ctx.Resolver.Get("pnpm")
require.NotNil(t, pnpm)
require.Equal(t, tt.wantVersion, pnpm.Version)
require.Equal(t, tt.wantSource, pnpm.Source)
})
}
}
func TestGetPackageManagerPackages_PnpmVersionPrecedence(t *testing.T) {
tmpDir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "package.json"), []byte("{}"), 0o644))
require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "pnpm-lock.yaml"), []byte("lockfileVersion: '6.0'\n"), 0o644))
t.Run("engines override lockfile", func(t *testing.T) {
ctx := testingUtils.CreateGenerateContext(t, tmpDir)
miseStep := ctx.NewMiseStepBuilder("test")
packageJson := NewPackageJson()
packageJson.Engines["pnpm"] = "10"
PackageManagerPnpm.GetPackageManagerPackages(ctx, packageJson, miseStep)
pnpm := ctx.Resolver.Get("pnpm")
require.NotNil(t, pnpm)
require.Equal(t, "10", pnpm.Version)
require.Equal(t, "package.json > engines > pnpm", pnpm.Source)
})
t.Run("packageManager overrides lockfile", func(t *testing.T) {
ctx := testingUtils.CreateGenerateContext(t, tmpDir)
miseStep := ctx.NewMiseStepBuilder("test")
pm := "[email protected]"
packageJson := &PackageJson{PackageManager: &pm}
PackageManagerPnpm.GetPackageManagerPackages(ctx, packageJson, miseStep)
pnpm := ctx.Resolver.Get("pnpm")
require.NotNil(t, pnpm)
require.Equal(t, "10.4.1", pnpm.Version)
require.Equal(t, "package.json > packageManager", pnpm.Source)
require.True(t, pnpm.SkipMiseInstall)
})
}