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

Add integration tests for install_swiftpm_dependencies #109

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
d2454f0
Add `README` for `install_swiftpm_dependencies` tests
mokagio Aug 9, 2024
f9e5484
Add `install_swiftpm_dependencies` test for standalone package
mokagio Aug 9, 2024
6ef5c56
Add `install_swiftpm_dependencies` with `--use-spm` parameter
mokagio Aug 9, 2024
013598a
Put commands in the `$PATH` for `install_swiftpm_dependencies` tests
mokagio Aug 9, 2024
19529b3
Rename `tests/spm_caching` to `test/install_swiftpm_dependencies`
mokagio Aug 9, 2024
554cf33
Move test instructions to script to correctly export `$PATH`
mokagio Aug 9, 2024
90ba0b5
Fix inverted cmds in CI
mokagio Aug 9, 2024
4404044
Add test for `xcodeproj` with automatic detection
mokagio Aug 9, 2024
b205724
Add test for `xcodeproj` with explicit parameter
mokagio Aug 9, 2024
85dcd90
Add tests for `install_swiftpm_dependencies` with `xcworkspace`
mokagio Aug 9, 2024
85dac57
Remove backticks from group name—They don't render
mokagio Aug 9, 2024
ddb2ab5
Rename `standalone_package` to `package`
mokagio Aug 9, 2024
e95ccb0
Rename `xcodeproj` to `project`
mokagio Aug 9, 2024
7c942cd
Make `test_workspace_*` executable
mokagio Aug 9, 2024
4dac87b
Add tests for scenario with both workspace and project in the same dir
mokagio Aug 9, 2024
0304f6a
Fix outdated scripts names
mokagio Aug 9, 2024
7b124c4
Add `set -o pipefail` to test scripts to avoid false positives
mokagio Aug 9, 2024
4c60f49
Fix typo in test script path
mokagio Aug 9, 2024
a1d2f84
Move test scripts in dedicated folder to keep tidy
mokagio Aug 9, 2024
4e7c222
Update `xcworkspace` lookup to avoid false negative
mokagio Aug 9, 2024
2a4899f
Revert "Update `xcworkspace` lookup to avoid false negative"
mokagio Aug 29, 2024
ce6e396
Remove trailing whitespaces from `install_swiftpm_dependencies`
mokagio Aug 29, 2024
b42adf0
Use `shopt -s nullglob` to avoid false negative
mokagio Aug 29, 2024
80cc173
Code-generate project
mokagio Aug 29, 2024
ec2b5c1
Add handling to auto-detect standalone project
mokagio Aug 29, 2024
a215f6c
Generate `Package.resolved` for xcodegen project
mokagio Aug 29, 2024
a36ed9f
Fix path in `test_workspace_and_project_automatic.sh`
mokagio Aug 29, 2024
d883aa2
Replace hardcode `project` with code-generated one
mokagio Aug 29, 2024
51f9063
Replace project in workspace+project setup with code-generated one
mokagio Aug 29, 2024
79e8cb4
Fix code-gen Buildkite script paths
mokagio Aug 29, 2024
001daaa
Add missing `xcodegen` in some CI tests
mokagio Aug 29, 2024
204634f
Fix `pushd` not working from Makefile - Bypass it
mokagio Aug 29, 2024
dc7fd41
Streamline `.gitignore` in `workspace`
mokagio Aug 29, 2024
f1df028
Use `make` in `project` for consistency
mokagio Aug 29, 2024
ccb5a53
Dry Buildkite pipeline
mokagio Aug 29, 2024
580d605
DRY env setup for SPM tests
mokagio Aug 29, 2024
3cef0bc
DRY `xcodebuild` call in tests
mokagio Aug 29, 2024
f1e11ad
Fix unbound TESTS_LOCATION
mokagio Aug 29, 2024
eb12b6a
Fix wrong path for `xcodebuild`
mokagio Aug 29, 2024
a2c39ce
Remove misplaced `USE_SPM` set
mokagio Aug 29, 2024
0bd7b11
Use a fixture `Package.resolved`
mokagio Sep 3, 2024
125318e
Add test for `xcodeproj` without `Package.resolved`
mokagio Sep 3, 2024
8ea9a2f
Make a `~~~` group print a confirmation to avoid empty logs
mokagio Sep 3, 2024
d4fda04
Refine error logging in `test_project_no_package.sh`
mokagio Sep 3, 2024
9ffb391
Apply suggestions from code review
AliSoftware Sep 4, 2024
52018fd
Revert change for `workspace_and_project/Makefile`
AliSoftware Sep 4, 2024
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
39 changes: 39 additions & 0 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
# For convenience with the many install_swiftpm_dependencies steps, we declare agents and env for macOS in the root, then specialize the fewer remaining steps
agents:
queue: mac
env:
IMAGE_ID: xcode-15.4

