From a102f5cd7db3810e3c6f347af658ea1790bd655b Mon Sep 17 00:00:00 2001 From: iska Date: Mon, 13 Mar 2017 23:01:53 +0100 Subject: [PATCH] Fix loading of test resources Building and testing the project via Xcode copies the resources into the testing bundle, which is not the case for SwiftPM. Hence the helper method, that tries loading the resource from the bundle first. If it is nil then the resource is loaded from the test module path directly. --- Tests/HTMLKitTests/CSSSelectorTest.m | 4 ++-- Tests/HTMLKitTests/HTML5LibTokenizerTest.m | 10 ++++------ .../HTML5LibTreeConstructionTest.m | 8 +++----- Tests/HTMLKitTests/HTMLKitParserPerformance.m | 5 ++--- Tests/HTMLKitTests/HTMLKitTestUtil.h | 1 + Tests/HTMLKitTests/HTMLKitTestUtil.m | 19 +++++++++++++++++++ .../HTMLKitTokenizerPerformance.m | 5 ++--- 7 files changed, 33 insertions(+), 19 deletions(-) diff --git a/Tests/HTMLKitTests/CSSSelectorTest.m b/Tests/HTMLKitTests/CSSSelectorTest.m index a85191e..2c1feb6 100644 --- a/Tests/HTMLKitTests/CSSSelectorTest.m +++ b/Tests/HTMLKitTests/CSSSelectorTest.m @@ -11,6 +11,7 @@ #import "HTMLDocument.h" #import "HTMLElement.h" #import "CSSSelectors.h" +#import "HTMLKitTestUtil.h" static NSString * const CSSTests = @"css-tests"; @@ -18,8 +19,7 @@ static NSString * const CSSTests = @"css-tests"; + (NSArray *)loadCSSSelectorTests { - NSString *path = [[NSBundle bundleForClass:self.class] resourcePath]; - path = [path stringByAppendingPathComponent:CSSTests]; + NSString *path = [HTMLKitTestUtil pathForFixture:CSSTests ofType:nil inDirectory:nil]; NSMutableArray *tests = [NSMutableArray array]; NSArray *testFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil]; diff --git a/Tests/HTMLKitTests/HTML5LibTokenizerTest.m b/Tests/HTMLKitTests/HTML5LibTokenizerTest.m index f864634..b46f8e4 100644 --- a/Tests/HTMLKitTests/HTML5LibTokenizerTest.m +++ b/Tests/HTMLKitTests/HTML5LibTokenizerTest.m @@ -9,21 +9,19 @@ #import "HTML5LibTokenizerTest.h" #import "HTMLTokenizerStates.h" #import "HTMLTokens.h" +#import "HTMLKitTestUtil.h" static NSString * const HTML5LibTests = @"html5lib-tests"; -static NSString * const TOKENIZER = @"tokenizer"; +static NSString * const Tokenizer = @"tokenizer"; @implementation HTML5LibTokenizerTest + (NSDictionary *)loadHTML5LibTokenizerTests { - NSString *path = [[NSBundle bundleForClass:self.class] resourcePath]; - path = [path stringByAppendingPathComponent:HTML5LibTests]; - path = [path stringByAppendingPathComponent:TOKENIZER]; - - NSMutableDictionary *testsMap = [NSMutableDictionary dictionary]; + NSString *path = [HTMLKitTestUtil pathForFixture:Tokenizer ofType:nil inDirectory:HTML5LibTests]; NSArray *testFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil]; + NSMutableDictionary *testsMap = [NSMutableDictionary dictionary]; for (NSString *testFile in testFiles) { if (![testFile.pathExtension isEqualToString:@"test"]) { continue; diff --git a/Tests/HTMLKitTests/HTML5LibTreeConstructionTest.m b/Tests/HTMLKitTests/HTML5LibTreeConstructionTest.m index 659a094..e46a5c0 100644 --- a/Tests/HTMLKitTests/HTML5LibTreeConstructionTest.m +++ b/Tests/HTMLKitTests/HTML5LibTreeConstructionTest.m @@ -13,6 +13,7 @@ #import "HTMLElement.h" #import "HTMLText.h" #import "HTMLComment.h" +#import "HTMLKitTestUtil.h" static NSString * const HTML5LibTests = @"html5lib-tests"; static NSString * const TreeConstruction = @"tree-construction"; @@ -21,13 +22,10 @@ static NSString * const TreeConstruction = @"tree-construction"; + (NSDictionary *)loadHTML5LibTreeConstructionTests { - NSString *path = [[NSBundle bundleForClass:self.class] resourcePath]; - path = [path stringByAppendingPathComponent:HTML5LibTests]; - path = [path stringByAppendingPathComponent:TreeConstruction]; - - NSMutableDictionary *testsMap = [NSMutableDictionary dictionary]; + NSString *path = [HTMLKitTestUtil pathForFixture:TreeConstruction ofType:nil inDirectory:HTML5LibTests]; NSArray *testFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil]; + NSMutableDictionary *testsMap = [NSMutableDictionary dictionary]; for (NSString *testFile in testFiles) { if (![testFile.pathExtension isEqualToString:@"dat"]) { continue; diff --git a/Tests/HTMLKitTests/HTMLKitParserPerformance.m b/Tests/HTMLKitTests/HTMLKitParserPerformance.m index b51373a..d7343b2 100644 --- a/Tests/HTMLKitTests/HTMLKitParserPerformance.m +++ b/Tests/HTMLKitTests/HTMLKitParserPerformance.m @@ -8,6 +8,7 @@ #import #import "HTMLParser.h" +#import "HTMLKitTestUtil.h" @interface HTMLKitParserPerformance : XCTestCase @@ -19,9 +20,7 @@ - (void)testParserPerformance { - NSString *path = [[NSBundle bundleForClass:self.class] resourcePath]; - path = [path stringByAppendingPathComponent:@"Fixtures"]; - path = [path stringByAppendingPathComponent:@"HTML Standard.html"]; + NSString *path = [HTMLKitTestUtil pathForFixture:@"HTML Standard" ofType:@"html" inDirectory:@"Fixtures"]; NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; diff --git a/Tests/HTMLKitTests/HTMLKitTestUtil.h b/Tests/HTMLKitTests/HTMLKitTestUtil.h index 700561e..d390424 100644 --- a/Tests/HTMLKitTests/HTMLKitTestUtil.h +++ b/Tests/HTMLKitTests/HTMLKitTestUtil.h @@ -12,5 +12,6 @@ + (NSInvocation *)addTestToClass:(Class)cls withName:(NSString *)name block:(id)block; + (id)ivarForInstacne:(id)instance name:(NSString *)name; ++ (NSString *)pathForFixture:(NSString *)fixture ofType:(NSString *)type inDirectory:(NSString *)directory; @end diff --git a/Tests/HTMLKitTests/HTMLKitTestUtil.m b/Tests/HTMLKitTests/HTMLKitTestUtil.m index c24f526..a70ceb9 100644 --- a/Tests/HTMLKitTests/HTMLKitTestUtil.m +++ b/Tests/HTMLKitTests/HTMLKitTestUtil.m @@ -32,4 +32,23 @@ return object_getIvar(instance, ivar); } ++ (NSString *)pathForFixture:(NSString *)fixture ofType:(NSString *)type inDirectory:(NSString *)directory +{ + // Try testing bundle first + NSString *path = [[NSBundle bundleForClass:self.class] pathForResource:fixture ofType:type inDirectory:directory]; + if (path) { + return path; + } + + path = [[@(__FILE__) stringByDeletingLastPathComponent] stringByDeletingLastPathComponent]; + if (directory) { + path = [path stringByAppendingPathComponent:directory]; + } + + NSString *resource = type ? [NSString stringWithFormat:@"%@.%@", fixture, type] : fixture; + path = [path stringByAppendingPathComponent:resource]; + + return path; +} + @end diff --git a/Tests/HTMLKitTests/HTMLKitTokenizerPerformance.m b/Tests/HTMLKitTests/HTMLKitTokenizerPerformance.m index eb7ee8a..bbb3115 100644 --- a/Tests/HTMLKitTests/HTMLKitTokenizerPerformance.m +++ b/Tests/HTMLKitTests/HTMLKitTokenizerPerformance.m @@ -10,6 +10,7 @@ #import "HTMLTokenizer.h" #import "HTMLTokenizerStates.h" #import "HTMLTokens.h" +#import "HTMLKitTestUtil.h" @interface HTMLKitTokenizerPerformance : XCTestCase @@ -19,9 +20,7 @@ - (void)testTokenizerPerformance { - NSString *path = [[NSBundle bundleForClass:self.class] resourcePath]; - path = [path stringByAppendingPathComponent:@"Fixtures"]; - path = [path stringByAppendingPathComponent:@"HTML Standard.html"]; + NSString *path = [HTMLKitTestUtil pathForFixture:@"HTML Standard" ofType:@"html" inDirectory:@"Fixtures"]; NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];