Skip to content

Commit a3b4657

Browse files
committed
Add integration test for react apps
1 parent 5b23edc commit a3b4657

20 files changed

+415
-26
lines changed

build.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,6 @@ func Build(entryResolver EntryResolver,
136136
return packit.BuildResult{}, err
137137
}
138138

139-
cacheFolder := filepath.Join(os.TempDir(), NODE_MODULES_CACHE)
140-
err = os.Mkdir(cacheFolder, os.ModePerm)
141-
if err != nil {
142-
return packit.BuildResult{}, err
143-
}
144-
145-
linkName := filepath.Join(layer.Path, "node_modules", ".cache")
146-
err = os.RemoveAll(linkName)
147-
if err != nil {
148-
return packit.BuildResult{}, err
149-
}
150-
151-
err = os.Symlink(cacheFolder, linkName)
152-
if err != nil {
153-
return packit.BuildResult{}, err
154-
}
155-
156139
err = linker.Link(filepath.Join(projectPath, "node_modules"), filepath.Join(layer.Path, "node_modules"))
157140
if err != nil {
158141
return packit.BuildResult{}, err
@@ -268,6 +251,18 @@ func Build(entryResolver EntryResolver,
268251
return packit.BuildResult{}, err
269252
}
270253

254+
linkName := filepath.Join(layer.Path, "node_modules", ".cache")
255+
err = os.RemoveAll(linkName)
256+
if err != nil {
257+
return packit.BuildResult{}, err
258+
}
259+
260+
cacheFolder := filepath.Join(os.TempDir(), NODE_MODULES_CACHE)
261+
err = os.Symlink(cacheFolder, linkName)
262+
if err != nil {
263+
return packit.BuildResult{}, err
264+
}
265+
271266
if build {
272267
err = symlinkResolver.Copy(filepath.Join(projectPath, "package-lock.json"), buildLayerPath, layer.Path)
273268
if err != nil {

build_test.go

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
2828
layersDir string
2929
workingDir string
3030
cnbDir string
31+
tempDir string
3132

3233
processLayerDir string
3334
processWorkingDir string
@@ -60,7 +61,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
6061
cnbDir, err = os.MkdirTemp("", "cnb")
6162
Expect(err).NotTo(HaveOccurred())
6263

63-
tempDir := t.TempDir()
64+
tempDir = t.TempDir()
6465
t.Setenv("TMPDIR", tempDir)
6566

6667
t.Setenv("BP_NODE_PROJECT_PATH", "")
@@ -120,7 +121,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
120121
environment,
121122
symlinkResolver,
122123
)
123-
124124
})
125125

