Skip to content

Commit 74feb40

Browse files
committed
add coverage tests
1 parent 6be786c commit 74feb40

File tree

2 files changed

+182
-46
lines changed

2 files changed

+182
-46
lines changed

Sources/Commands/SwiftTestCommand.swift

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ package enum CoverageFormat: String, ExpressibleByArgument, CaseIterable, Compar
240240

241241
extension CoverageFormat: Encodable {}
242242

243-
enum CoveragePrintPathMode: String, ExpressibleByArgument, CaseIterable {
243+
package enum CoveragePrintPathMode: String, ExpressibleByArgument, CaseIterable {
244244
case json
245245
case text
246246

@@ -1131,11 +1131,11 @@ extension SwiftTestCommand {
11311131
}
11321132
}
11331133

1134-
func printCodeCovPath(
1134+
package func getCodeCovOutputPaths(
11351135
_ swiftCommandState: SwiftCommandState,
11361136
formats: [CoverageFormat],
11371137
printMode: CoveragePrintPathMode,
1138-
) async throws {
1138+
) async throws -> String {
11391139
var coverageData = [CoverageFormat : AbsolutePath]()
11401140
for format in formats {
11411141
let config = try await self.getCodeCoverageConfiguration(swiftCommandState, format: format)
@@ -1165,7 +1165,20 @@ extension SwiftTestCommand {
11651165
data = try encoder.encode(coverageOutput)
11661166
}
11671167
}
1168-
print(String(decoding: data, as: UTF8.self))
1168+
return String(decoding: data, as: UTF8.self)
1169+
}
1170+
1171+
package func printCodeCovPath(
1172+
_ swiftCommandState: SwiftCommandState,
1173+
formats: [CoverageFormat],
1174+
printMode: CoveragePrintPathMode,
1175+
) async throws {
1176+
let output = try await self.getCodeCovOutputPaths(
1177+
swiftCommandState,
1178+
formats: formats,
1179+
printMode: printMode,
1180+
)
1181+
print(output)
11691182
}
11701183
}
11711184

Tests/CommandsTests/CoverageTests.swift

Lines changed: 165 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
import Foundation
1414
import RegexBuilder
1515
import Commands
16+
import CoreCommands
1617
import _InternalTestSupport
1718
import enum PackageModel.BuildConfiguration
1819
import func Basics.resolveSymlinks
1920
import struct Basics.AbsolutePath
2021
import struct SPMBuildCore.BuildSystemProvider
2122
import var Basics.localFileSystem
2223
import Testing
24+
import class TSCBasic.BufferedOutputByteStream
2325

2426
@Suite(
2527
.serializedIfOnWindows,
@@ -284,6 +286,7 @@ struct CoverageTests {
284286
"configuration",
285287
"coverage.html.report.args.txt",
286288
]
289+
287290
@Test(
288291
.tags(
289292
.Feature.Command.Test,
@@ -296,8 +299,6 @@ struct CoverageTests {
296299
buildData: BuildData,
297300
isResponseFileOutputAbsolutePath: Bool,
298301
) async throws {
299-
// let buildSystem = BuildSystemProvider.Kind.native
300-
// let configuration = BuildConfiguration.debug
301302
let buildSystem = buildData.buildSystem
302303
let configuration = buildData.config
303304
try await withKnownIssue{
@@ -337,6 +338,42 @@ struct CoverageTests {
337338
}
338339
}
339340

341+
@Test(
342+
.tags(
343+
.Feature.Command.Test,
344+
),
345+
arguments: getBuildData(for: SupportedBuildSystemOnAllPlatforms),
346+
)
347+
func existenceOfResponseFileWithNotOutputDirectorySpecifiedUsedTheDefaultLocation(
348+
buildData: BuildData,
349+
) async throws {
350+
let buildSystem = buildData.buildSystem
351+
let configuration = buildData.config
352+
// Verify the output directory argument specified in the response file override the default location.
353+
try await fixture(name: "Coverage/Simple") { fixturePath in
354+
let responseFilePath = fixturePath.appending(components: responseFilePathComponents)
355+
let responseFileContent = "--tab-size=10"
356+
357+
try localFileSystem.createDirectory(responseFilePath.parentDirectory, recursive: true)
358+
try localFileSystem.writeFileContents(responseFilePath, string: responseFileContent)
359+
360+
let (stdout, _) = try await executeSwiftTest(
361+
fixturePath,
362+
configuration: configuration,
363+
extraArgs: [
364+
"--show-coverage-path",
365+
"--coverage-format",
366+
"html",
367+
],
368+
buildSystem: buildSystem,
369+
)
370+
let actualOutput = stdout.trimmingCharacters(in: .whitespacesAndNewlines)
371+
372+
#expect(actualOutput.contains(fixturePath.pathString))
373+
}
374+
}
375+
376+
340377
@Test(
341378
arguments: getBuildData(for: SupportedBuildSystemOnAllPlatforms),
342379
)
@@ -346,27 +383,6 @@ struct CoverageTests {
346383
// Verify the arguments specified in the response file are used.
347384
try await fixture(name: "Coverage/Simple") { fixturePath in
348385
let responseFilePath = fixturePath.appending(components: responseFilePathComponents)
349-
let expectedRegex = Regex {
350-
"debug: "
351-
OneOrMore {
352-
.word
353-
354-
}
355-
"llvm-cov show"
356-
ZeroOrMore {
357-
.any
358-
}
359-
OneOrMore(.whitespace)
360-
"@"
361-
ZeroOrMore { // >> /tmp and /var can be symlinks, so we take this into account
362-
.any
363-
}
364-
"\(responseFilePath)"
365-
OneOrMore(.whitespace)
366-
ZeroOrMore {
367-
.any
368-
}
369-
}.anchorsMatchLineEndings()
370386

371387
try localFileSystem.writeFileContents(responseFilePath, string: "")
372388
expectFileExists(at: responseFilePath)
@@ -384,30 +400,137 @@ struct CoverageTests {
384400
$0.contains("llvm-cov show") && $0.contains(responseFileArgument) //$0.contains("@\(responseFilePath)")
385401
}
386402
#expect(contains.count >= 1)
387-
// #expect(
388-
// stderr.contains(expectedRegex),
389-
// """
390-
// expectedRegex: \(expectedRegex)
391-
// responseFilePath: \(responseFilePath)
392-
393-
// stderr\n------\n\(stderr)
394-
// """
395-
// )
396403
}
397404
}
398405
}
399406

400407
@Suite
401-
struct showCoveragePathTests {
402-
@Test
403-
func printPathModeTests() async throws {
404-
Issue.record("""
405-
Test needs to be implemented. Need to implnente cross matix of:
406-
- single/multiple formats
407-
- print in text/json format
408-
- specifying the same format type multiple times
409-
- response file [overrides | does not override] output directory
410-
""")
408+
struct ShowCoveragePathTests {
409+
let commonTestArgs = [
410+
"--show-codecov-path"
411+
]
412+
struct ShowCoveragePathTestData {
413+
let formats: [CoverageFormat]
414+
let printMode: CoveragePrintPathMode
415+
let expected: String
416+
}
417+
@Test(
418+
arguments: getBuildData(for: SupportedBuildSystemOnAllPlatforms), [
419+
ShowCoveragePathTestData(
420+
formats: [CoverageFormat.html],
421+
printMode: CoveragePrintPathMode.text,
422+
expected: "$(DEFAULT_BUILD_OUTPUT)/codecov/Simple-html",
423+
),
424+
ShowCoveragePathTestData(
425+
formats: [CoverageFormat.json],
426+
printMode: CoveragePrintPathMode.text,
427+
expected: "$(DEFAULT_BUILD_OUTPUT)/codecov/Simple.json",
428+
),
429+
ShowCoveragePathTestData(
430+
formats: [CoverageFormat.html, .json],
431+
printMode: CoveragePrintPathMode.text,
432+
expected: """
433+
Html: $(DEFAULT_BUILD_OUTPUT)/codecov/Simple-html
434+
Json: $(DEFAULT_BUILD_OUTPUT)/codecov/Simple.json
435+
""",
436+
),
437+
ShowCoveragePathTestData(
438+
formats: [CoverageFormat.json, .html],
439+
printMode: CoveragePrintPathMode.text,
440+
expected: """
441+
Html: $(DEFAULT_BUILD_OUTPUT)/codecov/Simple-html
442+
Json: $(DEFAULT_BUILD_OUTPUT)/codecov/Simple.json
443+
""",
444+
),
445+
ShowCoveragePathTestData(
446+
formats: [CoverageFormat.json, .html, .json],
447+
printMode: CoveragePrintPathMode.text,
448+
expected: """
449+
Html: $(DEFAULT_BUILD_OUTPUT)/codecov/Simple-html
450+
Json: $(DEFAULT_BUILD_OUTPUT)/codecov/Simple.json
451+
""",
452+
),
453+
454+
455+
ShowCoveragePathTestData(
456+
formats: [CoverageFormat.html],
457+
printMode: CoveragePrintPathMode.json,
458+
expected: """
459+
{
460+
"html" : "$(DEFAULT_BUILD_OUTPUT)/codecov/Simple-html"
461+
}
462+
""",
463+
),
464+
ShowCoveragePathTestData(
465+
formats: [CoverageFormat.json],
466+
printMode: CoveragePrintPathMode.json,
467+
expected: """
468+
{
469+
"json" : "$(DEFAULT_BUILD_OUTPUT)/codecov/Simple.json"
470+
}
471+
""",
472+
),
473+
ShowCoveragePathTestData(
474+
formats: [CoverageFormat.html, .json],
475+
printMode: CoveragePrintPathMode.json,
476+
expected: """
477+
{
478+
"html" : "$(DEFAULT_BUILD_OUTPUT)/codecov/Simple-html",
479+
"json" : "$(DEFAULT_BUILD_OUTPUT)/codecov/Simple.json"
480+
}
481+
""",
482+
),
483+
ShowCoveragePathTestData(
484+
formats: [CoverageFormat.json, .html],
485+
printMode: CoveragePrintPathMode.json,
486+
expected: """
487+
{
488+
"html" : "$(DEFAULT_BUILD_OUTPUT)/codecov/Simple-html",
489+
"json" : "$(DEFAULT_BUILD_OUTPUT)/codecov/Simple.json"
490+
}
491+
""",
492+
),
493+
ShowCoveragePathTestData(
494+
formats: [CoverageFormat.json, .html, .json],
495+
printMode: CoveragePrintPathMode.json,
496+
expected: """
497+
{
498+
"html" : "$(DEFAULT_BUILD_OUTPUT)/codecov/Simple-html",
499+
"json" : "$(DEFAULT_BUILD_OUTPUT)/codecov/Simple.json"
500+
}
501+
""",
502+
),
503+
504+
]
505+
)
506+
func specifiedFormatsFormatInTextModeOnlyDisplaysThePath(
507+
buildData: BuildData,
508+
testData: ShowCoveragePathTestData,
509+
) async throws {
510+
let buildSystem = buildData.buildSystem
511+
let configuration = buildData.config
512+
try await fixture(name: "Coverage/Simple") { fixturePath in
513+
let defaultBuildOUtput = try await executeSwiftBuild(
514+
fixturePath,
515+
configuration: configuration,
516+
extraArgs: ["--show-bin-path"],
517+
518+
buildSystem: buildSystem,
519+
).stdout.trimmingCharacters(in: .whitespacesAndNewlines)
520+
let updatedExpected = testData.expected.replacing("$(DEFAULT_BUILD_OUTPUT)", with: defaultBuildOUtput)
521+
522+
let actual = try await executeSwiftTest(
523+
fixturePath,
524+
configuration: configuration,
525+
extraArgs: self.commonTestArgs + [
526+
"--show-codecov-path-mode",
527+
testData.printMode.rawValue,
528+
] + testData.formats.flatMap({ ["--coverage-format", $0.rawValue]}),
529+
buildSystem: buildSystem,
530+
).stdout.trimmingCharacters(in: .whitespacesAndNewlines)
531+
532+
#expect(actual == updatedExpected)
533+
}
411534
}
412535
}
413536

0 commit comments

Comments
 (0)