diff --git a/integration/integration_test.go b/integration/integration_test.go index a8bc0661..1997f74d 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -1,7 +1,12 @@ package integration import ( + "bytes" + "io" + "os" + "os/exec" "path/filepath" + "regexp" "testing" "github.com/cloudfoundry/dagger" @@ -75,4 +80,41 @@ func testIntegration(t *testing.T, when spec.G, it spec.S) { Expect(body).To(ContainSubstring("Package A value 2")) }) }) + + when("the app is pushed twice", func() { + it("does not reinstall node_modules", func() { + appDir := filepath.Join("testdata", "simple_app") + app, err := dagger.PackBuild(appDir, nodeBP, bp) + Expect(err).ToNot(HaveOccurred()) + defer app.Destroy() + + Expect(app.BuildLogs()).To(MatchRegexp("node_modules .*: Contributing to layer")) + + buildLogs := &bytes.Buffer{} + + // TODO: Move this to dagger + + _, imageID, _, err := app.Info() + Expect(err).NotTo(HaveOccurred()) + + cmd := exec.Command("pack", "build", imageID, "--builder", "cfbuildpacks/cflinuxfs3-cnb-test-builder", "--clear-cache", "--buildpack", nodeBP, "--buildpack", bp) + cmd.Dir = appDir + cmd.Stdout = io.MultiWriter(os.Stdout, buildLogs) + cmd.Stderr = io.MultiWriter(os.Stderr, buildLogs) + Expect(cmd.Run()).To(Succeed()) + + const ansi = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))" + + re := regexp.MustCompile(ansi) + strippedLogs := re.ReplaceAllString(buildLogs.String(), "") + + Expect(strippedLogs).To(MatchRegexp("node_modules .*: Reusing cached layer")) + Expect(strippedLogs).NotTo(MatchRegexp("node_modules .*: Contributing to layer")) + + Expect(app.Start()).To(Succeed()) + + _, _, err = app.HTTPGet("/") + Expect(err).NotTo(HaveOccurred()) + }) + }) } diff --git a/modules/modules.go b/modules/modules.go index 0b864008..d66dae93 100644 --- a/modules/modules.go +++ b/modules/modules.go @@ -83,7 +83,7 @@ func NewContributor(context build.Build, pkgManager PackageManager) (Contributor } func (c Contributor) Contribute() error { - return c.modulesLayer.Contribute(c.Metadata, func(layer layers.Layer) error { + if err := c.modulesLayer.Contribute(c.Metadata, func(layer layers.Layer) error { offlineCache := filepath.Join(c.app.Root, "npm-packages-offline-cache") modulesDir := filepath.Join(c.modulesLayer.Root, yarn.ModulesDir) @@ -109,15 +109,6 @@ func (c Contributor) Contribute() error { return fmt.Errorf("unable make layer: %s", err.Error()) } - //nodeModules := filepath.Join(c.app.Root, yarn.ModulesDir) - //if err := helper.CopyDirectory(nodeModules, filepath.Join(layer.Root, yarn.ModulesDir)); err != nil { - // return fmt.Errorf(`unable to copy "%s" to "%s": %s`, nodeModules, layer.Root, err.Error()) - //} - - //if err := os.RemoveAll(nodeModules); err != nil { - // return fmt.Errorf("unable to remove node_modules from the app dir: %s", err.Error()) - //} - yarnCache := filepath.Join(c.app.Root, yarn.CacheDir) if err := helper.CopyDirectory(yarnCache, filepath.Join(layer.Root, yarn.CacheDir)); err != nil { return fmt.Errorf(`unable to copy "%s" to "%s": %s`, yarnCache, layer.Root, err.Error()) @@ -135,14 +126,14 @@ func (c Contributor) Contribute() error { return err } - if err := layer.OverrideSharedEnv("npm_config_nodedir", os.Getenv("NODE_HOME")); err != nil { - return err - } + return layer.OverrideSharedEnv("npm_config_nodedir", os.Getenv("NODE_HOME")) + }, c.flags()...); err != nil { + return err + } - return c.launchLayer.WriteApplicationMetadata(layers.Metadata{ - Processes: []layers.Process{{"web", "yarn start"}}, - }) - }, c.flags()...) + return c.launchLayer.WriteApplicationMetadata(layers.Metadata{ + Processes: []layers.Process{{"web", "yarn start"}}, + }) } func (c Contributor) flags() []layers.Flag { diff --git a/scripts/install_tools.sh b/scripts/install_tools.sh index 7d3b5400..856f9751 100755 --- a/scripts/install_tools.sh +++ b/scripts/install_tools.sh @@ -21,6 +21,8 @@ if [ "$#" -eq 1 ]; then fi install_pack() { + if [[ -f ".bin/pack" ]]; then return 0; fi + OS=$(uname -s) if [[ $OS == "Darwin" ]]; then @@ -75,11 +77,11 @@ expand() { rm $PACK_ARTIFACT } -configure_pack() { - pack add-stack org.cloudfoundry.stacks.cflinuxfs3 \ - --build-image cfbuildpacks/cflinuxfs3-cnb-experimental:build \ - --run-image cfbuildpacks/cflinuxfs3-cnb-experimental:run || echo "Ignoring add stack error" -} +#configure_pack() { +# pack add-stack org.cloudfoundry.stacks.cflinuxfs3 \ +# --build-image cfbuildpacks/cflinuxfs3-cnb-experimental:build \ +# --run-image cfbuildpacks/cflinuxfs3-cnb-experimental:run || echo "Ignoring add stack error" +#} cd "$( dirname "${BASH_SOURCE[0]}" )/.." @@ -87,6 +89,6 @@ mkdir -p .bin export PATH=$(pwd)/.bin:$PATH install_pack -configure_pack +#configure_pack install_packager