Skip to content

Commit

Permalink
add tests for isErrorPathTooLong
Browse files Browse the repository at this point in the history
  • Loading branch information
philprime committed Jan 16, 2025
1 parent 0591b65 commit d0605ac
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Sources/Sentry/include/SentryFileManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ NS_ASSUME_NONNULL_BEGIN
@class SentryOptions;
@class SentrySession;

#if SENTRY_TEST || SENTRY_TEST_CI
BOOL isErrorPathTooLong(NSError *error);

BOOL createDirectoryIfNotExists(NSString *path, NSError **error);
#endif // SENTRY_TEST || SENTRY_TEST_CI

NS_SWIFT_NAME(SentryFileManager)
@interface SentryFileManager : NSObject
SENTRY_NO_INIT
Expand Down
128 changes: 128 additions & 0 deletions Tests/SentryTests/Helper/SentryFileManagerTests.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// swiftlint:disable file_length

@testable import Sentry
import SentryTestUtils
import XCTest
Expand Down Expand Up @@ -723,6 +725,132 @@ class SentryFileManagerTests: XCTestCase {
try "garbage".write(to: URL(fileURLWithPath: sut.timezoneOffsetFilePath), atomically: true, encoding: .utf8)
XCTAssertNil(sut.readTimezoneOffset())
}

func testIsErrorPathTooLong_underlyingErrorsAvailableAndMultipleErrorsGiven_shouldUseErrorInUserInfo() throws {
// -- Arrange --
guard #available(macOS 11.0, iOS 14.5, watchOS 7.4, tvOS 14.5, *) else {
throw XCTSkip("This test is only for macOS 11 and above")
}
// When accessing via `underlyingErrors`, the first result is the error set with `NSUnderlyingErrorKey`.
// This test asserts if that behavior changes.
let error = NSError(domain: NSCocoaErrorDomain, code: 1, userInfo: [
NSMultipleUnderlyingErrorsKey: [
NSError(domain: NSCocoaErrorDomain, code: 2, userInfo: nil)
],
NSUnderlyingErrorKey: NSError(domain: NSPOSIXErrorDomain, code: Int(ENAMETOOLONG), userInfo: nil)
])
// -- Act --
let result = isErrorPathTooLong(error)
// -- Assert --
XCTAssertTrue(result)
}

func testIsErrorPathTooLong_underlyingErrorsAvailableAndMultipleErrorsEmpty_shouldUseErrorInUserInfo() throws {
// -- Arrange --
guard #available(macOS 11.0, iOS 14.5, watchOS 7.4, tvOS 14.5, *) else {
throw XCTSkip("Test is disabled for this OS version")
}
// When accessing via `underlyingErrors`, the first result is the error set with `NSUnderlyingErrorKey`.
// This test asserts if that behavior changes.
let error = NSError(domain: NSCocoaErrorDomain, code: 1, userInfo: [
NSMultipleUnderlyingErrorsKey: [],

Check failure on line 756 in Tests/SentryTests/Helper/SentryFileManagerTests.swift

View workflow job for this annotation

GitHub Actions / Unit macOS - Xcode 16.2 - OS latest

'NSMultipleUnderlyingErrorsKey' is only available in macOS 11.3 or newer

Check failure on line 756 in Tests/SentryTests/Helper/SentryFileManagerTests.swift

View workflow job for this annotation

GitHub Actions / Unit iOS - Thread Sanitizer

empty collection literal requires an explicit type

Check failure on line 756 in Tests/SentryTests/Helper/SentryFileManagerTests.swift

View workflow job for this annotation

GitHub Actions / Unit macOS - Xcode 15.4 - OS latest

'NSMultipleUnderlyingErrorsKey' is only available in macOS 11.3 or newer
NSUnderlyingErrorKey: NSError(domain: NSPOSIXErrorDomain, code: Int(ENAMETOOLONG), userInfo: nil)
])
// -- Act --
let result = isErrorPathTooLong(error)
// -- Assert --
XCTAssertTrue(result)
}

func testIsErrorPathTooLong_underlyingErrorsAvailableAndMultipleErrorsNotSet_shouldUseErrorInUserInfo() throws {
// -- Arrange --
guard #available(macOS 11.0, iOS 14.5, watchOS 7.4, tvOS 14.5, *) else {
throw XCTSkip("Test is disabled for this OS version")
}
// When accessing via `underlyingErrors`, the first result is the error set with `NSUnderlyingErrorKey`.
// This test asserts if that behavior changes.
let error = NSError(domain: NSCocoaErrorDomain, code: 1, userInfo: [
NSUnderlyingErrorKey: NSError(domain: NSPOSIXErrorDomain, code: Int(ENAMETOOLONG), userInfo: nil)
])
// -- Act --
let result = isErrorPathTooLong(error)
// -- Assert --
XCTAssertTrue(result)
}

