-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathnpm_test.go
More file actions
153 lines (119 loc) · 4.72 KB
/
npm_test.go
File metadata and controls
153 lines (119 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package integration_test
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"github.com/cloudfoundry/switchblade"
"github.com/sclevine/spec"
. "github.com/cloudfoundry/switchblade/matchers"
. "github.com/onsi/gomega"
)
func testNPM(platform switchblade.Platform, fixtures string) func(*testing.T, spec.G, spec.S) {
return func(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
Eventually = NewWithT(t).Eventually
name string
)
it.Before(func() {
var err error
name, err = switchblade.RandomName()
Expect(err).NotTo(HaveOccurred())
})
it.After(func() {
Expect(platform.Delete.Execute(name)).To(Succeed())
})
it("successfully deploys and vendors the dependencies", func() {
source := filepath.Join(fixtures, "npm")
Expect(filepath.Join(source, "node_modules")).NotTo(BeADirectory())
deployment, logs, err := platform.Deploy.Execute(name, source)
Expect(err).NotTo(HaveOccurred())
Expect(logs.String()).To(SatisfyAll(
MatchRegexp("PRO TIP:(.*) It is recommended to vendor the application's Node.js dependencies"),
ContainSubstring("Current dir: /tmp/app"),
ContainSubstring("Running heroku-prebuild (npm)"),
ContainSubstring("Running heroku-postbuild (npm)"),
))
Eventually(deployment).Should(Serve("Hello, World!"))
Eventually(deployment).Should(Serve(SatisfyAll(
ContainSubstring("Text: Hello Buildpacks Team"),
ContainSubstring("Text: Goodbye Buildpacks Team"),
)).WithEndpoint("/prepost"))
Eventually(deployment).Should(Serve(ContainSubstring("Successfully created mysql client")).WithEndpoint("/mysql"))
})
context("when a specific npm version is specified in the package.json", func() {
var source string
it.Before(func() {
var err error
source, err = switchblade.Source(filepath.Join(fixtures, "npm"))
Expect(err).NotTo(HaveOccurred())
file, err := os.OpenFile(filepath.Join(source, "package.json"), os.O_RDWR, 0600)
Expect(err).NotTo(HaveOccurred())
var pkg map[string]interface{}
Expect(json.NewDecoder(file).Decode(&pkg)).To(Succeed())
Expect(file.Close()).To(Succeed())
pkg["engines"] = map[string]string{"npm": "^8"}
// Remove cpu-features (native module) because npm 8's bundled
// node-gyp v9.1.0 requires distutils, which was removed in Python 3.12+.
// This test validates npm version selection, not native compilation.
deps := pkg["dependencies"].(map[string]interface{})
delete(deps, "cpu-features")
pkg["dependencies"] = deps
content, err := json.Marshal(pkg)
Expect(err).NotTo(HaveOccurred())
Expect(os.WriteFile(filepath.Join(source, "package.json"), content, 0600)).To(Succeed())
// Also remove the require('cpu-features') from server.js so the
// app doesn't crash at startup trying to load the missing module.
serverJS, err := os.ReadFile(filepath.Join(source, "server.js"))
Expect(err).NotTo(HaveOccurred())
serverJS = []byte(strings.Replace(string(serverJS), "const features = require('cpu-features')();\n", "", 1))
Expect(os.WriteFile(filepath.Join(source, "server.js"), serverJS, 0600)).To(Succeed())
})
it.After(func() {
Expect(os.RemoveAll(source)).To(Succeed())
})
it("uses the specified npm version", func() {
deployment, logs, err := platform.Deploy.
Execute(name, source)
Expect(err).NotTo(HaveOccurred())
Eventually(deployment).Should(Serve("Hello, World!"))
Expect(logs.String()).To(SatisfyAll(
ContainSubstring("engines.npm (package.json): ^8"),
ContainSubstring("Downloading and installing npm ^8"),
))
})
})
context("when there are unmet dependencies", func() {
var source string
it.Before(func() {
var err error
source, err = switchblade.Source(filepath.Join(fixtures, "npm"))
Expect(err).NotTo(HaveOccurred())
var pkg map[string]interface{}
content, err := os.ReadFile(filepath.Join(source, "package.json"))
Expect(err).NotTo(HaveOccurred())
Expect(json.Unmarshal(content, &pkg)).To(Succeed())
dependencies, ok := pkg["dependencies"].(map[string]interface{})
Expect(ok).To(BeTrue())
dependencies["grunt-steroids"] = "0.2.3"
pkg["dependencies"] = dependencies
content, err = json.Marshal(pkg)
Expect(err).NotTo(HaveOccurred())
Expect(os.WriteFile(filepath.Join(source, "package.json"), content, 0600)).To(Succeed())
})
it.After(func() {
Expect(os.RemoveAll(source)).To(Succeed())
})
it("prints a warning", func() {
_, logs, err := platform.Deploy.
Execute(name, source)
Expect(err).NotTo(HaveOccurred())
Expect(logs).To(ContainLines(
ContainSubstring("Unmet dependencies don't fail npm install but may cause runtime issues"),
))
})
})
}
}