|
| 1 | +import * as assert from "assert"; |
| 2 | +import * as path from "path"; |
| 3 | +import * as vscode from "vscode"; |
| 4 | +import { BazelQuery } from "../src/bazel/bazel_query"; |
| 5 | +import { blaze_query } from "../src/protos"; |
| 6 | + |
| 7 | +describe("BazelQuery", () => { |
| 8 | + const workspacePath = path.join( |
| 9 | + __dirname, |
| 10 | + "..", |
| 11 | + "..", |
| 12 | + "test", |
| 13 | + "bazel_workspace", |
| 14 | + ); |
| 15 | + |
| 16 | + async function setQueryOptionsConfig( |
| 17 | + packages?: string[], |
| 18 | + targets?: string[], |
| 19 | + ): Promise<void> { |
| 20 | + const config = vscode.workspace.getConfiguration("bazel"); |
| 21 | + if (packages !== undefined) { |
| 22 | + await config.update("queryOptions.packages", packages); |
| 23 | + } |
| 24 | + if (targets !== undefined) { |
| 25 | + await config.update("queryOptions.targets", targets); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + beforeEach(async () => { |
| 30 | + // Reset config before each test |
| 31 | + await setQueryOptionsConfig([], []); |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(async () => { |
| 35 | + // Reset config after each test |
| 36 | + await setQueryOptionsConfig([], []); |
| 37 | + }); |
| 38 | + |
| 39 | + describe("queryTargets", () => { |
| 40 | + it("should merge config options with additionalOptions", async function () { |
| 41 | + this.timeout(10000); |
| 42 | + await setQueryOptionsConfig(undefined, ["--keep_going"]); |
| 43 | + |
| 44 | + const query = new BazelQuery("bazel", workspacePath); |
| 45 | + let capturedOptions: string[] = []; |
| 46 | + |
| 47 | + // Mock the run method to capture options |
| 48 | + // @ts-expect-error - accessing protected method for testing |
| 49 | + const originalRun = query.run.bind(query); |
| 50 | + // @ts-expect-error - accessing protected method for testing |
| 51 | + query.run = async (options: string[]) => { |
| 52 | + capturedOptions = options; |
| 53 | + // Return a minimal valid QueryResult proto |
| 54 | + const result = blaze_query.QueryResult.create({ |
| 55 | + target: [], |
| 56 | + }); |
| 57 | + return Buffer.from(blaze_query.QueryResult.encode(result).finish()); |
| 58 | + }; |
| 59 | + |
| 60 | + await query.queryTargets("//...", { |
| 61 | + additionalOptions: ["--output=json"], |
| 62 | + }); |
| 63 | + |
| 64 | + // Verify config options come before additionalOptions |
| 65 | + const queryIndex = capturedOptions.indexOf("//..."); |
| 66 | + const keepGoingIndex = capturedOptions.indexOf("--keep_going"); |
| 67 | + const outputJsonIndex = capturedOptions.indexOf("--output=json"); |
| 68 | + |
| 69 | + assert.ok(queryIndex >= 0, "Query should be in options"); |
| 70 | + assert.ok(keepGoingIndex >= 0, "Config option should be in options"); |
| 71 | + assert.ok(outputJsonIndex >= 0, "Additional option should be in options"); |
| 72 | + assert.ok( |
| 73 | + keepGoingIndex < outputJsonIndex, |
| 74 | + "Config options should come before additionalOptions", |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should use empty array when config is not set", async function () { |
| 79 | + this.timeout(10000); |
| 80 | + await setQueryOptionsConfig(undefined, []); |
| 81 | + |
| 82 | + const query = new BazelQuery("bazel", workspacePath); |
| 83 | + let capturedOptions: string[] = []; |
| 84 | + |
| 85 | + // @ts-expect-error - accessing protected method for testing |
| 86 | + const originalRun = query.run.bind(query); |
| 87 | + // @ts-expect-error - accessing protected method for testing |
| 88 | + query.run = async (options: string[]) => { |
| 89 | + capturedOptions = options; |
| 90 | + const result = blaze_query.QueryResult.create({ |
| 91 | + target: [], |
| 92 | + }); |
| 93 | + return Buffer.from(blaze_query.QueryResult.encode(result).finish()); |
| 94 | + }; |
| 95 | + |
| 96 | + await query.queryTargets("//...", { |
| 97 | + additionalOptions: ["--output=json"], |
| 98 | + }); |
| 99 | + |
| 100 | + // Verify only additionalOptions are present (no config options) |
| 101 | + const keepGoingIndex = capturedOptions.indexOf("--keep_going"); |
| 102 | + const outputJsonIndex = capturedOptions.indexOf("--output=json"); |
| 103 | + |
| 104 | + assert.strictEqual( |
| 105 | + keepGoingIndex, |
| 106 | + -1, |
| 107 | + "Config option should not be present", |
| 108 | + ); |
| 109 | + assert.ok(outputJsonIndex >= 0, "Additional option should be present"); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe("queryPackages", () => { |
| 114 | + it("should merge config options with additionalOptions", async function () { |
| 115 | + this.timeout(10000); |
| 116 | + await setQueryOptionsConfig(["--keep_going"], undefined); |
| 117 | + |
| 118 | + const query = new BazelQuery("bazel", workspacePath); |
| 119 | + let capturedOptions: string[] = []; |
| 120 | + |
| 121 | + // Mock the run method to capture options |
| 122 | + // @ts-expect-error - accessing protected method for testing |
| 123 | + const originalRun = query.run.bind(query); |
| 124 | + // @ts-expect-error - accessing protected method for testing |
| 125 | + query.run = async (options: string[]) => { |
| 126 | + capturedOptions = options; |
| 127 | + // Return empty result |
| 128 | + return Buffer.from(""); |
| 129 | + }; |
| 130 | + |
| 131 | + await query.queryPackages("//...", { |
| 132 | + additionalOptions: ["--output=json"], |
| 133 | + }); |
| 134 | + |
| 135 | + // Verify config options come before additionalOptions |
| 136 | + const queryIndex = capturedOptions.indexOf("//..."); |
| 137 | + const keepGoingIndex = capturedOptions.indexOf("--keep_going"); |
| 138 | + const outputJsonIndex = capturedOptions.indexOf("--output=json"); |
| 139 | + |
| 140 | + assert.ok(queryIndex >= 0, "Query should be in options"); |
| 141 | + assert.ok(keepGoingIndex >= 0, "Config option should be in options"); |
| 142 | + assert.ok(outputJsonIndex >= 0, "Additional option should be in options"); |
| 143 | + assert.ok( |
| 144 | + keepGoingIndex < outputJsonIndex, |
| 145 | + "Config options should come before additionalOptions", |
| 146 | + ); |
| 147 | + }); |
| 148 | + |
| 149 | + it("should use empty array when config is not set", async function () { |
| 150 | + this.timeout(10000); |
| 151 | + await setQueryOptionsConfig([], undefined); |
| 152 | + |
| 153 | + const query = new BazelQuery("bazel", workspacePath); |
| 154 | + let capturedOptions: string[] = []; |
| 155 | + |
| 156 | + // @ts-expect-error - accessing protected method for testing |
| 157 | + const originalRun = query.run.bind(query); |
| 158 | + // @ts-expect-error - accessing protected method for testing |
| 159 | + query.run = async (options: string[]) => { |
| 160 | + capturedOptions = options; |
| 161 | + return Buffer.from(""); |
| 162 | + }; |
| 163 | + |
| 164 | + await query.queryPackages("//...", { |
| 165 | + additionalOptions: ["--output=json"], |
| 166 | + }); |
| 167 | + |
| 168 | + // Verify only additionalOptions are present (no config options) |
| 169 | + const keepGoingIndex = capturedOptions.indexOf("--keep_going"); |
| 170 | + const outputJsonIndex = capturedOptions.indexOf("--output=json"); |
| 171 | + |
| 172 | + assert.strictEqual( |
| 173 | + keepGoingIndex, |
| 174 | + -1, |
| 175 | + "Config option should not be present", |
| 176 | + ); |
| 177 | + assert.ok(outputJsonIndex >= 0, "Additional option should be present"); |
| 178 | + }); |
| 179 | + }); |
| 180 | +}); |
0 commit comments