diff --git a/lib/modules/manager/bazel/__snapshots__/update.spec.ts.snap b/lib/modules/manager/bazel/__snapshots__/update.spec.ts.snap deleted file mode 100644 index 86e80709fb25d25e9bc5032bd99b084ce11f3b19..0000000000000000000000000000000000000000 --- a/lib/modules/manager/bazel/__snapshots__/update.spec.ts.snap +++ /dev/null @@ -1,167 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`modules/manager/bazel/update updateDependency updates commit to tag 1`] = ` -" -workspace(name = \\"angular_bazel_example\\") - -go_repository( - name = \\"com_github_bitly_go-nsq\\", - importpath = \\"github.com/bitly/go-nsq\\", - tag = \\"v1.0.5\\" -) - -go_repository( - name = \\"com_github_google_uuid\\", - importpath = \\"github.com/google/uuid\\", - commit = \\"aaa09d789f3dba190787f8b4454c7d3c936fe123\\", # v1.0.3 -) - -go_repository( - name = \\"com_gopkgin_mgo_v2\\", - importpath = \\"gopkg.in/mgo.v2\\", - tag = \\"v2\\" -) - -git_repository( - name = \\"build_bazel_rules_nodejs\\", - remote = \\"https://github.com/bazelbuild/rules_nodejs.git\\", - tag = \\"0.3.1\\", -) - -load(\\"@build_bazel_rules_nodejs//:defs.bzl\\", \\"node_repositories\\") -node_repositories(package_json = [\\"//:package.json\\"]) - -git_repository( - name = \\"build_bazel_rules_typescript\\", - remote = \\"https://github.com/bazelbuild/rules_typescript.git\\", - tag = \\"0.6.1\\", -) - -load(\\"@build_bazel_rules_typescript//:defs.bzl\\", \\"ts_repositories\\") - -ts_repositories() - -http_archive( - name=\\"distroless\\", - sha256=\\"f7a6ecfb8174a1dd4713ea3b21621072996ada7e8f1a69e6ae7581be137c6dd6\\", - strip_prefix=\\"distroless-446923c3756ceeaa75888f52fcbdd48bb314fbf8\\", - urls=[\\"https://github.com/GoogleContainerTools/distroless/archive/446923c3756ceeaa75888f52fcbdd48bb314fbf8.tar.gz\\"] -) - -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\\", - ], -) - -local_repository( - name = \\"angular\\", - path = \\"node_modules/@angular/bazel\\", -) - -local_repository( - name = \\"rxjs\\", - path = \\"node_modules/rxjs/src\\", -) - -git_repository( - name = \\"io_bazel_rules_sass\\", - remote = \\"https://github.com/bazelbuild/rules_sass.git\\", - tag = \\"0.0.3\\", -) - -load(\\"@io_bazel_rules_sass//sass:sass.bzl\\", \\"sass_repositories\\") - -sass_repositories() - -git_repository( - name = \\"com_github_bazelbuild_buildtools\\", - remote = \\"https://github.com/bazelbuild/buildtools.git\\", - # Note, this commit matches the version of buildifier in angular/ngcontainer - commit = \\"b3b620e8bcff18ed3378cd3f35ebeb7016d71f71\\", -) - -http_archive( - name = \\"io_bazel_rules_go\\", - url = \\"https://github.com/bazelbuild/rules_go/releases/download/0.7.1/rules_go-0.7.1.tar.gz\\", - sha256 = \\"341d5eacef704415386974bc82a1783a8b7ffbff2ab6ba02375e1ca20d9b031c\\", -) - -http_archive( - name = \\"bazel_skylib\\", - sha256 = \\"b5f6abe419da897b7901f90cbab08af958b97a8f3575b0d3dd062ac7ce78541f\\", - strip_prefix = \\"bazel-skylib-0.5.0\\", - urls = [ - \\"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/archive/0.5.0.tar.gz\\", - \\"https://github.com/bazelbuild/bazel-skylib/archive/0.5.0.tar.gz\\", - ], -) - -http_archive( - name=\\"distroless\\", - sha256=\\"f7a6ecfb8174a1dd4713ea3b21621072996ada7e8f1a69e6ae7581be137c6dd6\\", - strip_prefix=\\"distroless-446923c3756ceeaa75888f52fcbdd48bb314fbf8\\", - urls=[\\"https://github.com/GoogleContainerTools/distroless/archive/446923c3756ceeaa75888f52fcbdd48bb314fbf8.tar.gz\\"] -) - -load(\\"@bazel_tools//tools/build_defs/repo:utils.bzl\\", \\"maybe\\") -maybe( - http_archive, - name = \\"io_bazel_rules_go\\", - sha256 = \\"2b1641428dff9018f9e85c0384f03ec6c10660d935b750e3fa1492a281a53b0f\\", - url = \\"https://github.com/bazelbuild/rules_go/releases/download/v0.29.0/rules_go-v0.29.0.zip\\", -) -maybe( - http_archive, - name = \\"bazel_gazelle\\", - sha256 = \\"de69a09dc70417580aabf20a28619bb3ef60d038470c7cf8442fafcf627c21cb\\", - urls = [ - \\"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz\\", - \\"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz\\", - ], -) -maybe( - go_repository, - name = \\"com_github_pkg_errors\\", - commit = \\"816c9085562cd7ee03e7f8188a1cfd942858cded\\", - importpath = \\"github.com/pkg/errors\\", -) - -load(\\"@io_bazel_rules_go//go:def.bzl\\", \\"go_rules_dependencies\\", \\"go_register_toolchains\\") - -go_rules_dependencies() - -go_register_toolchains() - -container_pull( - name = \\"py3_image_base\\", - digest = \\"sha256:d5a717649fd93ea5b9c430d7f84e4c37ba219eb53bd73ed1d4a5a98e9edd84a7\\", - registry = \\"gcr.io\\", - repository = \\"distroless/python3-debian10\\", - tag = \\"latest\\", -) - -http_file( - name=\\"distroless\\", - sha256=\\"f7a6ecfb8174a1dd4713ea3b21621072996ada7e8f1a69e6ae7581be137c6dd6\\", - strip_prefix=\\"distroless-446923c3756ceeaa75888f52fcbdd48bb314fbf8\\", - urls=[\\"https://github.com/GoogleContainerTools/distroless/archive/446923c3756ceeaa75888f52fcbdd48bb314fbf8.tar.gz\\"] -) -" -`; - -exports[`modules/manager/bazel/update updateDependency updates container_pull deptype and preserves comment 1`] = ` -"container_pull( - name=\\"hasura\\", - registry=\\"index.docker.io\\", - repository=\\"hasura/graphql-engine\\", - # v1.0.0-alpha31.cli-migrations 11/28 - digest=\\"sha256:2c29ba015faef92a3f55b37632fc373a7fbc2c9fddd31e317bf07113391c640b\\", - tag=\\"v1.0.0-alpha42.cli-migrations\\" - ) -" -`; diff --git a/lib/modules/manager/bazel/update.spec.ts b/lib/modules/manager/bazel/update.spec.ts index 98401844262b702f10de6618c7483056e70744ad..feef74f5b2ff38b4125b414fd737541f789371bc 100644 --- a/lib/modules/manager/bazel/update.spec.ts +++ b/lib/modules/manager/bazel/update.spec.ts @@ -1,21 +1,8 @@ -import { Readable } from 'stream'; -import { Fixtures } from '../../../../test/fixtures'; +import crypto from 'crypto'; import * as httpMock from '../../../../test/http-mock'; import type { UpdateType } from '../../../config/types'; import { updateDependency } from '.'; -const content = Fixtures.get('WORKSPACE1'); -const contentContainerPull = Fixtures.get('container_pull'); -const fileWithBzlExtension = Fixtures.get('repositories.bzl'); - -/* -git_repository( - name = "build_bazel_rules_nodejs", - remote = "https://github.com/bazelbuild/rules_nodejs.git", - tag = "0.1.8", -) -*/ - describe('modules/manager/bazel/update', () => { describe('updateDependency', () => { beforeEach(() => { @@ -23,194 +10,292 @@ describe('modules/manager/bazel/update', () => { }); it('updates git_repository tag', async () => { + const input = ` + git_repository( + name = "build_bazel_rules_nodejs", + remote = "https://github.com/bazelbuild/rules_nodejs.git", + tag = "0.1.8", + ) + `.trim(); + const upgrade = { depName: 'build_bazel_rules_nodejs', depType: 'git_repository', - managerData: { - def: `git_repository(\n name = "build_bazel_rules_nodejs",\n remote = "https://github.com/bazelbuild/rules_nodejs.git",\n tag = "0.1.8",\n)`, - }, + managerData: { def: input }, currentValue: '0.1.8', newValue: '0.2.0', }; + const output = input.replace('0.1.8', '0.2.0'); + const res = await updateDependency({ - fileContent: content, + fileContent: input, upgrade, }); - expect(res).not.toEqual(content); + + expect(res).toEqual(output); }); it('updates maybe(git_repository) tag', async () => { + const input = + ` + maybe( + git_repository, + name = "build_bazel_rules_nodejs", + remote = "https://github.com/bazelbuild/rules_nodejs.git", + tag = "0.1.8", + ) + `.trim() + '\n'; + const upgrade = { depName: 'build_bazel_rules_nodejs', depType: 'git_repository', - managerData: { - def: `maybe(\n git_repository,\n name = "build_bazel_rules_nodejs",\n remote = "https://github.com/bazelbuild/rules_nodejs.git",\n tag = "0.1.8",\n)`, - }, + managerData: { def: input }, currentValue: '0.1.8', newValue: '0.2.0', }; + const output = input.replace('0.1.8', '0.2.0'); + const res = await updateDependency({ - fileContent: content, + fileContent: input, upgrade, }); - expect(res).not.toEqual(content); - expect(res).toMatch(/maybe\([\n\s]*git_repository,/); + + expect(res).toEqual(output); }); it('updates container_pull deptype and preserves comment', async () => { - const upgrade = { - depName: 'hasura', - depType: 'container_pull', - managerData: { - def: `container_pull( + const input = ` + 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" - )`, - }, - currentValue: 'v1.0.0-alpha31.cli-migrations', - currentDigest: - 'sha256:a4e8d8c444ca04fe706649e82263c9f4c2a4229bc30d2a64561b5e1d20cc8548', - newDigest: - 'sha256:2c29ba015faef92a3f55b37632fc373a7fbc2c9fddd31e317bf07113391c640b', - newValue: 'v1.0.0-alpha42.cli-migrations', + ) + `.trim(); + + const currentValue = 'v1.0.0-alpha31.cli-migrations'; + const newValue = 'v1.0.0-alpha42.cli-migrations'; + + const currentDigest = + 'sha256:a4e8d8c444ca04fe706649e82263c9f4c2a4229bc30d2a64561b5e1d20cc8548'; + const newDigest = + 'sha256:2c29ba015faef92a3f55b37632fc373a7fbc2c9fddd31e317bf07113391c640b'; + + const upgrade = { + depName: 'hasura', + depType: 'container_pull', + managerData: { def: input }, + currentValue, + newValue, + currentDigest, + newDigest, }; - const res = await updateDependency({ - fileContent: contentContainerPull, - upgrade, - }); - expect(res).toMatchSnapshot(); - expect(res).not.toEqual(contentContainerPull); + + const output = input + .replace(`tag="${currentValue}"`, `tag="${newValue}"`) + .replace(currentDigest, newDigest); + + const res = await updateDependency({ fileContent: input, upgrade }); + + expect(res).toEqual(output); expect(res).toContain('# v1.0.0-alpha31.cli-migrations 11/28'); }); it('updates commit to tag', async () => { + const input = ` + go_repository( + name = "com_github_google_uuid", + importpath = "github.com/google/uuid", + commit = "dec09d789f3dba190787f8b4454c7d3c936fed9e" + ) + `.trim(); + + const currentDigest = 'dec09d789f3dba190787f8b4454c7d3c936fed9e'; + const newDigest = 'aaa09d789f3dba190787f8b4454c7d3c936fe123'; + const newValue = 'v1.0.3'; + const upgrade = { depName: 'com_github_google_uuid', depType: 'go_repository', - managerData: { - def: `go_repository( - name = "com_github_google_uuid", - importpath = "github.com/google/uuid", - commit = "dec09d789f3dba190787f8b4454c7d3c936fed9e" -) -`, - }, + managerData: { def: input }, currentValue: 'v0.0.0', - currentDigest: 'dec09d789f3dba190787f8b4454c7d3c936fed9e', - newDigest: 'aaa09d789f3dba190787f8b4454c7d3c936fe123', - newValue: 'v1.0.3', + currentDigest, + newDigest, + newValue, updateType: 'major' as UpdateType, }; - const res = await updateDependency({ - fileContent: content, - upgrade, - }); - expect(res).toMatchSnapshot(); - expect(res).not.toEqual(content); + + const output = input.replace( + `"${currentDigest}"`, + `"${newDigest}", # ${newValue}` + ); + + const res = await updateDependency({ fileContent: input, upgrade }); + + expect(res).toEqual(output); expect(res).toContain( '"aaa09d789f3dba190787f8b4454c7d3c936fe123", # v1.0.3' ); }); it('updates commit-based http archive', async () => { + const inputHash = + 'f7a6ecfb8174a1dd4713ea3b21621072996ada7e8f1a69e6ae7581be137c6dd6'; + const input = ` + http_archive( + name="distroless", + sha256="${inputHash}", + strip_prefix="distroless-446923c3756ceeaa75888f52fcbdd48bb314fbf8", + urls=["https://github.com/GoogleContainerTools/distroless/archive/446923c3756ceeaa75888f52fcbdd48bb314fbf8.tar.gz"] + ) + `.trim(); + + const currentDigest = '446923c3756ceeaa75888f52fcbdd48bb314fbf8'; + const newDigest = '033387ac8853e6cc1cd47df6c346bc53cbc490d8'; const upgrade = { depName: 'distroless', depType: 'http_archive', repo: 'GoogleContainerTools/distroless', - managerData: { - def: `http_archive(\n name="distroless",\n sha256="f7a6ecfb8174a1dd4713ea3b21621072996ada7e8f1a69e6ae7581be137c6dd6",\n strip_prefix="distroless-446923c3756ceeaa75888f52fcbdd48bb314fbf8",\n urls=["https://github.com/GoogleContainerTools/distroless/archive/446923c3756ceeaa75888f52fcbdd48bb314fbf8.tar.gz"]\n)`, - }, - currentDigest: '446923c3756ceeaa75888f52fcbdd48bb314fbf8', - newDigest: '033387ac8853e6cc1cd47df6c346bc53cbc490d8', + managerData: { def: input }, + currentDigest, + newDigest, }; + + const tarContent = Buffer.from('foo'); + const outputHash = crypto + .createHash('sha256') + .update(tarContent) + .digest('hex'); + + const output = input + .replace(currentDigest, newDigest) + .replace(currentDigest, newDigest) + .replace(inputHash, outputHash); + httpMock .scope('https://github.com') .get( '/GoogleContainerTools/distroless/archive/033387ac8853e6cc1cd47df6c346bc53cbc490d8.tar.gz' ) - .reply(200, Readable.from(['foo'])); - const res = await updateDependency({ - fileContent: content, - upgrade, - }); - expect(res).not.toEqual(content); + .reply(200, tarContent); + + const res = await updateDependency({ fileContent: input, upgrade }); + + expect(res).toEqual(output); }); it('updates http archive with content other then WORKSPACE', async () => { + const inputHash = + 'eb5c57e4c12e68c0c20bc774bfbc60a568e800d025557bc4ea022c6479acc867'; + const input = ` + http_archive( + name = "bazel_skylib", + sha256 = "${inputHash}", + strip_prefix = "bazel-skylib-0.6.0", + urls = ["https://github.com/bazelbuild/bazel-skylib/archive/0.6.0.tar.gz"], + ) + `.trim(); + + const currentValue = '0.6.0'; + const newValue = '0.8.0'; const upgrade = { depName: 'bazel_skylib', depType: 'http_archive', repo: 'bazelbuild/bazel-skylib', - managerData: { - def: `http_archive( - name = "bazel_skylib", - sha256 = "eb5c57e4c12e68c0c20bc774bfbc60a568e800d025557bc4ea022c6479acc867", - strip_prefix = "bazel-skylib-0.6.0", - urls = ["https://github.com/bazelbuild/bazel-skylib/archive/0.6.0.tar.gz"], - )`, - }, - currentValue: '0.6.0', - newValue: '0.8.0', + managerData: { def: input }, + currentValue, + newValue, }; + + const tarContent = Buffer.from('foo'); + const outputHash = crypto + .createHash('sha256') + .update(tarContent) + .digest('hex'); + + const output = input + .replace(currentValue, newValue) + .replace(currentValue, newValue) + .replace(inputHash, outputHash); + httpMock .scope('https://github.com') .get('/bazelbuild/bazel-skylib/archive/0.8.0.tar.gz') - .reply(200, Readable.from(['foo'])); - const res = await updateDependency({ - fileContent: content, - upgrade, - }); - expect(res).not.toEqual(fileWithBzlExtension); - expect(res?.indexOf('0.8.0')).not.toBe(-1); + .reply(200, tarContent); + + const res = await updateDependency({ fileContent: input, upgrade }); + + expect(res).toEqual(output); }); it('updates finds url instead of urls', async () => { + const inputHash = + 'eb5c57e4c12e68c0c20bc774bfbc60a568e800d025557bc4ea022c6479acc867'; + const input = ` + http_archive( + name = "bazel_skylib", + sha256 = "${inputHash}", + strip_prefix = "bazel-skylib-0.6.0", + url = "https://github.com/bazelbuild/bazel-skylib/archive/0.6.0.tar.gz", + ) + `.trim(); + + const currentValue = '0.6.0'; + const newValue = '0.8.0'; const upgrade = { depName: 'bazel_skylib', depType: 'http_archive', repo: 'bazelbuild/bazel-skylib', - managerData: { - def: `http_archive( - name = "bazel_skylib", - sha256 = "eb5c57e4c12e68c0c20bc774bfbc60a568e800d025557bc4ea022c6479acc867", - strip_prefix = "bazel-skylib-0.6.0", - url = "https://github.com/bazelbuild/bazel-skylib/archive/0.6.0.tar.gz", - )`, - }, - currentValue: '0.6.0', - newValue: '0.8.0', + managerData: { def: input }, + currentValue, + newValue, }; + + const tarContent = Buffer.from('foo'); + const outputHash = crypto + .createHash('sha256') + .update(tarContent) + .digest('hex'); + + const output = input + .replace(currentValue, newValue) + .replace(currentValue, newValue) + .replace(inputHash, outputHash); + httpMock .scope('https://github.com') .get('/bazelbuild/bazel-skylib/archive/0.8.0.tar.gz') - .reply(200, Readable.from(['foo'])); - const res = await updateDependency({ - fileContent: content, - upgrade, - }); - expect(res).not.toEqual(fileWithBzlExtension); + .reply(200, tarContent); + + const res = await updateDependency({ fileContent: input, upgrade }); + + expect(res).toEqual(output); expect(res?.indexOf('0.8.0')).not.toBe(-1); }); it('returns null if no urls resolve hashes', async () => { + const inputHash = + 'eb5c57e4c12e68c0c20bc774bfbc60a568e800d025557bc4ea022c6479acc867'; + const input = ` + http_archive( + name = "bazel_skyfoo", + sha256 = "${inputHash}", + strip_prefix = "bazel-skyfoo-0.6.0", + urls = ["https://github.com/bazelbuild/bazel-skyfoo/archive/0.6.0.tar.gz"], + ) + `.trim(); + + const currentValue = '0.6.0'; + const newValue = '0.8.0'; const upgrade = { depName: 'bazel_skylib', depType: 'http_archive', repo: 'bazelbuild/bazel-skyfoo', - managerData: { - def: `http_archive( - name = "bazel_skyfoo", - sha256 = "eb5c57e4c12e68c0c20bc774bfbc60a568e800d025557bc4ea022c6479acc867", - strip_prefix = "bazel-skyfoo-0.6.0", - urls = ["https://github.com/bazelbuild/bazel-skyfoo/archive/0.6.0.tar.gz"], - )`, - }, - currentValue: '0.6.0', - newValue: '0.8.0', + managerData: { def: input }, + currentValue, + newValue, }; httpMock @@ -218,142 +303,157 @@ describe('modules/manager/bazel/update', () => { .get('/bazelbuild/bazel-skyfoo/archive/0.8.0.tar.gz') .reply(500); - const res = await updateDependency({ - fileContent: content, - upgrade, - }); + const res = await updateDependency({ fileContent: input, upgrade }); + expect(res).toBeNull(); }); it('errors for http_archive without urls', async () => { + const input = ` + http_archive( + name = "bazel_skylib", + sha256 = "b5f6abe419da897b7901f90cbab08af958b97a8f3575b0d3dd062ac7ce78541f", + strip_prefix = "bazel-skylib-0.5.0", + ) + `.trim(); + const upgrade = { depName: 'bazel_skylib', depType: 'http_archive', repo: 'bazelbuild/bazel-skylib', - managerData: { - def: - ` -http_archive( - name = "bazel_skylib", - sha256 = "b5f6abe419da897b7901f90cbab08af958b97a8f3575b0d3dd062ac7ce78541f", - strip_prefix = "bazel-skylib-0.5.0", -) - `.trim() + '\n', - }, + managerData: { def: input }, currentValue: '0.5.0', newValue: '0.6.2', }; - const res = await updateDependency({ - fileContent: content, - upgrade, - }); + const res = await updateDependency({ fileContent: input, upgrade }); expect(res).toBeNull(); }); it('errors for maybe(http_archive) without urls', async () => { + const input = ` + maybe( + http_archive, + name = "bazel_skylib", + sha256 = "b5f6abe419da897b7901f90cbab08af958b97a8f3575b0d3dd062ac7ce78541f", + strip_prefix = "bazel-skylib-0.5.0", + ) + `.trim(); + const upgrade = { depName: 'bazel_skylib', depType: 'http_archive', repo: 'bazelbuild/bazel-skylib', - managerData: { - def: - ` -maybe( - http_archive, - name = "bazel_skylib", - sha256 = "b5f6abe419da897b7901f90cbab08af958b97a8f3575b0d3dd062ac7ce78541f", - strip_prefix = "bazel-skylib-0.5.0", -) - `.trim() + '\n', - }, + managerData: { def: input }, currentValue: '0.5.0', newValue: '0.6.2', }; - const res = await updateDependency({ - fileContent: content, - upgrade, - }); + const res = await updateDependency({ fileContent: input, upgrade }); expect(res).toBeNull(); }); it('updates http_archive with urls array', async () => { + const inputHash = + 'b5f6abe419da897b7901f90cbab08af958b97a8f3575b0d3dd062ac7ce78541f'; + const input = ` + http_archive( + name = "bazel_skylib", + sha256 = "${inputHash}", + strip_prefix = "bazel-skylib-0.5.0", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/archive/0.5.0.tar.gz", + "https://github.com/bazelbuild/bazel-skylib/archive/0.5.0.tar.gz", + ], + ) + `.trim(); + + const currentValue = '0.5.0'; + const newValue = '0.6.2'; const upgrade = { depName: 'bazel_skylib', depType: 'http_archive', repo: 'bazelbuild/bazel-skylib', - managerData: { - def: - ` -http_archive( - name = "bazel_skylib", - sha256 = "b5f6abe419da897b7901f90cbab08af958b97a8f3575b0d3dd062ac7ce78541f", - strip_prefix = "bazel-skylib-0.5.0", - urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/archive/0.5.0.tar.gz", - "https://github.com/bazelbuild/bazel-skylib/archive/0.5.0.tar.gz", - ], -) - `.trim() + '\n', - }, - currentValue: '0.5.0', - newValue: '0.6.2', + managerData: { def: input }, + currentValue, + newValue, }; + + const tarContent = Buffer.from('foo'); + const outputHash = crypto + .createHash('sha256') + .update(tarContent) + .digest('hex'); + + const output = input + .replace(currentValue, newValue) + .replace(currentValue, newValue) + .replace(currentValue, newValue) + .replace(inputHash, outputHash); + httpMock .scope('https://github.com') .get('/bazelbuild/bazel-skylib/archive/0.6.2.tar.gz') - .reply(200, Readable.from(['foo'])); + .reply(200, tarContent); + httpMock .scope('https://mirror.bazel.build') .get('/github.com/bazelbuild/bazel-skylib/archive/0.6.2.tar.gz') - .reply(200, Readable.from(['foo'])); - const res = await updateDependency({ - fileContent: content, - upgrade, - }); - expect(res).not.toEqual(content); - expect(res?.indexOf('0.5.0')).toBe(-1); - expect(res?.indexOf('0.6.2')).not.toBe(-1); + .reply(200, tarContent); + + const res = await updateDependency({ fileContent: input, upgrade }); + + expect(res).toEqual(output); }); it('updates maybe(http_archive) with urls array', async () => { + const inputHash = + 'b5f6abe419da897b7901f90cbab08af958b97a8f3575b0d3dd062ac7ce78541f'; + const input = ` + maybe( + http_archive, + name = "bazel_skylib", + sha256 = "${inputHash}", + strip_prefix = "bazel-skylib-0.5.0", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/archive/0.5.0.tar.gz", + "https://github.com/bazelbuild/bazel-skylib/archive/0.5.0.tar.gz", + ], + ) + `.trim(); + + const currentValue = '0.5.0'; + const newValue = '0.6.2'; const upgrade = { depName: 'bazel_skylib', depType: 'http_archive', repo: 'bazelbuild/bazel-skylib', - managerData: { - def: - ` -maybe( - http_archive, - name = "bazel_skylib", - sha256 = "b5f6abe419da897b7901f90cbab08af958b97a8f3575b0d3dd062ac7ce78541f", - strip_prefix = "bazel-skylib-0.5.0", - urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/archive/0.5.0.tar.gz", - "https://github.com/bazelbuild/bazel-skylib/archive/0.5.0.tar.gz", - ], -) - `.trim() + '\n', - }, - currentValue: '0.5.0', - newValue: '0.6.2', + managerData: { def: input }, + currentValue, + newValue, }; + + const tarContent = Buffer.from('foo'); + const outputHash = crypto + .createHash('sha256') + .update(tarContent) + .digest('hex'); + + const output = input + .replace(currentValue, newValue) + .replace(currentValue, newValue) + .replace(currentValue, newValue) + .replace(inputHash, outputHash); + httpMock .scope('https://github.com') .get('/bazelbuild/bazel-skylib/archive/0.6.2.tar.gz') - .reply(200, Readable.from(['foo'])); + .reply(200, tarContent); httpMock .scope('https://mirror.bazel.build') .get('/github.com/bazelbuild/bazel-skylib/archive/0.6.2.tar.gz') - .reply(200, Readable.from(['foo'])); - const res = await updateDependency({ - fileContent: content, - upgrade, - }); - expect(res).not.toEqual(content); - expect(res?.indexOf('0.5.0')).toBe(-1); - expect(res?.indexOf('0.6.2')).not.toBe(-1); - expect(res).toMatch(/maybe\([\n\s]*http_archive,/); + .reply(200, tarContent); + + const res = await updateDependency({ fileContent: input, upgrade }); + expect(res).toEqual(output); }); }); }); diff --git a/lib/modules/manager/bazel/update.ts b/lib/modules/manager/bazel/update.ts index 68579aeae85fea7db9d146e7440c0ab080801982..9c4b0904809b063a9ac19ca3d9347c5d296fb393 100644 --- a/lib/modules/manager/bazel/update.ts +++ b/lib/modules/manager/bazel/update.ts @@ -162,7 +162,7 @@ export async function updateDependency({ return null; } - let existingRegExStr = `${upgrade.depType}\\([^\\)]+name\\s*=\\s*"${upgrade.depName}"(.*\\n)+?\\s*\\)`; + let existingRegExStr = `(?:maybe\\s*\\(\\s*)?${upgrade.depType}(?:\\(|,)[^\\)]+name\\s*=\\s*"${upgrade.depName}"(.*\\n)+?\\s*\\)`; if (newDef.endsWith('\n')) { existingRegExStr += '\n'; }