Skip to content

Commit

Permalink
Fix nameExcludingExtension bug when there’s no extension (#102)
Browse files Browse the repository at this point in the history
Currently using `nameExcludingExtension` on a location that doesn’t
actually have an extension results in an empty string, which this
patch fixes. Now the full name is returned instead in those situations.
  • Loading branch information
JohnSundell authored Jan 1, 2020
1 parent 19daa3e commit 941052d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Sources/Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ public extension Location {

/// The name of the location, excluding its `extension`.
var nameExcludingExtension: String {
return name.split(separator: ".").dropLast().joined()
let components = name.split(separator: ".")
guard components.count > 1 else { return name }
return components.dropLast().joined()
}

/// The file extension of the item at the location.
Expand Down
10 changes: 10 additions & 0 deletions Tests/FilesTests/FilesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,16 @@ class FilesTests: XCTestCase {
}
}

func testNameExcludingExtensionWithoutExtension() {
performTest {
let file = try folder.createFile(named: "File")
let subfolder = try folder.createSubfolder(named: "Subfolder")

XCTAssertEqual(file.nameExcludingExtension, "File")
XCTAssertEqual(subfolder.nameExcludingExtension, "Subfolder")
}
}

func testRelativePaths() {
performTest {
let file = try folder.createFile(named: "FileA")
Expand Down

0 comments on commit 941052d

Please sign in to comment.