func testIsErrorPathTooLong_underlyingErrorsAvailableAndOnlyMultipleErrorsGiven_shouldUseErrorFirstError() throws {
// -- Arrange --
guard #available(macOS 11.0, iOS 14.5, watchOS 7.4, tvOS 14.5, *) else {
throw XCTSkip("Test is disabled for this OS version")
}
// When accessing via `underlyingErrors`, the first result is the error set with `NSUnderlyingErrorKey`.
// This test asserts if that behavior changes.
let error = NSError(domain: NSCocoaErrorDomain, code: 1, userInfo: [
NSMultipleUnderlyingErrorsKey: [

Check failure on line 789 in Tests/SentryTests/Helper/SentryFileManagerTests.swift

View workflow job for this annotation

GitHub Actions / Unit macOS - Xcode 16.2 - OS latest

'NSMultipleUnderlyingErrorsKey' is only available in macOS 11.3 or newer

Check failure on line 789 in Tests/SentryTests/Helper/SentryFileManagerTests.swift

View workflow job for this annotation

GitHub Actions / Unit macOS - Xcode 15.4 - OS latest

'NSMultipleUnderlyingErrorsKey' is only available in macOS 11.3 or newer
NSError(domain: NSPOSIXErrorDomain, code: Int(ENAMETOOLONG), userInfo: nil),
NSError(domain: NSCocoaErrorDomain, code: 2, userInfo: nil)
]
])
// -- Act --
let result = isErrorPathTooLong(error)
// -- Assert --
XCTAssertTrue(result)
}

func testIsErrorPathTooLong_underlyingErrorsNotAvailableAndErrorNotInUserInfo_shouldNotCheckError() throws {
// -- Arrange --
guard #unavailable(macOS 11.0, iOS 14.5, watchOS 7.4, tvOS 14.5) else {
throw XCTSkip("Test is disabled for this OS version")
}
// When accessing via `underlyingErrors`, the first result is the error set with `NSUnderlyingErrorKey`.
// This test asserts if that behavior changes.
let error = NSError(domain: NSCocoaErrorDomain, code: 1, userInfo: [:])
// -- Act --
let result = isErrorPathTooLong(error)
// -- Assert --
XCTAssertFalse(result)
}

func testIsErrorPathTooLong_underlyingErrorsNotAvailableAndNonErrorInUserInfo_shouldNotCheckError() throws {
// -- Arrange --
guard #unavailable(macOS 11.0, iOS 14.5, watchOS 7.4, tvOS 14.5) else {
throw XCTSkip("Test is disabled for this OS version")
}
// When accessing via `underlyingErrors`, the first result is the error set with `NSUnderlyingErrorKey`.
// This test asserts if that behavior changes.
let error = NSError(domain: NSCocoaErrorDomain, code: 1, userInfo: [
NSUnderlyingErrorKey: "This is not an error"
])
// -- Act --
let result = isErrorPathTooLong(error)
// -- Assert --
XCTAssertFalse(result)
}

func testIsErrorPathTooLong_underlyingErrorsNotAvailableAndErrorInUserInfo_shouldNotCheckError() throws {
// -- Arrange --
guard #unavailable(macOS 11.0, iOS 14.5, watchOS 7.4, tvOS 14.5) else {
throw XCTSkip("Test is disabled for this OS version")
}
// When accessing via `underlyingErrors`, the first result is the error set with `NSUnderlyingErrorKey`.
// This test asserts if that behavior changes.
let error = NSError(domain: NSCocoaErrorDomain, code: 1, userInfo: [
NSUnderlyingErrorKey: NSError(domain: NSPOSIXErrorDomain, code: Int(ENAMETOOLONG), userInfo: nil)
])
// -- Act --
let result = isErrorPathTooLong(error)
// -- Assert --
XCTAssertTrue(result)
}

func testIsErrrorPathTooLong_errorIsEnameTooLong_shouldReturnTrue() throws {
// -- Arrange --
let error = NSError(domain: NSPOSIXErrorDomain, code: Int(ENAMETOOLONG), userInfo: nil)
// -- Act --
let result = isErrorPathTooLong(error)
// -- Assert --
XCTAssertTrue(result)
}
}

#if os(iOS) || os(macOS) || targetEnvironment(macCatalyst)
Expand Down

0 comments on commit d0605ac

Please sign in to comment.