Skip to content

Commit

Permalink
test: add basic tests for npm with various versions of node
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Feb 27, 2024
1 parent db84512 commit 1b36500
Show file tree
Hide file tree
Showing 14 changed files with 197 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# Input hashes for repository rule npm_translate_lock(name = "npm", pnpm_lock = "//:pnpm-lock.yaml").
# This file should be checked into version control along with the pnpm-lock.yaml file.
.npmrc=-2065072158
pnpm-lock.yaml=1571246688
pnpm-lock.yaml=-863924260
examples/npm_deps/patches/meaning-of-life@1.0.0-pnpm.patch=-442666336
package.json=-275319675
pnpm-workspace.yaml=116986059
pnpm-workspace.yaml=2123448767
examples/js_binary/package.json=-41174383
examples/macro/package.json=857146175
examples/npm_deps/package.json=283109008
Expand All @@ -23,3 +23,4 @@ npm/private/test/vendored/semver-max/package.json=578664053
js/private/image/package.json=-1260474848
js/private/test/image/package.json=1295393035
js/private/test/js_run_devserver/package.json=-260856079
js/private/test/node_20_deps_issue/package.json=324243835
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ js/private/coverage/bundle/node_modules
js/private/image/node_modules
js/private/test/image/node_modules
js/private/test/js_run_devserver/node_modules
js/private/test/node_20_deps_issue/node_modules
js/private/worker/src/node_modules
node_modules/
npm/private/test/node_modules/
Expand Down
11 changes: 11 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ node_dev = use_extension(
dev_dependency = True,
)
use_repo(node_dev, "nodejs_toolchains")
use_repo(node_dev, "node20_linux_amd64")
use_repo(node_dev, "node20_darwin_arm64")
use_repo(node_dev, "node20_darwin_amd64")
use_repo(node_dev, "node20_linux_arm64")
use_repo(node_dev, "node20_linux_s390x")
use_repo(node_dev, "node20_linux_ppc64le")
use_repo(node_dev, "node20_windows_amd64")
use_repo(node_dev, "node18_linux_amd64")
use_repo(node_dev, "node18_darwin_arm64")
use_repo(node_dev, "node18_darwin_amd64")
Expand All @@ -96,6 +103,10 @@ node_dev.toolchain(
name = "node18",
node_version = "18.13.0",
)
node_dev.toolchain(
name = "node20",
node_version = "20.11.1",
)

############################################
# npm dependencies used by examples
Expand Down
5 changes: 5 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ nodejs_register_toolchains(
node_version = "18.13.0",
)

nodejs_register_toolchains(
name = "node20",
node_version = "20.11.1",
)

load("@bazel_skylib//lib:unittest.bzl", "register_unittest_toolchains")

register_unittest_toolchains()
Expand Down
6 changes: 6 additions & 0 deletions js/private/test/node-patches/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ TESTS = [
TOOLCHAINS_NAMES = [
"node16",
"node18",
"node20",
]

TOOLCHAINS_VERSIONS = [
Expand All @@ -27,6 +28,11 @@ TOOLCHAINS_VERSIONS = [
"@bazel_tools//src/conditions:darwin": "@node18_darwin_amd64//:node_toolchain",
"@bazel_tools//src/conditions:windows": "@node18_windows_amd64//:node_toolchain",
}),
select({
"@bazel_tools//src/conditions:linux_x86_64": "@node20_linux_amd64//:node_toolchain",
"@bazel_tools//src/conditions:darwin": "@node20_darwin_amd64//:node_toolchain",
"@bazel_tools//src/conditions:windows": "@node20_windows_amd64//:node_toolchain",
}),
]

# We need to copy the entry points to the bin so that the tests below don't follow the execroot
Expand Down
61 changes: 61 additions & 0 deletions js/private/test/node_20_deps_issue/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_run_binary")

npm_link_all_packages()

# Multiple node toolchains for testing across versions
TOOLCHAINS_NAMES = [
"node16",
"node18",
"node20",
]

TOOLCHAINS_VERSIONS = [
select({
"@bazel_tools//src/conditions:linux_x86_64": "@node16_linux_amd64//:node_toolchain",
"@bazel_tools//src/conditions:darwin": "@node16_darwin_amd64//:node_toolchain",
"@bazel_tools//src/conditions:windows": "@node16_windows_amd64//:node_toolchain",
}),
select({
"@bazel_tools//src/conditions:linux_x86_64": "@node18_linux_amd64//:node_toolchain",
"@bazel_tools//src/conditions:darwin": "@node18_darwin_amd64//:node_toolchain",
"@bazel_tools//src/conditions:windows": "@node18_windows_amd64//:node_toolchain",
}),
select({
"@bazel_tools//src/conditions:linux_x86_64": "@node20_linux_amd64//:node_toolchain",
"@bazel_tools//src/conditions:darwin": "@node20_darwin_amd64//:node_toolchain",
"@bazel_tools//src/conditions:windows": "@node20_windows_amd64//:node_toolchain",
}),
]

# A binary + run_binary + build_test for each node toolchain
[
[
js_binary(
name = "%s_maths" % toolchain_name,
testonly = True,
entry_point = "//js/private/test/node_20_deps_issue/packages/maths:__maths__entry_point",
node_toolchain = node_toolchain,
visibility = ["//visibility:private"],
),
js_run_binary(
name = "%s_bin" % toolchain_name,
testonly = True,
args = ["%s/%s_out" % (
package_name(),
toolchain_name,
)],
out_dirs = ["%s_out" % toolchain_name],
tool = ":%s_maths" % toolchain_name,
),
build_test(
name = "maths_%s_test" % toolchain_name,
targets = [":%s_bin" % toolchain_name],
),
]
for toolchain_name, node_toolchain in zip(
TOOLCHAINS_NAMES,
TOOLCHAINS_VERSIONS,
)
]
21 changes: 21 additions & 0 deletions js/private/test/node_20_deps_issue/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Vasilios Pantazopoulos

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 5 additions & 0 deletions js/private/test/node_20_deps_issue/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"mathjs": "^12.4.0"
}
}
25 changes: 25 additions & 0 deletions js/private/test/node_20_deps_issue/packages/maths/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
load("@aspect_bazel_lib//lib:directory_path.bzl", "directory_path")
load("@aspect_rules_js//npm:defs.bzl", "npm_package")

directory_path(
name = "__maths__entry_point",
testonly = True,
directory = ":maths_pkg",
path = "./bin/cli.js",
visibility = ["//js/private/test/node_20_deps_issue:__pkg__"],
)

npm_package(
name = "maths_pkg",
testonly = True,
srcs = glob([
"src/**",
"bin/**",
]) + [
":package.json",
],
data = [
"//js/private/test/node_20_deps_issue:node_modules/mathjs",
],
visibility = ["//visibility:private"],
)
5 changes: 5 additions & 0 deletions js/private/test/node_20_deps_issue/packages/maths/bin/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

import { main } from '../src/index.js'

main.apply(this, process.argv.slice(2))
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@node_20_deps_issue/maths",
"type": "module",
"description": "basic package representing an npm_package() importing pnpm package"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { evaluate } from 'mathjs'
import { writeFileSync } from 'fs'

export function main(outDir) {
writeFileSync(`${outDir}/output.txt`, `${evaluate('sin(45 deg) ^ 2')}`)
}
42 changes: 42 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ packages:
- 'js/private/image'
- 'js/private/test/image'
- 'js/private/test/js_run_devserver'
- 'js/private/test/node_20_deps_issue'
- 'npm/private/test'
- 'npm/private/test/npm_package'

0 comments on commit 1b36500

Please sign in to comment.