Skip to content

Commit 741cfed

Browse files
authored
Add support for swift test --quiet. (#421)
This PR adds support for `swift test --quiet` and generalizes various "is verbose" checks into "verbosity level" checks instead. Several of the properties of `__CommandLineArguments_v0` have been made optional because they are not required to be specified (and should never have been non-optional.) ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 8bbef19 commit 741cfed

File tree

8 files changed

+215
-61
lines changed

8 files changed

+215
-61
lines changed

Sources/Testing/EntryPoints/EntryPoint.swift

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func entryPoint(passing args: consuming __CommandLineArguments_v0?, eventHandler
2222

2323
do {
2424
let args = try args ?? parseCommandLineArguments(from: CommandLine.arguments())
25-
if args.listTests {
25+
if args.listTests ?? true {
2626
for testID in await listTestsForEntryPoint(Test.all) {
2727
#if SWT_TARGET_OS_APPLE && !SWT_NO_FILE_IO
2828
try? FileHandle.stdout.write("\(testID)\n")
@@ -55,7 +55,7 @@ func entryPoint(passing args: consuming __CommandLineArguments_v0?, eventHandler
5555
#if !SWT_NO_FILE_IO
5656
var options = Event.ConsoleOutputRecorder.Options()
5757
options = .for(.stderr)
58-
options.isVerbose = args.verbose
58+
options.verbosity = args.verbosity
5959
let eventRecorder = Event.ConsoleOutputRecorder(options: options) { string in
6060
try? FileHandle.stderr.write(string)
6161
}
@@ -143,13 +143,49 @@ public struct __CommandLineArguments_v0: Sendable {
143143
public init() {}
144144

145145
/// The value of the `--list-tests` argument.
146-
public var listTests = false
146+
public var listTests: Bool? = false
147147

148148
/// The value of the `--parallel` or `--no-parallel` argument.
149-
public var parallel = true
149+
public var parallel: Bool? = true
150150

151151
/// The value of the `--verbose` argument.
152-
public var verbose = false
152+
public var verbose: Bool? = false
153+
154+
/// The value of the `--very-verbose` argument.
155+
public var veryVerbose: Bool? = false
156+
157+
/// The value of the `--quiet` argument.
158+
public var quiet: Bool? = false
159+
160+
/// Storage for the ``verbosity`` property.
161+
private var _verbosity: Int?
162+
163+
/// The value of the `--verbosity` argument.
164+
///
165+
/// The value of this property may be synthesized from the `--verbose`,
166+
/// `--very-verbose`, or `--quiet` arguments.
167+
///
168+
/// When the value of this property is greater than `0`, additional output
169+
/// is provided. When the value of this property is less than `0`, some
170+
/// output is suppressed. The exact effects of this property are
171+
/// implementation-defined and subject to change.
172+
public var verbosity: Int {
173+
get {
174+
if let _verbosity {
175+
return _verbosity
176+
} else if veryVerbose == true {
177+
return 2
178+
} else if verbose == true {
179+
return 1
180+
} else if quiet == true {
181+
return -1
182+
}
183+
return 0
184+
}
185+
set {
186+
_verbosity = newValue
187+
}
188+
}
153189

154190
/// The value of the `--xunit-output` argument.
155191
public var xunitOutput: String?
@@ -201,7 +237,26 @@ public struct __CommandLineArguments_v0: Sendable {
201237
var xcTestCaseHostIdentifier: String?
202238
}
203239

204-
extension __CommandLineArguments_v0: Codable {}
240+
extension __CommandLineArguments_v0: Codable {
241+
// Explicitly list the coding keys so that storage properties like _verbosity
242+
// do not end up with leading underscores when encoded.
243+
enum CodingKeys: String, CodingKey {
244+
case listTests
245+
case parallel
246+
case verbose
247+
case veryVerbose
248+
case quiet
249+
case _verbosity = "verbosity"
250+
case xunitOutput
251+
case experimentalEventStreamOutput
252+
case experimentalEventStreamVersion
253+
case filter
254+
case skip
255+
case repetitions
256+
case repeatUntil
257+
case xcTestCaseHostIdentifier
258+
}
259+
}
205260

206261
/// Initialize this instance given a sequence of command-line arguments passed
207262
/// from Swift Package Manager.
@@ -268,9 +323,20 @@ func parseCommandLineArguments(from args: [String]) throws -> __CommandLineArgum
268323
result.parallel = false
269324
}
270325

271-
if args.contains("--verbose") || args.contains("-v") || args.contains("--very-verbose") || args.contains("--vv") {
326+
// Verbosity
327+
if let verbosityIndex = args.firstIndex(of: "--verbosity"), !isLastArgument(at: verbosityIndex),
328+
let verbosity = Int(args[args.index(after: verbosityIndex)]) {
329+
result.verbosity = verbosity
330+
}
331+
if args.contains("--verbose") || args.contains("-v") {
272332
result.verbose = true
273333
}
334+
if args.contains("--very-verbose") || args.contains("--vv") {
335+
result.veryVerbose = true
336+
}
337+
if args.contains("--quiet") || args.contains("-q") {
338+
result.quiet = true
339+
}
274340

275341
// Filtering
276342
func filterValues(forArgumentsWithLabel label: String) -> [String] {
@@ -308,7 +374,7 @@ public func configurationForEntryPoint(from args: __CommandLineArguments_v0) thr
308374
var configuration = Configuration()
309375

310376
// Parallelization (on by default)
311-
configuration.isParallelizationEnabled = args.parallel
377+
configuration.isParallelizationEnabled = args.parallel ?? true
312378

313379
#if !SWT_NO_FILE_IO
314380
// XML output

Sources/Testing/EntryPoints/SwiftPMEntryPoint.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ private import _TestingInternals
2525
///
2626
/// - Warning: This function is used by Swift Package Manager. Do not call it
2727
/// directly.
28-
@_disfavoredOverload public func __swiftPMEntryPoint(passing args: __CommandLineArguments_v0? = nil) async -> CInt {
28+
public func __swiftPMEntryPoint(passing args: __CommandLineArguments_v0? = nil) async -> CInt {
2929
await entryPoint(passing: args, eventHandler: nil)
3030
}
3131

Sources/Testing/EntryPoints/XCTestScaffold.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ public enum XCTestScaffold: Sendable {
106106
/// Private Use Area. To disable the use of SF Symbols on macOS, set the
107107
/// `SWT_SF_SYMBOLS_ENABLED` environment variable to `"false"` or `"0"`.
108108
///
109+
/// To adjust the verbosity of output, set the `SWT_VERBOSITY` environment
110+
/// variable to an integer value greater or less than `0` (the default level.)
111+
/// ``XCTestScaffold`` does not support the `--verbose`, `--very-verbose`, or
112+
/// `--quiet` command-line arguments passed to `swift test`.
113+
///
109114
/// ## See Also
110115
///
111116
/// - <doc:TemporaryGettingStarted>
@@ -135,7 +140,9 @@ public enum XCTestScaffold: Sendable {
135140

136141
var args = __CommandLineArguments_v0()
137142
args.parallel = false
138-
args.verbose = (Environment.flag(named: "SWT_VERBOSE_OUTPUT") == true)
143+
if let verbosity = Environment.variable(named: "SWT_VERBOSITY").flatMap(Int.init) {
144+
args.verbosity = verbosity
145+
}
139146

140147
// Specify the hosting XCTestCase instance. This value is currently only
141148
// used for exit tests.

Sources/Testing/Events/Recorder/Event.ConsoleOutputRecorder.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ extension Event {
5959
public var useSFSymbols = false
6060
#endif
6161

62-
/// Whether or not to record verbose output.
62+
/// The level of verbosity of the output.
6363
///
64-
/// When the value of this property is `true`, additional output is
65-
/// provided. The exact nature of the additional output is
64+
/// When the value of this property is greater than `0`, additional output
65+
/// is provided. When the value of this property is less than `0`, some
66+
/// output is suppressed. The exact effects of this property are
6667
/// implementation-defined and subject to change.
67-
public var isVerbose = false
68+
public var verbosity = 0
6869

6970
/// Storage for ``tagColors``.
7071
private var _tagColors = Tag.Color.predefined
@@ -306,7 +307,7 @@ extension Event.ConsoleOutputRecorder {
306307
/// - Returns: Whether any output was produced and written to this instance's
307308
/// destination.
308309
@discardableResult public func record(_ event: borrowing Event, in context: borrowing Event.Context) -> Bool {
309-
let messages = _humanReadableOutputRecorder.record(event, in: context, verbosely: options.isVerbose)
310+
let messages = _humanReadableOutputRecorder.record(event, in: context, verbosity: options.verbosity)
310311
for message in messages {
311312
let symbol = message.symbol?.stringValue(options: options) ?? " "
312313

0 commit comments

Comments
 (0)