Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions Sources/Basics/Netrc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,11 @@ public struct NetrcParser {
let matches = regex.matches(in: text, range: range)
var trimmedCommentsText = text
matches.forEach {
trimmedCommentsText = trimmedCommentsText
.replacingOccurrences(of: nsString.substring(with: $0.range), with: "")
let matchedString = nsString.substring(with: $0.range)
if !matchedString.starts(with: "\"") {
trimmedCommentsText = trimmedCommentsText
.replacingOccurrences(of: matchedString, with: "")
}
}
return trimmedCommentsText
}
Expand All @@ -151,12 +154,18 @@ private enum RegexUtil {
case machine, login, password, account, macdef, `default`

func capture(prefix: String = "", in match: NSTextCheckingResult, string: String) -> String? {
guard let range = Range(match.range(withName: prefix + rawValue), in: string) else { return nil }
return String(string[range])
if let quotedRange = Range(match.range(withName: prefix + rawValue + quotedIdentifier), in: string) {
return String(string[quotedRange])
} else if let range = Range(match.range(withName: prefix + rawValue), in: string) {
return String(string[range])
} else {
return nil
}
}
}

static let comments: String = "\\#[\\s\\S]*?.*$"
private static let quotedIdentifier = "quoted"
static let comments: String = "(\"[^\"]*\"|\\s#.*$)"
static let `default`: String = #"(?:\s*(?<default>default))"#
static let accountOptional: String = #"(?:\s*account\s+\S++)?"#
static let loginPassword: String =
Expand All @@ -171,6 +180,6 @@ private enum RegexUtil {
}

static func namedTrailingCapture(_ string: String, prefix: String = "") -> String {
#"\s*\#(string)\s+(?<\#(prefix + string)>\S++)"#
#"\s*\#(string)\s+(?:"(?<\#(prefix + string + quotedIdentifier)>[^"]*)"|(?<\#(prefix + string)>\S+))"#
}
}
77 changes: 76 additions & 1 deletion Tests/BasicsTests/NetrcTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -431,5 +431,80 @@ class NetrcTests: XCTestCase {
XCTAssertEqual(netrc.machines[1].login, "fred")
XCTAssertEqual(netrc.machines[1].password, "sunshine4ever")
}
}

func testComments() throws {
let content = """
# A comment at the beginning of the line
machine example.com # Another comment
login anonymous
password qw#erty
"""

let netrc = try NetrcParser.parse(content)

let machine = netrc.machines.first
XCTAssertEqual(machine?.name, "example.com")
XCTAssertEqual(machine?.login, "anonymous")
XCTAssertEqual(machine?.password, "qw#erty")
}

func testHashSymbolInPassword() throws {
let content = """
machine example.com
login anonymous
password qw#erty
"""

let netrc = try NetrcParser.parse(content)

let machine = netrc.machines.first
XCTAssertEqual(machine?.name, "example.com")
XCTAssertEqual(machine?.login, "anonymous")
XCTAssertEqual(machine?.password, "qw#erty")
}

func testQuotedPasswordWithSpace() throws {
let content = """
machine example.com
login anonymous
password "qw erty"
"""

let netrc = try NetrcParser.parse(content)

let machine = netrc.machines.first
XCTAssertEqual(machine?.name, "example.com")
XCTAssertEqual(machine?.login, "anonymous")
XCTAssertEqual(machine?.password, "qw erty")
}

func testQuotedPasswordWithSpaceAndHash() throws {
let content = """
machine example.com
login anonymous
password "qw #erty"
"""

let netrc = try NetrcParser.parse(content)

let machine = netrc.machines.first
XCTAssertEqual(machine?.name, "example.com")
XCTAssertEqual(machine?.login, "anonymous")
XCTAssertEqual(machine?.password, "qw #erty")
}

func testQuotedPasswordWithSpaceAndHashAndCommentAtEndOfLine() throws {
let content = """
machine example.com
login anonymous
password "qw #erty" # A comment
"""

let netrc = try NetrcParser.parse(content)

let machine = netrc.machines.first
XCTAssertEqual(machine?.name, "example.com")
XCTAssertEqual(machine?.login, "anonymous")
XCTAssertEqual(machine?.password, "qw #erty")
}
}