126126
it.After(func() {
@@ -158,13 +158,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
158158
buildLayer := result.Layers[0]
159159
Expect(buildLayer.Name).To(Equal("build-modules"))
160160
Expect(buildLayer.Path).To(Equal(filepath.Join(layersDir, "build-modules")))
161-
162-
nodeModuleCache := filepath.Join(layersDir, "build-modules", "node_modules", ".cache")
163-
_, err = os.Stat(nodeModuleCache)
164-
Expect(err).NotTo(HaveOccurred())
165-
// FIXME: Why does this not work?
166-
//Expect(info.Mode()&os.ModeSymlink == os.ModeSymlink).To(BeTrue())
167-
168161
Expect(buildLayer.SharedEnv).To(Equal(packit.Environment{}))
169162
Expect(buildLayer.BuildEnv).To(Equal(packit.Environment{
170163
"PATH.append": filepath.Join(layersDir, "build-modules", "node_modules", ".bin"),
@@ -432,6 +425,38 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
432425
Expect(symlinkResolver.ResolveCall.Receives.LockfilePath).To(Equal(filepath.Join(workingDir, "package-lock.json")))
433426
Expect(symlinkResolver.ResolveCall.Receives.LayerPath).To(Equal(filepath.Join(layersDir, "launch-modules")))
434427
})
428+
429+
it("symlinks node_modules/.cache to tmp/node_modules_cache in order to work for the run user", func() {
430+
err := os.MkdirAll(filepath.Join(tempDir, "node_modules_cache", "temp-content"), os.ModePerm)
431+
Expect(err).NotTo(HaveOccurred())
432+
result, err := build(packit.BuildContext{
433+
BuildpackInfo: packit.BuildpackInfo{
434+
SBOMFormats: []string{"application/vnd.cyclonedx+json", "application/spdx+json", "application/vnd.syft+json"},
435+
},
436+
Platform: packit.Platform{
437+
Path: "some-platform-path",
438+
},
439+
WorkingDir: workingDir,
440+
Layers: packit.Layers{Path: layersDir},
441+
CNBPath: cnbDir,
442+
Plan: packit.BuildpackPlan{
443+
Entries: []packit.BuildpackPlanEntry{
444+
{Name: "node_modules"},
445+
},
446+
},
447+
})
448+
Expect(err).NotTo(HaveOccurred())
449+
450+
Expect(len(result.Layers)).To(Equal(2))
451+
452+
launchLayer := result.Layers[0]
453+
Expect(launchLayer.Name).To(Equal("launch-modules"))
454+
Expect(launchLayer.Path).To(Equal(filepath.Join(layersDir, "launch-modules")))
455+
456+
linkedTmpContent := filepath.Join(launchLayer.Path, "node_modules", ".cache", "temp-content")
457+
_, err = os.Stat(linkedTmpContent)
458+
Expect(err).NotTo(HaveOccurred())
459+
})
435460
})
436461

437462
context("when node_modules is required at build and launch", func() {
@@ -681,6 +706,39 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
681706
Expect(symlinkResolver.CopyCall.Receives.SourceLayerPath).To(Equal(filepath.Join(buildLayer.Path)))
682707
Expect(symlinkResolver.CopyCall.Receives.TargetLayerPath).To(Equal(filepath.Join(launchLayer.Path)))
683708
})
709+
710+
it("symlinks node_modules/.cache to tmp/node_modules_cache in order to work for the run user", func() {
711+
err := os.MkdirAll(filepath.Join(tempDir, "node_modules_cache", "temp-content"), os.ModePerm)
712+
Expect(err).NotTo(HaveOccurred())
713+
result, err := build(packit.BuildContext{
714+
BuildpackInfo: packit.BuildpackInfo{
715+
SBOMFormats: []string{"application/vnd.cyclonedx+json", "application/spdx+json", "application/vnd.syft+json"},
716+
},
717+
Platform: packit.Platform{
718+
Path: "some-platform-path",
719+
},
720+
WorkingDir: workingDir,
721+
Layers: packit.Layers{Path: layersDir},
722+
CNBPath: cnbDir,
723+
Plan: packit.BuildpackPlan{
724+
Entries: []packit.BuildpackPlanEntry{
725+
{Name: "node_modules"},
726+
},
727+
},
728+
})
729+
Expect(err).NotTo(HaveOccurred())
730+
731+
Expect(len(result.Layers)).To(Equal(3))
732+
733+
launchLayer := result.Layers[1]
734+
Expect(launchLayer.Name).To(Equal("launch-modules"))
735+
Expect(launchLayer.Path).To(Equal(filepath.Join(layersDir, "launch-modules")))
736+
737+
linkedTmpContent := filepath.Join(launchLayer.Path, "node_modules", ".cache", "temp-content")
738+
_, err = os.Stat(linkedTmpContent)
739+
Expect(err).NotTo(HaveOccurred())
740+
})
741+
684742
})
685743

