-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(builtin): document how nodejs_binary#entry_point can use a direc… (
#2579) * feat(builtin): document how nodejs_binary#entry_point can use a directory * refactor: rename to directory_entry_point per discussion * refactor: make the feature more general, suitable to upstream to bazel-skylib
- Loading branch information
Showing
11 changed files
with
208 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"helpers for test assertions" | ||
|
||
load("@bazel_skylib//rules:diff_test.bzl", "diff_test") | ||
load("@bazel_skylib//rules:write_file.bzl", "write_file") | ||
load("@build_bazel_rules_nodejs//:index.bzl", "npm_package_bin") | ||
|
||
def assert_program_produces_stdout(name, tool, stdout, tags = []): | ||
write_file( | ||
name = "_write_expected_" + name, | ||
out = "expected_" + name, | ||
content = stdout, | ||
tags = tags, | ||
) | ||
|
||
npm_package_bin( | ||
name = "_write_actual_" + name, | ||
tool = tool, | ||
stdout = "actual_" + name, | ||
tags = tags, | ||
) | ||
|
||
diff_test( | ||
name = name, | ||
file1 = "expected_" + name, | ||
file2 = "actual_" + name, | ||
tags = tags, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
load("@bazel_skylib//rules:write_file.bzl", "write_file") | ||
load("@build_bazel_rules_nodejs//:index.bzl", "directory_file_path", "nodejs_binary", "pkg_npm") | ||
load("@npm//typescript:index.bzl", "tsc") | ||
load("//internal/common:assert.bzl", "assert_program_produces_stdout") | ||
|
||
filegroup( | ||
name = "ts_sources", | ||
srcs = [ | ||
"a.ts", | ||
"b.ts", | ||
], | ||
) | ||
|
||
# As the test fixture, produce a directory of js files | ||
tsc( | ||
name = "build", | ||
args = [ | ||
"$(execpaths :ts_sources)", | ||
"--outDir", | ||
"$(@D)", | ||
], | ||
data = [ | ||
":ts_sources", | ||
"@npm//@types/node", | ||
], | ||
output_dir = True, | ||
) | ||
|
||
############# | ||
# Test Case 1 | ||
# An pkg_npm can be an entry_point, using the standard npm semantics. | ||
# First, it needs to declare a "main" field in the package.json ... | ||
write_file( | ||
name = "write_package_json", | ||
out = "package.json", | ||
content = [json.encode({ | ||
"main": "build/a.js", | ||
})], | ||
) | ||
|
||
# ... then declare the package ... | ||
pkg_npm( | ||
name = "package", | ||
deps = [ | ||
"build", | ||
"package.json", | ||
], | ||
) | ||
|
||
# ... and finally run the program with the package as the entry_point. | ||
nodejs_binary( | ||
name = "run_a", | ||
data = ["package"], | ||
entry_point = "package", | ||
) | ||
|
||
# Now just assert that running that program produces the expected stdout | ||
assert_program_produces_stdout( | ||
name = "test_a", | ||
stdout = ["running entry point A"], | ||
# This requires runfiles | ||
tags = ["no-bazelci-windows"], | ||
tool = "run_a", | ||
) | ||
|
||
############# | ||
# Test Case 2 | ||
# Without the pkg_npm, we should still be able to run the program. | ||
# But we need an adapter rule that gives us the equivalent of the "main" field | ||
|
||
directory_file_path( | ||
name = "entry_point_b", | ||
directory = "build", | ||
path = "b.js", | ||
) | ||
|
||
nodejs_binary( | ||
name = "run_b", | ||
data = ["build"], | ||
entry_point = "entry_point_b", | ||
) | ||
|
||
# Now just assert that running that program produces the expected stdout | ||
assert_program_produces_stdout( | ||
name = "test_b", | ||
stdout = ["running entry point B"], | ||
tool = "run_b", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
if (require.main === module) { | ||
process.stdout.write('running entry point A') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
if (require.main === module) { | ||
process.stdout.write('running entry point B') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Copyright 2017 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""This module contains providers for working with TreeArtifacts. | ||
See https://github.com/bazelbuild/bazel-skylib/issues/300 | ||
(this feature could be upstreamed to bazel-skylib in the future) | ||
These are also called output directories, created by `ctx.actions.declare_directory`. | ||
""" | ||
|
||
DirectoryFilePathInfo = provider( | ||
doc = "Joins a label pointing to a TreeArtifact with a path nested within that directory.", | ||
fields = { | ||
"directory": "a TreeArtifact (ctx.actions.declare_directory)", | ||
"path": "path relative to the directory", | ||
}, | ||
) | ||
|
||
def _directory_file_path(ctx): | ||
if not ctx.file.directory.is_directory: | ||
fail("directory attribute must be created with Bazel declare_directory (TreeArtifact)") | ||
return [DirectoryFilePathInfo(path = ctx.attr.path, directory = ctx.file.directory)] | ||
|
||
directory_file_path = rule( | ||
doc = """Provide DirectoryFilePathInfo to reference some file within a directory. | ||
Otherwise there is no way to give a Bazel label for it.""", | ||
implementation = _directory_file_path, | ||
attrs = { | ||
"directory": attr.label( | ||
doc = "a directory", | ||
mandatory = True, | ||
allow_single_file = True, | ||
), | ||
"path": attr.string( | ||
doc = "a path within that directory", | ||
mandatory = True, | ||
), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.