| 1 |
// Copyright 2026 The Hugo Authors. All rights reserved. |
| 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 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language gxoverning permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package npm_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"strings" |
| 18 |
"testing" |
| 19 |
|
| 20 |
qt "github.com/frankban/quicktest" |
| 21 |
"github.com/gohugoio/hugo/hugolib" |
| 22 |
"github.com/gohugoio/hugo/modules" |
| 23 |
"github.com/gohugoio/hugo/modules/npm" |
| 24 |
"github.com/spf13/afero" |
| 25 |
) |
| 26 |
|
| 27 |
func getPackageBuilderTestFiles() string { |
| 28 |
files := ` |
| 29 |
-- hugo.toml -- |
| 30 |
-- package.json -- |
| 31 |
{ |
| 32 |
"workspaces": [ |
| 33 |
"packages/*" |
| 34 |
] |
| 35 |
} |
| 36 |
-- packages/hugoautogen/package.json -- |
| 37 |
-- packages/a/package.json -- |
| 38 |
PACKAGE_CONTENT |
| 39 |
-- packages/b/package.json -- |
| 40 |
PACKAGE_CONTENT |
| 41 |
-- packages/c/package.json -- |
| 42 |
PACKAGE_CONTENT |
| 43 |
-- packages/d/package.json -- |
| 44 |
PACKAGE_CONTENT |
| 45 |
-- packages/e/package.json -- |
| 46 |
PACKAGE_CONTENT |
| 47 |
` |
| 48 |
packageContent := `{ |
| 49 |
"name": "foo", |
| 50 |
"version": "0.1.1", |
| 51 |
"dependencies": { |
| 52 |
"react-dom": "1.1.1", |
| 53 |
"tailwindcss": "1.2.0", |
| 54 |
"@babel/cli": "7.8.4", |
| 55 |
"@babel/core": "7.9.0", |
| 56 |
"@babel/preset-env": "7.9.5" |
| 57 |
}, |
| 58 |
"devDependencies": { |
| 59 |
"postcss-cli": "7.1.0", |
| 60 |
"tailwindcss": "1.2.0", |
| 61 |
"@babel/cli": "7.8.4", |
| 62 |
"@babel/core": "7.9.0", |
| 63 |
"@babel/preset-env": "7.9.5" |
| 64 |
} |
| 65 |
}` |
| 66 |
files = strings.ReplaceAll(files, "PACKAGE_CONTENT", packageContent) |
| 67 |
return files |
| 68 |
} |
| 69 |
|
| 70 |
func TestPackageBuilder(t *testing.T) { |
| 71 |
files := getPackageBuilderTestFiles() |
| 72 |
b := hugolib.Test(t, files) |
| 73 |
fs := b.H.Fs.WorkingDirReadOnly |
| 74 |
sourceFs := b.H.BaseFs.ProjectSourceFs |
| 75 |
assetsFs := b.H.BaseFs.AssetsWithDuplicatesPreserved.Fs |
| 76 |
mods := b.H.Configs.Modules |
| 77 |
|
| 78 |
sum := npm.PackageFilesSum(fs, b.H.AllModules()) |
| 79 |
b.Assert(sum, qt.Equals, "528bb6507993888c") |
| 80 |
|
| 81 |
b.Assert(npm.Pack(sourceFs, assetsFs, mods), qt.IsNil) |
| 82 |
b.Assert(npm.NpmPackNeedsUpdate(sourceFs, mods), qt.IsFalse) |
| 83 |
|
| 84 |
meta1, err := afero.ReadFile(sourceFs, "packages/hugoautogen/hugo_packagemeta.json") |
| 85 |
b.Assert(err, qt.IsNil) |
| 86 |
|
| 87 |
b.Assert(npm.Pack(sourceFs, assetsFs, mods), qt.IsNil) |
| 88 |
b.Assert(npm.NpmPackNeedsUpdate(sourceFs, mods), qt.IsFalse) |
| 89 |
|
| 90 |
meta2, err := afero.ReadFile(sourceFs, "packages/hugoautogen/hugo_packagemeta.json") |
| 91 |
b.Assert(err, qt.IsNil) |
| 92 |
b.Assert(string(meta2), qt.Equals, string(meta1)) |
| 93 |
} |
| 94 |
|
| 95 |
// The generated workspace package.json should get a stable name and |
| 96 |
// "private": true, and preserve hand-set name/version/private on regeneration. |
| 97 |
// See issue 15245. |
| 98 |
func TestPackStableNameAndPrivate(t *testing.T) { |
| 99 |
files := getPackageBuilderTestFiles() |
| 100 |
b := hugolib.Test(t, files) |
| 101 |
sourceFs := b.H.BaseFs.ProjectSourceFs |
| 102 |
assetsFs := b.H.BaseFs.AssetsWithDuplicatesPreserved.Fs |
| 103 |
mods := b.H.Configs.Modules |
| 104 |
|
| 105 |
b.Assert(npm.Pack(sourceFs, assetsFs, mods), qt.IsNil) |
| 106 |
|
| 107 |
pkg, err := afero.ReadFile(sourceFs, "packages/hugoautogen/package.json") |
| 108 |
b.Assert(err, qt.IsNil) |
| 109 |
b.Assert(string(pkg), qt.Contains, `"name": "hugoautogen"`) |
| 110 |
b.Assert(string(pkg), qt.Contains, `"private": true`) |
| 111 |
b.Assert(string(pkg), qt.Contains, `"version": "0.1.0"`) |
| 112 |
|
| 113 |
// Hand-set fields survive a re-pack. |
| 114 |
edited := strings.NewReplacer( |
| 115 |
`"name": "hugoautogen"`, `"name": "@foo/hugoautogen"`, |
| 116 |
`"version": "0.1.0"`, `"version": "1.2.3"`, |
| 117 |
`"private": true`, `"private": false`, |
| 118 |
).Replace(string(pkg)) |
| 119 |
b.Assert(afero.WriteFile(sourceFs, "packages/hugoautogen/package.json", []byte(edited), 0o666), qt.IsNil) |
| 120 |
|
| 121 |
b.Assert(npm.Pack(sourceFs, assetsFs, mods), qt.IsNil) |
| 122 |
pkg, err = afero.ReadFile(sourceFs, "packages/hugoautogen/package.json") |
| 123 |
b.Assert(err, qt.IsNil) |
| 124 |
b.Assert(string(pkg), qt.Contains, `"name": "@foo/hugoautogen"`) |
| 125 |
b.Assert(string(pkg), qt.Contains, `"version": "1.2.3"`) |
| 126 |
b.Assert(string(pkg), qt.Contains, `"private": false`) |
| 127 |
} |
| 128 |
|
| 129 |
func BenchmarkPackageFilesSum(b *testing.B) { |
| 130 |
files := getPackageBuilderTestFiles() |
| 131 |
bb := hugolib.Test(b, files) |
| 132 |
fs := bb.H.Fs.WorkingDirReadOnly |
| 133 |
b.ResetTimer() |
| 134 |
|
| 135 |
for b.Loop() { |
| 136 |
sum := npm.PackageFilesSum(fs, modules.Modules{}) |
| 137 |
bb.Assert(sum, qt.Equals, "528bb6507993888c") |
| 138 |
} |
| 139 |
} |