Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
35 changes: 35 additions & 0 deletions Tests/PlainSettingsTests/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Foundation

private func expect(_ condition: @autoclosure () -> Bool, _ message: String) {
guard condition() else {
FileHandle.standardError.write(Data("FAIL: \(message)\n".utf8))
exit(1)
}
}

let shellScriptRule = PlainSettings(
patternFile: ".*",
patternMime: "^text/x-shellscript$",
isRegExp: true,
isCaseInsensitive: true,
UTI: "public.shell-script",
syntax: "shellscript"
)

expect(
shellScriptRule.test(filename: "hproxy", mimeType: "text/x-shellscript"),
"MIME regex should use the complete MIME value for short filenames"
)
expect(
shellScriptRule.test(
filename: String(repeating: "extensionless-shell-script-", count: 4),
mimeType: "text/x-shellscript"
),
"MIME regex should not use an out-of-bounds filename-derived range"
)
expect(
!shellScriptRule.test(filename: "script", mimeType: "text/x-python"),
"MIME regex should reject a different MIME type"
)

print("PlainSettingsTests passed")
13 changes: 13 additions & 0 deletions Tests/PlainSettingsTests/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh
set -eu

script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
repo_root=$(dirname "$(dirname "$script_dir")")
test_binary=$(mktemp "${TMPDIR:-/tmp}/PlainSettingsTests.XXXXXX")
trap 'rm -f "$test_binary"' EXIT

xcrun swiftc \
"$repo_root/XPCService/PlainSettings.swift" \
"$script_dir/main.swift" \
-o "$test_binary"
"$test_binary"
2 changes: 1 addition & 1 deletion XPCService/PlainSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ struct PlainSettings {
guard let regex = self.mimeRegExp else {
return false
}
guard regex.firstMatch(in: mimeType, options: [], range: NSRange(filename.startIndex..., in: filename)) != nil else {
guard regex.firstMatch(in: mimeType, options: [], range: NSRange(mimeType.startIndex..., in: mimeType)) != nil else {
return false
}
valid = true
Expand Down