Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OSSignposter sugar and make Logger not fail when testing. #51

Merged
merged 3 commits into from
Apr 23, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import Foundation
@_transparent
@usableFromInline
@inline(__always)
Expand Down
20 changes: 18 additions & 2 deletions Sources/LoggerDependency/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
extension Logger: DependencyKey {
public static var liveValue: Logger { Logger() }
public static var testValue: Logger {
/// - Note: It doesn't make a lot of sense to fail by default because we can't pass an
/// inspectable `Logger` value to assert logged messages when testing. Given the prominence of
/// logging failing by default is more annoying than useful in real-life scenarios.
/// Users who wish to assert that no logging occurs can override the `\.logger` dependency with
/// the `.unimplemented` logger.
public static var testValue: Logger { Logger() }
public static var previewValue: Logger { Logger() }

/// A `Logger` that fails when accessed while testing.
public static var unimplemented: Logger {
XCTFail(#"Unimplemented: @Dependency(\.logger)"#)
return Logger()
}
public static var previewValue: Logger { Logger() }
}

@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
Expand Down Expand Up @@ -54,4 +62,12 @@
Logger(subsystem: Bundle.main.bundleIdentifier ?? "", category: category)
}
}

@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
extension Logger {
/// Creates a `OSSignposter` to emit signpost to this logger value.
public var signpost: OSSignposter {
OSSignposter(logger: self)
}
}
#endif
10 changes: 3 additions & 7 deletions Tests/LoggerDependencyTests/LoggerDependencyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@
final class LoggerDependencyTests: XCTestCase {
@Dependency(\.logger) var logger

#if (os(iOS) || os(macOS) || os(tvOS) || os(watchOS)) && DEBUG
func testFailingTestLogger() {
XCTExpectFailure {
logger.log("TestValue")
}
}
#endif
func testNotFailingTestLogger() {
logger.log("TestValue")
}

func testLoggerCategory() {
@Dependency(\.logger["Logger.Dependency.Testing"]) var logger
Expand Down