steps:
- label: "🕵️ Lint"
command: make lint
agents:
queue: default

- label: "🔬 Test"
command: make test
agents:
queue: default
Comment on lines +10 to +16
Copy link
Contributor Author

Choose a reason for hiding this comment

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

By the way, these have been running on the upload queue all this time because of how the pipeline was configured in the IaC setup.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ooof. Nice catch.


- group: ":swift: install_swiftpm_dependencies Tests"
steps:
- label: ":swift: Standalone Swift Package - Autodetect"
command: tests/install_swiftpm_dependencies/test_scripts/test_package_automatic.sh

- label: ":swift: Standalone Swift Package - Explicit"
command: tests/install_swiftpm_dependencies/test_scripts/test_package_explicit.sh

- label: ":xcode: Xcode Project - Autodetect"
command: tests/install_swiftpm_dependencies/test_scripts/test_project_automatic.sh

- label: ":xcode: Xcode Project - Explicit"
command: tests/install_swiftpm_dependencies/test_scripts/test_project_explicit.sh

- label: ":xcode: Xcode Project - No Package.resolved"
command: tests/install_swiftpm_dependencies/test_scripts/test_project_no_package.sh

- label: ":xcode: Xcode Workspace - Autodetect"
command: tests/install_swiftpm_dependencies/test_scripts/test_workspace_automatic.sh

- label: ":xcode: Xcode Workspace - Explicit"
command: tests/install_swiftpm_dependencies/test_scripts/test_workspace_explicit.sh

- label: ":xcode: Xcode Workspace and Project - Autodetect"
command: tests/install_swiftpm_dependencies/test_scripts/test_workspace_and_project_automatic.sh

- label: ":xcode: Xcode Workspace and Project - Explicit"
command: tests/install_swiftpm_dependencies/test_scripts/test_workspace_and_project_explicit.sh
24 changes: 16 additions & 8 deletions bin/install_swiftpm_dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,21 @@ SPM_CACHE_LOCATION="${HOME}/Library/Caches/org.swift.swiftpm"
# Try to guess what to do if no option provided explicitly
if [[ -z "${XCWORKSPACE_PATH}" && -z "${XCODEPROJ_PATH}" && "${USE_SPM}" != "true" ]]; then
echo "No \`--workspace\`, \`--project\` or \`--use-spm\` flag provided. Trying to guess the correct one to use..."

shopt -s nullglob
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

