Skip to content

Commit 0d9fd92

Browse files
authored
Merge pull request #451 from iangmaia/add/new-tests-for-ios_get_app_version
Additional tests for `ios_get_app_version`
2 parents 8f63869 + 4e2cae3 commit 0d9fd92

File tree

5 files changed

+71
-21
lines changed

5 files changed

+71
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ _None_
2323
### Internal Changes
2424

2525
- Updates `activesupport` to `6.1.7.1`, addressing [a security issue](https://github.com/advisories/GHSA-j6gc-792m-qgm2). This is a major version change, but as the dependency is internal-only, it shouldn't be a breaking change for clients. [#441]
26+
- Add the explicit dependency to `xcodeproj (~> 1.22)`, used in this case to replace the previous manual parsing of `.xcconfig` files. [#451]
2627

2728
## 6.3.0
2829

Gemfile.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ PATH
1616
progress_bar (~> 1.3)
1717
rake (>= 12.3, < 14.0)
1818
rake-compiler (~> 1.0)
19+
xcodeproj (~> 1.22)
1920

2021
GEM
2122
remote: https://rubygems.org/

fastlane-plugin-wpmreleasetoolkit.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Gem::Specification.new do |spec|
3737
spec.add_dependency 'progress_bar', '~> 1.3'
3838
spec.add_dependency 'rake', '>= 12.3', '< 14.0'
3939
spec.add_dependency 'rake-compiler', '~> 1.0'
40+
spec.add_dependency 'xcodeproj', '~> 1.22'
4041

4142
# `google-cloud-storage` is required by fastlane, but we pin it in case it's not in the future
4243
spec.add_dependency 'google-cloud-storage', '~> 1.31'

lib/fastlane/plugin/wpmreleasetoolkit/helper/ios/ios_version_helper.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'xcodeproj'
2+
13
module Fastlane
24
module Helper
35
module Ios
@@ -23,6 +25,9 @@ module VersionHelper
2325
#
2426
def self.get_xcconfig_public_version(xcconfig_file:)
2527
version = read_long_version_from_config_file(xcconfig_file)
28+
29+
UI.user_error!(".xcconfig file doesn't have a version configured") if version.nil?
30+
2631
vp = get_version_parts(version)
2732
return "#{vp[MAJOR_NUMBER]}.#{vp[MINOR_NUMBER]}" unless is_hotfix?(version)
2833

@@ -292,19 +297,15 @@ def self.read_build_number_from_config_file(filePath)
292297
# Read the value of a given key from an `.xcconfig` file.
293298
#
294299
# @param [String] key The xcconfig key to get the value for
295-
# @param [String] filePath The path to the `.xcconfig` file to read the value from
300+
# @param [String] file_path The path to the `.xcconfig` file to read the value from
296301
#
297302
# @return [String] The value for the given key, or `nil` if the key was not found.
298303
#
299-
def self.read_from_config_file(key, filePath)
300-
File.open(filePath, 'r') do |f|
301-
f.each_line do |line|
302-
line = line.strip()
303-
return line.split('=')[1] if line.start_with?("#{key}=")
304-
end
305-
end
304+
def self.read_from_config_file(key, file_path)
305+
UI.user_error!(".xcconfig file #{file_path} not found") unless File.exist?(file_path)
306306

307-
return nil
307+
config = Xcodeproj::Config.new(file_path)
308+
config.attributes[key]
308309
end
309310

310311
# Ensure that the version provided is only composed of number parts and return the validated string

spec/ios_get_app_version_spec.rb

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,85 @@
33
describe Fastlane::Actions::IosGetAppVersionAction do
44
describe 'getting the public app version from the provided .xcconfig file' do
55
it 'parses the xcconfig file format correctly and gets the public version' do
6+
xcconfig_mock_content = <<~CONTENT
7+
// a comment
8+
VERSION_SHORT = 6
9+
VERSION_LONG = 6.30.0
10+
CONTENT
11+
12+
expect_version(xcconfig_mock_content: xcconfig_mock_content, expected_version: '6.30')
13+
end
14+
15+
it 'parses the xcconfig file format correctly and gets the public hotfix version' do
16+
xcconfig_mock_content = <<~CONTENT
17+
VERSION_SHORT = 6
18+
// a comment
19+
VERSION_LONG = 6.30.1
20+
CONTENT
21+
22+
expect_version(xcconfig_mock_content: xcconfig_mock_content, expected_version: '6.30.1')
23+
end
24+
25+
it 'parses the xcconfig with keys without spacing and gets the public version' do
626
xcconfig_mock_content = <<~CONTENT
727
// a comment
828
VERSION_SHORT=6
929
VERSION_LONG=6.30.0
1030
CONTENT
1131

12-
allow(File).to receive(:exist?).and_return(true)
13-
1432
expect_version(xcconfig_mock_content: xcconfig_mock_content, expected_version: '6.30')
1533
end
1634

17-
it 'parses the xcconfig file format correctly and gets the public hotfix version' do
35+
it 'parses the xcconfig with keys without spacing and gets the public hotfix version' do
1836
xcconfig_mock_content = <<~CONTENT
1937
VERSION_SHORT=6
2038
// a comment
2139
VERSION_LONG=6.30.1
2240
CONTENT
2341

24-
allow(File).to receive(:exist?).and_return(true)
25-
2642
expect_version(xcconfig_mock_content: xcconfig_mock_content, expected_version: '6.30.1')
2743
end
2844

29-
def expect_version(xcconfig_mock_content:, expected_version:)
30-
xcconfig_mock_file_path = File.join('mock', 'file', 'path')
45+
it 'fails to extract the version from an xcconfig file with an invalid format' do
46+
xcconfig_mock_content = <<~CONTENT
47+
VERSION_SHORT = 6
48+
VERSION_LONG 6.30.1
49+
CONTENT
50+
51+
expect do
52+
expect_version(xcconfig_mock_content: xcconfig_mock_content, expected_version: 'n/a')
53+
end.to raise_error(FastlaneCore::Interface::FastlaneError)
54+
end
55+
56+
it 'throws an error when the file is not found' do
57+
file_path = 'file/not/found'
3158

32-
allow(File).to receive(:open).with(xcconfig_mock_file_path, 'r').and_yield(StringIO.new(xcconfig_mock_content))
59+
expect do
60+
run_described_fastlane_action(
61+
public_version_xcconfig_file: file_path
62+
)
63+
end.to raise_error(FastlaneCore::Interface::FastlaneError)
64+
end
65+
66+
it "throws an error when there isn't a version configured in the .xcconfig file" do
67+
xcconfig_mock_content = <<~CONTENT
68+
VERSION_SHORT = 6
69+
// a comment
70+
CONTENT
71+
72+
expect do
73+
expect_version(xcconfig_mock_content: xcconfig_mock_content, expected_version: 'n/a')
74+
end.to raise_error(FastlaneCore::Interface::FastlaneError)
75+
end
3376

34-
version_result = run_described_fastlane_action(
35-
public_version_xcconfig_file: xcconfig_mock_file_path
36-
)
77+
def expect_version(xcconfig_mock_content:, expected_version:)
78+
with_tmp_file(named: 'mock_xcconfig.xcconfig', content: xcconfig_mock_content) do |tmp_file_path|
79+
version_result = run_described_fastlane_action(
80+
public_version_xcconfig_file: tmp_file_path
81+
)
3782

38-
expect(version_result).to eq(expected_version)
83+
expect(version_result).to eq(expected_version)
84+
end
3985
end
4086
end
4187
end

0 commit comments

Comments
 (0)