Skip to content

Commit 1612342

Browse files
authored
Merge pull request #1 from chkpnt/master
Write file to the specified outputPath
2 parents 5baf68e + 4343a90 commit 1612342

File tree

6 files changed

+46
-5
lines changed

6 files changed

+46
-5
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ install: Xcodecoverageconverter
2828
uninstall:
2929
@rm -rf "$(bindir)/xcc"
3030

31+
.PHONY: test
32+
test:
33+
@swift test
34+
3135
.PHONY: clean
3236
distclean:
3337
@rm -f $(BUILDDIR)/release

Package.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import PackageDescription
55

66
let package = Package(
77
name: "XcodeCoverageConverter",
8+
platforms: [
9+
.macOS(.v10_12),
10+
],
811
products: [
912
.executable(name: "xcc", targets: ["XcodeCoverageConverter"])
1013
],

Sources/Core/Commands/GenerateCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public extension Xccov.Commands.Generate {
8282
.verbose(verbose,
8383
onFailure: { failure in print("The following error occured while converting to output formats, \(failure)") },
8484
onSuccess: { _ in print("The payload has been exported to output formats with success") })
85-
.mapEach { $0.write() }
85+
.mapEach { $0.write(atPath: outputPath) }
8686
.map { _ in () }
8787
}
8888
}

Sources/Core/Commons/Tools/Export+Write.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77

88
public extension Export {
9-
func write() -> Result<Export, Xccov.Error> {
10-
self.content.write(filename: self.filename).map { self }
9+
func write(atPath path: String) -> Result<Export, Xccov.Error> {
10+
self.content.write(toFile: self.filename, atPath: path).map { self }
1111
}
1212
}

Sources/Core/Commons/Tools/String+Write.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
// Created by Thibault Wittemberg on 2020-06-03.
66
//
77

8+
import Foundation
9+
810
public extension String {
9-
func write(filename: String) -> Result<Void, Xccov.Error> {
10-
guard (try? self.write(toFile: filename, atomically: true, encoding: .utf8)) != nil else {
11+
func write(toFile filename: String, atPath path: String) -> Result<Void, Xccov.Error> {
12+
let file = NSString.path(withComponents: [path, filename])
13+
guard (try? FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: true)) != nil,
14+
(try? self.write(toFile: file, atomically: true, encoding: .utf8)) != nil else {
1115
return .failure(.unableToWriteFile(self))
1216
}
1317

Tests/CoreTests/Commands/GenerateCommandTests.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,41 @@ final class GenerateCommandTests: XCTestCase {
6767
XCTAssertEqual(receivedResult, Result.failure(Xccov.Error.conversionFailed("")))
6868
}
6969

70+
func testExecute_writes_cobertura_xml_to_outputPath() {
71+
// Given: a json report in a temporary directory
72+
let tmpDir = FileManager.default.uniqueTemporaryDirectory()
73+
defer { try? FileManager.default.removeItem(at: tmpDir) }
74+
_ = converterFixtureCoverageJson.write(toFile: "report.json", atPath: tmpDir.path)
75+
76+
// When: converting it to a cobertura-xml using the export method
77+
let receivedResult = Xccov.Commands.Generate.execute(
78+
jsonFile: "\(tmpDir.path)/report.json",
79+
outputPath: "\(tmpDir.path)/reports/coverage/",
80+
outputs: [.coberturaXml],
81+
excludeTargets: [], excludePackages: [], verbose: false
82+
)
83+
84+
// Then: a file "cobertura.xml" exists in the specified outputPath
85+
XCTAssertTrue(FileManager.default.fileExists(atPath: "\(tmpDir.path)/reports/coverage/cobertura.xml"))
86+
XCTAssertNoThrow(try receivedResult.get())
87+
}
88+
7089
static var allTests = [
7190
("testFilename_return_expected_value_for_cobertura", testFilename_return_expected_value_for_cobertura),
7291
("testConvert_to_coberturalXml_return_success_when_coverageReport_is_valid", testConvert_to_coberturalXml_return_success_when_coverageReport_is_valid),
7392
("testConvert_to_failable_return_failure", testConvert_to_failable_return_failure),
7493
("testConvert_to_several_outputs_return_success_when_coverageReport_is_valid", testConvert_to_several_outputs_return_success_when_coverageReport_is_valid),
7594
("testConvert_to_several_outputs_return_failure_when_a_conversion_fails", testConvert_to_several_outputs_return_failure_when_a_conversion_fails),
95+
("testExecute_writes_cobertura_xml_to_outputPath", testExecute_writes_cobertura_xml_to_outputPath),
7696
]
7797
}
98+
99+
private extension FileManager {
100+
101+
func uniqueTemporaryDirectory(_ uuid: UUID = .init()) -> URL {
102+
let result = self.temporaryDirectory.appendingPathComponent(uuid.uuidString)
103+
try? self.createDirectory(at: result, withIntermediateDirectories: true, attributes: nil)
104+
return result
105+
}
106+
107+
}

0 commit comments

Comments
 (0)