FOUND_ROOT_WORKSPACE_PATHS=(*.xcworkspace)
if [[ ${#FOUND_ROOT_WORKSPACE_PATHS[@]} -eq 1 && -d "${FOUND_ROOT_WORKSPACE_PATHS[0]}" && ! -f "Package.swift" ]]; then
FOUND_ROOT_PROJECT_PATHS=(*.xcodeproj)

if [[ ${#FOUND_ROOT_WORKSPACE_PATHS[@]} -eq 1 && -d "${FOUND_ROOT_WORKSPACE_PATHS[0]}" && ! -f "Package.swift" ]]; then
XCWORKSPACE_PATH="${FOUND_ROOT_WORKSPACE_PATHS[0]}"
echo " --> Found a single \`.xcworkspace\` file, and no \`Package.swift\` in the root of the repo."
echo " Defaulting to \`--workspace \"${XCWORKSPACE_PATH}\"\`."
elif [[ ${#FOUND_ROOT_WORKSPACE_PATHS[@]} -eq 0 && -f "Package.swift" ]]; then
echo " --> Found a \`Package.swift\`, and no \`.xcworkspace\` in the root of the repo."
elif [[ ${#FOUND_ROOT_WORKSPACE_PATHS[@]} -eq 0 && ${#FOUND_ROOT_PROJECT_PATHS[@] } -eq 1 && ! -f "Package.swift" ]]; then
XCODEPROJ_PATH="${FOUND_ROOT_PROJECT_PATHS[0]}"
echo " --> Found an \`.xcodeproj\`, and no \`Package.swift\` nor \`.xcworkspace\`in the root of the repo."
echo " Defaulting to \`--project \"${XCODEPROJ_PATH}\`"
elif [[ ${#FOUND_ROOT_WORKSPACE_PATHS[@]} -eq 0 && ${#FOUND_ROOT_PROJECT_PATHS[@] } -eq 0 && -f "Package.swift" ]]; then
echo " --> Found a \`Package.swift\`, and no \`.xcworkspace\` or \`.xcodeproj\` in the root of the repo."
echo " Defaulting to \`--use-spm\`"
USE_SPM=true
else
Expand Down Expand Up @@ -111,15 +119,15 @@ elif [[ "${USE_SPM}" == "true" ]]; then
swift package resolve
fi


# `checkouts` can be removed because the system can quickly generate them
# `checkouts` can be removed because the system can quickly generate them
# instead of needing to download them in the cache each time.
#
# `artifacts` should be removed because it causes issues when downloading
# certain packages to have the artifacts already present after extracting
#
# `artifacts` should be removed because it causes issues when downloading
# certain packages to have the artifacts already present after extracting
# cache
echo "~~~ Cleaning up cache files before saving cache"
rm -rf "${SPM_CACHE_LOCATION}/checkouts" "${SPM_CACHE_LOCATION}/artifacts"
echo "Done. Removed checkouts and artifacts subfolders from $SPM_CACHE_LOCATION"
Comment on lines 128 to +130
Copy link
Contributor Author

@mokagio mokagio Sep 3, 2024

Choose a reason for hiding this comment

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

Noticed this nitpick while checking the logs in the integration tests... The group had no logs under it, which made expanding it look odd.

Before (without echo)

image

After

image


# If this is the first time we've seen this particular cache key, save it for the future
echo "~~~ Saving SPM Cache"
Expand Down
4 changes: 4 additions & 0 deletions tests/install_swiftpm_dependencies/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
regenerate_fixtures:
xcodegen generate --spec ./project/project.yml
xcodebuild -resolvePackageDependencies -project ./project/Demo.xcodeproj
cp ./project/Demo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved ./package_resolved_fixtures/valid.resolved
17 changes: 17 additions & 0 deletions tests/install_swiftpm_dependencies/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Integration tests for `install_swiftpm_dependencies`

This folder contains a variety of Xcode and Swift Package projects to test the behavior of the `install_swiftpm_dependencies` in various configurations.

Note that currently with "testing" we mean: making sure the CI build doesn't fail. There isn't yet an automated setup to verify the correct command behavior.

Also note that the tests are intentionally at the integration / end-to-end level. We could structure `install_swiftpm_dependencies` so that it spits out the `xcodebuild` or `swift` it plans to run and test that, but we would lose important feedback on how `xcodebuild` and `swift` themselves behave.
Feedback on Apple's tooling behavior is crucial to get, given their known quirks.
Comment on lines +7 to +8
Copy link
Contributor

Choose a reason for hiding this comment

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

💯


## Development

The integration tests use [XcodeGen](https://github.com/yonaskolb/XcodeGen) to code-generate the project files and keep the repository footprint small.

However, by code-generating the xcodeproj file we lose tracking of the `Package.resolved` Xcode generates at build time, which is particularly problematic in CI.
To compensate for this, we have dedicated automation that tracks a reference `Package.resolved` file for CI to use.

If resolved file format were to change in the future, you can update the reference file via `make regenerate_resolved_fixtures`.
8 changes: 8 additions & 0 deletions tests/install_swiftpm_dependencies/package/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
15 changes: 15 additions & 0 deletions tests/install_swiftpm_dependencies/package/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"originHash" : "c908e0d89a41be29a11cf4ce76b34002b6c4863ff5d7eafd623b36c267284b12",
"pins" : [
{
"identity" : "screenobject",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Automattic/ScreenObject",
"state" : {
"revision" : "328db56c62aab91440ec5e07cc9f7eef6e26a26e",
"version" : "0.2.3"
}
}
],
"version" : 3
}
22 changes: 22 additions & 0 deletions tests/install_swiftpm_dependencies/package/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// swift-tools-version: 5.10

import PackageDescription

let package = Package(
name: "Demo",
products: [
.library(
name: "Demo",
targets: ["Demo"]),
],
dependencies: [
.package(url: "https://github.com/Automattic/ScreenObject", from: "0.2.3")
],
targets: [
.target(
name: "Demo"),
.testTarget(
name: "DemoTests",
dependencies: ["Demo"]),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
struct Demo {
static func sayHello() -> String {
"Hello, world!"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import XCTest
@testable import Demo

final class DemoTests: XCTestCase {
func testHello() throws {
XCTAssertEqual(Demo.sayHello(), "Hello, world!")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"originHash" : "5ca84ad21a87cfff30163bd7dd55abded567e0600b5a0adbc80cfe0c687ecc69",
"pins" : [
{
"identity" : "screenobject",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Automattic/ScreenObject",
"state" : {
"revision" : "328db56c62aab91440ec5e07cc9f7eef6e26a26e",
"version" : "0.2.3"
}
}
],
"version" : 3
}
5 changes: 5 additions & 0 deletions tests/install_swiftpm_dependencies/project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# For convenience with the code generation, ignore everything in this folder other that what's required to code-gen and test behavior
*
!project.yml
!Makefile
!Demo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
4 changes: 4 additions & 0 deletions tests/install_swiftpm_dependencies/project/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DEFAULT_GOAL := generate

generate:
xcodegen generate
28 changes: 28 additions & 0 deletions tests/install_swiftpm_dependencies/project/project.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Demo
options:
bundleIdPrefix: com.automattic.tests
packages:
ScreenObject:
url: https://github.com/Automattic/ScreenObject
from: 0.2.3
targets:
Demo:
type: framework
platform: iOS
sources: [../package/Sources]
settings:
GENERATE_INFOPLIST_FILE: YES
scheme:
testTargets: [DemoTests]
DemoTests:
target: Demo
type: bundle.unit-test
platform: iOS
sources: [../package/Tests/]
settings:
GENERATE_INFOPLIST_FILE: YES
# No need for code signing in this demo, plus, it's the test target
CODE_SIGNING_ALLOWED: NO
dependencies:
- target: Demo
- package: ScreenObject
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash -eu

set -o pipefail

echo "--- :xcode: Run tests to verify packages have been fetched and are available"
AliSoftware marked this conversation as resolved.
Show resolved Hide resolved
xcodebuild test \
-scheme Demo \
-configuration Debug \
-destination 'platform=iOS Simulator' \
| xcbeautify
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

if [ "${BASH_SOURCE[0]}" -ef "$0" ]; then
echo "$0 must be sourced"
exit 1
fi

set -eu

echo "--- :computer: Prepare environment"
TESTS_LOCATION="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
NEW_PATH=$PATH:"$TESTS_LOCATION/../../../bin"

export PATH=$NEW_PATH
export TESTS_LOCATION
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash -eu

set -o pipefail

source "$(dirname "${BASH_SOURCE[0]}")/set_up_environment.sh"

echo "--- :computer: Jump to test folder"
pushd "$TESTS_LOCATION/../package"

echo "--- :wrench: Run install_swiftpm_dependencies"
install_swiftpm_dependencies

echo "--- :xcode: Run tests to verify packages have been fetched and are available"
swift test
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash -eu

set -o pipefail

source "$(dirname "${BASH_SOURCE[0]}")/set_up_environment.sh"

echo "--- :computer: Jump to test folder"
pushd "$TESTS_LOCATION/../package"

echo "--- :wrench: Run install_swiftpm_dependencies"
install_swiftpm_dependencies --use-spm

echo "--- :xcode: Run tests to verify packages have been fetched and are available"
swift test
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash -eu

set -o pipefail

source "$(dirname "${BASH_SOURCE[0]}")/set_up_environment.sh"

echo "--- :computer: Jump to test folder"
pushd "$TESTS_LOCATION/../project"

echo "--- :computer: Generate project"
brew install xcodegen
make

echo "--- :computer: Copy Package.resolved fixture"
PROJECT="Demo.xcodeproj"
XCODE_SPM_PATH="$PROJECT/project.xcworkspace/xcshareddata/swiftpm"
mkdir -p "$XCODE_SPM_PATH"
cp "$TESTS_LOCATION/../package_resolved_fixtures/valid.resolved" "$XCODE_SPM_PATH/Package.resolved"
Comment on lines +14 to +18
Copy link
Contributor

Choose a reason for hiding this comment

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

👍


echo "--- :wrench: Run install_swiftpm_dependencies"
install_swiftpm_dependencies

"$TESTS_LOCATION/run_tests_with_xcodebuild.sh"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash -eu

set -o pipefail

source "$(dirname "${BASH_SOURCE[0]}")/set_up_environment.sh"

echo "--- :computer: Jump to test folder"
pushd "$TESTS_LOCATION/../project"

echo "--- :computer: Generate project"
brew install xcodegen
make

echo "--- :computer: Copy Package.resolved fixture"
PROJECT="Demo.xcodeproj"
XCODE_SPM_PATH="$PROJECT/project.xcworkspace/xcshareddata/swiftpm"
mkdir -p "$XCODE_SPM_PATH"
cp "$TESTS_LOCATION/../package_resolved_fixtures/valid.resolved" "$XCODE_SPM_PATH/Package.resolved"

echo "--- :wrench: Run install_swiftpm_dependencies"
install_swiftpm_dependencies --project $PROJECT

"$TESTS_LOCATION/run_tests_with_xcodebuild.sh"
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash -eu

set -o pipefail

source "$(dirname "${BASH_SOURCE[0]}")/set_up_environment.sh"

echo "--- :computer: Jump to test folder"
pushd "$TESTS_LOCATION/../project"

echo "--- :computer: Generate project"
brew install xcodegen
make

# Notice we do not set up the fixture Package.resolved.
# As such, we expect the call to the plugin to fail.

echo "--- :wrench: Run install_swiftpm_dependencies"
PROJECT=Demo.xcodeproj
LOGS_PATH=logs
set +e
install_swiftpm_dependencies --project $PROJECT 2>&1 | tee "$LOGS_PATH"
CMD_EXIT_STATUS=$?
set -e

EXPECTED="Unable to find \`Package.resolved\` file ($PROJECT/project.xcworkspace/xcshareddata/swiftpm/Package.resolved)"

if [[ $CMD_EXIT_STATUS -eq 0 ]]; then
echo "+++ :x: install_swiftpm_dependencies unexpectedly succeeded without a Package.resolved in the project folder!"
echo "Expected: $EXPECTED"
echo "Got: $(cat $LOGS_PATH)"
exit 1
Comment on lines +28 to +31
Copy link
Contributor Author

Choose a reason for hiding this comment

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

See test-the-tests PR #110

image

else
if grep -qF "$EXPECTED" "$LOGS_PATH"; then
echo "^^^ +++"
echo "+++ :white_check_mark: install_swiftpm_dependencies failed as expected because there is no Package.resolved in the project folder."
else
echo "+++ :x: install_swiftpm_dependencies failed, but the message it printed is not what we expected."
echo "Expected: $EXPECTED"
echo "Got: $(cat $LOGS_PATH)"
exit 1
Comment on lines +37 to +40
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Again, see #110

image

Copy link
Contributor

Choose a reason for hiding this comment

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

Love it!

fi
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash -eu

set -o pipefail

source "$(dirname "${BASH_SOURCE[0]}")/set_up_environment.sh"

echo "--- :computer: Jump to test folder"
pushd "$TESTS_LOCATION/../workspace_and_project"

echo "--- :computer: Generate project"
brew install xcodegen
AliSoftware marked this conversation as resolved.
Show resolved Hide resolved
make

echo "--- :wrench: Run install_swiftpm_dependencies"
install_swiftpm_dependencies

"$TESTS_LOCATION/run_tests_with_xcodebuild.sh"
Loading