|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the VS Code Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 the VS Code Swift project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of VS Code Swift project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | +import { expect } from "chai"; |
| 15 | +import * as sinon from "sinon"; |
| 16 | + |
| 17 | +import { FolderContext } from "@src/FolderContext"; |
| 18 | +import { LinuxMain } from "@src/LinuxMain"; |
| 19 | +import { SwiftPackage } from "@src/SwiftPackage"; |
| 20 | +import configuration, { FolderConfiguration } from "@src/configuration"; |
| 21 | +import { BuildConfigurationFactory } from "@src/debugger/buildConfig"; |
| 22 | +import { BuildFlags } from "@src/toolchain/BuildFlags"; |
| 23 | +import { SwiftToolchain } from "@src/toolchain/toolchain"; |
| 24 | +import { Version } from "@src/utilities/version"; |
| 25 | + |
| 26 | +import { MockedObject, instance, mockGlobalValue, mockObject } from "../../MockUtils"; |
| 27 | + |
| 28 | +suite("BuildConfig Test Suite", () => { |
| 29 | + let mockedFolderContext: MockedObject<FolderContext>; |
| 30 | + let mockedSwiftPackage: MockedObject<SwiftPackage>; |
| 31 | + let mockedToolchain: MockedObject<SwiftToolchain>; |
| 32 | + let mockedBuildFlags: MockedObject<BuildFlags>; |
| 33 | + |
| 34 | + const additionalTestArgumentsConfig = mockGlobalValue(configuration, "folder"); |
| 35 | + |
| 36 | + function createMockFolderConfig(additionalTestArguments: string[]): FolderConfiguration { |
| 37 | + return { |
| 38 | + testEnvironmentVariables: {}, |
| 39 | + additionalTestArguments, |
| 40 | + searchSubfoldersForPackages: false, |
| 41 | + autoGenerateLaunchConfigurations: false, |
| 42 | + disableAutoResolve: false, |
| 43 | + attachmentsPath: "", |
| 44 | + pluginPermissions: () => ({ trusted: false }), |
| 45 | + pluginArguments: () => [], |
| 46 | + } as FolderConfiguration; |
| 47 | + } |
| 48 | + |
| 49 | + suiteSetup(() => { |
| 50 | + mockedBuildFlags = mockObject<BuildFlags>({ |
| 51 | + getDarwinTarget: () => undefined, |
| 52 | + }); |
| 53 | + |
| 54 | + mockedToolchain = mockObject<SwiftToolchain>({ |
| 55 | + buildFlags: instance(mockedBuildFlags), |
| 56 | + swiftVersion: new Version(6, 0, 0), |
| 57 | + sanitizer: () => undefined, |
| 58 | + }); |
| 59 | + |
| 60 | + mockedSwiftPackage = mockObject<SwiftPackage>({ |
| 61 | + getTargets: sinon.stub().resolves([{ name: "TestTarget" }]), |
| 62 | + name: Promise.resolve("TestPackage"), |
| 63 | + }); |
| 64 | + |
| 65 | + mockedFolderContext = mockObject<FolderContext>({ |
| 66 | + toolchain: instance(mockedToolchain), |
| 67 | + swiftPackage: instance(mockedSwiftPackage), |
| 68 | + workspaceFolder: { uri: { fsPath: "/test/workspace" }, name: "TestWorkspace" } as any, |
| 69 | + swiftVersion: new Version(6, 0, 0), |
| 70 | + relativePath: "", |
| 71 | + linuxMain: { |
| 72 | + exists: true, |
| 73 | + } as any as LinuxMain, |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + suite("TEST_ONLY_ARGUMENTS filtering", () => { |
| 78 | + let filterArgumentsSpy: sinon.SinonSpy; |
| 79 | + |
| 80 | + setup(() => { |
| 81 | + // Reset any existing spies |
| 82 | + sinon.restore(); |
| 83 | + |
| 84 | + // Spy on the BuildFlags.filterArguments method |
| 85 | + filterArgumentsSpy = sinon.spy(BuildFlags, "filterArguments"); |
| 86 | + |
| 87 | + // Mock configuration.folder to return test arguments |
| 88 | + additionalTestArgumentsConfig.setValue(() => createMockFolderConfig([])); |
| 89 | + }); |
| 90 | + |
| 91 | + teardown(() => { |
| 92 | + sinon.restore(); |
| 93 | + }); |
| 94 | + |
| 95 | + test("filters out test-only arguments for test builds", async () => { |
| 96 | + additionalTestArgumentsConfig.setValue(() => |
| 97 | + createMockFolderConfig([ |
| 98 | + "--no-parallel", |
| 99 | + "--filter", |
| 100 | + "TestCase", |
| 101 | + "--enable-code-coverage", |
| 102 | + ]) |
| 103 | + ); |
| 104 | + |
| 105 | + const config = await BuildConfigurationFactory.buildAll( |
| 106 | + instance(mockedFolderContext), |
| 107 | + true, // isTestBuild |
| 108 | + false // isRelease |
| 109 | + ); |
| 110 | + |
| 111 | + expect(filterArgumentsSpy).to.have.been.calledOnce; |
| 112 | + const [args] = filterArgumentsSpy.firstCall.args; |
| 113 | + |
| 114 | + expect(args).to.deep.equal([ |
| 115 | + "--no-parallel", |
| 116 | + "--filter", |
| 117 | + "TestCase", |
| 118 | + "--enable-code-coverage", |
| 119 | + ]); |
| 120 | + |
| 121 | + expect(config.args).to.include("--build-tests"); |
| 122 | + }); |
| 123 | + |
| 124 | + test("preserves build-compatible arguments for test builds", async () => { |
| 125 | + additionalTestArgumentsConfig.setValue(() => |
| 126 | + createMockFolderConfig([ |
| 127 | + "--scratch-path", |
| 128 | + "/tmp/build", |
| 129 | + "-Xswiftc", |
| 130 | + "-enable-testing", |
| 131 | + ]) |
| 132 | + ); |
| 133 | + |
| 134 | + // Act: Build configuration for test build |
| 135 | + await BuildConfigurationFactory.buildAll( |
| 136 | + instance(mockedFolderContext), |
| 137 | + true, // isTestBuild |
| 138 | + false // isRelease |
| 139 | + ); |
| 140 | + |
| 141 | + expect(filterArgumentsSpy).to.have.been.calledOnce; |
| 142 | + const [args] = filterArgumentsSpy.firstCall.args; |
| 143 | + expect(args).to.deep.equal([ |
| 144 | + "--scratch-path", |
| 145 | + "/tmp/build", |
| 146 | + "-Xswiftc", |
| 147 | + "-enable-testing", |
| 148 | + ]); |
| 149 | + }); |
| 150 | + |
| 151 | + test("does not filter arguments for non-test builds", async () => { |
| 152 | + additionalTestArgumentsConfig.setValue(() => |
| 153 | + createMockFolderConfig(["--no-parallel", "--scratch-path", "/tmp/build"]) |
| 154 | + ); |
| 155 | + |
| 156 | + await BuildConfigurationFactory.buildAll( |
| 157 | + instance(mockedFolderContext), |
| 158 | + false, // isTestBuild |
| 159 | + false // isRelease |
| 160 | + ); |
| 161 | + |
| 162 | + expect(filterArgumentsSpy).to.not.have.been.called; |
| 163 | + }); |
| 164 | + |
| 165 | + test("handles empty additionalTestArguments", async () => { |
| 166 | + additionalTestArgumentsConfig.setValue(() => createMockFolderConfig([])); |
| 167 | + |
| 168 | + await BuildConfigurationFactory.buildAll( |
| 169 | + instance(mockedFolderContext), |
| 170 | + true, // isTestBuild |
| 171 | + false // isRelease |
| 172 | + ); |
| 173 | + |
| 174 | + expect(filterArgumentsSpy).to.have.been.calledOnce; |
| 175 | + const [args] = filterArgumentsSpy.firstCall.args; |
| 176 | + expect(args).to.deep.equal([]); |
| 177 | + }); |
| 178 | + |
| 179 | + test("handles mixed arguments correctly", async () => { |
| 180 | + additionalTestArgumentsConfig.setValue(() => |
| 181 | + createMockFolderConfig([ |
| 182 | + "--no-parallel", // test-only (should be filtered out) |
| 183 | + "--scratch-path", |
| 184 | + "/tmp", // build-compatible (should be preserved) |
| 185 | + "--filter", |
| 186 | + "MyTest", // test-only (should be filtered out) |
| 187 | + "-Xswiftc", |
| 188 | + "-O", // build-compatible (should be preserved) |
| 189 | + "--enable-code-coverage", // test-only (should be filtered out) |
| 190 | + "--verbose", // build-compatible (should be preserved) |
| 191 | + ]) |
| 192 | + ); |
| 193 | + |
| 194 | + await BuildConfigurationFactory.buildAll( |
| 195 | + instance(mockedFolderContext), |
| 196 | + true, // isTestBuild |
| 197 | + false // isRelease |
| 198 | + ); |
| 199 | + |
| 200 | + expect(filterArgumentsSpy).to.have.been.calledOnce; |
| 201 | + const [args] = filterArgumentsSpy.firstCall.args; |
| 202 | + expect(args).to.deep.equal([ |
| 203 | + "--no-parallel", |
| 204 | + "--scratch-path", |
| 205 | + "/tmp", |
| 206 | + "--filter", |
| 207 | + "MyTest", |
| 208 | + "-Xswiftc", |
| 209 | + "-O", |
| 210 | + "--enable-code-coverage", |
| 211 | + "--verbose", |
| 212 | + ]); |
| 213 | + }); |
| 214 | + |
| 215 | + test("has correct include values for arguments with parameters", () => { |
| 216 | + // Access the private static property through the class |
| 217 | + const filter = (BuildConfigurationFactory as any).TEST_ONLY_ARGUMENTS; |
| 218 | + |
| 219 | + // Arguments that take 1 parameter |
| 220 | + const oneParamArgs = ["--filter", "--skip", "--num-workers", "--xunit-output"]; |
| 221 | + oneParamArgs.forEach(arg => { |
| 222 | + const filterItem = filter.find((f: any) => f.argument === arg); |
| 223 | + expect(filterItem, `${arg} should be in exclusion filter`).to.exist; |
| 224 | + expect(filterItem.include, `${arg} should exclude 1 parameter`).to.equal(1); |
| 225 | + }); |
| 226 | + |
| 227 | + // Arguments that take no parameters (flags) |
| 228 | + const noParamArgs = ["--parallel", "--no-parallel", "--list-tests"]; |
| 229 | + noParamArgs.forEach(arg => { |
| 230 | + const filterItem = filter.find((f: any) => f.argument === arg); |
| 231 | + expect(filterItem, `${arg} should be in exclusion filter`).to.exist; |
| 232 | + expect(filterItem.include, `${arg} should exclude 0 parameters`).to.equal(0); |
| 233 | + }); |
| 234 | + }); |
| 235 | + |
| 236 | + test("excludes expected test-only arguments", () => { |
| 237 | + // Access the private static property through the class |
| 238 | + const filter = (BuildConfigurationFactory as any).TEST_ONLY_ARGUMENTS; |
| 239 | + |
| 240 | + expect(filter).to.be.an("array"); |
| 241 | + |
| 242 | + // Verify test-only arguments are included in the exclusion list |
| 243 | + expect(filter.some((f: any) => f.argument === "--no-parallel")).to.be.true; |
| 244 | + expect(filter.some((f: any) => f.argument === "--parallel")).to.be.true; |
| 245 | + expect(filter.some((f: any) => f.argument === "--filter")).to.be.true; |
| 246 | + expect(filter.some((f: any) => f.argument === "--skip")).to.be.true; |
| 247 | + expect(filter.some((f: any) => f.argument === "--list-tests")).to.be.true; |
| 248 | + expect(filter.some((f: any) => f.argument === "--attachments-path")).to.be.true; |
| 249 | + expect(filter.some((f: any) => f.argument === "--enable-testable-imports")).to.be.true; |
| 250 | + expect(filter.some((f: any) => f.argument === "--xunit-output")).to.be.true; |
| 251 | + }); |
| 252 | + |
| 253 | + test("does not exclude build-compatible arguments", () => { |
| 254 | + // Access the private static property through the class |
| 255 | + const filter = (BuildConfigurationFactory as any).TEST_ONLY_ARGUMENTS; |
| 256 | + |
| 257 | + // Verify build-compatible arguments are NOT in the exclusion list |
| 258 | + expect(filter.some((f: any) => f.argument === "--scratch-path")).to.be.false; |
| 259 | + expect(filter.some((f: any) => f.argument === "-Xswiftc")).to.be.false; |
| 260 | + expect(filter.some((f: any) => f.argument === "--build-system")).to.be.false; |
| 261 | + expect(filter.some((f: any) => f.argument === "--sdk")).to.be.false; |
| 262 | + expect(filter.some((f: any) => f.argument === "--verbose")).to.be.false; |
| 263 | + expect(filter.some((f: any) => f.argument === "--configuration")).to.be.false; |
| 264 | + }); |
| 265 | + }); |
| 266 | +}); |
0 commit comments