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

Support testing via SPM #935

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
17cc18b
WIP
HeMet Mar 29, 2021
ac14a7f
Fix path to the resources of test target
HeMet Mar 30, 2021
6c965f3
Introduce SourceryLib target for testing purpose. It is the same as S…
HeMet Mar 30, 2021
aa0a5d0
Import main target as SourceryLib when appropriate
HeMet Mar 30, 2021
e1d462c
Re-enable specs that cause linker errors previously. JS spec left dis…
HeMet Mar 30, 2021
095875d
Add instruction how to fix error while running tests from Xcode
HeMet Mar 30, 2021
a35e51e
No need to set ejsPath manually when framework is built with SPM anym…
HeMet Mar 30, 2021
18df4f7
Fix file references and build settings
HeMet Mar 30, 2021
6cd1247
Use single define `SPM` that is set when the project is built by SPM
HeMet Mar 31, 2021
e3ecc51
Add CodableContextTests
HeMet Mar 31, 2021
d2cb069
Fix build when building with SPM alone
HeMet Mar 31, 2021
24d77d5
WIP TemplatesTests
HeMet Mar 31, 2021
053c61b
Fix executable name
HeMet Apr 1, 2021
895dd74
Update TemplatesTests
HeMet Apr 1, 2021
26e02d7
Fix compilation of Xcode's project
HeMet Apr 1, 2021
562dd5b
Add note
HeMet Apr 1, 2021
17af675
Replace SPM flag with SWIFT_PACKAGE
HeMet Apr 1, 2021
6026175
Revert Code signing settings
HeMet Apr 1, 2021
df18702
Use release version of Nimble
HeMet Apr 1, 2021
87b2414
Point Quick to 3.0.0 version
HeMet Apr 1, 2021
3381803
Optimize imports
HeMet Apr 1, 2021
34fb840
Exclude some files
HeMet Apr 1, 2021
be41da7
Fix error message
HeMet Apr 1, 2021
4425c59
Update template
HeMet Apr 1, 2021
52e10c8
Change wording
HeMet Apr 2, 2021
cc48ac3
Add entry to CHANGELOG
HeMet Apr 2, 2021
f7e3345
Rakefile: fix path to ejs.js
HeMet Apr 2, 2021
4c76ed8
Rakefile: fix path to ejs.js #2
HeMet Apr 2, 2021
3cef485
CircleCI config: fix path to ejs.js
HeMet Apr 2, 2021
238d210
Add location of lib_InternalSwiftSyntaxParser.dylib to the -rpath
HeMet Apr 2, 2021
130c0ec
Use bundled version of lib_InternalSwiftSyntaxParser.dylib
HeMet Apr 2, 2021
dec797d
Fix Nimble dependency
HeMet Apr 3, 2021
6f4273a
Update -rpath of executable too. Otherwise it won't find the library …
HeMet Apr 3, 2021
6acf0cc
Merge branch 'master' into spm-tests
krzysztofzablocki Apr 13, 2021
c1ba0ff
Fix imports
HeMet Apr 13, 2021
52929d5
Updated bundled sources
HeMet Apr 13, 2021
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
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- run: set -o pipefail
- run: bundle exec rake build
- run: bundle exec rake tests
- run: swift build && .build/x86_64-apple-macosx/debug/sourcery --sources SourceryRuntime/Sources --templates Templates/Templates --output SourceryRuntime/Sources --ejsPath SourceryJS/Sources/ejs.js
- run: swift build && .build/x86_64-apple-macosx/debug/sourcery --sources SourceryRuntime/Sources --templates Templates/Templates --output SourceryRuntime/Sources --ejsPath SourceryJS/Resources/ejs.js
- run: bundle exec slather coverage --scheme "$XCODE_SCHEME" --workspace "$XCODE_WORKSPACE" --output-directory $CIRCLE_ARTIFACTS --binary-basename SourceryRuntime --binary-basename Sourcery --cobertura-xml Sourcery.xcodeproj || true
- run: bash <(curl -s https://codecov.io/bash) -f $CIRCLE_ARTIFACTS/cobertura.xml -X coveragepy -X gcov -X xcode

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

## Internal changes
- Removes manual parsing of `TypeName`, only explicit parser / configuration is now used
- Add support for testing via `SPM`
- Updted SwiftLint, Quick and Nible to latest versions

## 1.3.4
Expand Down
36 changes: 36 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@
"version": "0.9.1"
}
},
{
"package": "CwlCatchException",
"repositoryURL": "https://github.com/mattgallagher/CwlCatchException.git",
"state": {
"branch": null,
"revision": "f809deb30dc5c9d9b78c872e553261a61177721a",
"version": "2.0.0"
}
},
{
"package": "CwlPreconditionTesting",
"repositoryURL": "https://github.com/mattgallagher/CwlPreconditionTesting.git",
"state": {
"branch": null,
"revision": "02b7a39a99c4da27abe03cab2053a9034379639f",
"version": "2.0.0"
}
},
{
"package": "Komondor",
"repositoryURL": "https://github.com/shibapm/Komondor.git",
Expand All @@ -28,6 +46,15 @@
"version": "1.0.6"
}
},
{
"package": "Nimble",
"repositoryURL": "https://github.com/Quick/Nimble.git",
"state": {
"branch": null,
"revision": "e491a6731307bb23783bf664d003be9b2fa59ab5",
"version": "9.0.0"
}
},
{
"package": "PackageConfig",
"repositoryURL": "https://github.com/shibapm/PackageConfig.git",
Expand All @@ -46,6 +73,15 @@
"version": "1.0.0"
}
},
{
"package": "Quick",
"repositoryURL": "https://github.com/Quick/Quick.git",
"state": {
"branch": null,
"revision": "8cce6acd38f965f5baa3167b939f86500314022b",
"version": "3.1.2"
}
},
{
"package": "ShellOut",
"repositoryURL": "https://github.com/JohnSundell/ShellOut.git",
Expand Down
179 changes: 160 additions & 19 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
// swift-tools-version:5.3

import PackageDescription
import Foundation

let sourceryDependencies: [Target.Dependency] = [
"SourceryFramework",
"SourceryRuntime",
"SourceryStencil",
"SourceryJS",
"SourcerySwift",
"Commander",
"PathKit",
"Yams",
"StencilSwiftKit",
"SwiftSyntax",
"XcodeProj",
"TryCatch"
]

let package = Package(
name: "Sourcery",
Expand All @@ -9,6 +25,8 @@ let package = Package(
],
products: [
.executable(name: "sourcery", targets: ["Sourcery"]),
// For testing purpose. The linker has problems linking against executable.
.library(name: "SourceryLib", targets: ["SourceryLib"]),
.library(name: "SourceryRuntime", targets: ["SourceryRuntime"]),
.library(name: "SourceryStencil", targets: ["SourceryStencil"]),
.library(name: "SourceryJS", targets: ["SourceryJS"]),
Expand All @@ -24,23 +42,29 @@ let package = Package(
.package(name: "XcodeProj", url: "https://github.com/tuist/xcodeproj", .exact("7.18.0")),
.package(name: "SwiftSyntax",
url: "https://github.com/apple/swift-syntax.git",
.exact("0.50300.0"))
.exact("0.50300.0")),
.package(url: "https://github.com/Quick/Quick.git", from: "3.0.0"),
.package(url: "https://github.com/Quick/Nimble.git", from: "9.0.0")
],
targets: [
.target(name: "Sourcery", dependencies: [
"SourceryFramework",
"SourceryRuntime",
"SourceryStencil",
"SourceryJS",
"SourcerySwift",
"Commander",
"PathKit",
"Yams",
"StencilSwiftKit",
"SwiftSyntax",
"XcodeProj",
"TryCatch",
]),
.target(
name: "Sourcery",
dependencies: sourceryDependencies,
exclude: [
"Templates",
"Info.plist"
]
),
.target(
name: "SourceryLib",
dependencies: sourceryDependencies,
path: "Sourcery",
exclude: [
"main.swift",
"Templates",
"Info.plist"
]
),
.target(name: "SourceryRuntime"),
.target(name: "SourceryUtils", dependencies: [
"PathKit"
Expand All @@ -56,14 +80,131 @@ let package = Package(
"SourceryRuntime",
"StencilSwiftKit",
]),
.target(name: "SourceryJS", dependencies: [
"PathKit"
]),
.target(
name: "SourceryJS",
dependencies: [
"PathKit"
],
exclude: [
"Info.plist"
],
resources: [
.copy("Resources/ejs.js")
]
),
.target(name: "SourcerySwift", dependencies: [
"PathKit",
"SourceryRuntime",
"SourceryUtils"
]),
.target(name: "TryCatch", path: "TryCatch"),
.target(
name: "CodableContext",
path: "Templates/Tests",
exclude: [
"Context/AutoCases.swift",
"Context/AutoEquatable.swift",
"Context/AutoHashable.swift",
"Context/AutoLenses.swift",
"Context/AutoMockable.swift",
"Context/LinuxMain.swift",
"Generated/AutoCases.generated.swift",
"Generated/AutoEquatable.generated.swift",
"Generated/AutoHashable.generated.swift",
"Generated/AutoLenses.generated.swift",
"Generated/AutoMockable.generated.swift",
"Generated/LinuxMain.generated.swift",
"Expected",
"Info.plist",
"TemplatesTests.swift"
],
sources: [
"Context/AutoCodable.swift",
"Generated/AutoCodable.generated.swift"
]
),
.target(name: "TryCatch", path: "TryCatch", exclude: ["Info.plist"]),
.testTarget(
name: "SourceryLibTests",
dependencies: [
"SourceryLib",
"Quick",
"Nimble"
],
exclude: [
"Info.plist"
],
resources: [
.copy("Stub/Configs"),
.copy("Stub/Errors"),
.copy("Stub/JavaScriptTemplates"),
.copy("Stub/SwiftTemplates"),
.copy("Stub/Performance-Code"),
.copy("Stub/Result"),
.copy("Stub/Templates"),
.copy("Stub/Source")
]
),
.testTarget(
name: "CodableContextTests",
dependencies: [
"CodableContext",
"Quick",
"Nimble"
],
path: "Templates/CodableContextTests",
exclude: [
"Info.plist"
]
),
.testTarget(
name: "TemplatesTests",
dependencies: [
"Quick",
"Nimble",
"PathKit"
],
path: "Templates",
exclude: [
"CodableContext",
"CodableContextTests",
"Tests/Generated",
"Tests/Info.plist",
"default.profraw"
],
sources: [
// LinuxMain is not compiled as part of the target
// since there is no way to run script before compilation begins.
"Tests/TemplatesTests.swift"
],
resources: [
.copy("Templates"),
.copy("Tests/Context"),
.copy("Tests/Expected")
]
)
]
)

hookInternalSwiftSyntaxParser()

/// We need to manually add an -rpath to the project so the tests can run via Xcode
/// If we are running from console (swift build & friend) we don't need to do it
func hookInternalSwiftSyntaxParser() {
let isFromTerminal = ProcessInfo.processInfo.environment.values.contains("/usr/bin/swift")
Copy link
Owner

@krzysztofzablocki krzysztofzablocki Apr 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use this approach of having the flag but don't use /usr/bin/swift, link to the one that's bundled in the repo as this is proper way to do it, that way the SDK matches the dylib regardless of toolchain user has

Copy link
Collaborator Author

@HeMet HeMet Apr 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should it check then if not /usr/bin/swift?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't really care about user setup for the generic sourcery use, that's why we bundle our own stuff so we don't need to rely, their setup can only affect Swift templates and there are some checks there as far as I remember

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@krzysztofzablocki I understand that. I have already fixed the path the hook adds to -rpath so it points to bundled version of the library.
But what about isFromTerminal flag? You said /usr/bin/swift should not be used. So what should we check to determine if -rpath should be modified?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this use is fine, it just checks if its using swift build but it's not pulling anything from it

if !isFromTerminal {
package
.targets
.filter { $0.isTest || $0.name == "Sourcery" }
.forEach { $0.installSwiftSyntaxParser() }
}
}

extension PackageDescription.Target {
func installSwiftSyntaxParser() {
linkerSettings = [.unsafeFlags(["-rpath", packageRoot])]
}

var packageRoot: String {
return URL(fileURLWithPath: #file).deletingLastPathComponent().path
}
}
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ task :build do
# print_info "swift at #{swift_path}"
# `cp "$(dirname #{swift_path})/../lib/swift/macosx/lib_InternalSwiftSyntaxParser.dylib" "build/lib/lib_InternalSwiftSyntaxParser.dylib"`
sh %Q(cp lib_InternalSwiftSyntaxParser.dylib #{CLI_DIR}lib)
sh %Q(cp SourceryJS/Sources/ejs.js #{CLI_DIR}bin)
sh %Q(cp SourceryJS/Resources/ejs.js #{CLI_DIR}bin)
`mv #{BUILD_DIR}release/sourcery #{CLI_DIR}bin/`
`install_name_tool -delete_rpath @loader_path #{CLI_DIR}bin/sourcery`
`install_name_tool -delete_rpath /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx #{CLI_DIR}bin/sourcery`
Expand Down
16 changes: 12 additions & 4 deletions Sourcery.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
6393BC6D2019F58D0044E8CC /* SourceryJS.h in Headers */ = {isa = PBXBuildFile; fileRef = 6393BC6B2019F58D0044E8CC /* SourceryJS.h */; settings = {ATTRIBUTES = (Public, ); }; };
6393BC792019F5C90044E8CC /* EJSTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09618B3B1E61EEE100B49D5F /* EJSTemplate.swift */; };
6393BC92201B50910044E8CC /* JavaScriptTemplateSpecs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09618B491E6626EA00B49D5F /* JavaScriptTemplateSpecs.swift */; };
6393BC93201B55230044E8CC /* ejs.js in Resources */ = {isa = PBXBuildFile; fileRef = 09618B411E620D8600B49D5F /* ejs.js */; };
6393BC98201B57620044E8CC /* JavaScriptTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6393BC97201B57620044E8CC /* JavaScriptTemplate.swift */; };
63CC359520246CCB0017D839 /* Xcode+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09C61FA51E76109E005EA58A /* Xcode+Extensions.swift */; };
63E1AD0E1F4093EB00D01FED /* Configs in Resources */ = {isa = PBXBuildFile; fileRef = 63E1AD0D1F4093EB00D01FED /* Configs */; };
84AB2FD82613CBE700E5CC39 /* ejs.js in Resources */ = {isa = PBXBuildFile; fileRef = 84AB2FD72613CBE700E5CC39 /* ejs.js */; };
990B63465A767D7147E0B29B /* Pods_SourceryJS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0C82AE0D8342A0606F1E8CE /* Pods_SourceryJS.framework */; };
B4E02BEC53C447247C765980 /* Pods_SourceryStencil.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6C8BF671DB1330E3E4951214 /* Pods_SourceryStencil.framework */; };
B5089B5221D1817F007A001C /* SourceryRuntime.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 64E201A31EAE8FBF00EAD8A2 /* SourceryRuntime.framework */; };
Expand Down Expand Up @@ -295,7 +295,6 @@
09346DC71E02195300D6358C /* StructSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StructSpec.swift; sourceTree = "<group>"; };
094415C71E78A46A00ABE04A /* Configuration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Configuration.swift; sourceTree = "<group>"; };
09618B3B1E61EEE100B49D5F /* EJSTemplate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EJSTemplate.swift; sourceTree = "<group>"; };
09618B411E620D8600B49D5F /* ejs.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = ejs.js; sourceTree = "<group>"; };
09618B431E620E3200B49D5F /* JSExport.ejs */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = JSExport.ejs; sourceTree = "<group>"; };
09618B491E6626EA00B49D5F /* JavaScriptTemplateSpecs.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JavaScriptTemplateSpecs.swift; sourceTree = "<group>"; };
09618B4E1E662B0A00B49D5F /* JavaScriptTemplates */ = {isa = PBXFileReference; lastKnownFileType = folder; path = JavaScriptTemplates; sourceTree = "<group>"; };
Expand Down Expand Up @@ -334,6 +333,7 @@
6A1278DE322EEEC30934B058 /* Pods_SourceryTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SourceryTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
6C8BF671DB1330E3E4951214 /* Pods_SourceryStencil.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SourceryStencil.framework; sourceTree = BUILT_PRODUCTS_DIR; };
79EDC093C51A602ADB2AAE88 /* Pods-SourceryFramework.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SourceryFramework.debug.xcconfig"; path = "Target Support Files/Pods-SourceryFramework/Pods-SourceryFramework.debug.xcconfig"; sourceTree = "<group>"; };
84AB2FD72613CBE700E5CC39 /* ejs.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = ejs.js; sourceTree = "<group>"; };
8A7B2DB2F4780241A50AF3A3 /* Pods-SourceryStencil.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SourceryStencil.debug.xcconfig"; path = "Target Support Files/Pods-SourceryStencil/Pods-SourceryStencil.debug.xcconfig"; sourceTree = "<group>"; };
AC5C7A379558EBA48F09B22E /* Pods-SourcerySwift.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SourcerySwift.debug.xcconfig"; path = "Target Support Files/Pods-SourcerySwift/Pods-SourcerySwift.debug.xcconfig"; sourceTree = "<group>"; };
B5BCF9F321D161B5009DF652 /* SourceryFramework.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SourceryFramework.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -665,6 +665,7 @@
6393BC6A2019F58D0044E8CC /* SourceryJS */ = {
isa = PBXGroup;
children = (
84AB2FD62613CBAF00E5CC39 /* Resources */,
6393BC8E201B4DCE0044E8CC /* Sources */,
6393BC6B2019F58D0044E8CC /* SourceryJS.h */,
6393BC6C2019F58D0044E8CC /* Info.plist */,
Expand All @@ -676,7 +677,6 @@
isa = PBXGroup;
children = (
09618B3B1E61EEE100B49D5F /* EJSTemplate.swift */,
09618B411E620D8600B49D5F /* ejs.js */,
);
path = Sources;
sourceTree = "<group>";
Expand All @@ -698,6 +698,14 @@
path = SourceryRuntime;
sourceTree = "<group>";
};
84AB2FD62613CBAF00E5CC39 /* Resources */ = {
isa = PBXGroup;
children = (
84AB2FD72613CBE700E5CC39 /* ejs.js */,
);
path = Resources;
sourceTree = "<group>";
};
B5BCF9F421D161B5009DF652 /* SourceryFramework */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1442,7 +1450,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
6393BC93201B55230044E8CC /* ejs.js in Resources */,
84AB2FD82613CBE700E5CC39 /* ejs.js in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
4 changes: 4 additions & 0 deletions Sourcery/Templates/TypedSpec.stencil
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
//sourcery:file:../../../SourceryTests/Models/TypedSpec
import Quick
import Nimble
#if SWIFT_PACKAGE
import SourceryLib
#else
import Sourcery
#endif
@testable import SourceryFramework
@testable import SourceryRuntime

Expand Down
File renamed without changes.
10 changes: 7 additions & 3 deletions SourceryJS/Sources/EJSTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ open class EJSTemplate {
}

/// Should be set to the path of EJS before rendering any template.
/// By default reads ejsbundle.js from framework bundle.
/// If framework is built with SPM this property should be set manually.
public static var ejsPath: Path! = Bundle(for: EJSTemplate.self).path(forResource: "ejs", ofType: "js").map({ Path($0) })
/// By default reads ejs.js from framework bundle.
#if SWIFT_PACKAGE
static let bundle = Bundle.module
#else
static let bundle = Bundle(for: EJSTemplate.self)
#endif
public static var ejsPath: Path! = bundle.path(forResource: "ejs", ofType: "js").map({ Path($0) })

public let sourcePath: Path
public let templateString: String
Expand Down
Loading