Skip to content

Commit fdf1e9b

Browse files
committed
Add integration test for react apps
Co-authored-by: Ralf Pannemans <[email protected]>
1 parent 504aec4 commit fdf1e9b

20 files changed

+423
-26
lines changed

build.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -168,23 +168,6 @@ func Build(entryResolver EntryResolver,
168168
return packit.BuildResult{}, err
169169
}
170170

171-
cacheFolder := filepath.Join(os.TempDir(), NODE_MODULES_CACHE)
172-
err = os.Mkdir(cacheFolder, os.ModePerm)
173-
if err != nil {
174-
return packit.BuildResult{}, err
175-
}
176-
177-
linkName := filepath.Join(layer.Path, "node_modules", ".cache")
178-
err = os.RemoveAll(linkName)
179-
if err != nil {
180-
return packit.BuildResult{}, err
181-
}
182-
183-
err = os.Symlink(cacheFolder, linkName)
184-
if err != nil {
185-
return packit.BuildResult{}, err
186-
}
187-
188171
err = linker.Link(filepath.Join(projectPath, "node_modules"), filepath.Join(layer.Path, "node_modules"))
189172
if err != nil {
190173
return packit.BuildResult{}, err
@@ -301,6 +284,18 @@ func Build(entryResolver EntryResolver,
301284
return packit.BuildResult{}, err
302285
}
303286

287+
linkName := filepath.Join(layer.Path, "node_modules", ".cache")
288+
err = os.RemoveAll(linkName)
289+
if err != nil {
290+
return packit.BuildResult{}, err
291+
}
292+
293+
cacheFolder := filepath.Join(os.TempDir(), NODE_MODULES_CACHE)
294+
err = os.Symlink(cacheFolder, linkName)
295+
if err != nil {
296+
return packit.BuildResult{}, err
297+
}
298+
304299
if build {
305300
err = symlinkResolver.Copy(filepath.Join(projectPath, "package-lock.json"), buildLayerPath, layer.Path)
306301
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"),
@@ -434,6 +427,38 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
434427
Expect(symlinkResolver.ResolveCall.Receives.LockfilePath).To(Equal(filepath.Join(workingDir, "package-lock.json")))
435428
Expect(symlinkResolver.ResolveCall.Receives.LayerPath).To(Equal(filepath.Join(layersDir, "launch-modules")))
436429
})
430+
431+
it("symlinks node_modules/.cache to tmp/node_modules_cache in order to work for the run user", func() {
432+
err := os.MkdirAll(filepath.Join(tempDir, "node_modules_cache", "temp-content"), os.ModePerm)
433+
Expect(err).NotTo(HaveOccurred())
434+
result, err := build(packit.BuildContext{
435+
BuildpackInfo: packit.BuildpackInfo{
436+
SBOMFormats: []string{"application/vnd.cyclonedx+json", "application/spdx+json", "application/vnd.syft+json"},
437+
},
438+
Platform: packit.Platform{
439+
Path: "some-platform-path",
440+
},
441+
WorkingDir: workingDir,
442+
Layers: packit.Layers{Path: layersDir},
443+
CNBPath: cnbDir,
444+
Plan: packit.BuildpackPlan{
445+
Entries: []packit.BuildpackPlanEntry{
446+
{Name: "node_modules"},
447+
},
448+
},
449+
})
450+
Expect(err).NotTo(HaveOccurred())
451+
452+
Expect(len(result.Layers)).To(Equal(2))
453+
454+
launchLayer := result.Layers[0]
455+
Expect(launchLayer.Name).To(Equal("launch-modules"))
456+
Expect(launchLayer.Path).To(Equal(filepath.Join(layersDir, "launch-modules")))
457+
458+
linkedTmpContent := filepath.Join(launchLayer.Path, "node_modules", ".cache", "temp-content")
459+
_, err = os.Stat(linkedTmpContent)
460+
Expect(err).NotTo(HaveOccurred())
461+
})
437462
})
438463

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

690748
context("when one npmrc binding is detected", func() {
@@ -779,6 +837,10 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
779837
if err != nil {
780838
return err
781839
}
840+
err = os.MkdirAll(filepath.Join(ld, "node_modules"), os.ModePerm)
841+
if err != nil {
842+
return err
843+
}
782844

783845
return nil
784846
}

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: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
if settings.Extensions.UbiNodejsExtension.Online != "" {
44+
pullPolicy = "always"
45+
}
46+
47+
})
48+
49+
it.After(func() {
50+
for id := range containerIDs {
51+
Expect(docker.Container.Remove.Execute(id)).To(Succeed())
52+
}
53+
54+
for id := range imageIDs {
55+
Expect(docker.Image.Remove.Execute(id)).To(Succeed())
56+
}
57+
58+
Expect(os.RemoveAll(source)).To(Succeed())
59+
})
60+
61+
context("when the app is a react app", func() {
62+
it("works", func() {
63+
var err error
64+
source, err = occam.Source(filepath.Join("testdata", "react_app"))
65+
Expect(err).NotTo(HaveOccurred())
66+
67+
build := pack.Build.
68+
WithPullPolicy(pullPolicy).
69+
WithExtensions(
70+
settings.Extensions.UbiNodejsExtension.Online,
71+
).
72+
WithBuildpacks(
73+
settings.Buildpacks.NodeEngine.Online,
74+
settings.Buildpacks.NPMInstall.Online,
75+
settings.Buildpacks.BuildPlan.Online,
76+
)
77+
78+
image, logs, err := build.Execute(name, source)
79+
Expect(err).NotTo(HaveOccurred(), logs.String)
80+
81+
container, err := docker.Container.Run.
82+
WithCommand("npm start").
83+
WithEnv(map[string]string{"PORT": "8080"}).
84+
WithPublish("8080").
85+
Execute(image.ID)
86+
Expect(err).NotTo(HaveOccurred())
87+
88+
containerIDs[container.ID] = struct{}{}
89+
90+
Eventually(container).Should(BeAvailable())
91+
92+
Eventually(func() string {
93+
cLogs, err := docker.Container.Logs.Execute(container.ID)
94+
Expect(err).NotTo(HaveOccurred())
95+
return cLogs.String()
96+
}).WithTimeout(3 * time.Second).Should(ContainSubstring("webpack compiled successfully"))
97+
})
98+
})
99+
}
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)