Skip to content

Commit 60c89ef

Browse files
authored
Unit test for MarkdownFile.write() (#2)
* Unit test for MarkdownFile.write() * New test with base path
1 parent 22e053e commit 60c89ef

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

Sources/MarkdownGenerator/MarkdownFile.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ public struct MarkdownFile {
3939
/// - content: MarkdownConvertible entity that will be rendered
4040
/// as the Markdown content of the file. Can be an `Array`.
4141
public init(filename: String, basePath: String = "", content: MarkdownConvertible) {
42-
self.filename = filename
43-
self.basePath = basePath
42+
self.filename = filename.trimmingCharacters(in: .whitespacesAndNewlines)
43+
self.basePath = basePath.trimmingCharacters(in: .whitespacesAndNewlines)
4444
self.content = content
4545
}
4646

4747
/// Computed property containing the file path (`<basePath>/<filename>.md`)
4848
public var filePath: String {
49-
return "\(basePath)/\(filename).md"
49+
return basePath.isEmpty ? "\(filename).md" : "\(basePath)/\(filename).md"
5050
}
5151

5252
/// Generate and write the Markdown file to disk.
@@ -63,6 +63,9 @@ public struct MarkdownFile {
6363
}
6464

6565
private func createDirectory(path: String) throws {
66+
guard path.isEmpty == false else {
67+
return
68+
}
6669
var isDir: ObjCBool = false
6770
if FileManager.default.fileExists(atPath: path, isDirectory: &isDir) == false {
6871
try FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)

Tests/MarkdownGeneratorTests/MarkdownFileTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ class MarkdownFileTests: XCTestCase {
1515
XCTAssertEqual(file.filePath, "Path/To/File/Test.md")
1616
}
1717

18+
func testFileWrite() throws {
19+
let content = MarkdownHeader(title: "Hello World!")
20+
let file = MarkdownFile(filename: "WriteTest", content: content)
21+
try file.write()
22+
23+
let fileContent = try String(contentsOfFile: "WriteTest.md")
24+
XCTAssertEqual(fileContent, content.markdown)
25+
}
26+
27+
func testFileWriteWithFolder() throws {
28+
let content = MarkdownHeader(title: "Hello World!")
29+
let file = MarkdownFile(filename: "WriteTest", basePath: "AFolder", content: content)
30+
try file.write()
31+
32+
let fileContent = try String(contentsOfFile: "AFolder/WriteTest.md")
33+
XCTAssertEqual(fileContent, content.markdown)
34+
}
35+
1836
static var allTests = [
1937
("testFilePath", testFilePath)
2038
]

codecov.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
comment:
2+
layout: "reach, diff, flags, files"
3+
behavior: default
4+
require_changes: false # if true: only post the comment if coverage changes
5+
require_base: no # [yes :: must have a base report to post]
6+
require_head: yes # [yes :: must have a head report to post]
7+
branches: null
8+

0 commit comments

Comments
 (0)