-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add alias targets for unversioned reference to direct deps
Also add starlark unit testing for translate_package_lock as it's going to get more complex.
- Loading branch information
Showing
5 changed files
with
167 additions
and
15 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,3 @@ | ||
load(":translate_package_lock_tests.bzl", "translate_package_lock_tests") | ||
|
||
translate_package_lock_tests(name = "test_translate_package_lock") |
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,108 @@ | ||
"""Unit tests for starlark helpers | ||
See https://docs.bazel.build/versions/main/skylark/testing.html#for-testing-starlark-utilities | ||
""" | ||
|
||
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest") | ||
load("//js/private:translate_package_lock.bzl", "translate_package_lock") | ||
|
||
_lockfile = json.decode(""" | ||
{ | ||
"name": "test", | ||
"lockfileVersion": 2, | ||
"requires": true, | ||
"packages": { | ||
"": { | ||
"devDependencies": { | ||
"@gregmagolan/test-b": "^0.0.2" | ||
} | ||
} | ||
}, | ||
"dependencies": { | ||
"@gregmagolan/test-a": { | ||
"version": "0.0.1", | ||
"resolved": "https://registry.npmjs.org/@gregmagolan/test-a/-/test-a-0.0.1.tgz", | ||
"integrity": "sha512-a==", | ||
"dev": true | ||
}, | ||
"@gregmagolan/test-b": { | ||
"version": "0.0.2", | ||
"resolved": "https://registry.npmjs.org/@gregmagolan/test-b/-/test-b-0.0.2.tgz", | ||
"integrity": "sha512-b==", | ||
"dev": true, | ||
"requires": { | ||
"@gregmagolan/test-a": "0.0.1" | ||
} | ||
} | ||
} | ||
} | ||
""") | ||
|
||
def _naming_test_impl(ctx): | ||
env = unittest.begin(ctx) | ||
asserts.equals(env, "npm_acorn-10.2.3", translate_package_lock.repository_name("acorn", "10.2.3")) | ||
asserts.equals(env, "npm__types_node-11.2.3", translate_package_lock.repository_name("@types/node", "11.2.3")) | ||
return unittest.end(env) | ||
|
||
def _repository_bzl_test_impl(ctx): | ||
env = unittest.begin(ctx) | ||
|
||
expected_header = """\ | ||
# @generated by package_lock.bzl from {package_lock} | ||
load("@aspect_rules_js//js:npm_import.bzl", "npm_import") | ||
def npm_repositories(): | ||
"Define external repositories to fetch each tarball individually from npm on-demand." | ||
""" | ||
expected_a = """\ | ||
# @generated from [package-lock.json snippet here] | ||
npm_import( | ||
name = "npm__gregmagolan_test-a-0.0.1", | ||
integrity = "sha512-a==", | ||
package = "@gregmagolan/test-a", | ||
version = "0.0.1", | ||
deps = [], | ||
) | ||
""" | ||
expected_b = """\ | ||
# @generated from [package-lock.json snippet here] | ||
npm_import( | ||
name = "npm__gregmagolan_test-b-0.0.2", | ||
integrity = "sha512-b==", | ||
package = "@gregmagolan/test-b", | ||
version = "0.0.2", | ||
deps = ["@npm__gregmagolan_test-a-0.0.1"], | ||
) | ||
""" | ||
actual = translate_package_lock.testonly_import_dependencies(_lockfile) | ||
asserts.equals(env, 3, len(actual)) | ||
asserts.equals(env, expected_header, actual[0]) | ||
asserts.equals(env, expected_a, actual[1]) | ||
asserts.equals(env, expected_b, actual[2]) | ||
return unittest.end(env) | ||
|
||
def _aliases_test_impl(ctx): | ||
env = unittest.begin(ctx) | ||
mock_repository_ctx = struct() | ||
actual = translate_package_lock.testonly_define_aliases(mock_repository_ctx, _lockfile, False) | ||
asserts.equals(env, 1, len(actual.items())) | ||
|
||
# Note: @gregmagolan/test-a should not appear here since it's only a transitive dependency | ||
expected = { | ||
"@gregmagolan/test-b/BUILD.bazel": """# @generated by package_lock.bzl | ||
alias(name = "test-b", actual = "@npm__gregmagolan_test-b-0.0.2", visibility = ["//visibility:public"]) | ||
""", | ||
} | ||
asserts.equals(env, expected, actual) | ||
return unittest.end(env) | ||
|
||
# The unittest library requires that we export the test cases as named test rules, | ||
# but their names are arbitrary and don't appear anywhere. | ||
t0_test = unittest.make(_naming_test_impl) | ||
t1_test = unittest.make(_repository_bzl_test_impl) | ||
t2_test = unittest.make(_aliases_test_impl) | ||
|
||
def translate_package_lock_tests(name): | ||
unittest.suite(name, t0_test, t1_test, t2_test) |
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