From 078a24fca88a35f0db7454e9adba06f2c0182ffe Mon Sep 17 00:00:00 2001 From: Shao Date: Tue, 7 May 2019 21:15:38 +0800 Subject: [PATCH 1/5] add SwiftPackageManager support. --- .gitignore | 1 + Package.swift | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 Package.swift diff --git a/.gitignore b/.gitignore index 442bdb8..9f5ae1c 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ DerivedData *.hmap *.ipa *.xcuserstate +.build/ # CocoaPods # diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..90f685f --- /dev/null +++ b/Package.swift @@ -0,0 +1,29 @@ +// swift-tools-version:5.0 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "SwiftCSV", + products: [ + // Products define the executables and libraries produced by a package, and make them visible to other packages. + .library( + name: "SwiftCSV", + targets: ["SwiftCSV"]), + ], + dependencies: [ + // Dependencies declare other packages that this package depends on. + // .package(url: /* package url */, from: "1.0.0"), + ], + targets: [ + // Targets are the basic building blocks of a package. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages which this package depends on. + .target( + name: "SwiftCSV", + path: "SwiftCSV"), + .testTarget( + name: "SwiftCSVTests", + dependencies: ["SwiftCSV"], + path: "SwiftCSVTests"), + ] +) From 6d0ef17debf9017cbd2d0dba89493767720a2220 Mon Sep 17 00:00:00 2001 From: Shao Date: Wed, 8 May 2019 22:26:39 +0800 Subject: [PATCH 2/5] fix error which can't read csv file when run swiftpm's test command. --- SwiftCSVTests/PerformanceTest.swift | 2 +- SwiftCSVTests/ResourceHelper.swift | 14 ++++++++++++++ SwiftCSVTests/URLTests.swift | 4 ++-- 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 SwiftCSVTests/ResourceHelper.swift diff --git a/SwiftCSVTests/PerformanceTest.swift b/SwiftCSVTests/PerformanceTest.swift index 6eb8a27..1465f3e 100644 --- a/SwiftCSVTests/PerformanceTest.swift +++ b/SwiftCSVTests/PerformanceTest.swift @@ -13,7 +13,7 @@ class PerformanceTest: XCTestCase { var csv: CSV! override func setUp() { - let csvURL = Bundle(for: CSVTests.self).url(forResource: "large", withExtension: "csv")! + let csvURL = ResourceHelper.url(forResource: "large", withExtension: "csv")! csv = try! CSV(url: csvURL) } diff --git a/SwiftCSVTests/ResourceHelper.swift b/SwiftCSVTests/ResourceHelper.swift new file mode 100644 index 0000000..e3fae2f --- /dev/null +++ b/SwiftCSVTests/ResourceHelper.swift @@ -0,0 +1,14 @@ +import Foundation + +struct ResourceHelper { + static func url(forResource name: String, withExtension type: String) -> URL? { + let bundle = Bundle(for: CSVTests.self) + if let url = bundle.url(forResource: name, withExtension: type) { + return url + } else if let realBundle = Bundle(path: "\(bundle.bundlePath)/../../../../SwiftCSVTests") { + return realBundle.url(forResource: name, withExtension: type) + } else { + return nil + } + } +} \ No newline at end of file diff --git a/SwiftCSVTests/URLTests.swift b/SwiftCSVTests/URLTests.swift index 6fac691..aaaddf9 100644 --- a/SwiftCSVTests/URLTests.swift +++ b/SwiftCSVTests/URLTests.swift @@ -13,7 +13,7 @@ class URLTests: XCTestCase { var csv: CSV! func testEmptyFields() { - let csvURL = Bundle(for: CSVTests.self).url(forResource: "empty_fields", withExtension: "csv")! + let csvURL = ResourceHelper.url(forResource: "empty_fields", withExtension: "csv")! csv = try! CSV(url: csvURL) let expected = [ ["id": "1", "name": "John", "age": "23"], @@ -29,7 +29,7 @@ class URLTests: XCTestCase { } func testQuotes() { - let csvURL = Bundle(for: CSVTests.self).url(forResource: "quotes", withExtension: "csv")! + let csvURL = ResourceHelper.url(forResource: "quotes", withExtension: "csv")! csv = try! CSV(url: csvURL) let expected = [ ["id": "4", "name, first": "Alex", "name, last": "Smith"], From 2bafd81bef56994bc389c862df74aaf4cb4dc79a Mon Sep 17 00:00:00 2001 From: Shao Date: Wed, 8 May 2019 23:00:19 +0800 Subject: [PATCH 3/5] add file reference to xcode to make test passed. --- SwiftCSV.xcodeproj/project.pbxproj | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/SwiftCSV.xcodeproj/project.pbxproj b/SwiftCSV.xcodeproj/project.pbxproj index 6cf1959..63389d0 100644 --- a/SwiftCSV.xcodeproj/project.pbxproj +++ b/SwiftCSV.xcodeproj/project.pbxproj @@ -65,6 +65,7 @@ BEE5461E1CBBB15900C0666F /* Description.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEE5461D1CBBB15900C0666F /* Description.swift */; }; E46085921CCB1E8F00385286 /* large.csv in Resources */ = {isa = PBXBuildFile; fileRef = E46085911CCB1E8F00385286 /* large.csv */; }; E46085941CCB1F5C00385286 /* PerformanceTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = E46085931CCB1F5C00385286 /* PerformanceTest.swift */; }; + F5C19F502283243C00920B06 /* ResourceHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5C19F4F2283243C00920B06 /* ResourceHelper.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -124,6 +125,7 @@ BEE5461D1CBBB15900C0666F /* Description.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Description.swift; sourceTree = ""; }; E46085911CCB1E8F00385286 /* large.csv */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = large.csv; sourceTree = ""; }; E46085931CCB1F5C00385286 /* PerformanceTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PerformanceTest.swift; sourceTree = ""; }; + F5C19F4F2283243C00920B06 /* ResourceHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ResourceHelper.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -243,6 +245,7 @@ BE06B67C1CB7267B009578CC /* empty_fields.csv */, BE06B6811CB7287F009578CC /* quotes.csv */, E46085911CCB1E8F00385286 /* large.csv */, + F5C19F4F2283243C00920B06 /* ResourceHelper.swift */, ); name = Res; sourceTree = ""; @@ -558,6 +561,7 @@ files = ( E46085941CCB1F5C00385286 /* PerformanceTest.swift in Sources */, 3D1E59C71945FFAD001CF760 /* CSVTests.swift in Sources */, + F5C19F502283243C00920B06 /* ResourceHelper.swift in Sources */, 508975E11DBF3E51006F3DBE /* EnumeratedViewTests.swift in Sources */, 3D3749E3194D6DF7008F262A /* TSVTests.swift in Sources */, BE06B6801CB726B5009578CC /* URLTests.swift in Sources */, From 23cf70cd69d2c019677b35cd85bf716835784ee9 Mon Sep 17 00:00:00 2001 From: Shao Date: Wed, 8 May 2019 23:00:45 +0800 Subject: [PATCH 4/5] add code comment. --- SwiftCSVTests/ResourceHelper.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/SwiftCSVTests/ResourceHelper.swift b/SwiftCSVTests/ResourceHelper.swift index e3fae2f..663de93 100644 --- a/SwiftCSVTests/ResourceHelper.swift +++ b/SwiftCSVTests/ResourceHelper.swift @@ -1,5 +1,7 @@ import Foundation +// Find url of resource. +// This is a workaround for SwiftPM, becasue SwiftPM is not yet support for include resources with targets.(https://bugs.swift.org/browse/SR-2866) struct ResourceHelper { static func url(forResource name: String, withExtension type: String) -> URL? { let bundle = Bundle(for: CSVTests.self) @@ -11,4 +13,4 @@ struct ResourceHelper { return nil } } -} \ No newline at end of file +} From 9bcefaa09cba73e0f0cbd735478f53d3638e000d Mon Sep 17 00:00:00 2001 From: Shao Date: Thu, 9 May 2019 09:59:35 +0800 Subject: [PATCH 5/5] add resourcehelper's file reference to fix test error. --- SwiftCSV.xcodeproj/project.pbxproj | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/SwiftCSV.xcodeproj/project.pbxproj b/SwiftCSV.xcodeproj/project.pbxproj index 63389d0..8222c91 100644 --- a/SwiftCSV.xcodeproj/project.pbxproj +++ b/SwiftCSV.xcodeproj/project.pbxproj @@ -66,6 +66,8 @@ E46085921CCB1E8F00385286 /* large.csv in Resources */ = {isa = PBXBuildFile; fileRef = E46085911CCB1E8F00385286 /* large.csv */; }; E46085941CCB1F5C00385286 /* PerformanceTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = E46085931CCB1F5C00385286 /* PerformanceTest.swift */; }; F5C19F502283243C00920B06 /* ResourceHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5C19F4F2283243C00920B06 /* ResourceHelper.swift */; }; + F5C19F512283C0C100920B06 /* ResourceHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5C19F4F2283243C00920B06 /* ResourceHelper.swift */; }; + F5C19F522283C0C300920B06 /* ResourceHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5C19F4F2283243C00920B06 /* ResourceHelper.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -589,6 +591,7 @@ files = ( 5FB74BE01CCB9312009DDBF1 /* CSVTests.swift in Sources */, 5FB74BE11CCB9312009DDBF1 /* QuotedTests.swift in Sources */, + F5C19F512283C0C100920B06 /* ResourceHelper.swift in Sources */, 508975E21DBF3E51006F3DBE /* EnumeratedViewTests.swift in Sources */, 5FB74BE21CCB9312009DDBF1 /* TSVTests.swift in Sources */, 5FB74BE31CCB9312009DDBF1 /* URLTests.swift in Sources */, @@ -616,6 +619,7 @@ files = ( 5FB74BE51CCB931F009DDBF1 /* CSVTests.swift in Sources */, 5FB74BE61CCB931F009DDBF1 /* QuotedTests.swift in Sources */, + F5C19F522283C0C300920B06 /* ResourceHelper.swift in Sources */, 508975E31DBF3E51006F3DBE /* EnumeratedViewTests.swift in Sources */, 5FB74BE71CCB931F009DDBF1 /* TSVTests.swift in Sources */, 5FB74BE81CCB931F009DDBF1 /* URLTests.swift in Sources */,