686744
context("when one npmrc binding is detected", func() {
@@ -775,6 +833,10 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
775833
if err != nil {
776834
return err
777835
}
836+
err = os.MkdirAll(filepath.Join(ld, "node_modules"), os.ModePerm)
837+
if err != nil {
838+
return err
839+
}
778840

779841
return nil
780842
}

integration/init_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func TestIntegration(t *testing.T) {
126126
suite("EmptyNodeModules", testEmptyNodeModules)
127127
suite("Logging", testLogging)
128128
suite("NativeModules", testNativeModules)
129+
suite("NodeModulesCache", testReact)
129130
suite("NoNodeModules", testNoNodeModules)
130131
suite("Npmrc", testNpmrc)
131132
suite("PackageLockMismatch", testPackageLockMismatch)

integration/react_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package integration_test
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
"time"
8+
9+
"github.com/paketo-buildpacks/occam"
10+
"github.com/sclevine/spec"
11+
12+
. "github.com/onsi/gomega"
13+
. "github.com/paketo-buildpacks/occam/matchers"
14+
)
15+
16+
func testReact(t *testing.T, context spec.G, it spec.S) {
17+
var (
18+
Expect = NewWithT(t).Expect
19+
Eventually = NewWithT(t).Eventually
20+
21+
pack occam.Pack
22+
docker occam.Docker
23+
imageIDs map[string]struct{}
24+
containerIDs map[string]struct{}
25+
26+
name string
27+
source string
28+
29+
pullPolicy = "never"
30+
)
31+
32+
it.Before(func() {
33+
imageIDs = make(map[string]struct{})
34+
containerIDs = make(map[string]struct{})
35+
36+
pack = occam.NewPack().WithNoColor()
37+
docker = occam.NewDocker()
38+
39+
var err error
40+
name, err = occam.RandomName()
41+
Expect(err).NotTo(HaveOccurred())
42+
})
43+
44+
it.After(func() {
45+
for id := range containerIDs {
46+
Expect(docker.Container.Remove.Execute(id)).To(Succeed())
47+
}
48+
49+
for id := range imageIDs {
50+
Expect(docker.Image.Remove.Execute(id)).To(Succeed())
51+
}
52+
53+
Expect(os.RemoveAll(source)).To(Succeed())
54+
})
55+
56+
context("when the app is a react app", func() {
57+
it("works", func() {
58+
var err error
59+
source, err = occam.Source(filepath.Join("testdata", "react_app"))
60+
Expect(err).NotTo(HaveOccurred())
61+
62+
build := pack.Build.
63+
WithPullPolicy(pullPolicy).
64+
WithBuildpacks(
65+
settings.Buildpacks.NodeEngine.Online,
66+
settings.Buildpacks.NPMInstall.Online,
67+
settings.Buildpacks.BuildPlan.Online,
68+
)
69+
70+
image, logs, err := build.Execute(name, source)
71+
Expect(err).NotTo(HaveOccurred(), logs.String)
72+
73+
container, err := docker.Container.Run.
74+
WithCommand("npm start").
75+
WithEnv(map[string]string{"PORT": "8080"}).
76+
WithPublish("8080").
77+
Execute(image.ID)
78+
Expect(err).NotTo(HaveOccurred())
79+
80+
containerIDs[container.ID] = struct{}{}
81+
82+
Eventually(container).Should(BeAvailable())
83+
84+
Eventually(func() string {
85+
cLogs, err := docker.Container.Logs.Execute(container.ID)
86+
Expect(err).NotTo(HaveOccurred())
87+
return cLogs.String()
88+
}).WithTimeout(3 * time.Second).Should(ContainSubstring("webpack compiled successfully"))
89+
})
90+
})
91+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "react-app",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@testing-library/jest-dom": "^5.17.0",
7+
"@testing-library/react": "^13.4.0",
8+
"@testing-library/user-event": "^13.5.0",
9+
"react": "^18.2.0",
10+
"react-dom": "^18.2.0",
11+
"react-scripts": "5.0.1",
12+
"web-vitals": "^2.1.4"
13+
},
14+
"scripts": {
15+
"start": "react-scripts start",
16+
"build": "react-scripts build",
17+
"test": "react-scripts test",
18+
"eject": "react-scripts eject"
19+
},
20+
"eslintConfig": {
21+
"extends": [
22+
"react-app",
23+
"react-app/jest"
24+
]
25+
},
26+
"browserslist": {
27+
"production": [
28+
">0.2%",
29+
"not dead",
30+
"not op_mini all"
31+
],
32+
"development": [
33+
"last 1 chrome version",
34+
"last 1 firefox version",
35+
"last 1 safari version"
36+
]
37+
}
38+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[[requires]]
2+
name = "node"
3+
4+
[requires.metadata]
5+
launch = true
6+
7+
[[requires]]
8+
name = "node_modules"
9+
10+
[requires.metadata]
11+
launch = true
3.78 KB
Binary file not shown.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<meta
9+
name="description"
10+
content="Web site created using create-react-app"
11+
/>
12+
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13+
<!--
14+
manifest.json provides metadata used when your web app is installed on a
15+
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
16+
-->
17+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18+
<!--
19+
Notice the use of %PUBLIC_URL% in the tags above.
20+
It will be replaced with the URL of the `public` folder during the build.
21+
Only files inside the `public` folder can be referenced from the HTML.
22+
23+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
24+
work correctly both with client-side routing and a non-root public URL.
25+
Learn how to configure a non-root public URL by running `npm run build`.
26+
-->
27+
<title>React App</title>
28+
</head>
29+
<body>
30+
<noscript>You need to enable JavaScript to run this app.</noscript>
31+
<div id="root"></div>
32+
<!--
33+
This HTML file is a template.
34+
If you open it directly in the browser, you will see an empty page.
35+
36+
You can add webfonts, meta tags, or analytics to this file.
37+
The build step will place the bundled scripts into the <body> tag.
38+
39+
To begin the development, run `npm start` or `yarn start`.
40+
To create a production bundle, use `npm run build` or `yarn build`.
41+
-->
42+
</body>
43+
</html>
5.22 KB
Loading
9.44 KB
Loading

0 commit comments

Comments
 (0)