From 5caa59aaa151462c4c62a0d46633102f475078db Mon Sep 17 00:00:00 2001
From: Sergei Zharinov <zharinov@users.noreply.github.com>
Date: Sat, 11 Feb 2023 10:58:15 +0300
Subject: [PATCH] test(bazel): Use `codeBlock` in tests (#20345)

---
 lib/modules/manager/bazel/extract.spec.ts | 76 ++++++++++++-----------
 lib/modules/manager/bazel/parser.spec.ts  | 32 ++++++----
 lib/modules/manager/bazel/types.ts        | 11 ++++
 3 files changed, 69 insertions(+), 50 deletions(-)

diff --git a/lib/modules/manager/bazel/extract.spec.ts b/lib/modules/manager/bazel/extract.spec.ts
index 85e8d2d50a..2657dee280 100644
--- a/lib/modules/manager/bazel/extract.spec.ts
+++ b/lib/modules/manager/bazel/extract.spec.ts
@@ -1,3 +1,4 @@
+import { codeBlock } from 'common-tags';
 import { Fixtures } from '../../../../test/fixtures';
 import { extractPackageFile as _extractPackageFile } from '.';
 
@@ -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([
         {
@@ -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');
@@ -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');
@@ -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",
diff --git a/lib/modules/manager/bazel/parser.spec.ts b/lib/modules/manager/bazel/parser.spec.ts
index 42dc65c863..7c0de48a14 100644
--- a/lib/modules/manager/bazel/parser.spec.ts
+++ b/lib/modules/manager/bazel/parser.spec.ts
@@ -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);
 
@@ -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",
@@ -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);
 
@@ -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);
 
@@ -157,7 +161,8 @@ 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",
@@ -165,7 +170,8 @@ describe('modules/manager/bazel/parser', () => {
             "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);
 
diff --git a/lib/modules/manager/bazel/types.ts b/lib/modules/manager/bazel/types.ts
index 56f16a17b5..ce1233b12f 100644
--- a/lib/modules/manager/bazel/types.ts
+++ b/lib/modules/manager/bazel/types.ts
@@ -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]
-- 
GitLab