Debian Codegraph @

modules/npm/package_builder_integration_test.go

raw
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 func BenchmarkPackageFilesSum(b *testing.B) {
96 files := getPackageBuilderTestFiles()
97 bb := hugolib.Test(b, files)
98 fs := bb.H.Fs.WorkingDirReadOnly
99 b.ResetTimer()
100
101 for b.Loop() {
102 sum := npm.PackageFilesSum(fs, modules.Modules{})
103 bb.Assert(sum, qt.Equals, "528bb6507993888c")
104 }
105 }