Skip to content

Commit

Permalink
Add integration test for react apps
Browse files Browse the repository at this point in the history
  • Loading branch information
c0d1ngm0nk3y committed Jun 21, 2024
1 parent 7bce36c commit 3367c6c
Show file tree
Hide file tree
Showing 20 changed files with 415 additions and 26 deletions.
29 changes: 12 additions & 17 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,6 @@ func Build(entryResolver EntryResolver,
return packit.BuildResult{}, err
}

cacheFolder := filepath.Join(os.TempDir(), NODE_MODULES_CACHE)
err = os.Mkdir(cacheFolder, os.ModePerm)
if err != nil {
return packit.BuildResult{}, err
}

linkName := filepath.Join(layer.Path, "node_modules", ".cache")
err = os.RemoveAll(linkName)
if err != nil {
return packit.BuildResult{}, err
}

err = os.Symlink(cacheFolder, linkName)
if err != nil {
return packit.BuildResult{}, err
}

err = linker.Link(filepath.Join(projectPath, "node_modules"), filepath.Join(layer.Path, "node_modules"))
if err != nil {
return packit.BuildResult{}, err
Expand Down Expand Up @@ -268,6 +251,18 @@ func Build(entryResolver EntryResolver,
return packit.BuildResult{}, err
}

linkName := filepath.Join(layer.Path, "node_modules", ".cache")
err = os.RemoveAll(linkName)
if err != nil {
return packit.BuildResult{}, err
}

cacheFolder := filepath.Join(os.TempDir(), NODE_MODULES_CACHE)
err = os.Symlink(cacheFolder, linkName)
if err != nil {
return packit.BuildResult{}, err
}

if build {
err = symlinkResolver.Copy(filepath.Join(projectPath, "package-lock.json"), buildLayerPath, layer.Path)
if err != nil {
Expand Down
80 changes: 71 additions & 9 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
layersDir string
workingDir string
cnbDir string
tempDir string

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

tempDir := t.TempDir()
tempDir = t.TempDir()
t.Setenv("TMPDIR", tempDir)

t.Setenv("BP_NODE_PROJECT_PATH", "")
Expand Down Expand Up @@ -120,7 +121,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
environment,
symlinkResolver,
)

})

it.After(func() {
Expand Down Expand Up @@ -158,13 +158,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
buildLayer := result.Layers[0]
Expect(buildLayer.Name).To(Equal("build-modules"))
Expect(buildLayer.Path).To(Equal(filepath.Join(layersDir, "build-modules")))

nodeModuleCache := filepath.Join(layersDir, "build-modules", "node_modules", ".cache")
_, err = os.Stat(nodeModuleCache)
Expect(err).NotTo(HaveOccurred())
// FIXME: Why does this not work?
//Expect(info.Mode()&os.ModeSymlink == os.ModeSymlink).To(BeTrue())

Expect(buildLayer.SharedEnv).To(Equal(packit.Environment{}))
Expect(buildLayer.BuildEnv).To(Equal(packit.Environment{
"PATH.append": filepath.Join(layersDir, "build-modules", "node_modules", ".bin"),
Expand Down Expand Up @@ -432,6 +425,38 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Expect(symlinkResolver.ResolveCall.Receives.LockfilePath).To(Equal(filepath.Join(workingDir, "package-lock.json")))
Expect(symlinkResolver.ResolveCall.Receives.LayerPath).To(Equal(filepath.Join(layersDir, "launch-modules")))
})

it("symlinks node_modules/.cache to tmp/node_modules_cache in order to work for the run user", func() {
err := os.MkdirAll(filepath.Join(tempDir, "node_modules_cache", "temp-content"), os.ModePerm)
Expect(err).NotTo(HaveOccurred())
result, err := build(packit.BuildContext{
BuildpackInfo: packit.BuildpackInfo{
SBOMFormats: []string{"application/vnd.cyclonedx+json", "application/spdx+json", "application/vnd.syft+json"},
},
Platform: packit.Platform{
Path: "some-platform-path",
},
WorkingDir: workingDir,
Layers: packit.Layers{Path: layersDir},
CNBPath: cnbDir,
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{
{Name: "node_modules"},
},
},
})
Expect(err).NotTo(HaveOccurred())

Expect(len(result.Layers)).To(Equal(2))

launchLayer := result.Layers[0]
Expect(launchLayer.Name).To(Equal("launch-modules"))
Expect(launchLayer.Path).To(Equal(filepath.Join(layersDir, "launch-modules")))

linkedTmpContent := filepath.Join(launchLayer.Path, "node_modules", ".cache", "temp-content")
_, err = os.Stat(linkedTmpContent)
Expect(err).NotTo(HaveOccurred())
})
})

context("when node_modules is required at build and launch", func() {
Expand Down Expand Up @@ -681,6 +706,39 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Expect(symlinkResolver.CopyCall.Receives.SourceLayerPath).To(Equal(filepath.Join(buildLayer.Path)))
Expect(symlinkResolver.CopyCall.Receives.TargetLayerPath).To(Equal(filepath.Join(launchLayer.Path)))
})

