From ac2097cddde3555c8327bfb19a7b74a69a92efa7 Mon Sep 17 00:00:00 2001 From: Doug Parker Date: Tue, 16 Feb 2021 19:00:28 -0800 Subject: [PATCH] feat(typescript): add `data` attribute Adds the `data` attribute to `ts_project()`. This functions like most `data` attributes as defined by [common Bazel definitions](https://docs.bazel.build/versions/master/be/common-definitions.html#typical-attributes). All it really does is add the provided files to the `DefaultInfo()` runfiles object, where they will be propagated to any consuming rules. --- packages/typescript/internal/ts_project.bzl | 5 ++++- .../test/ts_project/data/BUILD.bazel | 22 +++++++++++++++++++ .../test/ts_project/data/data.spec.ts | 13 +++++++++++ .../typescript/test/ts_project/data/foo.txt | 1 + .../test/ts_project/data/tsconfig.json | 10 +++++++++ 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 packages/typescript/test/ts_project/data/BUILD.bazel create mode 100644 packages/typescript/test/ts_project/data/data.spec.ts create mode 100644 packages/typescript/test/ts_project/data/foo.txt create mode 100644 packages/typescript/test/ts_project/data/tsconfig.json diff --git a/packages/typescript/internal/ts_project.bzl b/packages/typescript/internal/ts_project.bzl index baa1398b86..28d76c0317 100644 --- a/packages/typescript/internal/ts_project.bzl +++ b/packages/typescript/internal/ts_project.bzl @@ -23,6 +23,7 @@ _DEFAULT_TYPESCRIPT_PACKAGE = ( _ATTRS = { "args": attr.string_list(), + "data": attr.label_list(default = [], allow_files = True), "declaration_dir": attr.string(), "deps": attr.label_list( providers = [ @@ -223,7 +224,9 @@ def _ts_project_impl(ctx): DefaultInfo( files = default_outputs_depset, runfiles = ctx.runfiles( - transitive_files = default_outputs_depset, + transitive_files = depset(ctx.files.data, transitive = [ + default_outputs_depset, + ]), collect_default = True, ), ), diff --git a/packages/typescript/test/ts_project/data/BUILD.bazel b/packages/typescript/test/ts_project/data/BUILD.bazel new file mode 100644 index 0000000000..0514fe18de --- /dev/null +++ b/packages/typescript/test/ts_project/data/BUILD.bazel @@ -0,0 +1,22 @@ +load("//packages/jasmine:index.bzl", "jasmine_node_test") +load("//packages/typescript:index.bzl", "ts_project") + +ts_project( + name = "data", + testonly = True, + srcs = ["data.spec.ts"], + composite = True, + # Should be readable at runtime under runfiles directory. + data = ["foo.txt"], + extends = "//packages/typescript/test/ts_project:tsconfig-base.json", + tsconfig = "tsconfig.json", + deps = [ + "@npm//@types/jasmine", + "@npm//@types/node", + ], +) + +jasmine_node_test( + name = "test", + srcs = [":data"], +) diff --git a/packages/typescript/test/ts_project/data/data.spec.ts b/packages/typescript/test/ts_project/data/data.spec.ts new file mode 100644 index 0000000000..851a669a67 --- /dev/null +++ b/packages/typescript/test/ts_project/data/data.spec.ts @@ -0,0 +1,13 @@ +import {promises as fs} from 'fs'; + +const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']!); + +const FOO_PATH = + runfiles.resolve('build_bazel_rules_nodejs/packages/typescript/test/ts_project/data/foo.txt'); + +describe('data', () => { + it('makes files available at runtime', async () => { + const foo = await fs.readFile(FOO_PATH, {encoding: 'utf8'}); + expect(foo).toBe('Foo\n'); + }); +}); diff --git a/packages/typescript/test/ts_project/data/foo.txt b/packages/typescript/test/ts_project/data/foo.txt new file mode 100644 index 0000000000..bc56c4d894 --- /dev/null +++ b/packages/typescript/test/ts_project/data/foo.txt @@ -0,0 +1 @@ +Foo diff --git a/packages/typescript/test/ts_project/data/tsconfig.json b/packages/typescript/test/ts_project/data/tsconfig.json new file mode 100644 index 0000000000..f71d6d2250 --- /dev/null +++ b/packages/typescript/test/ts_project/data/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../tsconfig-base.json", + "compilerOptions": { + "types": ["jasmine", "node"] + }, + "references": [ + {"path": "./"} + ], + "include": ["*.spec.ts"] +}