Skip to content

Commit f60162e

Browse files
authored
Access filesystem outside of GULHeartbeatDateStorage initializer (#5977)
* Lazily create fileURL * Changelog * Added setup check to verify init does not access file system * Shortened constant name
1 parent 5d29cfc commit f60162e

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

GoogleUtilities/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# 6.7.0 -- M75
2+
- Lazily access filesystem outside of `GULHeartbeatDateStorage` initializer. (#5969)
23
- Update source imports to use repo-relative headers. (#5824)
34
- Source cleanups to remove pre-iOS 8 code. (#5841)
45

GoogleUtilities/Environment/GULHeartbeatDateStorage.m

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@
2020
@interface GULHeartbeatDateStorage ()
2121
/** The storage to store the date of the last sent heartbeat. */
2222
@property(nonatomic, readonly) NSFileCoordinator *fileCoordinator;
23+
/** The name of the file that stores heartbeat information. */
24+
@property(nonatomic, readonly) NSString *fileName;
2325
@end
2426

2527
@implementation GULHeartbeatDateStorage
2628

29+
@synthesize fileURL = _fileURL;
30+
2731
- (instancetype)initWithFileName:(NSString *)fileName {
2832
if (fileName == nil) {
2933
return nil;
@@ -32,11 +36,21 @@ - (instancetype)initWithFileName:(NSString *)fileName {
3236
self = [super init];
3337
if (self) {
3438
_fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
39+
_fileName = fileName;
40+
}
41+
return self;
42+
}
43+
44+
/** Lazy getter for fileURL
45+
* @return fileURL where heartbeat information is stored.
46+
*/
47+
- (NSURL *)fileURL {
48+
if (!_fileURL) {
3549
NSURL *directoryURL = [[self class] directoryPathURL];
3650
[[self class] checkAndCreateDirectory:directoryURL fileCoordinator:_fileCoordinator];
37-
_fileURL = [directoryURL URLByAppendingPathComponent:fileName];
51+
_fileURL = [directoryURL URLByAppendingPathComponent:_fileName];
3852
}
39-
return self;
53+
return _fileURL;
4054
}
4155

4256
/** Returns the URL path of the Application Support folder.

GoogleUtilities/Example/Tests/Environment/GULHeartbeatDateStorageTest.m

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
#import "GoogleUtilities/Environment/Private/GULHeartbeatDateStorage.h"
1919

2020
@interface GULHeartbeatDateStorageTest : XCTestCase
21-
@property(nonatomic) NSURL *fileURL;
2221
@property(nonatomic) GULHeartbeatDateStorage *storage;
2322
@end
2423

24+
static NSString *const kTestFileName = @"GULStorageHeartbeatTest";
25+
2526
@implementation GULHeartbeatDateStorageTest
2627

2728
- (void)setUp {
@@ -38,7 +39,10 @@ - (void)setUp {
3839
error:&error],
3940
@"Error: %@", error);
4041
}
41-
self.storage = [[GULHeartbeatDateStorage alloc] initWithFileName:@"GULStorageHeartbeatTest"];
42+
43+
self.storage = [[GULHeartbeatDateStorage alloc] initWithFileName:kTestFileName];
44+
45+
[self assertInitializationDoesNotAccessFileSystem];
4246
}
4347

4448
- (void)tearDown {
@@ -53,4 +57,24 @@ - (void)testHeartbeatDateForTag {
5357
[[self.storage heartbeatDateForTag:@"fire-iid"] timeIntervalSinceReferenceDate]);
5458
}
5559

60+
#pragma mark - Private Helpers
61+
62+
- (void)assertInitializationDoesNotAccessFileSystem {
63+
NSURL *fileURL = [self heartbeatFileURL];
64+
NSError *error;
65+
BOOL fileIsReachable = [fileURL checkResourceIsReachableAndReturnError:&error];
66+
XCTAssertFalse(fileIsReachable,
67+
@"GULHeartbeatDateStorage initialization should not access the file system.");
68+
XCTAssertNotNil(error, @"Error: %@", error);
69+
}
70+
71+
- (NSURL *)heartbeatFileURL {
72+
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(
73+
NSApplicationSupportDirectory, NSUserDomainMask, YES) firstObject];
74+
NSArray<NSString *> *components = @[ documentsPath, @"Google/FIRApp", kTestFileName ];
75+
NSString *fileString = [NSString pathWithComponents:components];
76+
NSURL *fileURL = [NSURL fileURLWithPath:fileString];
77+
return fileURL;
78+
}
79+
5680
@end

0 commit comments

Comments
 (0)