it("symlinks node_modules/.cache to tmp/node_modules_cache in order to work for the run user", func() {
err := os.MkdirAll(filepath.Join(tempDir, "node_modules_cache", "temp-content"), os.ModePerm)
Expect(err).NotTo(HaveOccurred())
result, err := build(packit.BuildContext{
BuildpackInfo: packit.BuildpackInfo{
SBOMFormats: []string{"application/vnd.cyclonedx+json", "application/spdx+json", "application/vnd.syft+json"},
},
Platform: packit.Platform{
Path: "some-platform-path",
},
WorkingDir: workingDir,
Layers: packit.Layers{Path: layersDir},
CNBPath: cnbDir,
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{
{Name: "node_modules"},
},
},
})
Expect(err).NotTo(HaveOccurred())

Expect(len(result.Layers)).To(Equal(3))

launchLayer := result.Layers[1]
Expect(launchLayer.Name).To(Equal("launch-modules"))
Expect(launchLayer.Path).To(Equal(filepath.Join(layersDir, "launch-modules")))

linkedTmpContent := filepath.Join(launchLayer.Path, "node_modules", ".cache", "temp-content")
_, err = os.Stat(linkedTmpContent)
Expect(err).NotTo(HaveOccurred())
})

})

context("when one npmrc binding is detected", func() {
Expand Down Expand Up @@ -775,6 +833,10 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
if err != nil {
return err
}
err = os.MkdirAll(filepath.Join(ld, "node_modules"), os.ModePerm)
if err != nil {
return err
}

return nil
}
Expand Down
1 change: 1 addition & 0 deletions integration/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func TestIntegration(t *testing.T) {
suite("EmptyNodeModules", testEmptyNodeModules)
suite("Logging", testLogging)
suite("NativeModules", testNativeModules)
suite("NodeModulesCache", testReact)
suite("NoNodeModules", testNoNodeModules)
suite("Npmrc", testNpmrc)
suite("PackageLockMismatch", testPackageLockMismatch)
Expand Down
91 changes: 91 additions & 0 deletions integration/react_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package integration_test

import (
"os"
"path/filepath"
"testing"
"time"

"github.com/paketo-buildpacks/occam"
"github.com/sclevine/spec"

. "github.com/onsi/gomega"
. "github.com/paketo-buildpacks/occam/matchers"
)

func testReact(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
Eventually = NewWithT(t).Eventually

pack occam.Pack
docker occam.Docker
imageIDs map[string]struct{}
containerIDs map[string]struct{}

name string
source string

pullPolicy = "never"
)

it.Before(func() {
imageIDs = make(map[string]struct{})
containerIDs = make(map[string]struct{})

pack = occam.NewPack().WithNoColor()
docker = occam.NewDocker()

var err error
name, err = occam.RandomName()
Expect(err).NotTo(HaveOccurred())
})

it.After(func() {
for id := range containerIDs {
Expect(docker.Container.Remove.Execute(id)).To(Succeed())
}

for id := range imageIDs {
Expect(docker.Image.Remove.Execute(id)).To(Succeed())
}

Expect(os.RemoveAll(source)).To(Succeed())
})

context("when the app is a react app", func() {
it("works", func() {
var err error
source, err = occam.Source(filepath.Join("testdata", "react_app"))
Expect(err).NotTo(HaveOccurred())

build := pack.Build.
WithPullPolicy(pullPolicy).
WithBuildpacks(
settings.Buildpacks.NodeEngine.Online,
settings.Buildpacks.NPMInstall.Online,
settings.Buildpacks.BuildPlan.Online,
)

image, logs, err := build.Execute(name, source)
Expect(err).NotTo(HaveOccurred(), logs.String)

container, err := docker.Container.Run.
WithCommand("npm start").
WithEnv(map[string]string{"PORT": "8080"}).
WithPublish("8080").
Execute(image.ID)
Expect(err).NotTo(HaveOccurred())

containerIDs[container.ID] = struct{}{}

Eventually(container).Should(BeAvailable())

Eventually(func() string {
cLogs, err := docker.Container.Logs.Execute(container.ID)
Expect(err).NotTo(HaveOccurred())
return cLogs.String()
}).WithTimeout(3 * time.Second).Should(ContainSubstring("webpack compiled successfully"))
})
})
}
38 changes: 38 additions & 0 deletions integration/testdata/react_app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "react-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
11 changes: 11 additions & 0 deletions integration/testdata/react_app/plan.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[requires]]
name = "node"

[requires.metadata]
launch = true

[[requires]]
name = "node_modules"

[requires.metadata]
launch = true
Binary file added integration/testdata/react_app/public/favicon.ico
Binary file not shown.
43 changes: 43 additions & 0 deletions integration/testdata/react_app/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Binary file added integration/testdata/react_app/public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added integration/testdata/react_app/public/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions integration/testdata/react_app/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions integration/testdata/react_app/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
Loading

0 comments on commit 3367c6c

Please sign in to comment.