Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(bazel): Use codeBlock in tests #20345

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 39 additions & 37 deletions lib/modules/manager/bazel/extract.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { codeBlock } from 'common-tags';
import { Fixtures } from '../../../../test/fixtures';
import { extractPackageFile as _extractPackageFile } from '.';

Expand Down Expand Up @@ -63,15 +64,16 @@ describe('modules/manager/bazel/extract', () => {

it('extracts dependencies for container_pull deptype', () => {
const res = extractPackageFile(
codeBlock`
container_pull(
name="hasura",
registry="index.docker.io",
repository="hasura/graphql-engine",
# v1.0.0-alpha31.cli-migrations 11/28
digest="sha256:a4e8d8c444ca04fe706649e82263c9f4c2a4229bc30d2a64561b5e1d20cc8548",
tag="v1.0.0-alpha31.cli-migrations"
)
`
container_pull(
name="hasura",
registry="index.docker.io",
repository="hasura/graphql-engine",
# v1.0.0-alpha31.cli-migrations 11/28
digest="sha256:a4e8d8c444ca04fe706649e82263c9f4c2a4229bc30d2a64561b5e1d20cc8548",
tag="v1.0.0-alpha31.cli-migrations"
)`
);
expect(res?.deps).toMatchObject([
{
Expand All @@ -87,13 +89,13 @@ describe('modules/manager/bazel/extract', () => {

it('check remote option in go_repository', () => {
const successStory = extractPackageFile(
`
go_repository(
name = "test_repository",
importpath = "github.com/google/uuid",
remote = "https://github.com/test/uuid-fork",
commit = "dec09d789f3dba190787f8b4454c7d3c936fed9e"
)
codeBlock`
go_repository(
name = "test_repository",
importpath = "github.com/google/uuid",
remote = "https://github.com/test/uuid-fork",
commit = "dec09d789f3dba190787f8b4454c7d3c936fed9e"
)
`
);
expect(successStory?.deps[0].datasource).toBe('go');
Expand All @@ -102,37 +104,37 @@ go_repository(
);

const badStory = extractPackageFile(
`
go_repository(
name = "test_repository",
importpath = "github.com/google/uuid",
remote = "https://github.com/test/uuid.git#branch",
commit = "dec09d789f3dba190787f8b4454c7d3c936fed9e"
)
codeBlock`
go_repository(
name = "test_repository",
importpath = "github.com/google/uuid",
remote = "https://github.com/test/uuid.git#branch",
commit = "dec09d789f3dba190787f8b4454c7d3c936fed9e"
)
`
);
expect(badStory?.deps[0].skipReason).toBe('unsupported-remote');

const gheStory = extractPackageFile(
`
go_repository(
name = "test_repository",
importpath = "github.com/google/uuid",
remote = "https://github.mycompany.com/test/uuid",
commit = "dec09d789f3dba190787f8b4454c7d3c936fed9e"
)
codeBlock`
go_repository(
name = "test_repository",
importpath = "github.com/google/uuid",
remote = "https://github.mycompany.com/test/uuid",
commit = "dec09d789f3dba190787f8b4454c7d3c936fed9e"
)
`
);
expect(gheStory?.deps[0].skipReason).toBe('unsupported-remote');

const gitlabRemote = extractPackageFile(
`
go_repository(
name = "test_repository",
importpath = "github.com/google/uuid",
remote = "https://gitlab.com/test/uuid",
commit = "dec09d789f3dba190787f8b4454c7d3c936fed9e"
)
codeBlock`
go_repository(
name = "test_repository",
importpath = "github.com/google/uuid",
remote = "https://gitlab.com/test/uuid",
commit = "dec09d789f3dba190787f8b4454c7d3c936fed9e"
)
`
);
expect(gitlabRemote?.deps[0].skipReason).toBe('unsupported-remote');
Expand All @@ -142,7 +144,7 @@ go_repository(
// Sequential http_archive
// See https://github.com/aspect-build/rules_swc/commit/d4989f9dfed781dc0226421fb9373b45052e7bc8
const res = extractPackageFile(
`
codeBlock`
http_archive(
name = "aspect_rules_js",
sha256 = "db9f446752fe4100320cf8487e8fd476b9af0adf6b99b601bcfd70b289bb0598",
Expand Down
32 changes: 19 additions & 13 deletions lib/modules/manager/bazel/parser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { codeBlock } from 'common-tags';
import { parse } from './parser';
import { extract } from './rules';

describe('modules/manager/bazel/parser', () => {
it('parses rules input', () => {
const input = [
'go_repository(name = "foo")',
'maybe(go_repository, name = "bar", deps = ["baz", "qux"])',
].join('\n');
const input = codeBlock`
go_repository(name = "foo")
maybe(go_repository, name = "bar", deps = ["baz", "qux"])
`;

const res = parse(input);

Expand Down Expand Up @@ -46,7 +47,7 @@ describe('modules/manager/bazel/parser', () => {
});

it('parses multiple archives', () => {
const input = `
const input = codeBlock`
http_archive(
name = "aspect_rules_js",
sha256 = "db9f446752fe4100320cf8487e8fd476b9af0adf6b99b601bcfd70b289bb0598",
Expand All @@ -57,7 +58,8 @@ describe('modules/manager/bazel/parser', () => {
name = "rules_nodejs",
sha256 = "5aef09ed3279aa01d5c928e3beb248f9ad32dde6aafe6373a8c994c3ce643064",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/5.5.3/rules_nodejs-core-5.5.3.tar.gz"],
)`;
)
`;

const res = parse(input);

Expand Down Expand Up @@ -120,11 +122,13 @@ describe('modules/manager/bazel/parser', () => {
});

it('parses http_archive', () => {
const input = `http_archive(
name = "rules_nodejs",
sha256 = "5aef09ed3279aa01d5c928e3beb248f9ad32dde6aafe6373a8c994c3ce643064",
url = "https://github.com/bazelbuild/rules_nodejs/releases/download/5.5.3/rules_nodejs-core-5.5.3.tar.gz",
)`;
const input = codeBlock`
http_archive(
name = "rules_nodejs",
sha256 = "5aef09ed3279aa01d5c928e3beb248f9ad32dde6aafe6373a8c994c3ce643064",
url = "https://github.com/bazelbuild/rules_nodejs/releases/download/5.5.3/rules_nodejs-core-5.5.3.tar.gz",
)
`;

const res = parse(input);

Expand Down Expand Up @@ -157,15 +161,17 @@ describe('modules/manager/bazel/parser', () => {
});

it('parses http_archive with prefixes and multiple urls', () => {
const input = `http_archive(
const input = codeBlock`
http_archive(
name = "bazel_toolchains",
sha256 = "4b1468b254a572dbe134cc1fd7c6eab1618a72acd339749ea343bd8f55c3b7eb",
strip_prefix = "bazel-toolchains-d665ccfa3e9c90fa789671bf4ef5f7c19c5715c4",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-toolchains/archive/d665ccfa3e9c90fa789671bf4ef5f7c19c5715c4.tar.gz",
"https://github.com/bazelbuild/bazel-toolchains/archive/d665ccfa3e9c90fa789671bf4ef5f7c19c5715c4.tar.gz",
],
)`;
)
`;

const res = parse(input);

Expand Down
11 changes: 11 additions & 0 deletions lib/modules/manager/bazel/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,22 @@ export interface StringFragment extends FragmentBase {
export type NestedFragment = ArrayFragment | RecordFragment;
export type Fragment = NestedFragment | StringFragment;

/**
* Parsed bazel files are represented as nested arrays and objects,
* which is enough for Renovate purposes.
*/
export type FragmentData =
| string
| FragmentData[]
| { [k: string]: FragmentData };

/**
* To access a fragment, we provide its path in the tree.
*
* The first element is the index of the rule in the file,
* which had been chosen over the rule name because it helps
* to deal with duplicate rule names in `if-else` branches.
*/
export type FragmentPath =
| [number]
| [number, string]
Expand Down