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 resource helper to correctly get resource URLs when building in Xcode #132

Merged
merged 2 commits into from
Sep 18, 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
7 changes: 5 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.0
// swift-tools-version:5.5

import PackageDescription

Expand All @@ -21,7 +21,10 @@ let package = Package(
.testTarget(
name: "SwiftCSVTests",
dependencies: ["SwiftCSV"],
path: "SwiftCSVTests"),
path: "SwiftCSVTests",
resources: [
.copy("TestData")
]),
],
swiftLanguageVersions: [.v5, .v4_2]
)
119 changes: 68 additions & 51 deletions SwiftCSV.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion SwiftCSVTests/PerformanceTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ class PerformanceTest: XCTestCase {
var csv: CSV<Named>!

override func setUpWithError() throws {
let csvURL = ResourceHelper.url(forResource: "large", withExtension: "csv")!
let testFilePath = "TestData/large"
let testFileExtension = "csv"
guard let csvURL = ResourceHelper.url(forResource: testFilePath, withExtension: testFileExtension) else {
XCTAssertNotNil(nil, "Could not get URL for \(testFilePath).\(testFileExtension) from Test Bundle")
return
}

csv = try CSV<Named>(url: csvURL)
}

Expand Down
7 changes: 6 additions & 1 deletion SwiftCSVTests/QuotedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ class QuotedTests: XCTestCase {
}

func testEmbeddedQuotes() throws {
let csvURL = ResourceHelper.url(forResource: "wonderland", withExtension: "csv")!
let testFilePath = "TestData/wonderland"
let testFileExtension = "csv"
guard let csvURL = ResourceHelper.url(forResource: testFilePath, withExtension: testFileExtension) else {
XCTAssertNotNil(nil, "Could not get URL for \(testFilePath).\(testFileExtension) from Test Bundle")
return
}
csv = try CSV(url: csvURL)

/*
Expand Down
24 changes: 17 additions & 7 deletions SwiftCSVTests/ResourceHelper.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
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)
// This is a workaround for Xcode, when testing from the Xcode project (not the SPM package) bundle.module is not available...

struct ResourceHelper {
static func url(forResource name: String, withExtension type: String) -> URL? {

#if SWIFT_PACKAGE
return Bundle.module.url(forResource: name, withExtension: type)
#else
// Xcode project
let bundle = Bundle(for: NamedViewTests.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

// In Xcode, folders are stripped from the resources folder.
var finalName = name
var slashCharSet = CharacterSet()
slashCharSet.insert("/")
let parts = name.components(separatedBy: slashCharSet)
if parts.count > 1 {
finalName = parts.last!
}
return bundle.url(forResource: finalName, withExtension: type)
#endif
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 24 additions & 4 deletions SwiftCSVTests/URLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ class URLTests: XCTestCase {
var csv: CSV<Named>!

func testEmptyFields() throws {
let csvURL = ResourceHelper.url(forResource: "empty_fields", withExtension: "csv")!
let testFilePath = "TestData/empty_fields"
let testFileExtension = "csv"
guard let csvURL = ResourceHelper.url(forResource: testFilePath, withExtension: testFileExtension) else {
XCTAssertNotNil(nil, "Could not get URL for \(testFilePath).\(testFileExtension) from Test Bundle")
return
}
csv = try CSV<Named>(url: csvURL)
let expected = [
["id": "1", "name": "John", "age": "23"],
Expand All @@ -29,7 +34,12 @@ class URLTests: XCTestCase {
}

func testQuotes() throws {
let csvURL = ResourceHelper.url(forResource: "quotes", withExtension: "csv")!
let testFilePath = "TestData/quotes"
let testFileExtension = "csv"
guard let csvURL = ResourceHelper.url(forResource: testFilePath, withExtension: testFileExtension) else {
XCTAssertNotNil(nil, "Could not get URL for \(testFilePath).\(testFileExtension) from Test Bundle")
return
}
csv = try CSV<Named>(url: csvURL)
let expected = [
["id": "4", "name, first": "Alex", "name, last": "Smith"],
Expand Down Expand Up @@ -62,7 +72,12 @@ class URLTests: XCTestCase {
}

func testUTF8() throws {
let csvURL = ResourceHelper.url(forResource: "utf8_with_bom", withExtension: "csv")!
let testFilePath = "TestData/utf8_with_bom"
let testFileExtension = "csv"
guard let csvURL = ResourceHelper.url(forResource: testFilePath, withExtension: testFileExtension) else {
XCTAssertNotNil(nil, "Could not get URL for \(testFilePath).\(testFileExtension) from Test Bundle")
return
}
csv = try CSV(url: csvURL)

XCTAssertFalse(csv.header.first!.hasPrefix("\u{FEFF}"))
Expand All @@ -81,7 +96,12 @@ class URLTests: XCTestCase {
}

func testUTF8Delimited() throws {
let csvURL = ResourceHelper.url(forResource: "utf8_with_bom", withExtension: "csv")!
let testFilePath = "TestData/utf8_with_bom"
let testFileExtension = "csv"
guard let csvURL = ResourceHelper.url(forResource: testFilePath, withExtension: testFileExtension) else {
XCTAssertNotNil(nil, "Could not get URL for \(testFilePath).\(testFileExtension) from Test Bundle")
return
}
csv = try CSV(url: csvURL, delimiter: .comma)

XCTAssertFalse(csv.header.first!.hasPrefix("\u{FEFF}"))
Expand Down
Loading