Debian Codegraph @

modules/npm/package_builder_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
15
16 import (
17 "strings"
18 "testing"
19
20 qt "github.com/frankban/quicktest"
21 )
22
23 const templ = `{
24 "name": "foo",
25 "version": "0.1.1",
26 "scripts": {},
27 "dependencies": {
28 "react-dom": "1.1.1",
29 "tailwindcss": "1.2.0",
30 "@babel/cli": "7.8.4",
31 "@babel/core": "7.9.0",
32 "@babel/preset-env": "7.9.5"
33 },
34 "devDependencies": {
35 "postcss-cli": "7.1.0",
36 "tailwindcss": "1.2.0",
37 "@babel/cli": "7.8.4",
38 "@babel/core": "7.9.0",
39 "@babel/preset-env": "7.9.5"
40 }
41 }`
42
43 func TestPackageBuilder(t *testing.T) {
44 c := qt.New(t)
45
46 b := &packageBuilder{
47 devDependencies: make(map[string]any),
48 devDependenciesComments: make(map[string]any),
49 dependencies: make(map[string]any),
50 dependenciesComments: make(map[string]any),
51 }
52 b.Add("", strings.NewReader(templ))
53 c.Assert(b.Err(), qt.IsNil)
54
55 b.Add("mymod", strings.NewReader(`{
56 "dependencies": {
57 "react-dom": "9.1.1",
58 "add1": "1.1.1"
59 },
60 "devDependencies": {
61 "tailwindcss": "error",
62 "add2": "2.1.1"
63 }
64 }`))
65
66 b.Add("mymod", strings.NewReader(`{
67 "dependencies": {
68 "react-dom": "error",
69 "add1": "error",
70 "add3": "3.1.1"
71 },
72 "devDependencies": {
73 "tailwindcss": "error",
74 "add2": "error",
75 "add4": "4.1.1"
76
77 }
78 }`))
79
80 c.Assert(b.Err(), qt.IsNil)
81
82 c.Assert(b.dependencies, qt.DeepEquals, map[string]any{
83 "@babel/cli": "7.8.4",
84 "add1": "1.1.1",
85 "add3": "3.1.1",
86 "@babel/core": "7.9.0",
87 "@babel/preset-env": "7.9.5",
88 "react-dom": "1.1.1",
89 "tailwindcss": "1.2.0",
90 })
91
92 c.Assert(b.devDependencies, qt.DeepEquals, map[string]any{
93 "tailwindcss": "1.2.0",
94 "@babel/cli": "7.8.4",
95 "@babel/core": "7.9.0",
96 "add2": "2.1.1",
97 "add4": "4.1.1",
98 "@babel/preset-env": "7.9.5",
99 "postcss-cli": "7.1.0",
100 })
101 }