diff --git a/scripts/__tests__/format-swift-test.js b/scripts/__tests__/format-swift-test.js new file mode 100644 index 00000000000..1239400a110 --- /dev/null +++ b/scripts/__tests__/format-swift-test.js @@ -0,0 +1,63 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @noflow + * @format + */ + +'use strict'; + +jest.mock('../format-utils', () => ({ + IS_META_CHECKOUT: false, + commandVersion: jest.fn(), + findMetaTool: jest.fn(() => null), +})); +jest.mock('tinyglobby', () => ({globSync: jest.fn(() => [])})); + +const originalSwiftFormat = process.env.SWIFT_FORMAT; + +beforeEach(() => { + delete process.env.SWIFT_FORMAT; +}); + +afterEach(() => { + if (originalSwiftFormat === undefined) { + delete process.env.SWIFT_FORMAT; + } else { + process.env.SWIFT_FORMAT = originalSwiftFormat; + } + jest.resetModules(); + jest.clearAllMocks(); + jest.restoreAllMocks(); +}); + +test.each([ + ['6.4', true], + ['6.2', false], +])( + 'checks the Swift toolchain version when the formatter reports main (%s)', + (version, accepted) => { + const {commandVersion} = require('../format-utils'); + const {globSync} = require('tinyglobby'); + commandVersion.mockImplementation((command, args) => { + if (command === 'swift' && args?.[0] === 'format') { + return {status: 0, output: 'main'}; + } + if (command === 'swift') { + return { + status: 0, + output: `Apple Swift version ${version} (swiftlang)`, + }; + } + return {status: 1, output: ''}; + }); + jest.spyOn(console, 'warn').mockImplementation(() => {}); + + require('../format-swift'); + + expect(globSync).toHaveBeenCalledTimes(accepted ? 1 : 0); + }, +); diff --git a/scripts/format-swift.js b/scripts/format-swift.js index 74885bd7d79..342a16efd7a 100644 --- a/scripts/format-swift.js +++ b/scripts/format-swift.js @@ -81,7 +81,20 @@ function findSwiftFormat() { } for (const [command, prefixArguments] of candidates) { const result = commandVersion(command, prefixArguments); - const version = parseSwiftFormatVersion(result.output); + let version = parseSwiftFormatVersion(result.output); + if ( + result.status === 0 && + version == null && + result.output.trim() === 'main' && + command === 'swift' && + prefixArguments.length === 1 && + prefixArguments[0] === 'format' + ) { + const toolchain = commandVersion(command); + if (toolchain.status === 0) { + version = parseSwiftFormatVersion(toolchain.output); + } + } if ( result.status === 0 && version != null &&