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

[action][spm] add simulator flag for swift compiler #21707

Merged
merged 17 commits into from Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion fastlane/lib/fastlane/actions/spm.rb
Expand Up @@ -10,6 +10,15 @@ def self.run(params)
cmd << "--configuration #{params[:configuration]}" if params[:configuration]
cmd << "--disable-sandbox" if params[:disable_sandbox]
cmd << "--verbose" if params[:verbose]
if params[:simulator]
simulator_flags = [
"-Xswiftc", "-sdk",
"-Xswiftc", "$(xcrun --sdk #{params[:simulator]} --show-sdk-path)",
"-Xswiftc", "-target",
"-Xswiftc", "x86_64-apple-ios$(xcrun --sdk #{params[:simulator]} --show-sdk-version | cut -d '.' -f 1)-simulator"
rogerluan marked this conversation as resolved.
Show resolved Hide resolved
]
cmd += simulator_flags
end
cmd << params[:command] if package_commands.include?(params[:command])
cmd << "--enable-code-coverage" if params[:enable_code_coverage] && (params[:command] == 'generate-xcodeproj' || params[:command] == 'test')
if params[:xcconfig]
Expand Down Expand Up @@ -93,7 +102,15 @@ def self.available_options
env_name: "FL_SPM_VERBOSE",
description: "Increase verbosity of informational output",
type: Boolean,
default_value: false)
default_value: false),
FastlaneCore::ConfigItem.new(key: :simulator,
env_name: "FL_SPM_SIMULATOR",
description: "Specifies the simulator to pass for Swift Compiler (one of: #{valid_simulators.join(', ')})",
type: String,
optional: true,
verify_block: proc do |value|
UI.user_error!("Please pass a valid simulator. Use one of the following: #{valid_simulators.join(', ')}") unless valid_simulators.include?(value)
end)
]
end

Expand Down Expand Up @@ -139,6 +156,10 @@ def self.valid_configurations
def self.xcpretty_output_types
%w(simple test knock tap)
end

def self.valid_simulators
%w(iphonesimulator macossimulator)
end
end
end
end
37 changes: 37 additions & 0 deletions fastlane/spec/actions_specs/spm_spec.rb
Copy link
Member

Choose a reason for hiding this comment

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

Since it looks like the macOS simulator command wasn't working (? can't really tell for sure ATM), is there any way we can add a unit test that makes sure not only the output command is the expected string, but that the command actually works?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't able to find a way to actually test the code in unit tests. I was thinking of using open3 to catch the result of executing command, but then testing the swift build command needs an actual SPM. So wasn't making sense to me. Any suggestion?

Copy link
Member

Choose a reason for hiding this comment

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

Uhm, if we wanted, we could create a sample SPM just for testing purposes. But, on a second thought, I think we actually might not need this, as long as we test it manually and make sure it works with all configurations, this should be fine. The reason is that we'd be validating whether "swift build" works, and that's not really the intent of what fastlane's unit tests should be :)

So this is good as is 🙏 thank you!

Expand Up @@ -339,6 +339,43 @@
expect(result).to eq("set -o pipefail && swift package --verbose generate-xcodeproj --xcconfig-overrides Package.xcconfig 2>&1 | xcpretty --simple")
end
end

context "when simulator is specified" do
it "adds simulator flags to the build command" do
result = Fastlane::FastFile.new.parse("lane :test do
spm(
command: 'build',
simulator: 'iphonesimulator'
)
end").runner.execute(:test)

expect(result).to eq("swift build -Xswiftc -sdk -Xswiftc $(xcrun --sdk iphonesimulator --show-sdk-path) -Xswiftc -target -Xswiftc x86_64-apple-ios$(xcrun --sdk iphonesimulator --show-sdk-version | cut -d '.' -f 1)-simulator")
end

it "raises an error if simulator syntax is invalid" do
expect do
Fastlane::FastFile.new.parse("lane :test do
spm(
command: 'build',
simulator: 'invalid_simulator'
)
end").runner.execute(:test)
end.to raise_error("Invalid simulator syntax. Please use 'iphonesimulator', 'ipadsimulator', or 'macossimulator'.")
gharary marked this conversation as resolved.
Show resolved Hide resolved
end
end

context "when simulator is specified for non-package commands" do
it "raises an error" do
expect do
Fastlane::FastFile.new.parse("lane :test do
spm(
command: 'clean',
simulator: 'iphonesimulator'
)
end").runner.execute(:test)
end.to raise_error("Simulator is only applicable to package-related commands.")
end
end
end
end
end