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

refactor: rename Bugsnag.addAttribute to addMetadata, add tests #454

Closed
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Bugsnag Notifiers on other platforms.

* Add a breadcrumb when Bugsnag first starts with the message "Bugsnag loaded"
[#445](https://github.com/bugsnag/bugsnag-cocoa/pull/445)

* `Bugsnag.addAttribute:value:tab:` is now `Bugsnag.addMetadataToSection::key:value:`
[#454](https://github.com/bugsnag/bugsnag-cocoa/pull/454)

* Add a per-Event `apiKey` property. This defaults to the global
`BugsnagConfiguration` value but can be overridden in event passed to the
Expand Down
13 changes: 7 additions & 6 deletions Source/Bugsnag.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,16 @@ static NSString *_Nonnull const BugsnagSeverityInfo = @"info";
*
* See also [Bugsnag configuration].metaData;
*
* @param attributeName The name of the data.
* @param key The name of the data.
*
* @param value Its value.
* @param value Its value.
*
* @param tabName The tab to show it on on the Bugsnag dashboard.
* @param section The tab to show it on on the Bugsnag dashboard.
*/
+ (void)addAttribute:(NSString *_Nonnull)attributeName
withValue:(id _Nullable)value
toTabWithName:(NSString *_Nonnull)tabName;
+ (void)addMetadataToSection:(NSString *_Nonnull)section
key:(NSString *_Nonnull)key
value:(id _Nullable)value
NS_SWIFT_NAME(addMetadata(_:key:value:));

/** Remove custom data from Bugsnag reports.
*
Expand Down
14 changes: 9 additions & 5 deletions Source/Bugsnag.m
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,17 @@ + (void)internalClientNotify:(NSException *_Nonnull)exception
}
}

+ (void)addAttribute:(NSString *)attributeName
withValue:(id)value
toTabWithName:(NSString *)tabName {
/**
* Add custom data to send to Bugsnag with every exception. If value is nil,
* delete the current value for attributeName
*/
+ (void)addMetadataToSection:(NSString *_Nonnull)section
key:(NSString *_Nonnull)key
value:(id _Nullable)value {
if ([self bugsnagStarted]) {
[self.notifier.configuration.metadata addAttribute:attributeName
[self.notifier.configuration.metadata addAttribute:key
withValue:value
toTabWithName:tabName];
toTabWithName:section];
}
}

Expand Down
67 changes: 67 additions & 0 deletions Tests/BugsnagTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// BugsnagTests.m
// Tests
//
// Created by Robin Macharg on 04/02/2020.
// Copyright © 2020 Bugsnag. All rights reserved.
//
// Unit tests of global Bugsnag behaviour

#import "Bugsnag.h"
#import "BugsnagTestConstants.h"
#import <XCTest/XCTest.h>

@interface BugsnagTests : XCTestCase

@end

@implementation BugsnagTests

/**
* Test that global metadata is added correctly, applied to each event, and
* deleted appropriately.
*/
- (void)testBugsnagMetadataAddition {

NSError *error;
BugsnagConfiguration *configuration = [[BugsnagConfiguration alloc] initWithApiKey:DUMMY_APIKEY_32CHAR_1 error:&error];
[configuration addBeforeSendBlock:^bool(NSDictionary * _Nonnull rawEventData, BugsnagEvent * _Nonnull reports) {
return false;
}];
[Bugsnag startBugsnagWithConfiguration:configuration];
[Bugsnag addMetadataToSection:@"mySection1" key:@"aKey1" value:@"aValue1"];

// We should see our added metadata in every request. Let's try a couple:

NSException *exception1 = [[NSException alloc] initWithName:@"exception1" reason:@"reason1" userInfo:nil];
NSException *exception2 = [[NSException alloc] initWithName:@"exception2" reason:@"reason2" userInfo:nil];

[Bugsnag notify:exception1 block:^(BugsnagEvent * _Nonnull report) {
XCTAssertEqual([[[report metadata] valueForKey:@"mySection1"] valueForKey:@"aKey1"], @"aValue1");
XCTAssertEqual([report errorClass], @"exception1");
XCTAssertEqual([report errorMessage], @"reason1");
XCTAssertNil([[report metadata] valueForKey:@"mySection2"]);

// Add some additional metadata once we're sure it's not already there
[Bugsnag addMetadataToSection:@"mySection2" key:@"aKey2" value:@"aValue2"];
}];

[Bugsnag notify:exception2 block:^(BugsnagEvent * _Nonnull report) {
XCTAssertEqual([[[report metadata] valueForKey:@"mySection1"] valueForKey:@"aKey1"], @"aValue1");
XCTAssertEqual([[[report metadata] valueForKey:@"mySection2"] valueForKey:@"aKey2"], @"aValue2");
XCTAssertEqual([report errorClass], @"exception2");
XCTAssertEqual([report errorMessage], @"reason2");
}];

// Check nil value causes deletions

[Bugsnag addMetadataToSection:@"mySection1" key:@"aKey1" value:nil];
[Bugsnag addMetadataToSection:@"mySection2" key:@"aKey2" value:nil];

[Bugsnag notify:exception1 block:^(BugsnagEvent * _Nonnull report) {
XCTAssertNil([[[report metadata] valueForKey:@"mySection1"] valueForKey:@"aKey1"]);
XCTAssertNil([[[report metadata] valueForKey:@"mySection2"] valueForKey:@"aKey2"]);
}];
}

@end
11 changes: 11 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,15 @@ Swift:
- Bugsnag.setBreadcrumbCapacity(40)
let config = BugsnagConfiguration()
+ config.setMaxBreadcrumbs(40)
let config = try BugsnagConfiguration("VALID 32 CHARACTER API KEY")

ObjC:

- [Bugsnag addAttribute:WithValuetoTabWithName:]
+ [Bugsnag addMetadataToSection:key:value:]

Swift:

- Bugsnag.addAttribute(attributeName:withValue:toTabWithName:)
+ Bugsnag.addMetadata(_:key:value:)
```
8 changes: 8 additions & 0 deletions iOS/Bugsnag.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
000E6EA423D8AC8C009D8194 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 000E6EA023D8AC8C009D8194 /* README.md */; };
000E6EA523D8AC8C009D8194 /* VERSION in Resources */ = {isa = PBXBuildFile; fileRef = 000E6EA123D8AC8C009D8194 /* VERSION */; };
000E6EA623D8AC8C009D8194 /* CHANGELOG.md in Resources */ = {isa = PBXBuildFile; fileRef = 000E6EA223D8AC8C009D8194 /* CHANGELOG.md */; };
00D7ACAD23E9C63000FBE4A7 /* BugsnagTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 00D7ACAC23E9C63000FBE4A7 /* BugsnagTests.m */; };
00D7ACAF23EABBC800FBE4A7 /* BugsnagSwiftTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00D7ACAE23EABBC800FBE4A7 /* BugsnagSwiftTests.swift */; };
4B47970A22A9AE1F00FF9C2E /* BugsnagEventFromKSCrashReportTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B47970922A9AE1F00FF9C2E /* BugsnagEventFromKSCrashReportTest.m */; };
4B775FCF22CBDEB4004839C5 /* BugsnagCollectionsBSGDictInsertIfNotNilTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B775FCE22CBDEB4004839C5 /* BugsnagCollectionsBSGDictInsertIfNotNilTest.m */; };
4BE6C42622CAD61A0056305D /* BugsnagCollectionsBSGDictMergeTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BE6C42522CAD61A0056305D /* BugsnagCollectionsBSGDictMergeTest.m */; };
Expand Down Expand Up @@ -412,6 +414,8 @@
000E6EA023D8AC8C009D8194 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
000E6EA123D8AC8C009D8194 /* VERSION */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = VERSION; path = ../VERSION; sourceTree = "<group>"; };
000E6EA223D8AC8C009D8194 /* CHANGELOG.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = CHANGELOG.md; path = ../CHANGELOG.md; sourceTree = "<group>"; };
00D7ACAC23E9C63000FBE4A7 /* BugsnagTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = BugsnagTests.m; path = ../../Tests/BugsnagTests.m; sourceTree = "<group>"; };
00D7ACAE23EABBC800FBE4A7 /* BugsnagSwiftTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BugsnagSwiftTests.swift; sourceTree = "<group>"; };
4B3B193422CA7B0900475354 /* BugsnagCollectionsBSGDictSetSafeObjectTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = BugsnagCollectionsBSGDictSetSafeObjectTest.m; path = ../../Tests/BugsnagCollectionsBSGDictSetSafeObjectTest.m; sourceTree = "<group>"; };
4B47970922A9AE1F00FF9C2E /* BugsnagEventFromKSCrashReportTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BugsnagEventFromKSCrashReportTest.m; sourceTree = "<group>"; };
4B775FCE22CBDEB4004839C5 /* BugsnagCollectionsBSGDictInsertIfNotNilTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = BugsnagCollectionsBSGDictInsertIfNotNilTest.m; path = ../../Tests/BugsnagCollectionsBSGDictInsertIfNotNilTest.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -638,6 +642,7 @@
isa = PBXGroup;
children = (
000E6E9D23D8690F009D8194 /* BugsnagSwiftConfigurationTests.swift */,
00D7ACAE23EABBC800FBE4A7 /* BugsnagSwiftTests.swift */,
);
path = "Swift Tests";
sourceTree = "<group>";
Expand Down Expand Up @@ -755,6 +760,7 @@
000E6E9B23D84DB1009D8194 /* BugsnagTestConstants.h */,
000E6E9C23D8690E009D8194 /* Tests-Bridging-Header.h */,
000DF29323DB4B4900A883CE /* TestConstants.m */,
00D7ACAC23E9C63000FBE4A7 /* BugsnagTests.m */,
);
name = Tests;
path = BugsnagTests;
Expand Down Expand Up @@ -1244,6 +1250,7 @@
E78C1EFE1FCC778700B976D3 /* BugsnagUserTest.m in Sources */,
8A2C8F911C6BBFDD00846019 /* BugsnagSinkTests.m in Sources */,
E784D2551FD70B3B004B01E1 /* KSCrashState_Tests.m in Sources */,
00D7ACAD23E9C63000FBE4A7 /* BugsnagTests.m in Sources */,
8A2C8F901C6BBFDD00846019 /* BugsnagEventTests.m in Sources */,
E77316E31F73E89E00A14F06 /* BugsnagHandledStateTest.m in Sources */,
E70EE0961FD7071F00FA745C /* FileBasedTestCase.m in Sources */,
Expand All @@ -1262,6 +1269,7 @@
F4295995C3259BF7D9730BC4 /* BugsnagKSCrashSysInfoParserTest.m in Sources */,
E70E52152216E41C00A590AB /* BugsnagSessionTrackerStopTest.m in Sources */,
F4295F017754324FD52CCE46 /* RegisterErrorDataTest.m in Sources */,
00D7ACAF23EABBC800FBE4A7 /* BugsnagSwiftTests.swift in Sources */,
F42952D83435C02F8D891C40 /* BugsnagThreadTest.m in Sources */,
4B47970A22A9AE1F00FF9C2E /* BugsnagEventFromKSCrashReportTest.m in Sources */,
);
Expand Down
39 changes: 39 additions & 0 deletions iOS/BugsnagTests/Swift Tests/BugsnagSwiftTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// BugsnagSwiftTests.swift
// Tests
//
// Created by Robin Macharg on 05/02/2020.
// Copyright © 2020 Bugsnag. All rights reserved.
//
// Swift unit tests of global Bugsnag behaviour

import XCTest

class BugsnagSwiftTests: XCTestCase {

/**
* Confirm that the method is exposed to Swift correctly
*/
func testAddMetadataToSectionIsExposedToSwiftCorrectly() {
do {
if let configuration = try BugsnagConfiguration(DUMMY_APIKEY_32CHAR_1) {
configuration.add { (_, _) -> Bool in
return false
}
Bugsnag.start(with: configuration)
Bugsnag.addMetadata("mySection1", key: "myKey1", value: "myValue1")

let exception1 = NSException(name: NSExceptionName(rawValue: "exception1"), reason: "reason1", userInfo: nil)

Bugsnag.notify(exception1) { (event) in
// Arbitrary test, replicating the ObjC one
let value = (event.metadata["mySection1"] as! [String : Any])["myKey1"] as! String
XCTAssertEqual(value, "myValue1")
}
}
}
catch let e as NSError {
print(e)
}
}
}