Skip to content

Commit

Permalink
Factor out npm install step of create-daml-app tests (digital-asset#1…
Browse files Browse the repository at this point in the history
…1294)

* Factor out npm install step of create-daml-app tests

This PR speeds up the create-daml-app tests by 30s or so by factoring
out the install of the unchanged npm deps into a genrule which we then
feed in again. This is a bit ugly but given how frequently we run
those tests I do think it is worth the uglyness.

changelog_begin
changelog_end

* .

changelog_begin
changelog_end

* .

changelog_begin
changelog_end
  • Loading branch information
cocreature authored Oct 20, 2021
1 parent 3a8b685 commit 17776f3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
43 changes: 39 additions & 4 deletions daml-assistant/integration-tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,55 @@ da_haskell_test(
],
)

genrule(
name = "create_daml_app_deps",
srcs = [
"//templates:create-daml-app/ui/package.json.template",
"//templates:create-daml-app-test-resources/testDeps.json",
],
outs = ["create_daml_app_deps.tar"],
cmd = """
set -eou pipefail
export PATH=$$PWD/$$(dirname $(location @nodejs//:node_bin)):$$PATH
DIR=$$(mktemp -d)
trap "rm -rf $$DIR" EXIT
mkdir -p $$DIR/cache
mkdir -p $$DIR/deps
# Generate a package.json based on the template with SDK deps dropped
# and merge in the test dependencies.
$(location @jq_dev_env//:bin/jq) -s \
'(.[0] | .name |= "create-daml-app" | .dependencies |= del(."@daml/ledger", ."@daml/react", ."@daml/types", ."@daml.js/__PROJECT_NAME__")) * .[1]' \
$(location //templates:create-daml-app/ui/package.json.template) \
$(location //templates:create-daml-app-test-resources/testDeps.json) > $$DIR/deps/package.json
NPM=$$PWD/$(location @nodejs//:npm_bin)
export NPM_CONFIG_CACHE=$$DIR/cache
(cd $$DIR/deps; $$NPM install --silent --no-update-notifier)
rm -rf $$DIR/deps/package.json
tar cf $(location create_daml_app_deps.tar) -C $$DIR deps
""",
tools = [
"@jq_dev_env//:bin/jq",
"@nodejs//:node_bin",
"@nodejs//:npm_bin",
],
) if not is_windows else None

da_haskell_library(
name = "create-daml-app-tests-lib",
srcs = ["src/DA/Daml/Assistant/CreateDamlAppTests.hs"],
data = [
"//release:sdk-release-tarball",
"@local_jdk//:bin/java.exe" if is_windows else "@local_jdk//:bin/java",
"@local_jdk//:bin/java",
"//:yarn",
"@patch_dev_env//:patch",
] + ([] if is_windows else ts_libraries + create_daml_app_resources),
":create_daml_app_deps.tar",
] + ts_libraries + create_daml_app_resources,
hackage_deps = [
"aeson",
"aeson-extra",
"base",
"bytestring",
"conduit",
"directory",
"extra",
"filepath",
Expand All @@ -198,7 +233,7 @@ da_haskell_library(
"//libs-haskell/da-hs-base",
"//libs-haskell/test-utils",
],
)
) if not is_windows else None

da_haskell_test(
name = "create-daml-app-tests-proj-name",
Expand All @@ -219,4 +254,4 @@ da_haskell_test(
# Exclusive until we stop hardcoding port numbers in index.test.ts
tags = ["exclusive"],
deps = [":create-daml-app-tests-lib"],
)
) if not is_windows else None
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
-- SPDX-License-Identifier: Apache-2.0
module DA.Daml.Assistant.CreateDamlAppTests (main) where

import Conduit
import Control.Exception.Extra
import Control.Monad
import Data.Aeson
import Data.Aeson.Extra.Merge
import qualified Data.ByteString.Lazy as BSL
import qualified Data.ByteString as BS
import qualified Data.Conduit.Tar.Extra as Tar.Conduit.Extra
import qualified Data.HashMap.Strict as HMS
import Data.List.Extra
import Data.Proxy (Proxy (..))
Expand Down Expand Up @@ -57,7 +59,7 @@ tests :: TestTree
tests =
withSdkResource $ \_ ->
askOption $ \(ProjectName projectName) -> do
testGroup "Create DAML App tests" [gettingStartedGuideTest projectName | not isWindows]
testGroup "Create DAML App tests" [gettingStartedGuideTest projectName]
where
gettingStartedGuideTest projectName = testCaseSteps "Getting Started Guide" $ \step ->
withTempDir $ \tmpDir' -> do
Expand Down Expand Up @@ -87,6 +89,11 @@ tests =
extraDepsFile <- locateRunfiles (mainWorkspace </> "templates" </> "create-daml-app-test-resources" </> "testDeps.json")
addTestDependencies (uiDir </> "package.json") extraDepsFile
step "Install dependencies for UI"
createDirectoryIfMissing True "node_modules"
cachedDeps <- locateRunfiles (mainWorkspace </> "daml-assistant" </> "integration-tests" </> "create_daml_app_deps.tar")
runConduitRes
$ sourceFileBS cachedDeps
.| Tar.Conduit.Extra.untar (Tar.Conduit.Extra.restoreFile (\a b -> fail (T.unpack $ a <> " " <> b)) ".")
retry 3 (callCommandSilent "npm-cli.js install")
step "Run linter"
callCommandSilent "npm-cli.js run-script lint -- --max-warnings 0"
Expand Down
5 changes: 4 additions & 1 deletion templates/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
# SPDX-License-Identifier: Apache-2.0
load("@os_info//:os_info.bzl", "is_windows")

exports_files(glob(["create-daml-app-test-resources/*"]) + ["copy-trigger/src/CopyTrigger.daml"])
exports_files(glob(["create-daml-app-test-resources/*"]) + [
"copy-trigger/src/CopyTrigger.daml",
"create-daml-app/ui/package.json.template",
])

# Split out into a separate rule so we can cheaply include this in the
# live-preview.
Expand Down

0 comments on commit 17776f3

Please sign in to comment.