|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +describe Fastlane::Wpmreleasetoolkit::Versioning::ContinuousBuildCodeFormatter do |
| 6 | + describe 'derives a versionCode' do |
| 7 | + it 'encodes major, minor and build number with the default build_digits (6)' do |
| 8 | + expect(described_class.new.build_code(major: 26, minor: 9, build_number: 84_231)).to eq(269_084_231) |
| 9 | + end |
| 10 | + |
| 11 | + it 'returns an Integer' do |
| 12 | + expect(described_class.new.build_code(major: 26, minor: 9, build_number: 84_231)).to be_a(Integer) |
| 13 | + end |
| 14 | + end |
| 15 | + |
| 16 | + describe 'monotonicity' do |
| 17 | + it 'increases with the build number within the same version' do |
| 18 | + formatter = described_class.new |
| 19 | + expect(formatter.build_code(major: 26, minor: 9, build_number: 84_232)) |
| 20 | + .to be > formatter.build_code(major: 26, minor: 9, build_number: 84_231) |
| 21 | + end |
| 22 | + |
| 23 | + it 'increases across a version bump even when the build number also increases' do |
| 24 | + formatter = described_class.new |
| 25 | + expect(formatter.build_code(major: 27, minor: 0, build_number: 84_232)) |
| 26 | + .to be > formatter.build_code(major: 26, minor: 9, build_number: 84_231) |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + describe 'custom build_digits' do |
| 31 | + it 'applies the configured multiplier (10^build_digits)' do |
| 32 | + # (2 * 10 + 6) * 10^4 + 123 = 26 * 10_000 + 123 = 260_123 |
| 33 | + expect(described_class.new(build_digits: 4).build_code(major: 2, minor: 6, build_number: 123)).to eq(260_123) |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + # The build number is allowed to exceed 10^build_digits. The docstring guarantees ordering still |
| 38 | + # holds in that case (overflow "only costs human-readability, not ordering") *because* the build |
| 39 | + # number is globally monotonic. These lock that in — and guard against a future change such as |
| 40 | + # masking the build number with `% 10**build_digits`, which would silently break ordering. |
| 41 | + context 'when the build number overflows its reserved digits (10^build_digits)' do |
| 42 | + it 'still increases within the same version across the overflow boundary' do |
| 43 | + formatter = described_class.new(build_digits: 4) # 10^4 = 10_000 |
| 44 | + # 269 * 10_000 + 10_000 = 2_700_000 vs 269 * 10_000 + 9_999 = 2_699_999 |
| 45 | + expect(formatter.build_code(major: 26, minor: 9, build_number: 10_000)) |
| 46 | + .to be > formatter.build_code(major: 26, minor: 9, build_number: 9_999) |
| 47 | + end |
| 48 | + |
| 49 | + it 'still increases across a version bump while both build numbers are past the overflow point' do |
| 50 | + formatter = described_class.new(build_digits: 4) # 10^4 = 10_000 |
| 51 | + # 270 * 10_000 + 25_001 = 2_725_001 vs 269 * 10_000 + 25_000 = 2_715_000 |
| 52 | + expect(formatter.build_code(major: 27, minor: 0, build_number: 25_001)) |
| 53 | + .to be > formatter.build_code(major: 26, minor: 9, build_number: 25_000) |
| 54 | + end |
| 55 | + |
| 56 | + it 'can render identically to a later version once overflowed — the documented readability cost, which is harmless under a monotonic build number' do |
| 57 | + formatter = described_class.new(build_digits: 4) # 10^4 = 10_000 |
| 58 | + # Overflow makes the digits ambiguous: 26.9 build 10_000 and 27.0 build 0 both come out as 2_700_000. |
| 59 | + # That's the "only costs human-readability" caveat — and it's safe because a globally monotonic |
| 60 | + # build number means (27, 0, build: 0) never *follows* (26, 9, build: 10_000); the counter only goes up. |
| 61 | + expect(formatter.build_code(major: 26, minor: 9, build_number: 10_000)) |
| 62 | + .to eq(formatter.build_code(major: 27, minor: 0, build_number: 0)) |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + describe 'validation' do |
| 67 | + it 'raises when minor is greater than 9' do |
| 68 | + expect { described_class.new.build_code(major: 26, minor: 10, build_number: 1) } |
| 69 | + .to raise_error(/Minor version \(10\) must be 9 or lower/) |
| 70 | + end |
| 71 | + |
| 72 | + it 'raises when the derived code exceeds the Play Store maximum' do |
| 73 | + # (210 * 10 + 0) * 10^6 = 2_100_000_000; +1 tips over the cap. |
| 74 | + expect { described_class.new.build_code(major: 210, minor: 0, build_number: 1) } |
| 75 | + .to raise_error(/exceeds the maximum allowed Play Store versionCode \(2100000000\)/) |
| 76 | + end |
| 77 | + |
| 78 | + it 'does not raise when the derived code is exactly at the Play Store maximum' do |
| 79 | + expect(described_class.new.build_code(major: 210, minor: 0, build_number: 0)).to eq(2_100_000_000) |
| 80 | + end |
| 81 | + |
| 82 | + it 'raises when the derived code is not positive (all-zeros input)' do |
| 83 | + expect { described_class.new.build_code(major: 0, minor: 0, build_number: 0) } |
| 84 | + .to raise_error(/Derived build code \(0\) must be a positive integer/) |
| 85 | + end |
| 86 | + |
| 87 | + it 'rejects a non-integer build_digits' do |
| 88 | + expect { described_class.new(build_digits: '6') } |
| 89 | + .to raise_error(/`build_digits` must be an integer, got: String/) |
| 90 | + end |
| 91 | + |
| 92 | + it 'rejects a non-positive build_digits' do |
| 93 | + expect { described_class.new(build_digits: 0) } |
| 94 | + .to raise_error(/`build_digits` must be a positive integer, got: 0/) |
| 95 | + end |
| 96 | + |
| 97 | + %w[major minor build_number].each do |component| |
| 98 | + it "rejects a non-integer #{component} with a user-friendly error" do |
| 99 | + args = { major: 26, minor: 9, build_number: 84_231 } |
| 100 | + args[component.to_sym] = '1' # e.g. an unparsed value from an env var or file read |
| 101 | + expect { described_class.new.build_code(**args) } |
| 102 | + .to raise_error(/`#{component}` must be an integer, got: String/) |
| 103 | + end |
| 104 | + |
| 105 | + it "rejects a negative #{component}" do |
| 106 | + args = { major: 26, minor: 9, build_number: 84_231 } |
| 107 | + args[component.to_sym] = -1 |
| 108 | + expect { described_class.new.build_code(**args) } |
| 109 | + .to raise_error(/`#{component}` must be a non-negative integer, got: -1/) |
| 110 | + end |
| 111 | + end |
| 112 | + end |
| 113 | +end |
0 commit comments