Skip to content

Commit 3cef4ce

Browse files
authored
SPM/Xcode Plugins (#2)
* Plugins support * Pass CLI argument * Process arguments * Correct way of passing CLI arguments * Update README.md
1 parent 9f7b060 commit 3cef4ce

File tree

4 files changed

+52
-28
lines changed

4 files changed

+52
-28
lines changed

Package.swift

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ let sharedSettings: [SwiftSetting] = [
66
.unsafeFlags(["-warnings-as-errors"])
77
]
88

9-
let enablePlugin = false
10-
11-
var products: [PackageDescription.Product] = [
9+
let products: [PackageDescription.Product] = [
1210
.executable(
1311
name: "xcode-selective-test",
1412
targets: ["xcode-selective-test"]
13+
),
14+
.plugin(
15+
name: "XcodeSelectiveTest",
16+
targets: ["SelectiveTestingPlugin"]
1517
)
1618
]
1719

18-
var targets: [PackageDescription.Target] = [
20+
let targets: [PackageDescription.Target] = [
1921
.executableTarget(
2022
name: "xcode-selective-test",
2123
dependencies: ["SelectiveTestingCore",
@@ -54,15 +56,7 @@ var targets: [PackageDescription.Target] = [
5456
name: "DependencyCalculatorTests",
5557
dependencies: ["DependencyCalculator", "Workspace", "PathKit", "SelectiveTestingCore"],
5658
resources: [.copy("ExamplePackages")]),
57-
]
58-
59-
if enablePlugin {
60-
products.append(.plugin(
61-
name: "XcodeSelectiveTest",
62-
targets: ["SelectiveTestingPlugin"]
63-
))
64-
65-
targets.append(.plugin(
59+
.plugin(
6660
name: "SelectiveTestingPlugin",
6761
capability: .command(
6862
intent: .custom(
@@ -72,8 +66,8 @@ if enablePlugin {
7266
.writeToPackageDirectory(reason: "Update test plan file")
7367
]),
7468
dependencies: ["xcode-selective-test"]
75-
))
76-
}
69+
)
70+
]
7771

7872
let package = Package(
7973
name: "XcodeSelectiveTesting",

Plugins/SelectiveTestingPlugin/SelectiveTestingPlugin.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ import PackagePlugin
77

88
@main
99
struct SelectiveTestingPlugin: CommandPlugin {
10-
private func run(_ executable: String) throws {
10+
private func run(_ executable: String, arguments: [String] = []) throws {
1111
let executableURL = URL(fileURLWithPath: executable)
1212

1313
let process = Process()
1414
process.executableURL = executableURL
15-
process.arguments = [
16-
]
15+
process.arguments = arguments
1716

1817
try process.run()
1918
process.waitUntilExit()
@@ -28,7 +27,7 @@ struct SelectiveTestingPlugin: CommandPlugin {
2827
FileManager().changeCurrentDirectoryPath(context.package.directory.string)
2928
let tool = try context.tool(named: "xcode-selective-test")
3029

31-
try run(tool.path.string)
30+
try run(tool.path.string, arguments: arguments)
3231
}
3332
}
3433

@@ -38,9 +37,20 @@ import XcodeProjectPlugin
3837
extension SelectiveTestingPlugin: XcodeCommandPlugin {
3938
func performCommand(context: XcodePluginContext, arguments: [String]) throws {
4039
FileManager().changeCurrentDirectoryPath(context.xcodeProject.directory.string)
40+
4141
let tool = try context.tool(named: "xcode-selective-test")
4242

43-
try run(tool.path.string)
43+
var toolArguments = arguments
44+
45+
if !toolArguments.contains(where: { $0 == "--test-plan" }),
46+
let testPlan = context.xcodeProject.filePaths.first(where: {
47+
$0.extension == "xctestplan"
48+
}) {
49+
print("Using \(testPlan.string) test plan")
50+
toolArguments.append(contentsOf: ["--test-plan", testPlan.string])
51+
}
52+
53+
try run(tool.path.string, arguments: toolArguments)
4454
}
4555
}
4656
#endif

README.md

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,27 @@ This technique saves time when testing locally and on the CI.
3030

3131
## Installation
3232

33+
### Xcode
34+
35+
Add to Xcode as SPM dependency.
36+
37+
- Open your project or workspace in Xcode
38+
- Select yout project in the file list in Xcode
39+
- In the right menu select "Project", open tab "Package Dependencies"
40+
- Select "+"
41+
- In the new window, paste `git@github.com:mikeger/XcodeSelectiveTesting` in the search field
42+
- Select project if necessary, put a checkbox on "XcodeSelectiveTesting" in the list
43+
- Click "Add Package"
44+
3345
### Using Swift Package Manager
3446

35-
Add `.package(url: "git@github.com:mikeger/XcodeSelectiveTesting", .upToNextMajor(from: "0.7.0"))` to your `Package.swift`'s `dependencies` section.
47+
Add `.package(url: "git@github.com:mikeger/XcodeSelectiveTesting", .upToNextMajor(from: "0.8.0"))` to your `Package.swift`'s `dependencies` section.
3648

3749
Use SPM to run the command: `swift run xcode-selective-test`.
3850

3951
### Using [Mint](https://github.com/yonaskolb/Mint)
4052

41-
`mint install mikeger/XcodeSelectiveTesting@0.7.0`
53+
`mint install mikeger/XcodeSelectiveTesting@0.8.0`
4254

4355
### Manually
4456

@@ -55,16 +67,23 @@ Run `swift test --filter "$(swift run xcode-selective-test . --json | jq -r ". |
5567

5668
NB: This command assumes you have [jq](https://jqlang.github.io/jq/) tool installed. You can install it with Homebrew via `brew install jq`.
5769

58-
### Use case: Xcode-based project, prepare test plan locally
70+
### Use case: Xcode-based project, run tests locally
71+
72+
1. Install the tool (see [Installation: Xcode](#xcode))
73+
2. Select your project in the Xcode's file list
74+
3. Right-click on it and select `SelectiveTestingPlugin`
75+
4. Wait for the tool to run
76+
5. Run tests normally, SelectiveTesting would modify your test plan according to the local changes
77+
78+
Alternatively, you can use CLI to achieve the same result:
5979

60-
1. Install the tool
61-
2. Run `mint run mikeger/XcodeSelectiveTesting@0.7.0 YourWorkspace.xcworkspace --test-plan YourTestPlan.xctestplan`
62-
3. Run tests normally, SelectiveTesting would modify your test plan according to the local changes
80+
1. Run `mint run mikeger/XcodeSelectiveTesting@0.8.0 YourWorkspace.xcworkspace --test-plan YourTestPlan.xctestplan`
81+
2. Run tests normally, XcodeSelectiveTesting would modify your test plan according to the local changes
6382

6483
### Use case: Xcode-based project, execute tests on the CI
6584

6685
1. Add code to install the tool
67-
2. Add a CI step before you execute your tests: `mint run mikeger/XcodeSelectiveTesting@0.7.0 YourWorkspace.xcworkspace --test-plan YourTestPlan.xctestplan --base-branch $PR_BASE_BRANCH`
86+
2. Add a CI step before you execute your tests: `mint run mikeger/XcodeSelectiveTesting@0.8.0 YourWorkspace.xcworkspace --test-plan YourTestPlan.xctestplan --base-branch $PR_BASE_BRANCH`
6887
3. Execute your tests
6988

7089
## How does this work?

Sources/DependencyCalculator/PackageMetadata.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ struct PackageTargetMetadata {
1616

1717
// TODO: Split in several methods
1818
static func parse(at path: Path) throws -> [PackageTargetMetadata] {
19-
let manifest = try Shell.execOrFail("cd \(path) && swift package dump-package").trimmingCharacters(in: .newlines)
19+
// NB: Flag `--disable-sandbox` is required to allow running SPM from an SPM plugin
20+
let manifest = try Shell.execOrFail("cd \(path) && swift package dump-package --disable-sandbox").trimmingCharacters(in: .newlines)
2021
guard let manifestData = manifest.data(using: .utf8),
2122
let manifestJson = try JSONSerialization.jsonObject(with: manifestData, options: []) as? [String: Any],
2223
let targets = manifestJson["targets"] as? [[String: Any]]

0 commit comments

Comments
 (0)